|
| 1 | +import React, { useRef, useEffect } from 'react' |
| 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' |
| 13 | + |
| 14 | +const GRAPH_MODE_MAP: { [mode: string]: 'lines' | 'markers' } = { |
| 15 | + [GraphMode.line]: 'lines', |
| 16 | + [GraphMode.points]: 'markers', |
| 17 | +} |
| 18 | + |
| 19 | +const isDarkTheme = document.body.classList.contains('theme_DARK') |
| 20 | + |
| 21 | +const colorPicker = (COLORS: IGoodColor[]) => { |
| 22 | + const color = new GoodColorPicker(COLORS) |
| 23 | + return (label: string) => color.getColor(label).color |
| 24 | +} |
| 25 | + |
| 26 | +const labelColors = colorPicker(isDarkTheme ? COLORS_DARK : COLORS) |
| 27 | + |
| 28 | +export default function Chart(props: ChartProps) { |
| 29 | + const chartContainer = useRef<any>() |
| 30 | + |
| 31 | + const colorPicker = labelColors |
| 32 | + |
| 33 | + useEffect(() => { |
| 34 | + Plotly.newPlot( |
| 35 | + chartContainer.current, |
| 36 | + getData(props), |
| 37 | + getLayout(props), |
| 38 | + { displayModeBar: false, autosizable: true, responsive: true, setBackground: () => 'transparent', }, |
| 39 | + ) |
| 40 | + chartContainer.current.on(PlotlyEvents.PLOTLY_HOVER, function (eventdata: PlotMouseEvent) { |
| 41 | + const points = eventdata.points[0] |
| 42 | + const pointNum = points.pointNumber |
| 43 | + Plotly.Fx.hover( |
| 44 | + chartContainer.current, |
| 45 | + props.data.map((_, i) => ({ |
| 46 | + curveNumber: i, |
| 47 | + pointNumber: pointNum |
| 48 | + })), |
| 49 | + Object.keys((chartContainer.current)._fullLayout._plots)) |
| 50 | + }) |
| 51 | + chartContainer.current.on(PlotlyEvents.PLOTLY_RELAYOUT, function (eventdata: PlotRelayoutEvent) { |
| 52 | + if (eventdata.autosize === undefined && eventdata['xaxis.autorange'] === undefined) { |
| 53 | + props.onRelayout() |
| 54 | + } |
| 55 | + }) |
| 56 | + |
| 57 | + chartContainer.current.on(PlotlyEvents.PLOTLY_DBLCLICK, () => props.onDoubleClick()) |
| 58 | + }, [props.chartConfig]) |
| 59 | + |
| 60 | + function getData(props: ChartProps): Partial<PlotData>[] { |
| 61 | + return props.data.map((timeSeries, i) => { |
| 62 | + |
| 63 | + const currentData = chartContainer.current.data |
| 64 | + const dataUnchanged = currentData && props.data === props.data |
| 65 | + /* |
| 66 | + * Time format for inclusion of milliseconds: |
| 67 | + * https://github.com/moment/moment/issues/4864#issuecomment-440142542 |
| 68 | + */ |
| 69 | + const x = dataUnchanged ? currentData[i].x |
| 70 | + : selectCol(timeSeries.datapoints, 0).map((time: number) => format(time, 'yyyy-MM-dd HH:mm:ss.SSS')) |
| 71 | + const y = dataUnchanged ? currentData[i].y : selectCol(timeSeries.datapoints, 1) |
| 72 | + |
| 73 | + return { |
| 74 | + x, |
| 75 | + y, |
| 76 | + yaxis: props.chartConfig.yAxis2 && props.chartConfig.keyToY2Axis[timeSeries.key] ? 'y2' : 'y', |
| 77 | + name: timeSeries.key, |
| 78 | + type: 'scatter', |
| 79 | + marker: { color: colorPicker(timeSeries.key) }, |
| 80 | + fill: props.chartConfig.fill ? 'tozeroy' : undefined, |
| 81 | + fillcolor: hexToRGBA(colorPicker(timeSeries.key), 0.3), |
| 82 | + mode: GRAPH_MODE_MAP[props.chartConfig.mode], |
| 83 | + line: { shape: props.chartConfig.staircase ? 'hv' : 'spline' }, |
| 84 | + } |
| 85 | + }) |
| 86 | + } |
| 87 | + |
| 88 | + function getLayout(props: ChartProps): Partial<Layout> { |
| 89 | + const axisConfig: { [key: string]: Partial<LayoutAxis> } = { |
| 90 | + xaxis: { |
| 91 | + title: props.chartConfig.xlabel, |
| 92 | + rangeslider: { |
| 93 | + visible: true, |
| 94 | + thickness: 0.03, |
| 95 | + bgcolor: isDarkTheme ? '#3D3D3D' : '#CDD7EA', |
| 96 | + bordercolor: 'red', |
| 97 | + }, |
| 98 | + color: isDarkTheme ? '#898A90' : '#527298' |
| 99 | + }, |
| 100 | + yaxis: { |
| 101 | + title: props.chartConfig.yAxisConfig.label, |
| 102 | + type: props.chartConfig.yAxisConfig.scale, |
| 103 | + fixedrange: true, |
| 104 | + color: isDarkTheme ? '#898A90' : '#527298', |
| 105 | + gridcolor: isDarkTheme ? '#898A90' : '#527298', |
| 106 | + }, |
| 107 | + yaxis2: { |
| 108 | + visible: props.chartConfig.yAxis2, |
| 109 | + title: props.chartConfig.yAxis2Config.label, |
| 110 | + type: props.chartConfig.yAxis2Config.scale, |
| 111 | + overlaying: 'y', |
| 112 | + side: 'right', |
| 113 | + fixedrange: true, |
| 114 | + color: isDarkTheme ? '#8191CF' : '#6E6E6E', |
| 115 | + gridcolor: isDarkTheme ? '#8191CF' : '#6E6E6E', |
| 116 | + } as LayoutAxis, |
| 117 | + } |
| 118 | + |
| 119 | + const legend: Partial<Legend> = { |
| 120 | + xanchor: 'center', |
| 121 | + yanchor: 'top', |
| 122 | + x: 0.5, |
| 123 | + y: -0.3, |
| 124 | + orientation: 'h', |
| 125 | + } |
| 126 | + return { |
| 127 | + ...axisConfig, |
| 128 | + legend, |
| 129 | + showlegend: true, |
| 130 | + title: props.chartConfig.title, |
| 131 | + uirevision: 1, |
| 132 | + autosize: true, |
| 133 | + font: { color: isDarkTheme ? 'darkgrey' : 'black' }, |
| 134 | + paper_bgcolor: 'rgba(0,0,0,0)', |
| 135 | + plot_bgcolor: 'rgba(0,0,0,0)', |
| 136 | + margin: { |
| 137 | + pad: 6 |
| 138 | + } |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + function selectCol(twoDArray: Datapoint[], colIndex: number) { |
| 143 | + return twoDArray.map(arr => arr[colIndex]) |
| 144 | + } |
| 145 | + |
| 146 | + return ( |
| 147 | + <div ref={chartContainer}></div> |
| 148 | + ) |
| 149 | +} |
0 commit comments