Skip to content

Commit 7387b2b

Browse files
authored
Merge pull request #844 from plotly/restyle-null-vs-undefined
Ignore undefined props on restyle/relayout
2 parents 4988665 + 1a8e0f5 commit 7387b2b

File tree

2 files changed

+96
-0
lines changed

2 files changed

+96
-0
lines changed

src/plot_api/plot_api.js

+4
Original file line numberDiff line numberDiff line change
@@ -1752,6 +1752,8 @@ Plotly.restyle = function restyle(gd, astr, val, traces) {
17521752
oldVal = param.get();
17531753
newVal = Array.isArray(vi) ? vi[i % vi.length] : vi;
17541754

1755+
if(newVal === undefined) continue;
1756+
17551757
// setting bin or z settings should turn off auto
17561758
// and setting auto should save bin or z settings
17571759
if(zscl.indexOf(ai) !== -1) {
@@ -2185,6 +2187,8 @@ Plotly.relayout = function relayout(gd, astr, val) {
21852187
parentFull = Lib.nestedProperty(fullLayout, ptrunk).get(),
21862188
diff;
21872189

2190+
if(vi === undefined) continue;
2191+
21882192
redoit[ai] = vi;
21892193

21902194
// axis reverse is special - it is its own inverse

test/jasmine/tests/plot_api_test.js

+92
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,40 @@ describe('Test plot api', function() {
4848
})
4949
.then(done);
5050
});
51+
52+
it('sets null values to their default', function(done) {
53+
var defaultWidth;
54+
Plotly.plot(gd, [{ x: [1, 2, 3], y: [1, 2, 3] }])
55+
.then(function() {
56+
defaultWidth = gd._fullLayout.width;
57+
return Plotly.relayout(gd, { width: defaultWidth - 25});
58+
})
59+
.then(function() {
60+
expect(gd._fullLayout.width).toBe(defaultWidth - 25);
61+
return Plotly.relayout(gd, { width: null });
62+
})
63+
.then(function() {
64+
expect(gd._fullLayout.width).toBe(defaultWidth);
65+
})
66+
.then(done);
67+
});
68+
69+
it('ignores undefined values', function(done) {
70+
var defaultWidth;
71+
Plotly.plot(gd, [{ x: [1, 2, 3], y: [1, 2, 3] }])
72+
.then(function() {
73+
defaultWidth = gd._fullLayout.width;
74+
return Plotly.relayout(gd, { width: defaultWidth - 25});
75+
})
76+
.then(function() {
77+
expect(gd._fullLayout.width).toBe(defaultWidth - 25);
78+
return Plotly.relayout(gd, { width: undefined });
79+
})
80+
.then(function() {
81+
expect(gd._fullLayout.width).toBe(defaultWidth - 25);
82+
})
83+
.then(done);
84+
});
5185
});
5286

5387
describe('Plotly.restyle', function() {
@@ -96,6 +130,64 @@ describe('Test plot api', function() {
96130
expect(gd.calcdata).toBeDefined();
97131
});
98132

133+
it('ignores undefined values', function() {
134+
var gd = {
135+
data: [{x: [1, 2, 3], y: [1, 2, 3], type: 'scatter'}],
136+
layout: {}
137+
};
138+
139+
mockDefaultsAndCalc(gd);
140+
141+
// Check to see that the color is updated:
142+
Plotly.restyle(gd, {'marker.color': 'blue'});
143+
expect(gd._fullData[0].marker.color).toBe('blue');
144+
145+
// Check to see that the color is unaffected:
146+
Plotly.restyle(gd, {'marker.color': undefined});
147+
expect(gd._fullData[0].marker.color).toBe('blue');
148+
});
149+
150+
it('restores null values to defaults', function() {
151+
var gd = {
152+
data: [{x: [1, 2, 3], y: [1, 2, 3], type: 'scatter'}],
153+
layout: {}
154+
};
155+
156+
mockDefaultsAndCalc(gd);
157+
var colorDflt = gd._fullData[0].marker.color;
158+
159+
// Check to see that the color is updated:
160+
Plotly.restyle(gd, {'marker.color': 'blue'});
161+
expect(gd._fullData[0].marker.color).toBe('blue');
162+
163+
// Check to see that the color is restored to the original default:
164+
Plotly.restyle(gd, {'marker.color': null});
165+
expect(gd._fullData[0].marker.color).toBe(colorDflt);
166+
});
167+
168+
it('can target specific traces by leaving properties undefined', function() {
169+
var gd = {
170+
data: [
171+
{x: [1, 2, 3], y: [1, 2, 3], type: 'scatter'},
172+
{x: [1, 2, 3], y: [3, 4, 5], type: 'scatter'}
173+
],
174+
layout: {}
175+
};
176+
177+
mockDefaultsAndCalc(gd);
178+
var colorDflt = [gd._fullData[0].marker.color, gd._fullData[1].marker.color];
179+
180+
// Check only second trace's color has been changed:
181+
Plotly.restyle(gd, {'marker.color': [undefined, 'green']});
182+
expect(gd._fullData[0].marker.color).toBe(colorDflt[0]);
183+
expect(gd._fullData[1].marker.color).toBe('green');
184+
185+
// Check both colors restored to the original default:
186+
Plotly.restyle(gd, {'marker.color': [null, null]});
187+
expect(gd._fullData[0].marker.color).toBe(colorDflt[0]);
188+
expect(gd._fullData[1].marker.color).toBe(colorDflt[1]);
189+
});
190+
99191
});
100192

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

0 commit comments

Comments
 (0)