@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,295 @@
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 { isArray, isEmpty } from "@react-fabric/utilities";
32
+ import { type EChartOption, type EChartsType } from "echarts";
33
+ import { memo, useEffect, useRef, useState, type FC } from "react";
34
+ import { ChartPalette } from "../theme/palettes";
35
+ import { type BaseChart } from "../types";
36
+ import { activityRenderer } from "../types/utils";
37
+ import { ChartContainer } from "../wrapper/ChartContainer";
38
+ import { ChartWrapper } from "../wrapper/ChartWrapper";
39
+ import { PaletteSelect } from "../wrapper/PaletteSelect";
40
+
41
+ export interface ActivityMapProps extends BaseChart {
42
+ heatmapPalette?: string[];
43
+ data: Array<Array<[low: number, high: number, count: number]>>;
44
+ time?: "day-hour" | "month-day";
45
+ type?: "scatter" | "heatmap";
46
+ /**
47
+ * day/month names
48
+ */
49
+ highLabels?: string[];
50
+ /**
51
+ * hours/day names
52
+ */
53
+ lowLabels?: string[];
54
+ onClick?: (highIndex: number, lowIndex: number) => void;
55
+ }
56
+
57
+ const DAY_HOUR = {
58
+ high: ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"],
59
+ low: [
60
+ "12a",
61
+ "1a",
62
+ "2a",
63
+ "3a",
64
+ "4a",
65
+ "5a",
66
+ "6a",
67
+ "7a",
68
+ "8a",
69
+ "9a",
70
+ "10a",
71
+ "11a",
72
+ "12p",
73
+ "1p",
74
+ "2p",
75
+ "3p",
76
+ "4p",
77
+ "5p",
78
+ "6p",
79
+ "7p",
80
+ "8p",
81
+ "9p",
82
+ "10p",
83
+ "11p",
84
+ ],
85
+ };
86
+ const MONTH_DAY = {
87
+ high: [
88
+ "Jan",
89
+ "Feb",
90
+ "Mar",
91
+ "Apr",
92
+ "May",
93
+ "Jun",
94
+ "Jul",
95
+ "Aug",
96
+ "Sep",
97
+ "Oct",
98
+ "Nov",
99
+ "Dec",
100
+ ],
101
+ low: [
102
+ "1",
103
+ "2",
104
+ "3",
105
+ "4",
106
+ "5",
107
+ "6",
108
+ "7",
109
+ "8",
110
+ "9",
111
+ "10",
112
+ "11",
113
+ "12",
114
+ "13",
115
+ "14",
116
+ "15",
117
+ "16",
118
+ "17",
119
+ "18",
120
+ "19",
121
+ "20",
122
+ "21",
123
+ "22",
124
+ "23",
125
+ "24",
126
+ "25",
127
+ "26",
128
+ "27",
129
+ "28",
130
+ "29",
131
+ "30",
132
+ "31",
133
+ ],
134
+ };
135
+
136
+ const ActivityMapChart = memo(
137
+ ({
138
+ data = [],
139
+ title,
140
+ onExport,
141
+ theme: chartTheme = "activity",
142
+ heatmapPalette,
143
+ highLabels,
144
+ lowLabels,
145
+ time = "day-hour",
146
+ type: chartType = "scatter",
147
+ onClick,
148
+ }: ActivityMapProps) => {
149
+ const chartRef = useRef<EChartsType>(null);
150
+ const [type, setType] = useState(chartType);
151
+ const [theme, setTheme] = useState(chartTheme);
152
+
153
+ useEffect(() => {
154
+ setType(chartType);
155
+ }, [chartType]);
156
+ useEffect(() => {
157
+ setTheme(chartTheme);
158
+ }, [chartTheme]);
159
+
160
+ const options = useMemoDebugger<EChartOption>(
161
+ () => {
162
+ const defaultMap = time === "day-hour" ? DAY_HOUR : MONTH_DAY;
163
+
164
+ const options: KeyValue = {
165
+ series: [],
166
+ };
167
+
168
+ if (isEmpty(data)) return {};
169
+
170
+ options.xAxis = {
171
+ type: "category",
172
+ data: lowLabels ?? defaultMap.low,
173
+ boundaryGap: type === "heatmap",
174
+ splitLine: {
175
+ show: true,
176
+ },
177
+ axisLine: {
178
+ show: false,
179
+ },
180
+ };
181
+ options.yAxis = {
182
+ type: "category",
183
+ axisLabel: {
184
+ margin: 16,
185
+ },
186
+ data: highLabels ?? defaultMap.high,
187
+ axisLine: {
188
+ show: false,
189
+ },
190
+ };
191
+ options.series = data.map((points, index) => {
192
+ const total = points.reduce((t, p) => t + p[2], 0);
193
+ return {
194
+ id: index,
195
+ name: defaultMap.high[index],
196
+ type,
197
+ symbolSize: function (dataItem: number[]) {
198
+ return dataItem[2] === 0
199
+ ? 0
200
+ : Math.min(Math.max(4, (dataItem[2] / total) * 256), 48);
201
+ },
202
+ data: points,
203
+ universalTransition: true,
204
+ };
205
+ });
206
+
207
+ if (type === "heatmap") {
208
+ const dataPoints = data.map((points) =>
209
+ Math.max(...points.map((pt) => pt[2])),
210
+ );
211
+ options.grid = {
212
+ top: 32,
213
+ bottom: 128,
214
+ };
215
+ options.visualMap = {
216
+ type: "piecewise",
217
+ min: 1,
218
+ show: type === "heatmap",
219
+ color: heatmapPalette ?? [...ChartPalette.Heatmap],
220
+ max: Math.max(...dataPoints),
221
+ calculable: true,
222
+ orient: "horizontal",
223
+ left: "center",
224
+ bottom: 48,
225
+ };
226
+ }
227
+
228
+ return {
229
+ grid: {
230
+ top: 32,
231
+ bottom: 64,
232
+ },
233
+ tooltip: {
234
+ trigger: "item",
235
+ confine: true,
236
+ position: "top",
237
+ valueFormatter: (c: AnyObject) => {
238
+ return isArray(c) ? c[2] : c;
239
+ },
240
+ } as AnyObject,
241
+ ...options,
242
+ };
243
+ },
244
+ [data, title, type],
245
+ "ActivityChart options",
246
+ );
247
+
248
+ return (
249
+ <ChartContainer
250
+ title={title}
251
+ onExport={onExport}
252
+ theme={theme}
253
+ options={options}
254
+ chartRef={chartRef}
255
+ isEmpty={isEmpty(data)}
256
+ emptyIcon={CoreIcons.chartActivityScatter}
257
+ dataTableRenderer={activityRenderer}
258
+ onClick={(e) => onClick?.(e.seriesIndex, e.dataIndex)}
259
+ >
260
+ <ToggleButtonGroup value={type} onChange={setType as AnyObject}>
261
+ <Button
262
+ size="sm"
263
+ variant="link"
264
+ value="scatter"
265
+ aria-label="scatter"
266
+ icon={CoreIcons.chartActivityScatter}
267
+ />
268
+ <Button
269
+ size="sm"
270
+ variant="link"
271
+ value="heatmap"
272
+ aria-label="heatmap"
273
+ icon={CoreIcons.chartActivityCalendar}
274
+ />
275
+ </ToggleButtonGroup>
276
+ <Divider vertical />
277
+ <PaletteSelect
278
+ theme={theme}
279
+ onClick={setTheme}
280
+ defaultTheme="activity"
281
+ />
282
+ </ChartContainer>
283
+ );
284
+ },
285
+ );
286
+ ActivityMapChart.displayName = "ActivityMapChart";
287
+
288
+ /**
289
+ * Chart showing activity map
290
+ */
291
+ export const ActivityMap: FC<ActivityMapProps> = (props) => (
292
+ <ChartWrapper>
293
+ <ActivityMapChart {...props} />
294
+ </ChartWrapper>
295
+ );
@@ -0,0 +1,186 @@
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 { compareValues, Format, isEmpty } from "@react-fabric/utilities";
32
+ import { type EChartOption, type EChartsType } from "echarts";
33
+ import { memo, useEffect, useRef, useState, type FC } from "react";
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
+ import { PaletteSelect } from "../wrapper/PaletteSelect";
39
+
40
+ export interface CountSeriesProps extends BaseChart, CountType {
41
+ type?: "pie" | "bar" | "column";
42
+ onClick?: (key: string) => void;
43
+ }
44
+
45
+ const CountSeriesChart: FC<CountSeriesProps> = memo(
46
+ ({
47
+ data,
48
+ title,
49
+ onExport,
50
+ theme: chartTheme,
51
+ type: chartType = "pie",
52
+ onClick,
53
+ }: CountSeriesProps) => {
54
+ const chartRef = useRef<EChartsType>(null);
55
+ const [type, setType] = useState(chartType);
56
+ const [theme, setTheme] = useState(chartTheme);
57
+
58
+ useEffect(() => {
59
+ setType(chartType);
60
+ }, [chartType]);
61
+ useEffect(() => {
62
+ setTheme(chartTheme);
63
+ }, [chartTheme]);
64
+
65
+ const options = useMemoDebugger<EChartOption>(
66
+ () => {
67
+ if (isEmpty(data)) {
68
+ chartRef.current?.clear();
69
+ return {};
70
+ }
71
+
72
+ const sorted = data?.sort(compareValues("desc", "count"));
73
+
74
+ const categoryAxis: AnyObject = {
75
+ type: "category",
76
+ name: title ?? "Count Series",
77
+ nameLocation: "center",
78
+ axisTick: {
79
+ show: false,
80
+ },
81
+ data: [],
82
+ } as EChartOption.XAxis;
83
+ const valueAxis: AnyObject = {
84
+ type: "value",
85
+ axisLabel: {
86
+ formatter: (val: AnyObject) => Format.number(val),
87
+ },
88
+ } as EChartOption.YAxis;
89
+
90
+ const series =
91
+ type === "pie"
92
+ ? [
93
+ {
94
+ type: "pie",
95
+ name: title ?? "Count Series",
96
+ data: sorted.map((item, index) => ({
97
+ id: item.id,
98
+ value: item.count,
99
+ name: item.label ?? item.id,
100
+ })),
101
+ universalTransition: true,
102
+ },
103
+ ]
104
+ : sorted.map((item, index) => ({
105
+ id: item.id,
106
+ type: "bar",
107
+ label: {
108
+ show: true,
109
+ rotate: type === "column" ? 90 : 0,
110
+ formatter: "{a}",
111
+ position: "inside",
112
+ color: "#fff",
113
+ },
114
+ data: [
115
+ {
116
+ name: title ?? "Count Series",
117
+ value: item.count,
118
+ },
119
+ ],
120
+ name: item.label ?? item.id,
121
+ universalTransition: true,
122
+ }));
123
+ return {
124
+ series,
125
+ countSeries: true,
126
+ xAxis:
127
+ type === "pie" ? null : type === "bar" ? valueAxis : categoryAxis,
128
+ yAxis:
129
+ type === "pie" ? null : type === "bar" ? categoryAxis : valueAxis,
130
+ };
131
+ },
132
+ [data, title, type],
133
+ "CountChart options",
134
+ );
135
+
136
+ return (
137
+ <ChartContainer
138
+ title={title}
139
+ onExport={onExport}
140
+ theme={theme}
141
+ options={options}
142
+ chartRef={chartRef}
143
+ isEmpty={isEmpty(data)}
144
+ emptyIcon={CoreIcons.chartPie}
145
+ dataTableRenderer={countRenderer}
146
+ onClick={(e) => onClick?.(e.data.id ?? e.seriesId)}
147
+ >
148
+ <ToggleButtonGroup value={type} onChange={setType as AnyObject}>
149
+ <Button
150
+ size="sm"
151
+ variant="link"
152
+ value="pie"
153
+ aria-label="pie"
154
+ icon={CoreIcons.chartPie}
155
+ />
156
+ <Button
157
+ size="sm"
158
+ variant="link"
159
+ value="column"
160
+ aria-label="column"
161
+ icon={CoreIcons.chartColumn}
162
+ />
163
+ <Button
164
+ size="sm"
165
+ variant="link"
166
+ value="bar"
167
+ aria-label="bar"
168
+ icon={CoreIcons.chartBar}
169
+ />
170
+ </ToggleButtonGroup>
171
+ <Divider vertical />
172
+ <PaletteSelect theme={theme} onClick={setTheme} />
173
+ </ChartContainer>
174
+ );
175
+ },
176
+ );
177
+ CountSeriesChart.displayName = "CountSeriesChart";
178
+
179
+ /**
180
+ * Chart to display simple data count series
181
+ */
182
+ export const CountSeries: FC<CountSeriesProps> = (props) => (
183
+ <ChartWrapper>
184
+ <CountSeriesChart {...props} />
185
+ </ChartWrapper>
186
+ );
@@ -0,0 +1,228 @@
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, useEffect, useRef, useState, type FC } from "react";
34
+ import { type BaseChart, type SeriesType } from "../types";
35
+ import { seriesRenderer } 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 DataSeriesProps extends BaseChart, SeriesType {
41
+ type?:
42
+ | "column"
43
+ | "line"
44
+ | "bar"
45
+ | "radar"
46
+ | "line-stacked"
47
+ | "column-stacked"
48
+ | "bar-stacked";
49
+ onClick?: (data: { category: string; series: string }) => void;
50
+ }
51
+
52
+ const DataSeriesChart: FC<DataSeriesProps> = memo(
53
+ ({
54
+ data,
55
+ onExport,
56
+ categories,
57
+ categoryAxisName,
58
+ valueAxisName,
59
+ title,
60
+ theme: chartTheme,
61
+ type: chartType = "column",
62
+ onClick,
63
+ }: DataSeriesProps) => {
64
+ const chartRef = useRef<EChartsType>(null);
65
+ const [type, setType] = useState(chartType);
66
+ const [theme, setTheme] = useState(chartTheme);
67
+
68
+ useEffect(() => {
69
+ setType(chartType);
70
+ }, [chartType]);
71
+ useEffect(() => {
72
+ setTheme(chartTheme);
73
+ }, [chartTheme]);
74
+
75
+ const options = useMemoDebugger<EChartOption>(
76
+ () => {
77
+ if (isEmpty(data)) {
78
+ chartRef.current?.clear();
79
+ return {};
80
+ }
81
+
82
+ const stack = type.includes("stacked") ? "stack" : undefined;
83
+
84
+ const [_type, stacked] = type.split("-");
85
+ const categoryAxis: AnyObject =
86
+ _type === "radar"
87
+ ? undefined
88
+ : ({
89
+ name: categoryAxisName,
90
+ type: "category",
91
+ nameGap: 32,
92
+ nameLocation: "center",
93
+ axisTick: {
94
+ show: false,
95
+ },
96
+ data: categories,
97
+ } as EChartOption.XAxis);
98
+ const valueAxis: AnyObject =
99
+ _type === "radar"
100
+ ? undefined
101
+ : ({
102
+ name: valueAxisName,
103
+ type: "value",
104
+ nameGap: 32,
105
+ nameLocation: "center",
106
+ } as EChartOption.YAxis);
107
+ const radar =
108
+ _type !== "radar"
109
+ ? undefined
110
+ : ({
111
+ shape: "circle",
112
+ indicator: categories?.map((c) => ({ name: c })),
113
+ } as AnyObject);
114
+
115
+ const series = data?.map((item, index) => ({
116
+ id: item.id,
117
+ stack,
118
+ areaStyle: stacked ? {} : undefined,
119
+ type: _type === "column" ? "bar" : _type,
120
+ name: item.label ?? item.id,
121
+ universalTransition: true,
122
+ data:
123
+ _type === "radar"
124
+ ? [
125
+ {
126
+ value: item.values,
127
+ id: item.id,
128
+ name: item.label ?? item.id,
129
+ },
130
+ ]
131
+ : item.values,
132
+ }));
133
+
134
+ return {
135
+ xAxis: type.startsWith("bar") ? valueAxis : categoryAxis,
136
+ yAxis: type.startsWith("bar") ? categoryAxis : valueAxis,
137
+ radar,
138
+ series,
139
+ tooltip: {
140
+ trigger: _type === "radar" ? "item" : "axis",
141
+ confine: true,
142
+ position: "top",
143
+ } as AnyObject,
144
+ };
145
+ },
146
+ [data, type, categories, categoryAxisName, valueAxisName],
147
+ "DataChart options",
148
+ );
149
+
150
+ return (
151
+ <ChartContainer
152
+ title={title}
153
+ onExport={onExport}
154
+ theme={theme}
155
+ options={options}
156
+ chartRef={chartRef}
157
+ isEmpty={isEmpty(data)}
158
+ emptyIcon={CoreIcons.chartColumn}
159
+ dataTableRenderer={seriesRenderer}
160
+ onClick={(e) => onClick?.({ category: e.name, series: e.seriesId })}
161
+ >
162
+ <ToggleButtonGroup value={type} onChange={setType as AnyObject}>
163
+ <Button
164
+ size="sm"
165
+ variant="link"
166
+ icon={CoreIcons.chartLine}
167
+ aria-label="line"
168
+ value="line"
169
+ />
170
+ <Button
171
+ size="sm"
172
+ variant="link"
173
+ icon={CoreIcons.chartColumn}
174
+ aria-label="column"
175
+ value="column"
176
+ />
177
+ <Button
178
+ size="sm"
179
+ variant="link"
180
+ icon={CoreIcons.chartBar}
181
+ aria-label="bar"
182
+ value="bar"
183
+ />
184
+ <Button
185
+ size="sm"
186
+ variant="link"
187
+ icon={CoreIcons.chartRadar}
188
+ aria-label="radar"
189
+ value="radar"
190
+ />
191
+ <Button
192
+ size="sm"
193
+ variant="link"
194
+ icon={CoreIcons.chartLineStacked}
195
+ aria-label="line-stacked"
196
+ value="line-stacked"
197
+ />
198
+ <Button
199
+ size="sm"
200
+ variant="link"
201
+ icon={CoreIcons.chartColumnStacked}
202
+ aria-label="column-stacked"
203
+ value="column-stacked"
204
+ />
205
+ <Button
206
+ size="sm"
207
+ variant="link"
208
+ icon={CoreIcons.chartBarStacked}
209
+ aria-label="bar-stacked"
210
+ value="bar-stacked"
211
+ />
212
+ </ToggleButtonGroup>
213
+ <Divider vertical />
214
+ <PaletteSelect theme={theme} onClick={setTheme} />
215
+ </ChartContainer>
216
+ );
217
+ },
218
+ );
219
+ DataSeriesChart.displayName = "DataSeriesChart";
220
+
221
+ /**
222
+ * Chart to display simple data category series
223
+ */
224
+ export const DataSeries: FC<DataSeriesProps> = (props) => (
225
+ <ChartWrapper>
226
+ <DataSeriesChart {...props} />
227
+ </ChartWrapper>
228
+ );