Skip to content

Commit 4c7a092

Browse files
committed
revisit marker opacities: no opacity with colorscale, now having
- constant value to set pathbar.opacity - marker.opacitybase and marker.opacitystep - no magical auto depthfade
1 parent 01c5f68 commit 4c7a092

25 files changed

+77
-36
lines changed

src/traces/treemap/attributes.js

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -139,15 +139,29 @@ module.exports = {
139139

140140
colors: sunburstAttrs.marker.colors,
141141

142-
opacity: {
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 based on the depth from the entry.',
151+
'This options is not available when having a `colorscale`.'
152+
].join(' ')
153+
},
154+
155+
opacitystep: {
143156
valType: 'number',
144157
editType: 'style',
145158
role: 'style',
146159
min: 0,
147160
max: 1,
161+
dflt: 0.5,
148162
description: [
149-
'Sets the opacity of the branches i.e. based on the hierarchy height.',
150-
'With colorscale it is defaulted to 1; otherwise it is defaulted to 0.7'
163+
'Sets the increment for opacity of the headers based on the depth from the entry.',
164+
'This options is not available when having a `colorscale`.'
151165
].join(' ')
152166
},
153167

@@ -217,6 +231,18 @@ module.exports = {
217231
].join(' ')
218232
},
219233

234+
opacity: {
235+
valType: 'number',
236+
editType: 'style',
237+
role: 'style',
238+
min: 0,
239+
dflt: 0.5,
240+
description: [
241+
'Sets the opacity of the pathbar',
242+
'This options is not available when having a `colorscale`.'
243+
].join(' ')
244+
},
245+
220246
textfont: extendFlat({}, pieAttrs.textfont, {
221247
description: 'Sets the font used inside `pathbar`.'
222248
}),

