Skip to content

Contour auto persist #1338

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

Merged
merged 3 commits into from
Jan 30, 2017
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 20 additions & 7 deletions src/components/colorscale/calc.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,12 @@ module.exports = function calc(trace, vals, containerStr, cLetter) {
inputContainer = trace._input;
}

var auto = container[cLetter + 'auto'],
min = container[cLetter + 'min'],
max = container[cLetter + 'max'],
var autoAttr = cLetter + 'auto',
minAttr = cLetter + 'min',
maxAttr = cLetter + 'max',
auto = container[autoAttr],
min = container[minAttr],
max = container[maxAttr],
scl = container.colorscale;

if(auto !== false || min === undefined) {
Expand All @@ -45,11 +48,21 @@ module.exports = function calc(trace, vals, containerStr, cLetter) {
max += 0.5;
}

container[cLetter + 'min'] = min;
container[cLetter + 'max'] = max;
container[minAttr] = min;
container[maxAttr] = max;

inputContainer[cLetter + 'min'] = min;
inputContainer[cLetter + 'max'] = max;
inputContainer[minAttr] = min;
inputContainer[maxAttr] = max;

/*
* If auto was explicitly false but min or max was missing,
* we filled in the missing piece here but later the trace does
* not look auto.
* Otherwise make sure the trace still looks auto as far as later
* changes are concerned.
*/
inputContainer[autoAttr] = (auto !== false ||
(min === undefined && max === undefined));

if(container.autocolorscale) {
if(min * max < 0) scl = scales.RdBu;
Expand Down
9 changes: 6 additions & 3 deletions src/plot_api/plot_api.js
Original file line number Diff line number Diff line change
Expand Up @@ -1599,7 +1599,10 @@ function _restyle(gd, aobj, _traces) {
flags.docolorbars = true;
}

if(recalcAttrs.indexOf(ai) !== -1) {
var aiArrayStart = ai.indexOf('['),
aiAboveArray = aiArrayStart === -1 ? ai : ai.substr(0, aiArrayStart);

if(recalcAttrs.indexOf(aiAboveArray) !== -1) {
// major enough changes deserve autoscale, autobin, and
// non-reversed axes so people don't get confused
if(['orientation', 'type'].indexOf(ai) !== -1) {
Expand All @@ -1622,8 +1625,8 @@ function _restyle(gd, aobj, _traces) {
}
flags.docalc = true;
}
else if(replotAttrs.indexOf(ai) !== -1) flags.doplot = true;
else if(autorangeAttrs.indexOf(ai) !== -1) flags.docalcAutorange = true;
else if(replotAttrs.indexOf(aiAboveArray) !== -1) flags.doplot = true;
else if(autorangeAttrs.indexOf(aiAboveArray) !== -1) flags.docalcAutorange = true;
}

// do we need to force a recalc?
Expand Down
10 changes: 9 additions & 1 deletion src/traces/contour/calc.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,15 @@ module.exports = function calc(gd, trace) {
}

// copy auto-contour info back to the source data.
trace._input.contours = extendFlat({}, contours);
// previously we copied the whole contours object back, but that had
// other info (coloring, showlines) that should be left to supplyDefaults
if(!trace._input.contours) trace._input.contours = {};
extendFlat(trace._input.contours, {
start: contours.start,
end: contours.end,
size: contours.size
});
trace._input.autocontour = true;
}
else {
// sanity checks on manually-supplied start/end/size
Expand Down
11 changes: 8 additions & 3 deletions test/jasmine/tests/contour_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -296,15 +296,20 @@ describe('contour calc', function() {
contoursFinal.forEach(function(spec) {
var out = _calc({
z: [[0, 2], [3, 5]],
contours: contoursIn,
contours: Lib.extendFlat({}, contoursIn),
ncontours: spec.inputNcontours
}).trace;

['start', 'end', 'size'].forEach(function(attr) {
expect(out.contours[attr]).toBe(spec[attr], [contoursIn, attr]);
expect(out.contours[attr]).toBe(spec[attr], [contoursIn, spec.inputNcontours, attr]);
// all these get copied back to the input trace
expect(out._input.contours[attr]).toBe(spec[attr], [contoursIn, attr]);
expect(out._input.contours[attr]).toBe(spec[attr], [contoursIn, spec.inputNcontours, attr]);
});

expect(out._input.autocontour).toBe(true);
expect(out._input.zauto).toBe(true);
expect(out._input.zmin).toBe(0);
expect(out._input.zmax).toBe(5);
});
});
});
Expand Down
29 changes: 29 additions & 0 deletions test/jasmine/tests/plot_api_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,36 @@ describe('Test plot api', function() {
expect(gd._fullData[0].marker.color).toBe(colorDflt[0]);
expect(gd._fullData[1].marker.color).toBe(colorDflt[1]);
});
});

describe('Plotly.restyle unmocked', function() {
var gd;

beforeEach(function() {
gd = createGraphDiv();
});

afterEach(function() {
destroyGraphDiv();
});

it('should redo auto z/contour when editing z array', function() {
Plotly.plot(gd, [{type: 'contour', z: [[1, 2], [3, 4]]}]).then(function() {
expect(gd.data[0].zauto).toBe(true, gd.data[0]);
expect(gd.data[0].zmin).toBe(1);
expect(gd.data[0].zmax).toBe(4);

expect(gd.data[0].autocontour).toBe(true);
expect(gd.data[0].contours).toEqual({start: 1.5, end: 3.5, size: 0.5});

return Plotly.restyle(gd, {'z[0][0]': 10});
}).then(function() {
expect(gd.data[0].zmin).toBe(2);
expect(gd.data[0].zmax).toBe(10);

expect(gd.data[0].contours).toEqual({start: 3, end: 9, size: 1});
});
});
});

describe('Plotly.deleteTraces', function() {
Expand Down