@react-fabric/chart 1.0.0-beta.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (88) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +10 -0
  3. package/dist/cjs/charts/ActivityMap.js +183 -0
  4. package/dist/cjs/charts/CountSeries.js +156 -0
  5. package/dist/cjs/charts/DataSeries.js +185 -0
  6. package/dist/cjs/charts/Generic.js +90 -0
  7. package/dist/cjs/charts/MapSeries.js +215 -0
  8. package/dist/cjs/charts/TimeSeries.js +213 -0
  9. package/dist/cjs/charts/TimeSlider.js +194 -0
  10. package/dist/cjs/charts/WordBubble.js +80 -0
  11. package/dist/cjs/index.js +23 -0
  12. package/dist/cjs/theme/darkBase.js +418 -0
  13. package/dist/cjs/theme/index.js +98 -0
  14. package/dist/cjs/theme/lightBase.js +415 -0
  15. package/dist/cjs/theme/palettes.js +113 -0
  16. package/dist/cjs/types/charts.js +56 -0
  17. package/dist/cjs/types/utils.js +104 -0
  18. package/dist/cjs/types/world.svg.js +5 -0
  19. package/dist/cjs/wrapper/Chart.module.css +1 -0
  20. package/dist/cjs/wrapper/ChartContainer.js +171 -0
  21. package/dist/cjs/wrapper/ChartToolbar.js +38 -0
  22. package/dist/cjs/wrapper/ChartWrapper.js +57 -0
  23. package/dist/cjs/wrapper/PaletteSelect.js +146 -0
  24. package/dist/esm/charts/ActivityMap.js +183 -0
  25. package/dist/esm/charts/CountSeries.js +156 -0
  26. package/dist/esm/charts/DataSeries.js +185 -0
  27. package/dist/esm/charts/Generic.js +90 -0
  28. package/dist/esm/charts/MapSeries.js +196 -0
  29. package/dist/esm/charts/TimeSeries.js +213 -0
  30. package/dist/esm/charts/TimeSlider.js +194 -0
  31. package/dist/esm/charts/WordBubble.js +80 -0
  32. package/dist/esm/index.js +11 -0
  33. package/dist/esm/theme/darkBase.js +418 -0
  34. package/dist/esm/theme/index.js +97 -0
  35. package/dist/esm/theme/lightBase.js +415 -0
  36. package/dist/esm/theme/palettes.js +113 -0
  37. package/dist/esm/types/charts.js +35 -0
  38. package/dist/esm/types/utils.js +101 -0
  39. package/dist/esm/types/world.svg.js +5 -0
  40. package/dist/esm/wrapper/Chart.module.css +1 -0
  41. package/dist/esm/wrapper/ChartContainer.js +152 -0
  42. package/dist/esm/wrapper/ChartToolbar.js +38 -0
  43. package/dist/esm/wrapper/ChartWrapper.js +57 -0
  44. package/dist/esm/wrapper/PaletteSelect.js +146 -0
  45. package/dist/types/charts/ActivityMap.d.ts +21 -0
  46. package/dist/types/charts/CountSeries.d.ts +10 -0
  47. package/dist/types/charts/DataSeries.d.ts +13 -0
  48. package/dist/types/charts/Generic.d.ts +56 -0
  49. package/dist/types/charts/MapSeries.d.ts +9 -0
  50. package/dist/types/charts/TimeSeries.d.ts +17 -0
  51. package/dist/types/charts/TimeSlider.d.ts +16 -0
  52. package/dist/types/charts/WordBubble.d.ts +9 -0
  53. package/dist/types/index.d.ts +9 -0
  54. package/dist/types/theme/darkBase.d.ts +391 -0
  55. package/dist/types/theme/index.d.ts +2 -0
  56. package/dist/types/theme/lightBase.d.ts +388 -0
  57. package/dist/types/theme/palettes.d.ts +45 -0
  58. package/dist/types/types/charts.d.ts +3 -0
  59. package/dist/types/types/index.d.ts +36 -0
  60. package/dist/types/types/utils.d.ts +4 -0
  61. package/dist/types/types/world.svg.d.ts +2 -0
  62. package/dist/types/wrapper/ChartContainer.d.ts +20 -0
  63. package/dist/types/wrapper/ChartToolbar.d.ts +3 -0
  64. package/dist/types/wrapper/ChartWrapper.d.ts +5 -0
  65. package/dist/types/wrapper/PaletteSelect.d.ts +1 -0
  66. package/package.json +58 -0
  67. package/src/charts/ActivityMap.tsx +295 -0
  68. package/src/charts/CountSeries.tsx +186 -0
  69. package/src/charts/DataSeries.tsx +228 -0
  70. package/src/charts/Generic.tsx +190 -0
  71. package/src/charts/MapSeries.tsx +217 -0
  72. package/src/charts/TimeSeries.tsx +236 -0
  73. package/src/charts/TimeSlider.tsx +235 -0
  74. package/src/charts/WordBubble.tsx +105 -0
  75. package/src/index.ts +32 -0
  76. package/src/theme/darkBase.ts +400 -0
  77. package/src/theme/index.ts +122 -0
  78. package/src/theme/lightBase.ts +397 -0
  79. package/src/theme/palettes.ts +589 -0
  80. package/src/types/charts.ts +32 -0
  81. package/src/types/index.ts +61 -0
  82. package/src/types/utils.ts +151 -0
  83. package/src/types/world.svg.ts +467 -0
  84. package/src/wrapper/Chart.module.css +90 -0
  85. package/src/wrapper/ChartContainer.tsx +217 -0
  86. package/src/wrapper/ChartToolbar.tsx +40 -0
  87. package/src/wrapper/ChartWrapper.tsx +46 -0
  88. package/src/wrapper/PaletteSelect.tsx +139 -0
