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

Lines changed: 7 additions & 21 deletions
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

Lines changed: 3 additions & 3 deletions
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

Lines changed: 11 additions & 10 deletions
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

Lines changed: 5 additions & 5 deletions
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

Lines changed: 33 additions & 15 deletions
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

0 commit comments

Comments
 (0)