This repository was archived by the owner on Jun 3, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 143
/
Copy pathGraph.react.js
522 lines (449 loc) · 14.7 KB
/
Graph.react.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
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
import React, {Component} from 'react';
import PropTypes from 'prop-types';
import {contains, intersection, filter, has, isNil, type, pluck} from 'ramda';
/* global Plotly:true */
const filterEventData = (gd, eventData, event) => {
let filteredEventData;
if (contains(event, ['click', 'hover', 'selected'])) {
const points = [];
if (isNil(eventData)) {
return null;
}
/*
* remove `data`, `layout`, `xaxis`, etc
* objects from the event data since they're so big
* and cause JSON stringify ciricular structure errors.
*
* also, pull down the `customdata` point from the data array
* into the event object
*/
const data = gd.data;
for(let i=0; i < eventData.points.length; i++) {
const fullPoint = eventData.points[i];
const pointData = filter(function(o) {
return !contains(type(o), ['Object', 'Array'])
}, fullPoint);
if (has('curveNumber', fullPoint) &&
has('pointNumber', fullPoint) &&
has('customdata', data[pointData.curveNumber])
) {
pointData['customdata'] = data[
pointData.curveNumber
].customdata[fullPoint.pointNumber];
}
// specific to histogram. see https://github.com/plotly/plotly.js/pull/2113/
if (has('pointNumbers', fullPoint)) {
pointData.pointNumbers = fullPoint.pointNumbers;
}
points[i] = pointData;
}
filteredEventData = {points}
} else if (event === 'relayout') {
/*
* relayout shouldn't include any big objects
* it will usually just contain the ranges of the axes like
* "xaxis.range[0]": 0.7715822247381828,
* "xaxis.range[1]": 3.0095292008680063`
*/
filteredEventData = eventData;
}
if (has('range', eventData)) {
filteredEventData.range = eventData.range;
}
if (has('lassoPoints', eventData)) {
filteredEventData.lassoPoints = eventData.lassoPoints;
}
return filteredEventData;
};
export default class PlotlyGraph extends Component {
constructor(props) {
super(props);
this.bindEvents = this.bindEvents.bind(this);
this._hasPlotted = false;
}
plot(props) {
const {id, figure, animate, animation_options, config} = props;
const gd = document.getElementById(id);
if (animate && this._hasPlotted && figure.data.length === gd.data.length) {
return Plotly.animate(id, figure, animation_options);
} else {
let PlotMethod;
if (intersection(
pluck('type', figure.data),
['candlestick', 'ohlc']).length
) {
PlotMethod = Plotly.newPlot;
} else {
PlotMethod = Plotly.react;
}
return PlotMethod(id, figure.data, figure.layout, config).then(
() => {
if (!this._hasPlotted) {
this.bindEvents();
Plotly.Plots.resize(document.getElementById(id));
this._hasPlotted = true;
}
}
);
}
}
bindEvents() {
const {id, fireEvent, setProps, clear_on_unhover} = this.props;
const gd = document.getElementById(id);
gd.on('plotly_click', (eventData) => {
const clickData = filterEventData(gd, eventData, 'click');
if (!isNil(clickData)) {
if (setProps) setProps({clickData});
if (fireEvent) fireEvent({event: 'click'});
}
});
gd.on('plotly_hover', (eventData) => {
const hoverData = filterEventData(gd, eventData, 'hover');
if (!isNil(hoverData)) {
if (setProps) setProps({hoverData});
if (fireEvent) fireEvent({event: 'hover'})
}
});
gd.on('plotly_selected', (eventData) => {
const selectedData = filterEventData(gd, eventData, 'selected');
if (!isNil(selectedData)) {
if (setProps) setProps({selectedData});
if (fireEvent) fireEvent({event: 'selected'});
}
});
gd.on('plotly_deselect', () => {
if (setProps) setProps({selectedData: null});
if (fireEvent) fireEvent({event: 'selected'});
});
gd.on('plotly_relayout', (eventData) => {
const relayoutData = filterEventData(gd, eventData, 'relayout');
if (!isNil(relayoutData)) {
if (setProps) setProps({relayoutData});
if (fireEvent) fireEvent({event: 'relayout'});
}
});
gd.on('plotly_unhover', () => {
if (clear_on_unhover) {
if (setProps) setProps({hoverData: null});
if (fireEvent) fireEvent({event: 'unhover'});
}
});
}
componentDidMount() {
this.plot(this.props).then(() => {
window.addEventListener('resize', () => {
Plotly.Plots.resize(document.getElementById(this.props.id));
});
});
}
componentWillUnmount() {
if (this.eventEmitter) {
this.eventEmitter.removeAllListeners();
}
}
shouldComponentUpdate(nextProps) {
return (
this.props.id !== nextProps.id ||
JSON.stringify(this.props.style) !== JSON.stringify(nextProps.style)
);
}
componentWillReceiveProps(nextProps) {
const idChanged = this.props.id !== nextProps.id;
if (idChanged) {
/*
* then the dom needs to get re-rendered with a new ID.
* the graph will get updated in componentDidUpdate
*/
return;
}
const figureChanged = this.props.figure !== nextProps.figure;
if (figureChanged) {
this.plot(nextProps);
}
}
componentDidUpdate(prevProps) {
if (prevProps.id !== this.props.id) {
this.plot(this.props);
}
}
render(){
const {className, id, style} = this.props;
return (
<div
key={id}
id={id}
style={style}
className={className}
/>
);
}
}
PlotlyGraph.propTypes = {
id: PropTypes.string.isRequired,
/**
* Data from latest click event
*/
clickData: PropTypes.object,
/**
* Data from latest hover event
*/
hoverData: PropTypes.object,
/**
* If True, `clear_on_unhover` will clear the `hoverData` property
* when the user "unhovers" from a point.
* If False, then the `hoverData` property will be equal to the
* data from the last point that was hovered over.
*/
clear_on_unhover: PropTypes.bool,
/**
* Data from latest select event
*/
selectedData: PropTypes.object,
/**
* Data from latest relayout event which occurs
* when the user zooms or pans on the plot
*/
relayoutData: PropTypes.object,
/**
* Plotly `figure` object. See schema:
* https://plot.ly/javascript/reference
*/
figure: PropTypes.object,
/**
* Generic style overrides on the plot div
*/
style: PropTypes.object,
/**
* className of the parent div
*/
className: PropTypes.string,
/**
* Beta: If true, animate between updates using
* plotly.js's `animate` function
*/
animate: PropTypes.bool,
/**
* Beta: Object containing animation settings.
* Only applies if `animate` is `true`
*/
animation_options: PropTypes.object,
/**
* Plotly.js config options.
* See https://plot.ly/javascript/configuration-options/
* for more info.
*/
config: PropTypes.shape({
/**
* no interactivity, for export or image generation
*/
staticPlot: PropTypes.bool,
/**
* we can edit titles, move annotations, etc - sets all pieces of `edits`
* unless a separate `edits` config item overrides individual parts
*/
editable: PropTypes.bool,
/**
* a set of editable properties
*/
edits: PropTypes.shape({
/**
* annotationPosition: the main anchor of the annotation, which is the
* text (if no arrow) or the arrow (which drags the whole thing leaving
* the arrow length & direction unchanged)
*/
annotationPosition: PropTypes.bool,
/**
* just for annotations with arrows, change the length and direction of the arrow
*/
annotationTail: PropTypes.bool,
annotationText: PropTypes.bool,
axisTitleText: PropTypes.bool,
colorbarPosition: PropTypes.bool,
colorbarTitleText: PropTypes.bool,
legendPosition: PropTypes.bool,
/**
* edit the trace name fields from the legend
*/
legendText: PropTypes.bool,
shapePosition: PropTypes.bool,
/**
* the global `layout.title`
*/
titleText: PropTypes.bool
}),
/**
* DO autosize once regardless of layout.autosize
* (use default width or height values otherwise)
*/
autosizable: PropTypes.bool,
/**
* set the length of the undo/redo queue
*/
queueLength: PropTypes.number,
/**
* if we DO autosize, do we fill the container or the screen?
*/
fillFrame: PropTypes.bool,
/**
* if we DO autosize, set the frame margins in percents of plot size
*/
frameMargins: PropTypes.number,
/**
* mousewheel or two-finger scroll zooms the plot
*/
scrollZoom: PropTypes.bool,
/**
* double click interaction (false, 'reset', 'autosize' or 'reset+autosize')
*/
doubleClick: PropTypes.oneOf([
false,
'reset',
'autosize',
'reset+autosize'
]),
/**
* new users see some hints about interactivity
*/
showTips: PropTypes.bool,
/**
* enable axis pan/zoom drag handles
*/
showAxisDragHandles: PropTypes.bool,
/**
* enable direct range entry at the pan/zoom drag points
* (drag handles must be enabled above)
*/
showAxisRangeEntryBoxes: PropTypes.bool,
/**
* link to open this plot in plotly
*/
showLink: PropTypes.bool,
/**
* if we show a link, does it contain data or just link to a plotly file?
*/
sendData: PropTypes.bool,
/**
* text appearing in the sendData link
*/
linkText: PropTypes.string,
/**
* display the mode bar (true, false, or 'hover')
*/
displayModeBar: PropTypes.oneOf([
true, false, 'hover'
]),
/**
* remove mode bar button by name.
* All modebar button names at https://github.com/plotly/plotly.js/blob/master/src/components/modebar/buttons.js
* Common names include:
* - sendDataToCloud
* - (2D): zoom2d, pan2d, select2d, lasso2d, zoomIn2d, zoomOut2d, autoScale2d, resetScale2d
* - (Cartesian): hoverClosestCartesian, hoverCompareCartesian
* - (3D): zoom3d, pan3d, orbitRotation, tableRotation, handleDrag3d, resetCameraDefault3d, resetCameraLastSave3d, hoverClosest3d
* - (Geo): zoomInGeo, zoomOutGeo, resetGeo, hoverClosestGeo
* - hoverClosestGl2d, hoverClosestPie, toggleHover, resetViews
*/
modeBarButtonsToRemove: PropTypes.array,
/**
* add mode bar button using config objects
*/
modeBarButtonsToAdd: PropTypes.array,
/**
* fully custom mode bar buttons as nested array,
* where the outer arrays represents button groups, and
* the inner arrays have buttons config objects or names of default buttons
*/
modeBarButtons: PropTypes.any,
/**
* add the plotly logo on the end of the mode bar
*/
displaylogo: PropTypes.bool,
/**
* increase the pixel ratio for Gl plot images
*/
plotGlPixelRatio: PropTypes.number,
/**
* URL to topojson files used in geo charts
*/
topojsonURL: PropTypes.string,
/**
* Mapbox access token (required to plot mapbox trace types)
* If using an Mapbox Atlas server, set this option to '',
* so that plotly.js won't attempt to authenticate to the public Mapbox server.
*/
mapboxAccessToken: PropTypes.any
}),
/**
*
*/
dashEvents: PropTypes.oneOf([
'click',
'hover',
'selected',
'relayout',
'unhover'
]),
/**
* Function that updates the state tree.
*/
setProps: PropTypes.func,
/**
* Function that fires events
*/
fireEvent: PropTypes.func
}
PlotlyGraph.defaultProps = {
clickData: null,
hoverData: null,
selectedData: null,
relayoutData: null,
figure: {data: [], layout: {}},
animate: false,
animation_options: {
frame: {
redraw: false
},
transition: {
duration: 750,
ease: 'cubic-in-out'
}
},
clear_on_unhover: false,
config: {
staticPlot: false,
editable: false,
edits: {
annotationPosition: false,
annotationTail: false,
annotationText: false,
axisTitleText: false,
colorbarPosition: false,
colorbarTitleText: false,
legendPosition: false,
legendText: false,
shapePosition: false,
titleText: false
},
autosizable: false,
queueLength: 0,
fillFrame: false,
frameMargins: 0,
scrollZoom: false,
doubleClick: 'reset+autosize',
showTips: true,
showAxisDragHandles: true,
showAxisRangeEntryBoxes: true,
showLink: false,
sendData: true,
linkText: 'Edit chart',
showSources: false,
displayModeBar: 'hover',
modeBarButtonsToRemove: [],
modeBarButtonsToAdd: [],
modeBarButtons: false,
displaylogo: true,
plotGlPixelRatio: 2,
topojsonURL: 'https://cdn.plot.ly/',
mapboxAccessToken: null
}
};