-
-
Notifications
You must be signed in to change notification settings - Fork 286
Expand file tree
/
Copy pathPlotlyRenderers.jsx
More file actions
235 lines (217 loc) · 6.74 KB
/
PlotlyRenderers.jsx
File metadata and controls
235 lines (217 loc) · 6.74 KB
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
import React from 'react';
import PropTypes from 'prop-types';
import { PivotData } from './Utilities';
/* eslint-disable react/prop-types */
// eslint can't see inherited propTypes!
function makeRenderer(
PlotlyComponent,
traceOptions = {},
layoutOptions = {},
transpose = false
) {
class Renderer extends React.PureComponent {
render() {
const pivotData = new PivotData(this.props);
const rowKeys = pivotData.getRowKeys();
const colKeys = pivotData.getColKeys();
const traceKeys = transpose ? colKeys : rowKeys;
if (traceKeys.length === 0) {
traceKeys.push([]);
}
const datumKeys = transpose ? rowKeys : colKeys;
if (datumKeys.length === 0) {
datumKeys.push([]);
}
let fullAggName = this.props.aggregatorName;
const numInputs =
this.props.aggregators[fullAggName]([])().numInputs || 0;
if (numInputs !== 0) {
fullAggName += ` of ${this.props.vals.slice(0, numInputs).join(', ')}`;
}
const data = traceKeys.map(traceKey => {
const values = [];
const labels = [];
for (const datumKey of datumKeys) {
const val = parseFloat(
pivotData
.getAggregator(
transpose ? datumKey : traceKey,
transpose ? traceKey : datumKey
)
.value()
);
values.push(isFinite(val) ? val : null);
labels.push(datumKey.join('-') || ' ');
}
const trace = { name: traceKey.join('-') || fullAggName };
if (traceOptions.type === 'pie') {
trace.values = values;
trace.labels = labels.length > 1 ? labels : [fullAggName];
} else {
trace.x = transpose ? values : labels;
trace.y = transpose ? labels : values;
}
return Object.assign(trace, traceOptions);
});
let titleText = fullAggName;
const hAxisTitle = transpose
? this.props.rows.join('-')
: this.props.cols.join('-');
const groupByTitle = transpose
? this.props.cols.join('-')
: this.props.rows.join('-');
if (hAxisTitle !== '') {
titleText += ` vs ${hAxisTitle}`;
}
if (groupByTitle !== '') {
titleText += ` by ${groupByTitle}`;
}
const layout = {
title: titleText,
hovermode: 'closest',
/* eslint-disable no-magic-numbers */
width: window.innerWidth / 1.5,
height: window.innerHeight / 1.4 - 50,
/* eslint-enable no-magic-numbers */
};
if (traceOptions.type === 'pie') {
const columns = Math.ceil(Math.sqrt(data.length));
const rows = Math.ceil(data.length / columns);
layout.grid = { columns, rows };
data.forEach((d, i) => {
d.domain = {
row: Math.floor(i / columns),
column: i - columns * Math.floor(i / columns),
};
if (data.length > 1) {
d.title = d.name;
}
});
if (data[0].labels.length === 1) {
layout.showlegend = false;
}
} else {
layout.xaxis = {
title: transpose ? fullAggName : null,
automargin: true,
};
layout.yaxis = {
title: transpose ? null : fullAggName,
automargin: true,
};
}
return (
<PlotlyComponent
data={data}
layout={Object.assign(
layout,
layoutOptions,
this.props.plotlyOptions
)}
config={this.props.plotlyConfig}
onUpdate={this.props.onRendererUpdate}
/>
);
}
}
Renderer.defaultProps = Object.assign({}, PivotData.defaultProps, {
plotlyOptions: {},
plotlyConfig: {},
});
Renderer.propTypes = Object.assign({}, PivotData.propTypes, {
plotlyOptions: PropTypes.object,
plotlyConfig: PropTypes.object,
onRendererUpdate: PropTypes.func,
});
return Renderer;
}
function makeScatterRenderer(PlotlyComponent) {
class Renderer extends React.PureComponent {
render() {
const pivotData = new PivotData(this.props);
const rowKeys = pivotData.getRowKeys();
const colKeys = pivotData.getColKeys();
if (rowKeys.length === 0) {
rowKeys.push([]);
}
if (colKeys.length === 0) {
colKeys.push([]);
}
const data = { x: [], y: [], text: [], type: 'scatter', mode: 'markers' };
rowKeys.map(rowKey => {
colKeys.map(colKey => {
const v = pivotData.getAggregator(rowKey, colKey).value();
if (v !== null) {
data.x.push(colKey.join('-'));
data.y.push(rowKey.join('-'));
data.text.push(v);
}
});
});
const layout = {
title: this.props.rows.join('-') + ' vs ' + this.props.cols.join('-'),
hovermode: 'closest',
/* eslint-disable no-magic-numbers */
xaxis: { title: this.props.cols.join('-'), automargin: true },
yaxis: { title: this.props.rows.join('-'), automargin: true },
width: window.innerWidth / 1.5,
height: window.innerHeight / 1.4 - 50,
/* eslint-enable no-magic-numbers */
};
return (
<PlotlyComponent
data={[data]}
layout={Object.assign(layout, this.props.plotlyOptions)}
config={this.props.plotlyConfig}
onUpdate={this.props.onRendererUpdate}
/>
);
}
}
Renderer.defaultProps = Object.assign({}, PivotData.defaultProps, {
plotlyOptions: {},
plotlyConfig: {},
});
Renderer.propTypes = Object.assign({}, PivotData.propTypes, {
plotlyOptions: PropTypes.object,
plotlyConfig: PropTypes.object,
onRendererUpdate: PropTypes.func,
});
return Renderer;
}
export default function createPlotlyRenderers(PlotlyComponent) {
return {
'Grouped Column Chart': makeRenderer(
PlotlyComponent,
{ type: 'bar' },
{ barmode: 'group' }
),
'Stacked Column Chart': makeRenderer(
PlotlyComponent,
{ type: 'bar' },
{ barmode: 'relative' }
),
'Grouped Bar Chart': makeRenderer(
PlotlyComponent,
{ type: 'bar', orientation: 'h' },
{ barmode: 'group' },
true
),
'Stacked Bar Chart': makeRenderer(
PlotlyComponent,
{ type: 'bar', orientation: 'h' },
{ barmode: 'relative' },
true
),
'Line Chart': makeRenderer(PlotlyComponent),
'Dot Chart': makeRenderer(PlotlyComponent, { mode: 'markers' }, {}, true),
'Area Chart': makeRenderer(PlotlyComponent, { stackgroup: 1 }),
'Scatter Chart': makeScatterRenderer(PlotlyComponent),
'Multiple Pie Chart': makeRenderer(
PlotlyComponent,
{ type: 'pie', scalegroup: 1, hoverinfo: 'label+value', textinfo: 'none' },
{},
true
),
};
}