-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConnectedChart.jsx
276 lines (254 loc) · 7.31 KB
/
ConnectedChart.jsx
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
/*
* The most basic connected block chart
*/
import React from 'react';
import { compose } from 'redux';
import { connectAnythingToProviderData } from 'volto-datablocks/hocs';
import loadable from '@loadable/component';
import { mixProviderData, connectToDataParameters } from '../utils';
import Placeholder from 'volto-bise/components/theme/Placeholder/Placeholder';
import { settings } from '~/config';
import './fixes.css';
const LoadableBasicPlotly = loadable.lib(() =>
import('plotly.js/lib/index-basic'),
);
const LoadableCartesianPlotly = loadable.lib(() =>
import('plotly.js/lib/index-cartesian'),
);
const LoadableGeoPlotly = loadable.lib(() => import('plotly.js/lib/index-geo'));
const LoadableGl3dPlotly = loadable.lib(() =>
import('plotly.js/lib/index-gl3d'),
);
const LoadableGl2dPlotly = loadable.lib(() =>
import('plotly.js/lib/index-gl2d'),
);
const LoadableMapboxPlotly = loadable.lib(() =>
import('plotly.js/lib/index-mapbox'),
);
const LoadableFinancePlotly = loadable.lib(() =>
import('plotly.js/lib/index-finance'),
);
// const LoadablePlot = loadable(() =>
// import(
// /* webpackChunkName: "bise-react-plotly" */
// 'react-plotly.js'
// ),
// );
const plotlyMappings = {
basic: LoadableBasicPlotly,
cartesian: LoadableCartesianPlotly,
geo: LoadableGeoPlotly,
gl3d: LoadableGl3dPlotly,
gl2d: LoadableGl2dPlotly,
mapbox: LoadableMapboxPlotly,
finance: LoadableFinancePlotly,
};
/**
* basic: scatter, bar and pie
* cartesian: scatter, bar, box, heatmap, histogram, histogram2d, histogram2dcontour, image, pie, contour, scatterternary and violin
* geo: scatter, scattergeo and choropleth
* gl3d: scatter, scatter3d, surface, mesh3d, isosurface, volume, cone and streamtube
* gl2d: scatter, scattergl, splom, pointcloud, heatmapgl, contourgl and parcoords
* mapbox: scatter, scattermapbox, choroplethmapbox and densitymapbox
* finance: scatter, bar, histogram, pie, funnelarea, ohlc, candlestick, funnel, waterfall and indicator
*/
// TODO: make sure the partials here are in order of bundle size, ascending
const partials = [
{ name: 'basic', traces: ['scatter', 'bar', 'pie'] },
{
name: 'cartesian',
traces: [
'scatter',
'bar',
'box',
'heatmap',
'histogram',
'histogram2d',
'histogram2dcontour',
'image',
'pie',
'contour',
'scatterternary',
'violin',
],
},
{ name: 'geo', traces: ['scatter', 'scattergeo', 'choropleth'] },
{
name: 'gl3d',
traces: [
'scatter',
'scatter3d',
'surface',
'mesh3d',
'isosurface',
'volume',
'cone',
'streamtube',
],
},
{
name: 'gl2d',
traces: [
'scatter',
'scattergl',
'splom',
'pointcloud',
'heatmapgl',
'contourgl',
'parcoords',
],
},
{
name: 'mapbox',
traces: ['scatter', 'scattermapbox', 'choroplethmapbox', 'densitymapbox'],
},
{
name: 'finance',
traces: [
'scatter',
'bar',
'histogram',
'pie',
'funnelarea',
'ohlc',
'candlestick',
'funnel',
'waterfall',
'indicator',
],
},
];
const getPartialNameForTraceType = (tt) => {
const findings = partials.filter((x) => {
return x.traces.includes(tt);
});
if (findings.length === 0) {
return partials[partials.length - 1].name;
}
return findings[0].name;
};
const LoadableReactPlotly = loadable.lib(() =>
import('react-plotly.js/factory'),
);
/*
* @param { object } data The chart data, layout, extra config, etc.
* @param { boolean } useLiveData Will update the chart with the data from the provider
* @param { boolean } filterWithDataParameters Will filter live data with parameters from context
*
*/
function ConnectedChart(props) {
// console.log('connectedchart', props);
const chartData = props.data.chartData;
const useLiveData =
typeof props.useLiveData !== 'undefined' ? props.useLiveData : true;
const propsLayout = props.data && props.data.layout ? props.data.layout : {};
let layout = chartData?.layout ? chartData.layout : propsLayout;
layout = {
...layout,
autosize: true,
dragmode: false,
font: {
...layout.font,
family: settings.chartLayoutFontFamily || "'Roboto', sans-serif",
},
margin: {
l: 40, // default: 80
r: 40, // default: 80
b: 40, // default: 80
t: 50, // default: 100
},
};
if (layout.xaxis)
layout.xaxis = {
...layout.xaxis,
fixedrange: true,
hoverformat: props.hoverFormatXY || '.3s',
};
if (layout.yaxis)
layout.yaxis = {
...layout.yaxis,
hoverformat: props.hoverFormatXY || '.3s',
fixedrange: true,
};
// TODO: only use fallback data if chartData.data.url doesn't exist
// or the connected_data_parameters don't exist
// console.log('connected chart', props);
let data =
props.provider_data && useLiveData
? mixProviderData(
(chartData || {}).data,
props.provider_data,
props.connected_data_parameters,
)
: (chartData || {}).data || [];
//
// console.log('data after mix', data);
data = data.map((trace) => ({
...trace,
textfont: {
...trace.textfont,
family: settings.chartDataFontFamily || "'Roboto', sans-serif",
},
}));
console.log('DATA', getPartialNameForTraceType(data[0].type));
// TODO: load all above, in the file header, they are lazy loaded actually anyway
const LoadablePlotly =
plotlyMappings['cartesian' /*getPartialNameForTraceType(data[0].type)*/]; // loadable.lib(() =>
// import(`plotly.js/lib/index-${getPartialNameForTraceType(data[0].type)}`),
// );
return (
<div>
{/* {JSON.stringify(data)} */}
<Placeholder
getDOMElement={(val) => {
return val?.el;
}}
className="connected-chart"
partialVisibility={true}
offset={{ top: -100, bottom: -100 }}
delayedCall={true}
>
{({ nodeRef }) => (
<div className="connected-chart-wrapper">
<LoadablePlotly>
{(Plotly) => {
return (
<LoadableReactPlotly>
{({ default: createPlotlyComponent }) => {
const Plot = createPlotlyComponent(Plotly);
return (
<Plot
ref={nodeRef}
data={data}
layout={layout}
frames={[]}
config={{
displayModeBar: false,
editable: false,
responsive: true,
useResizeHandler: true,
}}
style={{
maxWidth: '100%',
margin: 'auto',
}}
/>
);
}}
</LoadableReactPlotly>
);
}}
</LoadablePlotly>
</div>
)}
</Placeholder>
</div>
);
}
export default compose(
connectAnythingToProviderData(
// injects provider_data
(props) => props.data.url || props.data.provider_url,
),
connectToDataParameters,
)(ConnectedChart);