Skip to content

Commit 88dbc9d

Browse files
authored
Merge pull request #1112 from plotly/colorscale-drawing-fix
Colorscale drawing fix
2 parents c883e57 + 0539e28 commit 88dbc9d

File tree

4 files changed

+38
-44
lines changed

4 files changed

+38
-44
lines changed

src/components/drawing/index.js

+10-39
Original file line numberDiff line numberDiff line change
@@ -276,9 +276,8 @@ drawing.singlePointStyle = function(d, sel, trace) {
276276

277277
// allow array marker and marker line colors to be
278278
// scaled by given max and min to colorscales
279-
var markerIn = (trace._input || {}).marker || {},
280-
markerScale = drawing.tryColorscale(marker, markerIn, ''),
281-
lineScale = drawing.tryColorscale(marker, markerIn, 'line.');
279+
var markerScale = drawing.tryColorscale(marker, ''),
280+
lineScale = drawing.tryColorscale(marker, 'line');
282281

283282
singlePointStyle(d, sel, trace, markerScale, lineScale, marker, markerLine);
284283

@@ -290,50 +289,22 @@ drawing.pointStyle = function(s, trace) {
290289
// allow array marker and marker line colors to be
291290
// scaled by given max and min to colorscales
292291
var marker = trace.marker;
293-
var markerIn = (trace._input || {}).marker || {},
294-
markerScale = drawing.tryColorscale(marker, markerIn, ''),
295-
lineScale = drawing.tryColorscale(marker, markerIn, 'line.');
292+
var markerScale = drawing.tryColorscale(marker, ''),
293+
lineScale = drawing.tryColorscale(marker, 'line');
296294

297295
s.each(function(d) {
298296
drawing.singlePointStyle(d, d3.select(this), trace, markerScale, lineScale);
299297
});
300298
};
301299

302-
// for a given color attribute (ie m -> mc = marker.color) look to see if we
303-
// have a colorscale for it (ie mscl, mcmin, mcmax) - if we do, translate
304-
// all numeric color values according to that scale
305-
drawing.tryColorscale = function(cont, contIn, prefix) {
306-
var colorArray = Lib.nestedProperty(cont, prefix + 'color').get(),
307-
scl = Lib.nestedProperty(cont, prefix + 'colorscale').get(),
308-
auto = Lib.nestedProperty(cont, prefix + 'cauto').get(),
309-
minProp = Lib.nestedProperty(cont, prefix + 'cmin'),
310-
maxProp = Lib.nestedProperty(cont, prefix + 'cmax'),
311-
min = minProp.get(),
312-
max = maxProp.get();
313-
314-
// TODO handle this in Colorscale.calc
315-
if(scl && Array.isArray(colorArray)) {
316-
if(auto || !isNumeric(min) || !isNumeric(max)) {
317-
min = Infinity;
318-
max = -Infinity;
319-
colorArray.forEach(function(color) {
320-
if(isNumeric(color)) {
321-
if(min > color) min = +color;
322-
if(max < color) max = +color;
323-
}
324-
});
325-
if(min > max) {
326-
min = 0;
327-
max = 1;
328-
}
329-
minProp.set(min);
330-
maxProp.set(max);
331-
Lib.nestedProperty(contIn, prefix + 'cmin').set(min);
332-
Lib.nestedProperty(contIn, prefix + 'cmax').set(max);
333-
}
300+
drawing.tryColorscale = function(marker, prefix) {
301+
var cont = prefix ? Lib.nestedProperty(marker, prefix).get() : marker,
302+
scl = cont.colorscale,
303+
colorArray = cont.color;
334304

305+
if(scl && Array.isArray(colorArray)) {
335306
return Colorscale.makeColorScaleFunc(
336-
Colorscale.extractScale(scl, min, max)
307+
Colorscale.extractScale(scl, cont.cmin, cont.cmax)
337308
);
338309
}
339310
else return Lib.identity;

src/traces/bar/style.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,8 @@ module.exports = function style(gd) {
4141
var trace = d[0].trace,
4242
marker = trace.marker,
4343
markerLine = marker.line,
44-
markerIn = (trace._input || {}).marker || {},
45-
markerScale = Drawing.tryColorscale(marker, markerIn, ''),
46-
lineScale = Drawing.tryColorscale(marker, markerIn, 'line.');
44+
markerScale = Drawing.tryColorscale(marker, ''),
45+
lineScale = Drawing.tryColorscale(marker, 'line');
4746

4847
d3.select(this).selectAll('path').each(function(d) {
4948
// allow all marker and marker line colors to be scaled
Loading

test/image/mocks/colorscale_constraint.json

+26-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,12 @@
66
"color": [1,2,3,4],
77
"cmin": 2,
88
"cmax": 3,
9-
"showscale": true
9+
"colorbar": {
10+
"title": "trace 0",
11+
"len": 0.3,
12+
"y": 0,
13+
"yanchor": "bottom"
14+
}
1015
}
1116
}, {
1217
"x": [1,2,3,4,5],
@@ -15,7 +20,26 @@
1520
"color": [1,2,3,4],
1621
"cmin": 1,
1722
"cmax": 4,
18-
"showscale": false
23+
"colorbar": {
24+
"title": "trace 1",
25+
"len": 0.3,
26+
"y": 0.3,
27+
"yanchor": "bottom"
28+
}
29+
}
30+
}, {
31+
"x": [1,2,3,4],
32+
"y": [2.5,2.5,2.5,2.5],
33+
"mode": "markers",
34+
"marker": {
35+
"color": [0,0,0,0],
36+
"size": 10,
37+
"colorbar": {
38+
"title": "trace 2",
39+
"len": 0.3,
40+
"y": 0.6,
41+
"yanchor": "bottom"
42+
}
1943
}
2044
}],
2145
"layout": {

0 commit comments

Comments
 (0)