forked from plotly/plotly.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgroupby.js
133 lines (104 loc) · 3.12 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
/**
* Copyright 2012-2016, 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('@src/lib');
var Lib = require('../lib');
/*eslint no-unused-vars: 0*/
// so that Plotly.register knows what to do with it
exports.moduleType = 'transform';
// determines to link between transform type and transform module
exports.name = 'groupby';
// ... as trace attributes
exports.attributes = {
active: {
valType: 'boolean',
dflt: true
},
groups: {
valType: 'data_array',
dflt: []
},
groupColors: {
valType: 'any',
dflt: {}
}
};
/**
* 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, fullData, layout) {
var transformOut = {};
function coerce(attr, dflt) {
return Lib.coerce(transformIn, transformOut, exports.attributes, attr, dflt);
}
var active = coerce('active');
if(!active) return transformOut;
coerce('groups');
coerce('groupColors');
// or some more complex logic using fullData and layout
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) {
// one-to-many case
var newData = [];
data.forEach(function(trace) {
newData = newData.concat(transformOne(trace, state));
});
return newData;
};
function transformOne(trace, state) {
var opts = state.transform;
var groups = opts.groups;
var groupNames = groups.filter(function(g, i, self) {
return self.indexOf(g) === i;
});
var newData = new Array(groupNames.length);
var len = Math.min(trace.x.length, trace.y.length, groups.length);
for(var i = 0; i < groupNames.length; i++) {
var groupName = groupNames[i];
// TODO is this the best pattern ???
// maybe we could abstract this out
var newTrace = newData[i] = Lib.extendDeep({}, trace);
newTrace.x = [];
newTrace.y = [];
for(var j = 0; j < len; j++) {
if(groups[j] !== groupName) continue;
newTrace.x.push(trace.x[j]);
newTrace.y.push(trace.y[j]);
}
newTrace.name = groupName;
newTrace.marker.color = opts.groupColors[groupName];
}
return newData;
}