Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Colorscale drawing fix #1112

Merged
merged 3 commits into from
Nov 10, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
49 changes: 10 additions & 39 deletions src/components/drawing/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -276,9 +276,8 @@ drawing.singlePointStyle = function(d, sel, trace) {

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

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

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

s.each(function(d) {
drawing.singlePointStyle(d, d3.select(this), trace, markerScale, lineScale);
});
};

// for a given color attribute (ie m -> mc = marker.color) look to see if we
// have a colorscale for it (ie mscl, mcmin, mcmax) - if we do, translate
// all numeric color values according to that scale
drawing.tryColorscale = function(cont, contIn, prefix) {
var colorArray = Lib.nestedProperty(cont, prefix + 'color').get(),
scl = Lib.nestedProperty(cont, prefix + 'colorscale').get(),
auto = Lib.nestedProperty(cont, prefix + 'cauto').get(),
minProp = Lib.nestedProperty(cont, prefix + 'cmin'),
maxProp = Lib.nestedProperty(cont, prefix + 'cmax'),
min = minProp.get(),
max = maxProp.get();

// TODO handle this in Colorscale.calc
if(scl && Array.isArray(colorArray)) {
if(auto || !isNumeric(min) || !isNumeric(max)) {
min = Infinity;
max = -Infinity;
colorArray.forEach(function(color) {
if(isNumeric(color)) {
if(min > color) min = +color;
if(max < color) max = +color;
}
});
if(min > max) {
min = 0;
max = 1;
}
minProp.set(min);
maxProp.set(max);
Lib.nestedProperty(contIn, prefix + 'cmin').set(min);
Lib.nestedProperty(contIn, prefix + 'cmax').set(max);
}
drawing.tryColorscale = function(marker, prefix) {
var cont = prefix ? Lib.nestedProperty(marker, prefix).get() : marker,
scl = cont.colorscale,
colorArray = cont.color;

if(scl && Array.isArray(colorArray)) {
return Colorscale.makeColorScaleFunc(
Colorscale.extractScale(scl, min, max)
Colorscale.extractScale(scl, cont.cmin, cont.cmax)
);
}
else return Lib.identity;
Expand Down
5 changes: 2 additions & 3 deletions src/traces/bar/style.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,8 @@ module.exports = function style(gd) {
var trace = d[0].trace,
marker = trace.marker,
markerLine = marker.line,
markerIn = (trace._input || {}).marker || {},
markerScale = Drawing.tryColorscale(marker, markerIn, ''),
lineScale = Drawing.tryColorscale(marker, markerIn, 'line.');
markerScale = Drawing.tryColorscale(marker, ''),
lineScale = Drawing.tryColorscale(marker, 'line');

d3.select(this).selectAll('path').each(function(d) {
// allow all marker and marker line colors to be scaled
Expand Down
Binary file modified test/image/baselines/colorscale_constraint.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
28 changes: 26 additions & 2 deletions test/image/mocks/colorscale_constraint.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,12 @@
"color": [1,2,3,4],
"cmin": 2,
"cmax": 3,
"showscale": true
"colorbar": {
"title": "trace 0",
"len": 0.3,
"y": 0,
"yanchor": "bottom"
}
}
}, {
"x": [1,2,3,4,5],
Expand All @@ -15,7 +20,26 @@
"color": [1,2,3,4],
"cmin": 1,
"cmax": 4,
"showscale": false
"colorbar": {
"title": "trace 1",
"len": 0.3,
"y": 0.3,
"yanchor": "bottom"
}
}
}, {
"x": [1,2,3,4],
"y": [2.5,2.5,2.5,2.5],
"mode": "markers",
"marker": {
"color": [0,0,0,0],
"size": 10,
"colorbar": {
"title": "trace 2",
"len": 0.3,
"y": 0.6,
"yanchor": "bottom"
}
}
}],
"layout": {
Expand Down