Skip to content

Commit abf8ec5

Browse files
committed
Skip undefined props on restyle
1 parent 4988665 commit abf8ec5

File tree

2 files changed

+60
-0
lines changed

2 files changed

+60
-0
lines changed

src/plot_api/plot_api.js

+2
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) {

test/jasmine/tests/plot_api_test.js

+58
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,64 @@ describe('Test plot api', function() {
9696
expect(gd.calcdata).toBeDefined();
9797
});
9898

99+
it('ignores undefined values', function() {
100+
var gd = {
101+
data: [{x: [1, 2, 3], y: [1, 2, 3], type: 'scatter'}],
102+
layout: {}
103+
};
104+
105+
mockDefaultsAndCalc(gd);
106+
107+
// Check to see that the color is updated:
108+
Plotly.restyle(gd, {'marker.color': 'blue'});
109+
expect(gd._fullData[0].marker.color).toBe('blue');
110+
111+
// Check to see that the color is unaffected:
112+
Plotly.restyle(gd, {'marker.color': undefined});
113+
expect(gd._fullData[0].marker.color).toBe('blue');
114+
});
115+
116+
it('restores null values to defaults', function() {
117+
var gd = {
118+
data: [{x: [1, 2, 3], y: [1, 2, 3], type: 'scatter'}],
119+
layout: {}
120+
};
121+
122+
mockDefaultsAndCalc(gd);
123+
var colorDflt = gd._fullData[0].marker.color;
124+
125+
// Check to see that the color is updated:
126+
Plotly.restyle(gd, {'marker.color': 'blue'});
127+
expect(gd._fullData[0].marker.color).toBe('blue');
128+
129+
// Check to see that the color is restored to the original default:
130+
Plotly.restyle(gd, {'marker.color': null});
131+
expect(gd._fullData[0].marker.color).toBe(colorDflt);
132+
});
133+
134+
it('can target specific traces by leaving properties undefined', function() {
135+
var gd = {
136+
data: [
137+
{x: [1, 2, 3], y: [1, 2, 3], type: 'scatter'},
138+
{x: [1, 2, 3], y: [3, 4, 5], type: 'scatter'}
139+
],
140+
layout: {}
141+
};
142+
143+
mockDefaultsAndCalc(gd);
144+
var colorDflt = [gd._fullData[0].marker.color, gd._fullData[1].marker.color];
145+
146+
// Check only second trace's color has been changed:
147+
Plotly.restyle(gd, {'marker.color': [undefined, 'green']});
148+
expect(gd._fullData[0].marker.color).toBe(colorDflt[0]);
149+
expect(gd._fullData[1].marker.color).toBe('green');
150+
151+
// Check both colors restored to the original default:
152+
Plotly.restyle(gd, {'marker.color': [null, null]});
153+
expect(gd._fullData[0].marker.color).toBe(colorDflt[0]);
154+
expect(gd._fullData[1].marker.color).toBe(colorDflt[1]);
155+
});
156+
99157
});
100158

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

0 commit comments

Comments
 (0)