-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathBarChart.tsx
More file actions
58 lines (52 loc) · 1.71 KB
/
BarChart.tsx
File metadata and controls
58 lines (52 loc) · 1.71 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
import ReactEChartsCore from "echarts-for-react/lib/core";
import * as echarts from "echarts/core";
import { BarChart as EBarChart } 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([
EBarChart,
GridComponent,
TooltipComponent,
LegendComponent,
TitleComponent,
CanvasRenderer,
]);
interface BarChartProps {
x: string;
y: string;
title?: string;
horizontal?: boolean;
data?: string;
query?: string;
}
export function BarChart({ x, y, title, horizontal, data, query }: BarChartProps) {
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: horizontal
? { ...(theme as any).valueAxis, type: "value" as const }
: { ...(theme as any).categoryAxis, type: "category" as const, data: xValues },
yAxis: horizontal
? { ...(theme as any).categoryAxis, type: "category" as const, data: xValues }
: { ...(theme as any).valueAxis, type: "value" as const },
series: [{ type: "bar" as const, data: yValues, name: y }],
};
return (
<div className="mt-2 mb-3">
<ReactEChartsCore echarts={echarts} option={option} style={{ height: 291 }} />
</div>
);
}