-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathAreaChart.tsx
More file actions
61 lines (55 loc) · 1.57 KB
/
AreaChart.tsx
File metadata and controls
61 lines (55 loc) · 1.57 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
import ReactEChartsCore from "echarts-for-react/lib/core";
import * as echarts from "echarts/core";
import { LineChart } from "echarts/charts";
import {
GridComponent,
TooltipComponent,
LegendComponent,
TitleComponent,
} from "echarts/components";
import { CanvasRenderer } from "echarts/renderers";
import { getEchartsTheme } from "../echarts-theme";
import { useQueryData } from "./QueryProvider";
echarts.use([
LineChart,
GridComponent,
TooltipComponent,
LegendComponent,
TitleComponent,
CanvasRenderer,
]);
interface AreaChartProps {
x: string;
y: string;
title?: string;
data?: string;
query?: string;
}
export function AreaChart({ x, y, title, data, query }: AreaChartProps) {
const queryResult = useQueryData(query ?? "");
const rows = data ? JSON.parse(data) : queryResult.data;
const theme = getEchartsTheme();
const xValues = rows.map((r: Record<string, unknown>) => r[x]);
const yValues = rows.map((r: Record<string, unknown>) => r[y]);
const option = {
...theme,
title: title ? { ...theme.title, text: title } : undefined,
tooltip: { ...theme.tooltip, trigger: "axis" },
xAxis: { ...(theme as any).categoryAxis, type: "category" as const, data: xValues },
yAxis: { ...(theme as any).valueAxis, type: "value" as const },
series: [
{
type: "line" as const,
data: yValues,
name: y,
areaStyle: {},
symbolSize: 0,
},
],
};
return (
<div className="mt-2 mb-3">
<ReactEChartsCore echarts={echarts} option={option} style={{ height: 291 }} />
</div>
);
}