@@ -0,0 +1,190 @@
1
+ /*
2
+ * React Fabric
3
+ * @version: 1.0.0
4
+ *
5
+ *
6
+ * The MIT License (MIT)
7
+ * Copyright (c) 2024 Adarsh Pastakia
8
+ *
9
+ * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
10
+ * and associated documentation files (the "Software"), to deal in the Software without restriction,
11
+ * including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
12
+ * and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
13
+ * subject to the following conditions:
14
+ *
15
+ * The above copyright notice and this permission notice shall be included in all copies or substantial
16
+ * portions of the Software.
17
+ *
18
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
19
+ * TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21
+ * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22
+ */
23
+
24
+ import { type EChartOption, type EChartsType } from "echarts";
25
+ import {
26
+ memo,
27
+ useEffect,
28
+ useImperativeHandle,
29
+ useRef,
30
+ type FC,
31
+ type RefObject,
32
+ } from "react";
33
+ import { type BaseChart } from "../types";
34
+ import { ChartContainer } from "../wrapper/ChartContainer";
35
+ import { ChartWrapper } from "../wrapper/ChartWrapper";
36
+
37
+ export interface GenericProps extends BaseChart {
38
+ chartRef?: RefObject<EChartsType>;
39
+
40
+ legend?: EChartOption["legend"];
41
+ toolbox?: EChartOption["toolbox"];
42
+ tooltip?: EChartOption["tooltip"];
43
+
44
+ actions?: JSX.Element;
45
+
46
+ /**
47
+ * 2d axis
48
+ */
49
+ xAxis?: EChartOption["xAxis"];
50
+ /**
51
+ * 2d axis
52
+ */
53
+ yAxis?: EChartOption["yAxis"];
54
+ /**
55
+ * chart series
56
+ */
57
+ series?: EChartOption["series"];
58
+
59
+ /**
60
+ * polar series
61
+ */
62
+ polar?: EChartOption["polar"];
63
+ /**
64
+ * polar axis
65
+ */
66
+ angleAxis?: EChartOption["angleAxis"];
67
+ /**
68
+ * polar axis
69
+ */
70
+ radiusAxis?: EChartOption["radiusAxis"];
71
+
72
+ /**
73
+ * radar series
74
+ */
75
+ radar?: EChartOption["radar"];
76
+
77
+ /**
78
+ * paarllel series
79
+ */
80
+ parallel?: EChartOption["parallel"];
81
+ /**
82
+ * parallel axis
83
+ */
84
+ parallelAxis?: EChartOption["parallelAxis"];
85
+
86
+ onClick?: (e: KeyValue) => void;
87
+ onRendered?: (e: KeyValue) => void;
88
+
89
+ onBrush?: (e: KeyValue) => void;
90
+ onBrushEnd?: (e: KeyValue) => void;
91
+ onBrushSelected?: (e: KeyValue) => void;
92
+
93
+ /**
94
+ * Data table renderer that returns HTML string
95
+ */
96
+ dataTableRenderer?: (chartOption: EChartOption) => string;
97
+ }
98
+
99
+ const GenericChartBase: FC<GenericProps> = memo(
100
+ ({
101
+ chartRef: _ref,
102
+ onBrush,
103
+ onBrushEnd,
104
+ onBrushSelected,
105
+ dataTableRenderer,
106
+ onClick,
107
+ onRendered,
108
+ actions,
109
+ title,
110
+ theme,
111
+ ...options
112
+ }: GenericProps) => {
113
+ const chartRef = useRef<EChartsType>(null);
114
+
115
+ useImperativeHandle<EChartsType | null, EChartsType | null>(
116
+ _ref,
117
+ () => chartRef.current,
118
+ [chartRef.current],
119
+ );
120
+
121
+ useEffect(() => {
122
+ const chart = chartRef.current;
123
+ if (chart && onRendered) {
124
+ chart.on("finished", onRendered);
125
+ return () => {
126
+ !chart.isDisposed() && chart.off("finished");
127
+ };
128
+ }
129
+ }, [chartRef.current, onRendered]);
130
+
131
+ useEffect(() => {
132
+ const chart = chartRef.current;
133
+ if (chart && onClick) {
134
+ chart.on("click", onClick);
135
+ return () => {
136
+ !chart.isDisposed() && chart.off("click");
137
+ };
138
+ }
139
+ }, [chartRef.current, onClick]);
140
+
141
+ useEffect(() => {
142
+ const chart = chartRef.current;
143
+ if (chart && onBrush) {
144
+ chart.on("brush", onBrush);
145
+ return () => {
146
+ !chart.isDisposed() && chart.off("brush");
147
+ };
148
+ }
149
+ }, [chartRef.current, onBrush]);
150
+
151
+ useEffect(() => {
152
+ const chart = chartRef.current;
153
+ if (chart && onBrushEnd) {
154
+ chart.on("brushend", onBrushEnd);
155
+ return () => {
156
+ !chart.isDisposed() && chart.off("brushend");
157
+ };
158
+ }
159
+ }, [chartRef.current, onBrushEnd]);
160
+
161
+ useEffect(() => {
162
+ const chart = chartRef.current;
163
+ if (chart && onBrushSelected) {
164
+ chart.on("brushselected", onBrushSelected);
165
+ return () => {
166
+ !chart.isDisposed() && chart.off("brushselected");
167
+ };
168
+ }
169
+ }, [chartRef.current, onBrushSelected]);
170
+
171
+ return (
172
+ <ChartContainer
173
+ theme={theme}
174
+ options={options}
175
+ chartRef={chartRef}
176
+ title={title}
177
+ dataTableRenderer={dataTableRenderer}
178
+ >
179
+ {actions}
180
+ </ChartContainer>
181
+ );
182
+ },
183
+ );
184
+ GenericChartBase.displayName = "GenericChart";
185
+
186
+ export const GenericChart: FC<GenericProps> = (props) => (
187
+ <ChartWrapper>
188
+ <GenericChartBase {...props} />
189
+ </ChartWrapper>
190
+ );
@@ -0,0 +1,217 @@
1
+ /*
2
+ * React Fabric
3
+ * @version: 1.0.0
4
+ *
5
+ *
6
+ * The MIT License (MIT)
7
+ * Copyright (c) 2024 Adarsh Pastakia
8
+ *
9
+ * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
10
+ * and associated documentation files (the "Software"), to deal in the Software without restriction,
11
+ * including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
12
+ * and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
13
+ * subject to the following conditions:
14
+ *
15
+ * The above copyright notice and this permission notice shall be included in all copies or substantial
16
+ * portions of the Software.
17
+ *
18
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
19
+ * TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21
+ * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22
+ */
23
+
24
+ import {
25
+ compareValues,
26
+ Countries,
27
+ Format,
28
+ isEmpty,
29
+ } from "@react-fabric/utilities";
30
+ import * as echarts from "echarts";
31
+ import { type EChartOption, type EChartsType } from "echarts";
32
+ import { memo, useEffect, useRef, useState, type FC } from "react";
33
+ import { ChartPalette } from "../theme/palettes";
34
+ import { type BaseChart, type CountType } from "../types";
35
+ import { countRenderer } from "../types/utils";
36
+ import { ChartContainer } from "../wrapper/ChartContainer";
37
+ import { ChartWrapper } from "../wrapper/ChartWrapper";
38
+
39
+ import {
40
+ Button,
41
+ CoreIcons,
42
+ ToggleButtonGroup,
43
+ useMemoDebugger,
44
+ } from "@react-fabric/core";
45
+ import WorldMap from "../types/world.svg";
46
+ echarts.registerMap("world", { svg: WorldMap });
47
+
48
+ export interface MapSeriesProps extends BaseChart, CountType {
49
+ onClick?: (key: string) => void;
50
+ }
51
+
52
+ const MapSeriesChart: FC<MapSeriesProps> = memo(
53
+ ({ data, onExport, theme: chartTheme, title, onClick }: MapSeriesProps) => {
54
+ const chartRef = useRef<EChartsType>(null);
55
+ const [type, setType] = useState("map");
56
+ const [zoom, setZoom] = useState(1.25);
57
+
58
+ const options = useMemoDebugger<EChartOption>(
59
+ () => {
60
+ if (isEmpty(data)) return {};
61
+
62
+ const sorted = data
63
+ .map((d) => ({
64
+ ...d,
65
+ label: d.label ?? Countries.name(d.id),
66
+ }))
67
+ .sort(compareValues("desc", "count"));
68
+ const valueMap = data.map((d) => d.count);
69
+
70
+ if (type === "column") {
71
+ return {
72
+ xAxis: {
73
+ type: "category",
74
+ nameLocation: "center",
75
+ axisTick: {
76
+ show: false,
77
+ },
78
+ axisLabel: {
79
+ rotate: 30,
80
+ },
81
+ data: [],
82
+ },
83
+ yAxis: {
84
+ type: "value",
85
+ axisLabel: {
86
+ formatter: (val: AnyObject) => Format.number(val),
87
+ },
88
+ },
89
+ series: sorted.map((item, index) => ({
90
+ id: item.id + index,
91
+ type: "bar",
92
+ label: {
93
+ show: true,
94
+ rotate: 90,
95
+ formatter: "{a}",
96
+ position: "inside",
97
+ color: "#fff",
98
+ },
99
+ data: [
100
+ {
101
+ value: item.count,
102
+ },
103
+ ],
104
+ name: item.label ?? item.id,
105
+ universalTransition: true,
106
+ })),
107
+ } as AnyObject;
108
+ }
109
+
110
+ return {
111
+ legend: {
112
+ show: false,
113
+ },
114
+ visualMap: {
115
+ type: "piecewise",
116
+ min: Math.min(...valueMap) - 1,
117
+ max: Math.max(...valueMap) + 1,
118
+ show: false,
119
+ color: [...ChartPalette.Heatmap],
120
+ calculable: true,
121
+ orient: "horizontal",
122
+ left: "center",
123
+ bottom: 48,
124
+ } as AnyObject,
125
+ series: [
126
+ {
127
+ id: "mapseries",
128
+ name: title ?? "Map Series",
129
+ type: "map",
130
+ map: "world",
131
+ aspectScale: 1,
132
+ roam: "move",
133
+ colorBy: "data",
134
+ nameProperty: "name",
135
+ universalTransition: true,
136
+ emphasis: {
137
+ label: { show: false },
138
+ },
139
+ select: {
140
+ disabled: true,
141
+ },
142
+ data: sorted.map(({ id, label: name, count: value }) => ({
143
+ id,
144
+ name,
145
+ value,
146
+ })),
147
+ },
148
+ ],
149
+ };
150
+ },
151
+ [data, title, type],
152
+ "MapChart options",
153
+ );
154
+
155
+ useEffect(() => {
156
+ setZoom(1.25);
157
+ }, [data, type]);
158
+
159
+ useEffect(() => {
160
+ chartRef.current?.setOption({
161
+ series: [
162
+ {
163
+ zoom,
164
+ },
165
+ ],
166
+ });
167
+ }, [zoom]);
168
+
169
+ return (
170
+ <ChartContainer
171
+ title={title}
172
+ onExport={onExport}
173
+ chartRef={chartRef}
174
+ options={options}
175
+ dataTableRenderer={countRenderer}
176
+ onClick={(e) => e.data && onClick?.(e?.data?.id ?? "")}
177
+ >
178
+ {type === "map" && (
179
+ <input
180
+ type="range"
181
+ min={1.25}
182
+ max={24}
183
+ step={0.25}
184
+ value={zoom}
185
+ onChange={(e) => setZoom(e.target.valueAsNumber)}
186
+ />
187
+ )}
188
+ <ToggleButtonGroup value={type} onChange={setType as AnyObject}>
189
+ <Button
190
+ size="sm"
191
+ variant="link"
192
+ icon={CoreIcons.map}
193
+ aria-label="map"
194
+ value="map"
195
+ />
196
+ <Button
197
+ size="sm"
198
+ variant="link"
199
+ icon={CoreIcons.chartColumn}
200
+ aria-label="column"
201
+ value="column"
202
+ />
203
+ </ToggleButtonGroup>
204
+ </ChartContainer>
205
+ );
206
+ },
207
+ );
208
+ MapSeriesChart.displayName = "MapSeriesChart";
209
+
210
+ /**
211
+ * Chart to display simple map data series
212
+ */
213
+ export const MapSeries: FC<MapSeriesProps> = (props) => (
214
+ <ChartWrapper>
215
+ <MapSeriesChart {...props} />
216
+ </ChartWrapper>
217
+ );
@@ -0,0 +1,236 @@
1
+ /*
2
+ * React Fabric
3
+ * @version: 1.0.0
4
+ *
5
+ *
6
+ * The MIT License (MIT)
7
+ * Copyright (c) 2024 Adarsh Pastakia
8
+ *
9
+ * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
10
+ * and associated documentation files (the "Software"), to deal in the Software without restriction,
11
+ * including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
12
+ * and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
13
+ * subject to the following conditions:
14
+ *
15
+ * The above copyright notice and this permission notice shall be included in all copies or substantial
16
+ * portions of the Software.
17
+ *
18
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
19
+ * TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21
+ * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22
+ */
23
+
24
+ import {
25
+ Button,
26
+ CoreIcons,
27
+ Divider,
28
+ ToggleButtonGroup,
29
+ useMemoDebugger,
30
+ } from "@react-fabric/core";
31
+ import { isEmpty } from "@react-fabric/utilities";
32
+ import { type EChartOption, type EChartsType } from "echarts";
33
+ import { memo, useCallback, useEffect, useRef, useState, type FC } from "react";
34
+ import { type BaseChart, type TimeSeriesType } from "../types";
35
+ import { timeSeriesRenderer } from "../types/utils";
36
+ import { ChartContainer } from "../wrapper/ChartContainer";
37
+ import { ChartWrapper } from "../wrapper/ChartWrapper";
38
+ import { PaletteSelect } from "../wrapper/PaletteSelect";
39
+
40
+ export interface TimeSeriesProps extends BaseChart, TimeSeriesType {
41
+ type?: "line" | "column" | "line-stacked" | "column-stacked";
42
+ onClick?: (data: { category: string; series: string }) => void;
43
+ onBrush?: (range: { start: Date; end: Date }) => void;
44
+ }
45
+
46
+ const TimeSeriesChart: FC<TimeSeriesProps> = memo(
47
+ ({
48
+ data,
49
+ onExport,
50
+ categoryAxisName,
51
+ valueAxisName,
52
+ title,
53
+ theme: chartTheme,
54
+ type: chartType = "column",
55
+ onClick,
56
+ onBrush,
57
+ }: TimeSeriesProps) => {
58
+ const chartRef = useRef<EChartsType>(null);
59
+ const [type, setType] = useState(chartType);
60
+ const [theme, setTheme] = useState(chartTheme);
61
+
62
+ useEffect(() => {
63
+ setType(chartType);
64
+ }, [chartType]);
65
+ useEffect(() => {
66
+ setTheme(chartTheme);
67
+ }, [chartTheme]);
68
+
69
+ const enableBrush = useCallback(() => {
70
+ chartRef.current?.dispatchAction({
71
+ type: "restore",
72
+ });
73
+ chartRef.current?.dispatchAction({
74
+ type: "takeGlobalCursor",
75
+ // If intending to enable brush, must set. Otherwise, the mouse will be disabled to brush.
76
+ key: "brush",
77
+ brushOption: {
78
+ // See more info in the `brushType` of "brush component".
79
+ // If set as `false`, the mouse is disabled to brush.
80
+ brushType: "lineX",
81
+ // See more info in the `brushModel` of "brush component".
82
+ // IF not set, use the `brushMode` of brush component.
83
+ brushMode: "rect",
84
+ },
85
+ });
86
+ }, []);
87
+
88
+ useEffect(() => {
89
+ const chart = chartRef.current;
90
+ if (chart && !chart?.isDisposed()) {
91
+ enableBrush();
92
+ let range = [0, 0];
93
+ chart.on("brushselected", (e: KeyValue) => {
94
+ range = e.batch?.[0]?.areas?.[0]?.coordRange;
95
+ });
96
+ chart.on("brushEnd", () => {
97
+ onBrush?.({ start: new Date(range?.[0]), end: new Date(range?.[1]) });
98
+ enableBrush();
99
+ });
100
+ return () => {
101
+ !chart.isDisposed() && chart.off("brushEnd");
102
+ !chart.isDisposed() && chart.off("brushselected");
103
+ };
104
+ }
105
+ }, [chartRef.current, onBrush]);
106
+
107
+ const options = useMemoDebugger<EChartOption>(
108
+ () => {
109
+ if (isEmpty(data)) {
110
+ chartRef.current?.clear();
111
+ return {};
112
+ }
113
+
114
+ const stack = type.includes("stacked") ? "stack" : undefined;
115
+
116
+ const [_type, stacked] = type.split("-");
117
+ const categoryAxis: AnyObject = {
118
+ name: categoryAxisName,
119
+ type: "time",
120
+ nameGap: 32,
121
+ nameLocation: "center",
122
+ axisTick: {
123
+ show: false,
124
+ },
125
+ axisLabel: {
126
+ formatter: {
127
+ year: "{yyyy}",
128
+ month: "{MMM}",
129
+ day: "{d}",
130
+ hour: "{HH}:{mm}",
131
+ minute: "{HH}:{mm}",
132
+ second: "{HH}:{mm}:{ss}",
133
+ millisecond: "{hh}:{mm}:{ss} {SSS}",
134
+ none: "{yyyy}-{MM}-{dd} {hh}:{mm}:{ss} {SSS}",
135
+ } as AnyObject,
136
+ },
137
+ } as EChartOption.XAxis;
138
+ const valueAxis: AnyObject = {
139
+ name: valueAxisName,
140
+ type: "value",
141
+ nameGap: 32,
142
+ nameLocation: "center",
143
+ } as EChartOption.YAxis;
144
+
145
+ const series = data?.map(
146
+ (item) =>
147
+ ({
148
+ id: item.id,
149
+ stack,
150
+ areaStyle: stacked ? {} : undefined,
151
+ symbol: "none",
152
+ type: _type === "column" ? "bar" : _type,
153
+ name: item.label ?? item.id,
154
+ data: item.values,
155
+ universalTransition: true,
156
+ }) as AnyObject,
157
+ );
158
+
159
+ return {
160
+ xAxis: categoryAxis,
161
+ yAxis: valueAxis,
162
+ series,
163
+ brush: {
164
+ toolbox: ["lineX", "clear"],
165
+ brushType: "lineX",
166
+ xAxisIndex: 0,
167
+ },
168
+ tooltip: {
169
+ trigger: "axis",
170
+ confine: true,
171
+ position: "top",
172
+ } as AnyObject,
173
+ };
174
+ },
175
+ [data, type, categoryAxisName, valueAxisName],
176
+ "TimeChart options",
177
+ );
178
+
179
+ return (
180
+ <ChartContainer
181
+ title={title}
182
+ onExport={onExport}
183
+ theme={theme}
184
+ options={options}
185
+ chartRef={chartRef}
186
+ isEmpty={isEmpty(data)}
187
+ emptyIcon={CoreIcons.chartColumn}
188
+ dataTableRenderer={timeSeriesRenderer}
189
+ onClick={(e) => onClick?.({ category: e.data[0], series: e.seriesId })}
190
+ >
191
+ <ToggleButtonGroup value={type} onChange={setType as AnyObject}>
192
+ <Button
193
+ size="sm"
194
+ variant="link"
195
+ icon={CoreIcons.chartLine}
196
+ value="line"
197
+ aria-label="line"
198
+ />
199
+ <Button
200
+ size="sm"
201
+ variant="link"
202
+ icon={CoreIcons.chartColumn}
203
+ value="column"
204
+ aria-label="column"
205
+ />
206
+ <Button
207
+ size="sm"
208
+ variant="link"
209
+ icon={CoreIcons.chartLineStacked}
210
+ value="line-stacked"
211
+ aria-label="line-stacked"
212
+ />
213
+ <Button
214
+ size="sm"
215
+ variant="link"
216
+ icon={CoreIcons.chartColumnStacked}
217
+ value="column-stacked"
218
+ aria-label="column-stacked"
219
+ />
220
+ </ToggleButtonGroup>
221
+ <Divider vertical />
222
+ <PaletteSelect theme={theme} onClick={setTheme} />
223
+ </ChartContainer>
224
+ );
225
+ },
226
+ );
227
+ TimeSeriesChart.displayName = "TimeSeriesChart";
228
+
229
+ /**
230
+ * Chart to display simple data time series
231
+ */
232
+ export const TimeSeries: FC<TimeSeriesProps> = (props) => (
233
+ <ChartWrapper>
234
+ <TimeSeriesChart {...props} />
235
+ </ChartWrapper>
236
+ );