-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathMultiLine.jsx
More file actions
189 lines (178 loc) · 4.68 KB
/
MultiLine.jsx
File metadata and controls
189 lines (178 loc) · 4.68 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
'use client';
import React, { useRef, useState } from 'react';
import ReactECharts from 'echarts-for-react';
import isEqual from 'lodash/isEqual';
import Loading from '../Loading';
import Link from 'next/link';
import {
ArrowTopRightOnSquareIcon,
} from '@heroicons/react/20/solid';
import CopyDropdown from '../CopyDropdown';
export default function MultiLine({ data, stack, fill, onSelect, link, metabaseLink, disableBrush = false, formatYAxis }) {
const [loading, setLoading] = useState(true);
const xAxis = Array.from(new Set(data.map((p) => p.x)));
const values = data.reduce((accumulator, val) => {
if (!(val.name in accumulator)) {
accumulator[val.name] = {
name: val.name,
data: new Array(xAxis.length).fill(0)
};
}
return accumulator;
}, {});
data.forEach((p) => (values[p.name].data[xAxis.indexOf(p.x)] = p.y));
const chartRef = useRef();
const colors = [
'#FAFF69',
'#FC74FF',
'#74ACFF',
'#74FFD5',
'#FF7C74',
'#74FF9B',
'#FFE074',
'#CF4B4B'
];
const mappedColors = {};
const series = Object.values(values).map((series, i) => {
let color = colors[i % colors.length];
if (series.name in mappedColors) {
color = mappedColors[series.name];
} else {
mappedColors[series.name] = color;
}
return stack
? {
type: 'line',
name: series.name,
data: series.data,
areaStyle: fill ? {} : null,
color: color,
stack: 'series'
}
: {
type: 'line',
name: series.name,
data: series.data,
areaStyle: fill ? {} : null,
color: color
};
});
const options = {
animation: false,
grid: {
left: '80px',
right: '24px'
},
tooltip: {
trigger: 'item',
textStyle: {
color: '#FAFF69',
fontWeight: 'bold',
fontSize: 16,
lineHeight: 24
},
backgroundColor: '#181818',
borderWidth: 0
},
xAxis: {
show: true,
type: 'category',
data: xAxis
},
legend: {
top: '5%',
left: '84px',
orient: 'vertical',
textStyle: {
color: '#FFFFFFF',
fontSize: 16
},
icon: 'circle',
backgroundColor: '#3F3F3F',
borderRadius: 5,
borderWidth: 1,
borderColor: '#626262',
padding: 10
},
yAxis: {
splitLine: {
show: true,
lineStyle: {
color: '#808691',
opacity: 0.3
}
},
axisLabel: {
formatter: (value, index) => {
return formatYAxis ? formatYAxis(value) : undefined;
}
}
},
series: series,
brush: disableBrush ? null : {
toolbox: ['lineX'],
brushType: 'lineX',
brushMode: 'single',
transformable: false
}
};
const onMouseOver = () => {
const echartsInstance = chartRef.current.getEchartsInstance();
echartsInstance.dispatchAction({
type: 'takeGlobalCursor',
key: 'brush',
brushOption: {
brushType: 'lineX'
}
});
};
const onBrushEnd = (params) => {
if (params.areas.length > 0) {
const echartsInstance = chartRef.current.getEchartsInstance();
let start = echartsInstance.convertFromPixel(
{ xAxisIndex: 0 },
params.areas[0].range[0]
);
let end = echartsInstance.convertFromPixel(
{ xAxisIndex: 0 },
params.areas[0].range[1]
);
start = start > 0 ? start : 0;
end = end < xAxis.length ? end : xAxis.length - 1;
onSelect && onSelect(xAxis[start], xAxis[end]);
}
};
const onChartReady = (echarts) => {
setLoading(false);
};
return (
<div
className='relative rounded-lg bg-slate-850 border border-slate-700 h-full justify-between flex flex-col'
onMouseOver={onMouseOver}>
<div className='px-[8px] pt-[8px] flex-row flex justify-end'>
{ metabaseLink && <CopyDropdown link={metabaseLink} />}
{ link && <Link href={link} target='_blank' className='w-5 ml-2'>
<ArrowTopRightOnSquareIcon className='h-5 w-5 flex-none icon-hover' aria-hidden='true'/>
</Link>}
</div>
<ReactECharts
ref={chartRef}
option={options}
style={{ width: '100%', height: '100%' }}
lazyUpdate
onChartReady={onChartReady}
onEvents={{
brushEnd: onBrushEnd
}}
shouldSetOption={(prevProps, currentProps) => {
const shouldRender = !isEqual(prevProps, currentProps);
if (shouldRender) {
setLoading(true);
}
return shouldRender;
}}
/>
{loading && <Loading />}
</div>
);
}