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

Skip to content

Commit 67058c9

Browse files
committed
Rework groupby styles into array of objects
1 parent f55e58d commit 67058c9

File tree

2 files changed

+117
-16
lines changed

2 files changed

+117
-16
lines changed

src/transforms/groupby.js

Lines changed: 51 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,24 @@ exports.attributes = {
3535
].join(' ')
3636
},
3737
style: {
38-
valType: 'any',
39-
dflt: {},
40-
description: [
41-
'Sets each group style.',
42-
'For example, with `groups` set to *[\'a\', \'b\', \'a\', \'b\']*',
43-
'and `style` set to *{ a: { marker: { color: \'red\' } }}',
44-
'marker points in group *\'a\'* will be drawn in red.'
45-
].join(' ')
38+
target: {
39+
valType: 'any',
40+
role: 'info',
41+
description: [
42+
'The group value which receives these styles.'
43+
].join(' ')
44+
},
45+
value: {
46+
valType: 'any',
47+
role: 'info',
48+
dflt: {},
49+
description: [
50+
'Sets each group style.',
51+
'For example, with `groups` set to *[\'a\', \'b\', \'a\', \'b\']*',
52+
'and `style` set to *{ a: { marker: { color: \'red\' } }}',
53+
'marker points in group *\'a\'* will be drawn in red.'
54+
].join(' ')
55+
},
4656
}
4757
};
4858

@@ -71,11 +81,27 @@ exports.supplyDefaults = function(transformIn) {
7181
if(!enabled) return transformOut;
7282

7383
coerce('groups');
74-
coerce('style');
84+
85+
var styleIn = transformIn.style;
86+
var styleOut = transformOut.style = [];
87+
88+
if(Array.isArray(styleIn)) {
89+
// If styles are an array, then sanitize them against the schema:
90+
for(var i = 0; i < styleIn.length; i++) {
91+
styleOut[i] = {};
92+
Lib.coerce(styleIn[i], styleOut[i], exports.attributes.style, 'target');
93+
Lib.coerce(styleIn[i], styleOut[i], exports.attributes.style, 'value');
94+
}
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;
99+
}
75100

76101
return transformOut;
77102
};
78103

104+
79105
/**
80106
* Apply transform !!!
81107
*
@@ -115,6 +141,7 @@ function pasteArray(newTrace, trace, j, a) {
115141
}
116142

117143
function transformOne(trace, state) {
144+
var i;
118145
var opts = state.transform;
119146
var groups = trace.transforms[state.transformIndex].groups;
120147

@@ -128,9 +155,20 @@ function transformOne(trace, state) {
128155

129156
var arrayAttrs = PlotSchema.findArrayAttributes(trace);
130157

131-
var style = opts.style || {};
158+
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+
}
169+
}
132170

133-
for(var i = 0; i < groupNames.length; i++) {
171+
for(i = 0; i < groupNames.length; i++) {
134172
var groupName = groupNames[i];
135173

136174
var newTrace = newData[i] = Lib.extendDeepNoArrays({}, trace);
@@ -145,9 +183,9 @@ function transformOne(trace, state) {
145183

146184
newTrace.name = groupName;
147185

148-
// there's no need to coerce style[groupName] here
186+
// there's no need to coerce styleIndex[groupName] here
149187
// as another round of supplyDefaults is done on the transformed traces
150-
newTrace = Lib.extendDeepNoArrays(newTrace, style[groupName] || {});
188+
newTrace = Lib.extendDeepNoArrays(newTrace, styleIndex[groupName] || {});
151189
}
152190

153191
return newData;

test/jasmine/tests/transform_groupby_test.js

Lines changed: 66 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,69 @@ describe('groupby', function() {
6464
});
6565
});
6666

67+
it('Accepts deprecated object notation for styles', function(done) {
68+
var oldStyleMockData = [{
69+
mode: 'markers',
70+
x: [1, -1, -2, 0, 1, 2, 3],
71+
y: [1, 2, 3, 1, 2, 3, 1],
72+
transforms: [{
73+
type: 'groupby',
74+
groups: ['a', 'a', 'b', 'a', 'b', 'b', 'a'],
75+
style: {
76+
a: {marker: {color: 'red'}},
77+
b: {marker: {color: 'blue'}}
78+
}
79+
}]
80+
}];
81+
var data = Lib.extendDeep([], oldStyleMockData);
82+
data[0].marker = { size: 20 };
83+
84+
var gd = createGraphDiv();
85+
var dims = [4, 3];
86+
87+
Plotly.plot(gd, data).then(function() {
88+
assertStyle(dims,
89+
['rgb(255, 0, 0)', 'rgb(0, 0, 255)'],
90+
[1, 1]
91+
);
92+
93+
return Plotly.restyle(gd, 'marker.opacity', 0.4);
94+
}).then(function() {
95+
assertStyle(dims,
96+
['rgb(255, 0, 0)', 'rgb(0, 0, 255)'],
97+
[0.4, 0.4]
98+
);
99+
100+
expect(gd._fullData[0].marker.opacity).toEqual(0.4);
101+
expect(gd._fullData[1].marker.opacity).toEqual(0.4);
102+
103+
return Plotly.restyle(gd, 'marker.opacity', 1);
104+
}).then(function() {
105+
assertStyle(dims,
106+
['rgb(255, 0, 0)', 'rgb(0, 0, 255)'],
107+
[1, 1]
108+
);
109+
110+
expect(gd._fullData[0].marker.opacity).toEqual(1);
111+
expect(gd._fullData[1].marker.opacity).toEqual(1);
112+
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+
});
128+
});
129+
67130
it('Plotly.restyle should work', function(done) {
68131
var data = Lib.extendDeep([], mockData0);
69132
data[0].marker = { size: 20 };
@@ -98,10 +161,10 @@ describe('groupby', function() {
98161
expect(gd._fullData[1].marker.opacity).toEqual(1);
99162

100163
return Plotly.restyle(gd, {
101-
'transforms[0].style': [
164+
'transforms[0].style': [[
102165
{target: 'a', value: {marker: {color: 'green'}}},
103166
{target: 'b', value: {marker: {color: 'red'}}}
104-
],
167+
]],
105168
'marker.opacity': 0.4
106169
});
107170
}).then(function() {
@@ -401,7 +464,7 @@ describe('groupby', function() {
401464
groups: ['a', 'a', 'b', 'a', 'b', 'b', 'a'],
402465
style: [
403466
{target: 'a', value: {marker: {color: 'red'}}},
404-
{taret: 'b', value: {marker: {color: 'blue'}}}
467+
{target: 'b', value: {marker: {color: 'blue'}}}
405468
]
406469
}]
407470
}];

0 commit comments

Comments
 (0)