Skip to content

Commit 5b86f99

Browse files
authored
Merge pull request #1338 from plotly/contour-auto-persist
Contour auto persist
2 parents 2070279 + 6407795 commit 5b86f99

File tree

5 files changed

+72
-14
lines changed

5 files changed

+72
-14
lines changed

src/components/colorscale/calc.js

+20-7
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,12 @@ module.exports = function calc(trace, vals, containerStr, cLetter) {
2727
inputContainer = trace._input;
2828
}
2929

30-
var auto = container[cLetter + 'auto'],
31-
min = container[cLetter + 'min'],
32-
max = container[cLetter + 'max'],
30+
var autoAttr = cLetter + 'auto',
31+
minAttr = cLetter + 'min',
32+
maxAttr = cLetter + 'max',
33+
auto = container[autoAttr],
34+
min = container[minAttr],
35+
max = container[maxAttr],
3336
scl = container.colorscale;
3437

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

48-
container[cLetter + 'min'] = min;
49-
container[cLetter + 'max'] = max;
51+
container[minAttr] = min;
52+
container[maxAttr] = max;
5053

51-
inputContainer[cLetter + 'min'] = min;
52-
inputContainer[cLetter + 'max'] = max;
54+
inputContainer[minAttr] = min;
55+
inputContainer[maxAttr] = max;
56+
57+
/*
58+
* If auto was explicitly false but min or max was missing,
59+
* we filled in the missing piece here but later the trace does
60+
* not look auto.
61+
* Otherwise make sure the trace still looks auto as far as later
62+
* changes are concerned.
63+
*/
64+
inputContainer[autoAttr] = (auto !== false ||
65+
(min === undefined && max === undefined));
5366

5467
if(container.autocolorscale) {
5568
if(min * max < 0) scl = scales.RdBu;

src/plot_api/plot_api.js

+6-3
Original file line numberDiff line numberDiff line change
@@ -1599,7 +1599,10 @@ function _restyle(gd, aobj, _traces) {
15991599
flags.docolorbars = true;
16001600
}
16011601

1602-
if(recalcAttrs.indexOf(ai) !== -1) {
1602+
var aiArrayStart = ai.indexOf('['),
1603+
aiAboveArray = aiArrayStart === -1 ? ai : ai.substr(0, aiArrayStart);
1604+
1605+
if(recalcAttrs.indexOf(aiAboveArray) !== -1) {
16031606
// major enough changes deserve autoscale, autobin, and
16041607
// non-reversed axes so people don't get confused
16051608
if(['orientation', 'type'].indexOf(ai) !== -1) {
@@ -1622,8 +1625,8 @@ function _restyle(gd, aobj, _traces) {
16221625
}
16231626
flags.docalc = true;
16241627
}
1625-
else if(replotAttrs.indexOf(ai) !== -1) flags.doplot = true;
1626-
else if(autorangeAttrs.indexOf(ai) !== -1) flags.docalcAutorange = true;
1628+
else if(replotAttrs.indexOf(aiAboveArray) !== -1) flags.doplot = true;
1629+
else if(autorangeAttrs.indexOf(aiAboveArray) !== -1) flags.docalcAutorange = true;
16271630
}
16281631

16291632
// do we need to force a recalc?

src/traces/contour/calc.js

+9-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,15 @@ module.exports = function calc(gd, trace) {
4242
}
4343

4444
// copy auto-contour info back to the source data.
45-
trace._input.contours = extendFlat({}, contours);
45+
// previously we copied the whole contours object back, but that had
46+
// other info (coloring, showlines) that should be left to supplyDefaults
47+
if(!trace._input.contours) trace._input.contours = {};
48+
extendFlat(trace._input.contours, {
49+
start: contours.start,
50+
end: contours.end,
51+
size: contours.size
52+
});
53+
trace._input.autocontour = true;
4654
}
4755
else {
4856
// sanity checks on manually-supplied start/end/size

test/jasmine/tests/contour_test.js

+8-3
Original file line numberDiff line numberDiff line change
@@ -296,15 +296,20 @@ describe('contour calc', function() {
296296
contoursFinal.forEach(function(spec) {
297297
var out = _calc({
298298
z: [[0, 2], [3, 5]],
299-
contours: contoursIn,
299+
contours: Lib.extendFlat({}, contoursIn),
300300
ncontours: spec.inputNcontours
301301
}).trace;
302302

303303
['start', 'end', 'size'].forEach(function(attr) {
304-
expect(out.contours[attr]).toBe(spec[attr], [contoursIn, attr]);
304+
expect(out.contours[attr]).toBe(spec[attr], [contoursIn, spec.inputNcontours, attr]);
305305
// all these get copied back to the input trace
306-
expect(out._input.contours[attr]).toBe(spec[attr], [contoursIn, attr]);
306+
expect(out._input.contours[attr]).toBe(spec[attr], [contoursIn, spec.inputNcontours, attr]);
307307
});
308+
309+
expect(out._input.autocontour).toBe(true);
310+
expect(out._input.zauto).toBe(true);
311+
expect(out._input.zmin).toBe(0);
312+
expect(out._input.zmax).toBe(5);
308313
});
309314
});
310315
});

test/jasmine/tests/plot_api_test.js

+29
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,36 @@ describe('Test plot api', function() {
282282
expect(gd._fullData[0].marker.color).toBe(colorDflt[0]);
283283
expect(gd._fullData[1].marker.color).toBe(colorDflt[1]);
284284
});
285+
});
286+
287+
describe('Plotly.restyle unmocked', function() {
288+
var gd;
289+
290+
beforeEach(function() {
291+
gd = createGraphDiv();
292+
});
285293

294+
afterEach(function() {
295+
destroyGraphDiv();
296+
});
297+
298+
it('should redo auto z/contour when editing z array', function() {
299+
Plotly.plot(gd, [{type: 'contour', z: [[1, 2], [3, 4]]}]).then(function() {
300+
expect(gd.data[0].zauto).toBe(true, gd.data[0]);
301+
expect(gd.data[0].zmin).toBe(1);
302+
expect(gd.data[0].zmax).toBe(4);
303+
304+
expect(gd.data[0].autocontour).toBe(true);
305+
expect(gd.data[0].contours).toEqual({start: 1.5, end: 3.5, size: 0.5});
306+
307+
return Plotly.restyle(gd, {'z[0][0]': 10});
308+
}).then(function() {
309+
expect(gd.data[0].zmin).toBe(2);
310+
expect(gd.data[0].zmax).toBe(10);
311+
312+
expect(gd.data[0].contours).toEqual({start: 3, end: 9, size: 1});
313+
});
314+
});
286315
});
287316

288317
describe('Plotly.deleteTraces', function() {

0 commit comments

Comments
 (0)