-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVegaChart.tsx
More file actions
56 lines (49 loc) · 1.5 KB
/
VegaChart.tsx
File metadata and controls
56 lines (49 loc) · 1.5 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
/*
* Copyright (c) 2019-2026 by Brockmann Consult Development team
* Permissions are hereby granted under the terms of the MIT License:
* https://opensource.org/licenses/MIT.
*/
import { useRef } from "react";
import { VegaEmbed } from "react-vega";
import type { TopLevelSpec } from "vega-lite";
import type { ComponentProps, ComponentState } from "@/index";
import { useSignalListeners } from "./hooks/useSignalListeners";
import { useVegaTheme, type VegaTheme } from "./hooks/useVegaTheme";
import { useResizeObserver } from "./hooks/useResizeObserver";
interface VegaChartState extends ComponentState {
theme?: VegaTheme | "default" | "system";
chart?: TopLevelSpec | null;
}
interface VegaChartProps extends ComponentProps, VegaChartState {}
export function VegaChart({
type,
id,
style,
theme,
chart,
onChange,
}: VegaChartProps) {
const { onEmbed } = useSignalListeners(chart, type, id, onChange);
const vegaTheme = useVegaTheme(theme);
const { containerSizeKey, containerCallbackRef } = useResizeObserver();
const embedDivRef = useRef<HTMLDivElement | null>(null);
if (chart) {
return (
<div id="chart-container" ref={containerCallbackRef} style={style}>
<VegaEmbed
key={containerSizeKey}
ref={embedDivRef}
spec={chart}
onEmbed={onEmbed}
options={{
actions: false,
theme: vegaTheme,
}}
style={style}
/>
</div>
);
} else {
return <div id={id} />;
}
}