Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit f67198b

Browse files
committed
Move old-style groupby styles fix to cleanData
1 parent 7beddad commit f67198b

File tree

3 files changed

+37
-45
lines changed

3 files changed

+37
-45
lines changed

src/plot_api/helpers.js

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,6 @@ function cleanAxRef(container, attr) {
215215
// Make a few changes to the data right away
216216
// before it gets used for anything
217217
exports.cleanData = function(data, existingData) {
218-
219218
// Enforce unique IDs
220219
var suids = [], // seen uids --- so we can weed out incoming repeats
221220
uids = data.concat(Array.isArray(existingData) ? existingData : [])
@@ -348,18 +347,35 @@ exports.cleanData = function(data, existingData) {
348347

349348
if(!Lib.isPlainObject(transform)) continue;
350349

351-
if(transform.type === 'filter') {
352-
if(transform.filtersrc) {
353-
transform.target = transform.filtersrc;
354-
delete transform.filtersrc;
355-
}
350+
switch(transform.type) {
351+
case 'filter':
352+
if(transform.filtersrc) {
353+
transform.target = transform.filtersrc;
354+
delete transform.filtersrc;
355+
}
356356

357-
if(transform.calendar) {
358-
if(!transform.valuecalendar) {
359-
transform.valuecalendar = transform.calendar;
357+
if(transform.calendar) {
358+
if(!transform.valuecalendar) {
359+
transform.valuecalendar = transform.calendar;
360+
}
361+
delete transform.calendar;
362+
}
363+
break;
364+
365+
case 'groupby':
366+
if(transform.style && !Array.isArray(transform.style)) {
367+
var prevStyles = transform.style;
368+
var styleKeys = Object.keys(prevStyles);
369+
370+
transform.style = [];
371+
for(var j = 0; j < styleKeys.length; j++) {
372+
transform.style.push({
373+
target: styleKeys[j],
374+
value: prevStyles[styleKeys[j]]
375+
});
376+
}
360377
}
361-
delete transform.calendar;
362-
}
378+
break;
363379
}
364380
}
365381
}

src/transforms/groupby.js

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -85,17 +85,12 @@ exports.supplyDefaults = function(transformIn) {
8585
var styleIn = transformIn.style;
8686
var styleOut = transformOut.style = [];
8787

88-
if(Array.isArray(styleIn)) {
89-
// If styles are an array, then sanitize them against the schema:
88+
if(styleIn) {
9089
for(var i = 0; i < styleIn.length; i++) {
9190
styleOut[i] = {};
9291
Lib.coerce(styleIn[i], styleOut[i], exports.attributes.style, 'target');
9392
Lib.coerce(styleIn[i], styleOut[i], exports.attributes.style, 'value');
9493
}
95-
} else {
96-
// Otherwise, this is deprecated keyed-object-style styles and we
97-
// just pass the object through as the index itself:
98-
transformOut.style = transformIn.style;
9994
}
10095

10196
return transformOut;
@@ -156,16 +151,9 @@ function transformOne(trace, state) {
156151
var arrayAttrs = PlotSchema.findArrayAttributes(trace);
157152

158153
var style = opts.style || [];
159-
var styleIndex = style;
160-
161-
// If the styles are an array, then translate it into an index keyed on the
162-
// target. Otherwise just use the object provided for the sake of backward
163-
// compatibility.
164-
if(Array.isArray(style)) {
165-
styleIndex = {};
166-
for(i = 0; i < style.length; i++) {
167-
styleIndex[style[i].target] = style[i].value;
168-
}
154+
var styleLookup = {};
155+
for(i = 0; i < style.length; i++) {
156+
styleLookup[style[i].target] = style[i].value;
169157
}
170158

171159
for(i = 0; i < groupNames.length; i++) {
@@ -183,9 +171,9 @@ function transformOne(trace, state) {
183171

184172
newTrace.name = groupName;
185173

186-
// there's no need to coerce styleIndex[groupName] here
174+
// there's no need to coerce styleLookup[groupName] here
187175
// as another round of supplyDefaults is done on the transformed traces
188-
newTrace = Lib.extendDeepNoArrays(newTrace, styleIndex[groupName] || {});
176+
newTrace = Lib.extendDeepNoArrays(newTrace, styleLookup[groupName] || {});
189177
}
190178

191179
return newData;

test/jasmine/tests/transform_groupby_test.js

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ var destroyGraphDiv = require('../assets/destroy_graph_div');
66
var assertDims = require('../assets/assert_dims');
77
var assertStyle = require('../assets/assert_style');
88

9-
109
describe('groupby', function() {
1110

1211
describe('one-to-many transforms:', function() {
@@ -109,22 +108,11 @@ describe('groupby', function() {
109108

110109
expect(gd._fullData[0].marker.opacity).toEqual(1);
111110
expect(gd._fullData[1].marker.opacity).toEqual(1);
111+
}).then(done);
112112

113-
return Plotly.restyle(gd, {
114-
'transforms[0].style': {
115-
a: {marker: {color: 'green'}},
116-
b: {marker: {color: 'red'}}
117-
},
118-
'marker.opacity': 0.4
119-
});
120-
}).then(function() {
121-
assertStyle(dims,
122-
['rgb(0, 128, 0)', 'rgb(255, 0, 0)'],
123-
[0.4, 0.4]
124-
);
125-
126-
done();
127-
});
113+
// The final test for restyle updates using deprecated syntax
114+
// is ommitted since old style syntax is *only* sanitized on
115+
// initial plot, *not* on restyle.
128116
});
129117

130118
it('Plotly.restyle should work', function(done) {

0 commit comments

Comments
 (0)