Skip to content

Commit 429324d

Browse files
committed
impelement depthfade
- drop opacitybase - drop opacitystep - combine colors with the background color according with their depth from the top
1 parent 1d159e6 commit 429324d

25 files changed

+66
-79
lines changed

src/traces/treemap/attributes.js

+7-21
Original file line numberDiff line numberDiff line change
@@ -139,30 +139,16 @@ module.exports = {
139139

140140
colors: sunburstAttrs.marker.colors,
141141

142-
opacitybase: {
143-
valType: 'number',
144-
editType: 'style',
145-
role: 'style',
146-
min: 0,
147-
max: 1,
148-
dflt: 0.5,
149-
description: [
150-
'Sets the base opacity of the headers above root based on the depth from the entry.',
151-
'Please note that the root itself would be drawn with opacity 1.',
152-
'This options is not available when having a `colorscale`.',
153-
].join(' ')
154-
},
155-
156-
opacitystep: {
157-
valType: 'number',
142+
depthfade: {
143+
valType: 'boolean',
158144
editType: 'style',
159145
role: 'style',
160-
min: 0,
161-
max: 1,
162-
dflt: 0.5,
163146
description: [
164-
'Sets the increment for opacity of the headers based on the depth from the entry.',
165-
'This options is not available when having a `colorscale`.'
147+
'Fades headers towards the background.',
148+
'When `marker.colors` are not set within the trace it is defaulted to *false*;',
149+
'otherwise it is defaulted to *true*.',
150+
'This options is not available when having a `colorscale` or',
151+
'when .'
166152
].join(' ')
167153
},
168154

src/traces/treemap/defaults.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,12 @@ module.exports = function supplyDefaults(traceIn, traceOut, defaultColor, layout
7272
var lineWidth = coerce('marker.line.width');
7373
if(lineWidth) coerce('marker.line.color', layout.paper_bgcolor);
7474

75-
coerce('marker.colors');
75+
var colors = coerce('marker.colors');
7676
var withColorscale = traceOut._hasColorscale = hasColorscale(traceIn, 'marker', 'colors');
7777
if(withColorscale) {
7878
colorscaleDefaults(traceIn, traceOut, layout, coerce, {prefix: 'marker.', cLetter: 'c'});
79+
} else {
80+
coerce('marker.depthfade', !(colors || []).length);
7981
}
8082

8183
var headerSize = traceOut.textfont.size * 2;
@@ -88,8 +90,6 @@ module.exports = function supplyDefaults(traceIn, traceOut, defaultColor, layout
8890
if(withColorscale) {
8991
colorscaleDefaults(traceIn, traceOut, layout, coerce, {prefix: 'marker.', cLetter: 'c'});
9092
} else {
91-
coerce('marker.opacitybase');
92-
coerce('marker.opacitystep');
9393
coerce('pathbar.opacity');
9494
}
9595

src/traces/treemap/draw_descendants.js

+11-10
Original file line numberDiff line numberDiff line change
@@ -65,16 +65,25 @@ module.exports = function drawDescendants(gd, cd, entry, slices, opts) {
6565

6666
var sliceData = allData.descendants();
6767

68+
var minVisibleDepth = Infinity;
69+
var maxVisibleDepth = -Infinity;
70+
6871
slices = slices.data(sliceData, function(pt) {
69-
// hide slices that won't show up on graph
70-
if(pt.depth >= trace._maxDepth) {
72+
var depth = pt.depth;
73+
if(depth >= trace._maxDepth) {
74+
// hide slices that won't show up on graph
7175
pt.x0 = pt.x1 = (pt.x0 + pt.x1) / 2;
7276
pt.y0 = pt.y1 = (pt.y0 + pt.y1) / 2;
77+
} else {
78+
minVisibleDepth = Math.min(minVisibleDepth, depth);
79+
maxVisibleDepth = Math.max(maxVisibleDepth, depth);
7380
}
7481

7582
return helpers.getPtId(pt);
7683
});
7784

85+
trace._maxVisibleLayers = isFinite(maxVisibleDepth) ? maxVisibleDepth - minVisibleDepth + 1 : 0;
86+
7887
slices.enter().append('g')
7988
.classed('slice', true);
8089

@@ -185,14 +194,6 @@ module.exports = function drawDescendants(gd, cd, entry, slices, opts) {
185194
isHeader: isHeader
186195
});
187196

188-
if(helpers.isOutsideText(trace, pt)) {
189-
// consider in/out diff font sizes
190-
pt.transform.targetY -= (
191-
helpers.getOutsideTextFontKey('size', trace, pt, fullLayout.font) -
192-
helpers.getInsideTextFontKey('size', trace, pt, fullLayout.font)
193-
);
194-
}
195-
196197
if(hasTransition) {
197198
sliceText.transition().attrTween('transform', function(pt2) {
198199
var interp = makeUpdateTextInterpolator(pt2, onPathbar, getRefRect(), [width, height]);

src/traces/treemap/plot.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -84,11 +84,16 @@ function plotOne(gd, cd, element, transitionOpts) {
8484
var hierarchy = cd0.hierarchy;
8585
var hasTransition = helpers.hasTransition(transitionOpts);
8686
var entry = helpers.findEntryWithLevel(hierarchy, trace.level);
87+
var isRoot = helpers.isHierarchyRoot(entry);
88+
8789
var maxDepth = helpers.getMaxDepth(trace);
8890
var hasVisibleDepth = function(pt) {
8991
return pt.data.depth - entry.data.depth < maxDepth;
9092
};
9193

94+
// copy
95+
trace._backgroundColor = fullLayout.paper_bgcolor;
96+
9297
var gs = fullLayout._size;
9398
var domain = trace.domain;
9499

@@ -146,12 +151,7 @@ function plotOne(gd, cd, element, transitionOpts) {
146151
return {};
147152
};
148153

149-
var isRoot = helpers.isHierarchyRoot(entry);
150-
151154
trace._entryDepth = entry.data.depth;
152-
if(isRoot) {
153-
trace._entryDepth++;
154-
}
155155

156156
// N.B. handle multiple-root special case
157157
if(cd0.hasMultipleRoots && isRoot) {

src/traces/treemap/style.js

+33-15
Original file line numberDiff line numberDiff line change
@@ -36,37 +36,55 @@ function styleOne(s, pt, trace, opts) {
3636
var lineColor;
3737
var lineWidth;
3838
var opacity;
39-
40-
var depthFade = function(n) {
41-
var base = trace.marker.opacitybase;
42-
var step = trace.marker.opacitystep;
43-
44-
return n === 0 ? base :
45-
Math.max(0, Math.min(1, n * step));
46-
};
39+
var fillColor = cdi.color;
4740

4841
if(hovered) {
4942
lineColor = trace._hovered.marker.line.color;
5043
lineWidth = trace._hovered.marker.line.width;
5144
opacity = trace._hovered.marker.opacity;
5245
} else {
53-
if(helpers.isHierarchyRoot(pt)) {
46+
var isRoot = helpers.isHierarchyRoot(pt);
47+
48+
if(!pt.onPathbar && !trace._hasColorscale && trace.marker.depthfade) {
49+
var fadedColor = Color.combine(Color.addOpacity(trace._backgroundColor, 1), fillColor);
50+
51+
// option 1 | using the height from the top
52+
var maxDepth = helpers.getMaxDepth(trace);
53+
var n;
54+
if(isFinite(maxDepth)) {
55+
if(helpers.isLeaf(pt)) {
56+
n = 0;
57+
} else {
58+
n = (trace._maxVisibleLayers) - (pt.data.depth - trace._entryDepth);
59+
}
60+
} else {
61+
n = pt.data.height + 1;
62+
}
63+
64+
// option 2 | using depth form the bottom
65+
// var n = pt.data.depth - trace._entryDepth + 1;
66+
67+
if(n > 0) {
68+
for(var i = 0; i < n; i++) {
69+
var ratio = 0.5 * i / n;
70+
fillColor = Color.combine(Color.addOpacity(fadedColor, ratio), fillColor);
71+
}
72+
}
73+
}
74+
75+
if(isRoot) {
5476
lineColor = 'rgba(0,0,0,0)';
5577
lineWidth = 0;
5678
} else {
5779
lineColor = Lib.castOption(trace, ptNumber, 'marker.line.color') || Color.defaultLine;
5880
lineWidth = Lib.castOption(trace, ptNumber, 'marker.line.width') || 0;
5981
}
6082

61-
opacity =
62-
trace._hasColorscale || helpers.isLeaf(pt) ? 1 :
63-
pt.onPathbar ? trace.pathbar.opacity :
64-
helpers.isHierarchyRoot(pt) ? 1 :
65-
depthFade(pt.data.depth - trace._entryDepth);
83+
opacity = pt.onPathbar ? trace.pathbar.opacity : null;
6684
}
6785

6886
s.style('stroke-width', lineWidth)
69-
.call(Color.fill, cdi.color)
87+
.call(Color.fill, fillColor)
7088
.call(Color.stroke, lineColor)
7189
.style('opacity', opacity);
7290
}
98 Bytes
Loading
-1.32 KB
Loading
3 Bytes
Loading
245 Bytes
Loading
84 Bytes
Loading
Loading
Loading
-8.58 KB
Loading
-6.41 KB
Loading
Loading
1.07 KB
Loading
-1.76 KB
Loading
-138 Bytes
Loading
1.47 KB
Loading
Loading
Loading

test/image/mocks/treemap_pad_mirror.json

-8
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@
1414
"pad": 0
1515
},
1616
"marker": {
17-
"opacitybase": 0.3,
18-
"opacitystep": 0.1,
1917
"pad": {
2018
"t": 16,
2119
"l": 8,
@@ -105,8 +103,6 @@
105103
"pad": 0
106104
},
107105
"marker": {
108-
"opacitybase": 0.3,
109-
"opacitystep": 0.1,
110106
"pad": {
111107
"t": 16,
112108
"l": 8,
@@ -196,8 +192,6 @@
196192
"pad": 0
197193
},
198194
"marker": {
199-
"opacitybase": 0.3,
200-
"opacitystep": 0.1,
201195
"pad": {
202196
"t": 16,
203197
"l": 8,
@@ -287,8 +281,6 @@
287281
"pad": 0
288282
},
289283
"marker": {
290-
"opacitybase": 0.3,
291-
"opacitystep": 0.1,
292284
"pad": {
293285
"t": 16,
294286
"l": 8,

test/image/mocks/treemap_pad_transpose.json

+4-8
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@
1414
"pad": 0
1515
},
1616
"marker": {
17-
"opacitybase": 0.5,
18-
"opacitystep": 0.05,
1917
"pad": {
2018
"t": 16,
2119
"l": 8,
@@ -105,8 +103,6 @@
105103
"pad": 0
106104
},
107105
"marker": {
108-
"opacitybase": 0.5,
109-
"opacitystep": 0.05,
110106
"pad": {
111107
"t": 16,
112108
"l": 8,
@@ -196,8 +192,6 @@
196192
"pad": 0
197193
},
198194
"marker": {
199-
"opacitybase": 0.5,
200-
"opacitystep": 0.05,
201195
"pad": {
202196
"t": 16,
203197
"l": 8,
@@ -287,8 +281,6 @@
287281
"pad": 0
288282
},
289283
"marker": {
290-
"opacitybase": 0.5,
291-
"opacitystep": 0.05,
292284
"pad": {
293285
"t": 16,
294286
"l": 8,
@@ -385,6 +377,7 @@
385377
"showarrow": false,
386378
"text": "base",
387379
"font": {
380+
"color": "white",
388381
"size": 16
389382
},
390383
"x": 0.25,
@@ -396,6 +389,7 @@
396389
"showarrow": false,
397390
"text": "flip y axis",
398391
"font": {
392+
"color": "white",
399393
"size": 16
400394
},
401395
"x": 0.25,
@@ -407,6 +401,7 @@
407401
"showarrow": false,
408402
"text": "flip x axis",
409403
"font": {
404+
"color": "white",
410405
"size": 16
411406
},
412407
"x": 0.75,
@@ -418,6 +413,7 @@
418413
"showarrow": false,
419414
"text": "flip both axes",
420415
"font": {
416+
"color": "white",
421417
"size": 16
422418
},
423419
"x": 0.75,

test/image/mocks/treemap_textposition.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -977,6 +977,6 @@
977977
"layout": {
978978
"width": 1200,
979979
"height": 1200,
980-
"paper_bgcolor": "#210"
980+
"paper_bgcolor": "black"
981981
}
982982
}

test/jasmine/tests/treemap_test.js

+2-8
Original file line numberDiff line numberDiff line change
@@ -116,17 +116,15 @@ describe('Test treemap defaults:', function() {
116116
expect(fullData[1].marker.line.color).toBe('#fff', 'dflt');
117117
});
118118

119-
it('should not coerce *marker.opacitybase*, *marker.opacitybase* and *pathbar.opacity* when having *colorscale*', function() {
119+
it('should not coerce *marker.opacitybase* and *pathbar.opacity* when having *colorscale*', function() {
120120
_supply([
121121
{labels: [1], parents: ['']},
122122
{labels: [1], parents: [''], marker: {colorscale: 'Blues'}}
123123
]);
124124

125125
expect(fullData[0].marker.opacitybase).toBe(0.5);
126-
expect(fullData[0].marker.opacitystep).toBe(0.5);
127126
expect(fullData[0].pathbar.opacity).toBe(0.5);
128127
expect(fullData[1].marker.opacitybase).toBe(undefined, 'not coerced');
129-
expect(fullData[1].marker.opacitystep).toBe(undefined, 'not coerced');
130128
expect(fullData[1].pathbar.opacity).toBe(undefined, 'not coerced');
131129
});
132130

@@ -1192,7 +1190,7 @@ describe('Test treemap restyle:', function() {
11921190
.then(done);
11931191
});
11941192

1195-
it('should be able to restyle *marker.opacitybase* and *marker.opacitystep*', function(done) {
1193+
it('should be able to restyle *marker.opacitybase*', function(done) {
11961194
var mock = {
11971195
data: [{
11981196
type: 'treemap', pathbar: { visible: false },
@@ -1228,14 +1226,10 @@ describe('Test treemap restyle:', function() {
12281226
})
12291227
.then(_restyle({'marker.opacitybase': 0.2}))
12301228
.then(_assert('lower marker.opacitybase', ['1', '1', '0.2', '0.5', '1', '1']))
1231-
.then(_restyle({'marker.opacitystep': 0.1}))
1232-
.then(_assert('lower marker.opacitystep', ['1', '1', '0.2', '0.1', '0.2', '1']))
12331229
.then(_restyle({'marker.opacitybase': 0.8}))
12341230
.then(_assert('raise marker.opacitybase', ['1', '1', '0.8', '0.1', '0.2', '1']))
12351231
.then(_restyle({'marker.opacitybase': null}))
12361232
.then(_assert('back to dflt', ['1', '1', '0.5', '0.1', '0.2', '1']))
1237-
.then(_restyle({'marker.opacitystep': null}))
1238-
.then(_assert('back to dflt', ['1', '1', '0.5', '0.5', '1', '1']))
12391233
.catch(failTest)
12401234
.then(done);
12411235
});

0 commit comments

Comments
 (0)