-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Copy pathgroupby.js
184 lines (151 loc) · 5.03 KB
/
groupby.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var Lib = require('../lib');
var PlotSchema = require('../plot_api/plot_schema');
var Plots = require('../plots/plots');
exports.moduleType = 'transform';
exports.name = 'groupby';
exports.attributes = {
enabled: {
valType: 'boolean',
dflt: true,
description: [
'Determines whether this group-by transform is enabled or disabled.'
].join(' ')
},
groups: {
valType: 'data_array',
dflt: [],
description: [
'Sets the groups in which the trace data will be split.',
'For example, with `x` set to *[1, 2, 3, 4]* and',
'`groups` set to *[\'a\', \'b\', \'a\', \'b\']*,',
'the groupby transform with split in one trace',
'with `x` [1, 3] and one trace with `x` [2, 4].'
].join(' ')
},
styles: {
_isLinkedToArray: 'style',
target: {
valType: 'string',
role: 'info',
description: [
'The group value which receives these styles.'
].join(' ')
},
value: {
valType: 'any',
role: 'info',
dflt: {},
description: [
'Sets each group styles.',
'For example, with `groups` set to *[\'a\', \'b\', \'a\', \'b\']*',
'and `styles` set to *[{target: \'a\', value: { marker: { color: \'red\' } }}]',
'marker points in group *\'a\'* will be drawn in red.'
].join(' ')
},
}
};
/**
* Supply transform attributes defaults
*
* @param {object} transformIn
* object linked to trace.transforms[i] with 'type' set to exports.name
* @param {object} fullData
* the plot's full data
* @param {object} layout
* the plot's (not-so-full) layout
*
* @return {object} transformOut
* copy of transformIn that contains attribute defaults
*/
exports.supplyDefaults = function(transformIn) {
var transformOut = {};
function coerce(attr, dflt) {
return Lib.coerce(transformIn, transformOut, exports.attributes, attr, dflt);
}
var enabled = coerce('enabled');
if(!enabled) return transformOut;
coerce('groups');
var styleIn = transformIn.styles;
var styleOut = transformOut.styles = [];
if(styleIn) {
for(var i = 0; i < styleIn.length; i++) {
styleOut[i] = {};
Lib.coerce(styleIn[i], styleOut[i], exports.attributes.styles, 'target');
Lib.coerce(styleIn[i], styleOut[i], exports.attributes.styles, 'value');
}
}
return transformOut;
};
/**
* Apply transform !!!
*
* @param {array} data
* array of transformed traces (is [fullTrace] upon first transform)
*
* @param {object} state
* state object which includes:
* - transform {object} full transform attributes
* - fullTrace {object} full trace object which is being transformed
* - fullData {array} full pre-transform(s) data array
* - layout {object} the plot's (not-so-full) layout
*
* @return {object} newData
* array of transformed traces
*/
exports.transform = function(data, state) {
var newData = [];
for(var i = 0; i < data.length; i++) {
newData = newData.concat(transformOne(data[i], state));
}
return newData;
};
function initializeArray(newTrace, a) {
Lib.nestedProperty(newTrace, a).set([]);
}
function pasteArray(newTrace, trace, j, a) {
Lib.nestedProperty(newTrace, a).set(
Lib.nestedProperty(newTrace, a).get().concat([
Lib.nestedProperty(trace, a).get()[j]
])
);
}
function transformOne(trace, state) {
var i;
var opts = state.transform;
var groups = trace.transforms[state.transformIndex].groups;
if(!(Array.isArray(groups)) || groups.length === 0) {
return trace;
}
var groupNames = Lib.filterUnique(groups),
newData = new Array(groupNames.length),
len = groups.length;
var arrayAttrs = PlotSchema.findArrayAttributes(trace);
var styles = opts.styles || [];
var styleLookup = {};
for(i = 0; i < styles.length; i++) {
styleLookup[styles[i].target] = styles[i].value;
}
for(i = 0; i < groupNames.length; i++) {
var groupName = groupNames[i];
var newTrace = newData[i] = Lib.extendDeepNoArrays({}, trace);
arrayAttrs.forEach(initializeArray.bind(null, newTrace));
for(var j = 0; j < len; j++) {
if(groups[j] !== groupName) continue;
arrayAttrs.forEach(pasteArray.bind(0, newTrace, trace, j));
}
newTrace.name = groupName;
Plots.clearExpandedTraceDefaultColors(newTrace);
// there's no need to coerce styleLookup[groupName] here
// as another round of supplyDefaults is done on the transformed traces
newTrace = Lib.extendDeepNoArrays(newTrace, styleLookup[groupName] || {});
}
return newData;
}