1
1
import React , { useRef , useEffect } from 'react'
2
- import Plotly , { Layout } from 'plotly.js-dist-min'
3
- import { Legend , LayoutAxis , PlotData } from 'plotly.js'
4
- import moment from 'moment'
5
- import * as d3Scale from 'd3-scale'
6
- import * as d3ScaleColor from 'd3-scale-chromatic'
7
- import { hexToRGBA , IGoodColor , GoodColorPicker , NODE_COLORS , NODE_COLORS_DARK , EDGE_COLORS , EDGE_COLORS_DARK } from './utils'
8
-
9
- import { Datapoint , TimeSeries , ChartConfig , GraphMode } from './interfaces'
10
-
11
- interface ChartProps {
12
- data : TimeSeries [ ]
13
- chartConfig : ChartConfig
14
- onRelayout : ( ) => void
15
- onDoubleClick : ( ) => void
16
- }
2
+ import Plotly from 'plotly.js-dist-min'
3
+ import { Legend , LayoutAxis , PlotData , PlotMouseEvent , Layout , PlotRelayoutEvent } from 'plotly.js'
4
+ import { format } from 'date-fns'
5
+ import { hexToRGBA , IGoodColor , GoodColorPicker , COLORS , COLORS_DARK } from './utils'
6
+
7
+ import {
8
+ Datapoint ,
9
+ GraphMode ,
10
+ ChartProps ,
11
+ PlotlyEvents ,
12
+ } from './interfaces'
17
13
18
14
const GRAPH_MODE_MAP : { [ mode : string ] : 'lines' | 'markers' } = {
19
15
[ GraphMode . line ] : 'lines' ,
@@ -27,10 +23,9 @@ const colorPicker = (COLORS: IGoodColor[]) => {
27
23
return ( label : string ) => color . getColor ( label ) . color
28
24
}
29
25
30
- const labelColors = colorPicker ( isDarkTheme ? NODE_COLORS_DARK : NODE_COLORS )
31
- const edgeColors = colorPicker ( isDarkTheme ? EDGE_COLORS_DARK : EDGE_COLORS )
26
+ const labelColors = colorPicker ( isDarkTheme ? COLORS_DARK : COLORS )
32
27
33
- export default function Chart ( props : any ) {
28
+ export default function Chart ( props : ChartProps ) {
34
29
const chartContainer = useRef < any > ( )
35
30
36
31
const colorPicker = labelColors
@@ -39,12 +34,12 @@ export default function Chart(props: any) {
39
34
Plotly . newPlot (
40
35
chartContainer . current ,
41
36
getData ( props ) ,
42
- getLayout ( props ) as any ,
37
+ getLayout ( props ) ,
43
38
{ displayModeBar : false , autosizable : true , responsive : true , setBackground : ( ) => 'transparent' , } ,
44
39
)
45
- chartContainer . current . on ( 'plotly_hover' , function ( eventdata ) {
46
- var points = eventdata . points [ 0 ]
47
- var pointNum = points . pointNumber
40
+ chartContainer . current . on ( PlotlyEvents . PLOTLY_HOVER , function ( eventdata : PlotMouseEvent ) {
41
+ const points = eventdata . points [ 0 ]
42
+ const pointNum = points . pointNumber
48
43
Plotly . Fx . hover (
49
44
chartContainer . current ,
50
45
props . data . map ( ( _ , i ) => ( {
@@ -53,37 +48,26 @@ export default function Chart(props: any) {
53
48
} ) ) ,
54
49
Object . keys ( ( chartContainer . current ) . _fullLayout . _plots ) )
55
50
} )
56
- chartContainer . current . on ( 'plotly_relayout' , function ( eventdata ) {
51
+ chartContainer . current . on ( PlotlyEvents . PLOTLY_RELAYOUT , function ( eventdata : PlotRelayoutEvent ) {
57
52
if ( eventdata . autosize === undefined && eventdata [ 'xaxis.autorange' ] === undefined ) {
58
53
props . onRelayout ( )
59
54
}
60
55
} )
61
56
62
- chartContainer . current . on ( 'plotly_doubleclick' , function ( eventdata ) {
63
- props . onDoubleClick ( )
64
- } )
57
+ chartContainer . current . on ( PlotlyEvents . PLOTLY_DBLCLICK , ( ) => props . onDoubleClick ( ) )
65
58
} , [ props . chartConfig ] )
66
59
67
- function getPlotArgs ( props : ChartProps ) {
68
- return [
69
- chartContainer . current ,
70
- getData ( props ) ,
71
- getLayout ( props ) ,
72
- { displayModeBar : false } ,
73
- ]
74
- }
75
-
76
60
function getData ( props : ChartProps ) : Partial < PlotData > [ ] {
77
61
return props . data . map ( ( timeSeries , i ) => {
78
62
79
- const currentData = ( chartContainer . current as any ) . data
63
+ const currentData = chartContainer . current . data
80
64
const dataUnchanged = currentData && props . data === props . data
81
65
/*
82
66
* Time format for inclusion of milliseconds:
83
67
* https://github.com/moment/moment/issues/4864#issuecomment-440142542
84
68
*/
85
69
const x = dataUnchanged ? currentData [ i ] . x
86
- : selectCol ( timeSeries . datapoints , 0 ) . map ( ( time : number ) => moment ( time ) . format ( 'YYYY -MM-DD[T] HH:mm:ss.SSS[Z] ') )
70
+ : selectCol ( timeSeries . datapoints , 0 ) . map ( ( time : number ) => format ( time , 'yyyy -MM-dd HH:mm:ss.SSS') )
87
71
const y = dataUnchanged ? currentData [ i ] . y : selectCol ( timeSeries . datapoints , 1 )
88
72
89
73
return {
0 commit comments