src/traces/treemap/defaults.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,11 @@ module.exports = function supplyDefaults(traceIn, traceOut, defaultColor, layout
8484

8585
if(withColorscale) {
8686
colorscaleDefaults(traceIn, traceOut, layout, coerce, {prefix: 'marker.', cLetter: 'c'});
87+
} else {
88+
coerce('marker.opacitybase');
89+
coerce('marker.opacitystep');
90+
coerce('pathbar.opacity');
8791
}
88-
coerce('marker.opacity', withColorscale ? 1 : 0.7);
8992

9093
traceOut._hovered = {
9194
marker: {

src/traces/treemap/draw_ancestors.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,7 @@ module.exports = function drawAncestors(gd, cd, entry, slices, opts) {
4141
var trace = cd0.trace;
4242
var hierarchy = cd0.hierarchy;
4343

44-
var entryDepth = entry.data.depth;
45-
46-
var eachWidth = opts.width / entryDepth;
44+
var eachWidth = width / trace._entryDepth;
4745

4846
var pathIds = helpers.listPath(entry.data, 'id');
4947

src/traces/treemap/plot.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,10 +141,14 @@ function plotOne(gd, cd, element, transitionOpts) {
141141
return {};
142142
};
143143

144-
var pad = trace.marker.pad;
144+
trace._entryDepth = entry.data.depth;
145+
if(helpers.isHierarchyRoot(entry)) {
146+
trace._entryDepth++;
147+
}
148+
145149
// N.B. handle multiple-root special case
146150
if(cd0.hasMultipleRoots && helpers.isHierarchyRoot(entry)) {
147-
maxDepth++; // increase maxDepth by one
151+
maxDepth++;
148152
}
149153
trace._maxDepth = maxDepth;
150154

@@ -288,6 +292,7 @@ function plotOne(gd, cd, element, transitionOpts) {
288292
x1 -= hasRight ? TEXTPAD : 0;
289293
}
290294

295+
var pad = trace.marker.pad;
291296
if(opts.isHeader) {
292297
x0 += pad.l - TEXTPAD;
293298
x1 -= pad.r - TEXTPAD;

src/traces/treemap/style.js

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
var d3 = require('d3');
1212
var Color = require('../../components/color');
1313
var Lib = require('../../lib');
14+
var helpers = require('../sunburst/helpers');
1415

1516
function style(gd) {
1617
gd._fullLayout._treemaplayer.selectAll('.trace').each(function(cd) {
@@ -29,39 +30,33 @@ function style(gd) {
2930
}
3031

3132
function styleOne(s, pt, trace, opts) {
33+
var hovered = (opts || {}).hovered;
3234
var cdi = pt.data.data;
3335
var ptNumber = cdi.i;
3436
var lineColor;
3537
var lineWidth;
3638
var opacity;
3739

38-
var depthFn = function(n) {
39-
return Math.pow(trace.marker.opacity, n);
40-
};
41-
42-
var add = function(a, b) {
43-
return (1 - a) * (1 - b);
44-
};
40+
var depthFade = function(n) {
41+
var base = trace.marker.opacitybase;
42+
var step = trace.marker.opacitystep;
4543

46-
var sumUpTo = function(n) {
47-
var sum = 0;
48-
for(var i = 0; i < n; i++) {
49-
sum += add(sum, depthFn(i));
50-
}
51-
return 1 - sum;
44+
return n === 0 ? base :
45+
Math.min(1, n * step);
5246
};
5347

54-
if((opts || {}).hovered) {
48+
if(hovered) {
5549
lineColor = trace._hovered.marker.line.color;
5650
lineWidth = trace._hovered.marker.line.width;
5751
opacity = trace._hovered.marker.opacity;
5852
} else {
5953
lineColor = Lib.castOption(trace, ptNumber, 'marker.line.color') || Color.defaultLine;
6054
lineWidth = Lib.castOption(trace, ptNumber, 'marker.line.width') || 0;
6155

62-
opacity = pt.onPathbar ?
63-
sumUpTo(pt.depth) :
64-
depthFn(pt.height);
56+
opacity =
57+
trace._hasColorscale || helpers.isLeaf(pt) ? 1 :
58+
pt.onPathbar ? trace.pathbar.opacity :
59+
depthFade(pt.data.depth - trace._entryDepth);
6560
}
6661

6762
s.style('stroke-width', lineWidth)
819 Bytes
Loading
217 Bytes
Loading
-7 Bytes
Loading
-504 Bytes
Loading
-717 Bytes
Loading
Loading
192 Bytes
Loading
627 Bytes
Loading
-982 Bytes
Loading
-3.11 KB
Loading
88 Bytes
Loading
-58 Bytes
Loading
Loading
-217 Bytes
Loading
Loading

test/image/mocks/treemap_packages_colorscale_allone.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
"side": "bottom"
1414
},
1515
"marker": {
16-
"opacity": 0.9,
1716
"line": {
1817
"color": "#777"
1918
},

test/image/mocks/treemap_pad_mirror.json

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414
"pad": 0
1515
},
1616
"marker": {
17-
"opacity": 0.8,
17+
"opacitybase": 0.3,
18+
"opacitystep": 0.1,
1819
"pad": {
1920
"t": 16,
2021
"l": 8,
@@ -104,7 +105,8 @@
104105
"pad": 0
105106
},
106107
"marker": {
107-
"opacity": 0.8,
108+
"opacitybase": 0.3,
109+
"opacitystep": 0.1,
108110
"pad": {
109111
"t": 16,
110112
"l": 8,
@@ -194,7 +196,8 @@
194196
"pad": 0
195197
},
196198
"marker": {
197-
"opacity": 0.8,
199+
"opacitybase": 0.3,
200+
"opacitystep": 0.1,
198201
"pad": {
199202
"t": 16,
200203
"l": 8,
@@ -284,7 +287,8 @@
284287
"pad": 0
285288
},
286289
"marker": {
287-
"opacity": 0.8,
290+
"opacitybase": 0.3,
291+
"opacitystep": 0.1,
288292
"pad": {
289293
"t": 16,
290294
"l": 8,

test/image/mocks/treemap_pad_transpose.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
"pad": 0
1515
},
1616
"marker": {
17+
"opacitybase": 0.5,
18+
"opacitystep": 0.05,
1719
"pad": {
1820
"t": 16,
1921
"l": 8,
@@ -103,6 +105,8 @@
103105
"pad": 0
104106
},
105107
"marker": {
108+
"opacitybase": 0.5,
109+
"opacitystep": 0.05,
106110
"pad": {
107111
"t": 16,
108112
"l": 8,
@@ -192,6 +196,8 @@
192196
"pad": 0
193197
},
194198
"marker": {
199+
"opacitybase": 0.5,
200+
"opacitystep": 0.05,
195201
"pad": {
196202
"t": 16,
197203
"l": 8,
@@ -281,6 +287,8 @@
281287
"pad": 0
282288
},
283289
"marker": {
290+
"opacitybase": 0.5,
291+
"opacitystep": 0.05,
284292
"pad": {
285293
"t": 16,
286294
"l": 8,

test/image/mocks/treemap_values_colorscale.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,6 @@
138138
"four"
139139
],
140140
"marker": {
141-
"opacity": 0.5,
142141
"cmin": 0,
143142
"cmax": 25,
144143
"colorscale": "Blackbody",
@@ -173,7 +172,7 @@
173172
},
174173
{
175174
"showarrow": false,
176-
"text": "branchvalues: <b>total</b><br>used as input to marker.color<br>with 0.5 opacity",
175+
"text": "branchvalues: <b>total</b><br>used as input to marker.color",
177176
"x": 0.75,
178177
"xanchor": "center",
179178
"y": 1.1,

test/jasmine/tests/treemap_test.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -114,14 +114,18 @@ describe('Test treemap defaults:', function() {
114114
expect(fullData[1].marker.line.color).toBe('#fff', 'dflt');
115115
});
116116

117-
it('should default *marker.opacity* depending on having or not having *colorscale*', function() {
117+
it('should not coerce *marker.opacitybase*, *marker.opacitybase* and *pathbar.opacity* when having *colorscale*', function() {
118118
_supply([
119119
{labels: [1], parents: ['']},
120120
{labels: [1], parents: [''], marker: {colorscale: 'Blues'}}
121121
]);
122122

123-
expect(fullData[0].marker.opacity).toBe(0.7, 'without colorscale');
124-
expect(fullData[1].marker.opacity).toBe(1, 'with colorscale');
123+
expect(fullData[0].marker.opacitybase).toBe(0.5);
124+
expect(fullData[0].marker.opacitystep).toBe(0.5);
125+
expect(fullData[0].pathbar.opacity).toBe(0.5);
126+
expect(fullData[1].marker.opacitybase).toBe(undefined, 'not coerced');
127+
expect(fullData[1].marker.opacitystep).toBe(undefined, 'not coerced');
128+
expect(fullData[1].pathbar.opacity).toBe(undefined, 'not coerced');
125129
});
126130

127131
it('should use *textfont.size* to adjust top, bottom , left and right *marker.pad* defaults', function() {

0 commit comments

Comments
 (0)