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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add midpoint attr to color scales #3549

Merged
merged 7 commits into from
Feb 19, 2019
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions src/components/colorscale/attributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ module.exports = function colorScaleAttrs(context, opts) {
var auto = cLetter + 'auto';
var min = cLetter + 'min';
var max = cLetter + 'max';
var mid = cLetter + 'mid';
var autoFull = code(contextHead + auto);
var minFull = code(contextHead + min);
var maxFull = code(contextHead + max);
var minmaxFull = minFull + ' and ' + maxFull;
Expand Down Expand Up @@ -160,6 +162,21 @@ module.exports = function colorScaleAttrs(context, opts) {
].join('')
};

attrs[mid] = {
valType: 'number',
role: 'info',
dflt: null,
editType: editTypeOverride || 'plot',
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@etpinard I wasn't sure what to put for editType and impliedEdits btw so I copied over the settings from max above... maybe it should match auto instead? Or be something else? Likely your tests will expose any problems but I just wanted to shine a light here.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Restyle got fixed in 23c1cf0

Is that the behaviour you had in mind?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I must admit I don't fully grok the various nuances of the types of edits, but the test reads fine at first glance!

impliedEdits: minmaxImpliedEdits,
description: [
'Sets the mid-point of the color domain by scaling ', minFull,
' and/or ', maxFull, ' to be equidistant to this point.',
effectDesc,
' Value should have the same units as ', colorAttrFull, '. ',
'Has no effect when ', autoFull, ' is `false`.'
].join('')
};

attrs.colorscale = {
valType: 'colorscale',
role: 'style',
Expand Down
11 changes: 11 additions & 0 deletions src/components/colorscale/calc.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,11 @@ module.exports = function calc(gd, trace, opts) {
var autoAttr = cLetter + 'auto';
var minAttr = cLetter + 'min';
var maxAttr = cLetter + 'max';
var midAttr = cLetter + 'mid';
var auto = container[autoAttr];
var min = container[minAttr];
var max = container[maxAttr];
var mid = container[midAttr];
var scl = container.colorscale;

if(auto !== false || min === undefined) {
Expand All @@ -36,6 +38,15 @@ module.exports = function calc(gd, trace, opts) {
max = Lib.aggNums(Math.max, null, vals);
}

if(auto !== false && mid !== undefined) {
if(max - mid > mid - min) {
min = mid - (max - mid);
}
else if(max - mid < mid - min) {
max = mid + (mid - min);
}
}

if(min === max) {
min -= 0.5;
max += 0.5;
Expand Down
1 change: 1 addition & 0 deletions src/components/colorscale/defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ module.exports = function colorScaleDefaults(traceIn, traceOut, layout, coerce,
coerce(prefix + cLetter + 'auto', !validMinMax);
coerce(prefix + cLetter + 'min');
coerce(prefix + cLetter + 'max');
coerce(prefix + cLetter + 'mid');

// handles both the trace case (autocolorscale is false by default) and
// the marker and marker.line case (autocolorscale is true by default)
Expand Down
1 change: 1 addition & 0 deletions src/traces/scattermapbox/attributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ module.exports = overrideAll({
cauto: markerAttrs.cauto,
cmax: markerAttrs.cmax,
cmin: markerAttrs.cmin,
cmid: markerAttrs.cmid,
autocolorscale: markerAttrs.autocolorscale,
reversescale: markerAttrs.reversescale,
showscale: markerAttrs.showscale,
Expand Down
2 changes: 1 addition & 1 deletion test/jasmine/tests/scattergeo_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ describe('Test scattergeo defaults', function() {
describe('Test scattergeo calc', function() {

function _calc(opts) {
var base = { type: 'scattermapbox' };
var base = { type: 'scattergeo' };
var trace = Lib.extendFlat({}, base, opts);
var gd = { data: [trace] };

Expand Down