@react-fabric/chart 1.0.0-beta.8 → 1.0.1

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 (82) hide show
  1. package/{src/wrapper/Chart.module.css → css/_chart.css} +11 -11
  2. package/css/styles.css +24 -0
  3. package/dist/cjs/charts/ActivityMap.js +70 -36
  4. package/dist/cjs/charts/CountSeries.js +67 -35
  5. package/dist/cjs/charts/DataSeries.js +75 -40
  6. package/dist/cjs/charts/Generic.js +41 -19
  7. package/dist/cjs/charts/MapSeries.js +41 -17
  8. package/dist/cjs/charts/TimeSeries.js +73 -40
  9. package/dist/cjs/charts/TimeSlider.js +40 -21
  10. package/dist/cjs/charts/WordBubble.js +65 -29
  11. package/dist/cjs/index.js +28 -0
  12. package/dist/cjs/theme/darkBase.js +13 -10
  13. package/dist/cjs/theme/index.js +43 -23
  14. package/dist/cjs/theme/lightBase.js +18 -15
  15. package/dist/cjs/theme/palettes.js +5 -54
  16. package/dist/cjs/types/charts.js +4 -2
  17. package/dist/cjs/types/utils.js +6 -9
  18. package/dist/cjs/wrapper/ChartContainer.js +26 -18
  19. package/dist/cjs/wrapper/ChartToolbar.js +4 -5
  20. package/dist/cjs/wrapper/ChartWrapper.js +4 -5
  21. package/dist/cjs/wrapper/PaletteSelect.js +31 -18
  22. package/dist/esm/charts/ActivityMap.js +71 -37
  23. package/dist/esm/charts/CountSeries.js +68 -36
  24. package/dist/esm/charts/DataSeries.js +76 -41
  25. package/dist/esm/charts/Generic.js +43 -21
  26. package/dist/esm/charts/MapSeries.js +41 -17
  27. package/dist/esm/charts/TimeSeries.js +74 -41
  28. package/dist/esm/charts/TimeSlider.js +41 -22
  29. package/dist/esm/charts/WordBubble.js +67 -31
  30. package/dist/esm/index.js +30 -0
  31. package/dist/esm/theme/darkBase.js +13 -10
  32. package/dist/esm/theme/index.js +43 -23
  33. package/dist/esm/theme/lightBase.js +18 -15
  34. package/dist/esm/theme/palettes.js +5 -54
  35. package/dist/esm/types/charts.js +4 -2
  36. package/dist/esm/types/utils.js +6 -9
  37. package/dist/esm/wrapper/ChartContainer.js +27 -19
  38. package/dist/esm/wrapper/ChartToolbar.js +4 -5
  39. package/dist/esm/wrapper/ChartWrapper.js +4 -5
  40. package/dist/esm/wrapper/PaletteSelect.js +32 -19
  41. package/dist/types/charts/ActivityMap.d.ts +20 -4
  42. package/dist/types/charts/CountSeries.d.ts +25 -3
  43. package/dist/types/charts/DataSeries.d.ts +26 -1
  44. package/dist/types/charts/Generic.d.ts +36 -52
  45. package/dist/types/charts/MapSeries.d.ts +31 -3
  46. package/dist/types/charts/TimeSeries.d.ts +24 -1
  47. package/dist/types/charts/TimeSlider.d.ts +19 -5
  48. package/dist/types/charts/WordBubble.d.ts +28 -5
  49. package/dist/types/index.d.ts +26 -0
  50. package/dist/types/theme/darkBase.d.ts +8 -5
  51. package/dist/types/theme/index.d.ts +3 -2
  52. package/dist/types/theme/lightBase.d.ts +8 -5
  53. package/dist/types/types/charts.d.ts +2 -1
  54. package/dist/types/types/index.d.ts +30 -12
  55. package/dist/types/types/world.svg.d.ts +1 -1
  56. package/dist/types/wrapper/ChartContainer.d.ts +7 -7
  57. package/dist/types/wrapper/ChartToolbar.d.ts +2 -3
  58. package/dist/types/wrapper/ChartWrapper.d.ts +3 -5
  59. package/dist/types/wrapper/PaletteSelect.d.ts +6 -1
  60. package/package.json +17 -21
  61. package/src/charts/ActivityMap.tsx +95 -94
  62. package/src/charts/CountSeries.tsx +73 -65
  63. package/src/charts/DataSeries.tsx +104 -98
  64. package/src/charts/Generic.tsx +56 -86
  65. package/src/charts/MapSeries.tsx +153 -151
  66. package/src/charts/TimeSeries.tsx +96 -79
  67. package/src/charts/TimeSlider.tsx +194 -195
  68. package/src/charts/WordBubble.tsx +97 -62
  69. package/src/index.ts +22 -17
  70. package/src/theme/darkBase.ts +12 -9
  71. package/src/theme/index.ts +92 -30
  72. package/src/theme/lightBase.ts +17 -14
  73. package/src/theme/palettes.ts +1 -52
  74. package/src/types/charts.ts +6 -2
  75. package/src/types/index.ts +40 -4
  76. package/src/types/utils.ts +18 -30
  77. package/src/wrapper/ChartContainer.tsx +48 -79
  78. package/src/wrapper/ChartToolbar.tsx +5 -12
  79. package/src/wrapper/ChartWrapper.tsx +5 -10
  80. package/src/wrapper/PaletteSelect.tsx +43 -40
  81. package/dist/cjs/wrapper/Chart.module.css +0 -1
  82. package/dist/esm/wrapper/Chart.module.css +0 -1
@@ -1,20 +1,20 @@
1
- import { type EChartOption, type EChartsType } from "echarts";
2
- import "echarts-wordcloud";
3
- import { type RefObject } from "react";
1
+ import { type EChartsOption, type EChartsType } from "echarts";
2
+ import { type Ref } from "react";
4
3
  import { type Theme } from "../types";
4
+ import echarts from "../types/charts";
5
5
  export declare const ChartContainer: import("react").MemoExoticComponent<({ theme, title, options, children, onResize, onClick, onExport, isEmpty, emptyIcon, chartRef: _ref, dataTableRenderer, }: {
6
6
  title?: string;
7
7
  theme?: Theme;
8
8
  isEmpty?: boolean;
9
9
  emptyIcon?: string;
10
- options: EChartOption;
10
+ options: EChartsOption;
11
11
  children?: AnyObject;
12
- chartRef?: RefObject<EChartsType>;
13
- onClick?: (event: AnyObject) => void;
12
+ chartRef?: Ref<EChartsType>;
13
+ onClick?: (event: echarts.ECElementEvent) => void;
14
14
  onExport?: (event: AnyObject) => void;
15
15
  dataTableRenderer?: (opt: KeyValue) => string;
16
16
  onResize?: (size: {
17
17
  width: number;
18
18
  height: number;
19
19
  }) => void;
20
- }) => import("react/jsx-runtime").JSX.Element>;
20
+ }) => import("react").JSX.Element>;
@@ -1,3 +1,2 @@
1
- import { type ChildrenProp } from "@react-fabric/core/dist/types/types";
2
- import { type FC } from "react";
3
- export declare const ChartToolbar: FC<ChildrenProp>;
1
+ import type { PropsWithChildren } from "react";
2
+ export declare function ChartToolbar({ children }: PropsWithChildren): import("react").JSX.Element;
@@ -1,5 +1,3 @@
1
- /// <reference types="react" />
2
- import "echarts-wordcloud";
3
- export declare const ChartWrapper: ({ children }: {
4
- children?: JSX.Element;
5
- }) => import("react/jsx-runtime").JSX.Element;
1
+ export declare function ChartWrapper({ children }: {
2
+ children?: React.ReactElement;
3
+ }): import("react").JSX.Element;
@@ -1 +1,6 @@
1
- export declare const PaletteSelect: ({ onClick, theme, defaultTheme, }: KeyValue) => import("react/jsx-runtime").JSX.Element;
1
+ import type { Theme } from "../types";
2
+ export declare function PaletteSelect({ onClick, theme, defaultTheme, }: {
3
+ theme?: Theme;
4
+ defaultTheme?: Theme;
5
+ onClick: (theme: Theme) => void;
6
+ }): import("react").JSX.Element;
package/package.json CHANGED
@@ -1,19 +1,21 @@
1
1
  {
2
2
  "name": "@react-fabric/chart",
3
- "version": "1.0.0-beta.8",
3
+ "version": "1.0.1",
4
4
  "description": "RUF: Charting package",
5
5
  "author": "Adarsh Pastakia <adarshpastakia@outlook.com>",
6
6
  "license": "MIT",
7
- "repository": "https://github.com/adarshpastakia/react-ui-framework/tree/main/packages/chart",
7
+ "repository": "https://github.com/adarshpastakia/react-fabric/tree/main/packages/chart",
8
8
  "source": "src/index.ts",
9
9
  "main": "dist/cjs/index.js",
10
10
  "module": "dist/esm/index.js",
11
11
  "types": "dist/types/index.d.ts",
12
12
  "directories": {
13
13
  "src": "src",
14
+ "styles": "css",
14
15
  "test": "tests"
15
16
  },
16
17
  "files": [
18
+ "css",
17
19
  "src",
18
20
  "dist"
19
21
  ],
@@ -22,8 +24,6 @@
22
24
  "registry": "https://registry.npmjs.org/"
23
25
  },
24
26
  "scripts": {
25
- "makelink": "yarn link",
26
- "removelink": "yarn unlink",
27
27
  "prebuild": "rm -rf ./dist",
28
28
  "build:dts": "tsc -p ./tsconfig.build.json",
29
29
  "build:rollup": "rollup -c ../../rollup.config.mjs",
@@ -33,26 +33,22 @@
33
33
  "watch": "npm-run-all -p watch:rollup watch:dts"
34
34
  },
35
35
  "dependencies": {
36
- "@react-fabric/core": "^1.0.0-beta.8",
37
- "@react-fabric/utilities": "^1.0.0-beta.8",
38
- "classnames": "^2.5.1",
39
- "color": "^4.2.3",
40
- "date-fns": "^4.0.0",
41
- "echarts": "^5.5.1",
42
- "echarts-wordcloud": "^2.1.0"
36
+ "@echarts-x/custom-word-cloud": "~1.0.1",
37
+ "@react-fabric/core": "^1.0.1",
38
+ "@react-fabric/utilities": "^1.0.1",
39
+ "chroma-js": "~3.2.0",
40
+ "date-fns": "~4.4.0",
41
+ "echarts": "~6.1.0"
43
42
  },
44
43
  "peerDependencies": {
45
- "i18next": "^23.15.1",
46
- "react": ">=18.0.0 <20.0.0",
47
- "react-dom": ">=18.0.0 <20.0.0",
48
- "react-i18next": "^15.0.2"
49
- },
50
- "devDependencies": {
51
- "@types/echarts": "^4.9.22"
44
+ "i18next": "~26.3.6",
45
+ "react": "~19.3.0",
46
+ "react-dom": "~19.3.0",
47
+ "react-i18next": "~17.0.11"
52
48
  },
53
49
  "engines": {
54
- "node": ">=18.12.0",
55
- "npm": ">=8.19.0"
50
+ "node": ">=24.0.0",
51
+ "npm": ">=11.0.0"
56
52
  },
57
- "gitHead": "9f1ede6e9629c9e79121354332cdebabad5878c5"
53
+ "gitHead": "1419a9df05b5e3850e1d2b4e896ab63df702c7e8"
58
54
  }
@@ -21,17 +21,10 @@
21
21
  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22
22
  */
23
23
 
24
- import {
25
- Button,
26
- CoreIcons,
27
- Divider,
28
- ToggleButtonGroup,
29
- useMemoDebugger,
30
- } from "@react-fabric/core";
24
+ import { Button, Divider, ToggleButtonGroup, useMemoDebugger } from "@react-fabric/core";
31
25
  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";
26
+ import { type EChartsOption, type EChartsType, type ScatterSeriesOption } from "echarts";
27
+ import { memo, useEffect, useRef, useState } from "react";
35
28
  import { type BaseChart } from "../types";
36
29
  import { activityRenderer } from "../types/utils";
37
30
  import { ChartContainer } from "../wrapper/ChartContainer";
@@ -40,7 +33,7 @@ import { PaletteSelect } from "../wrapper/PaletteSelect";
40
33
 
41
34
  export interface ActivityMapProps extends BaseChart {
42
35
  heatmapPalette?: string[];
43
- data: Array<Array<[low: number, high: number, count: number]>>;
36
+ series: Array<Array<[low: number, high: number, count: number]>>;
44
37
  time?: "day-hour" | "month-day";
45
38
  type?: "scatter" | "heatmap";
46
39
  /**
@@ -84,20 +77,7 @@ const DAY_HOUR = {
84
77
  ],
85
78
  };
86
79
  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
- ],
80
+ high: ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"],
101
81
  low: [
102
82
  "1",
103
83
  "2",
@@ -135,39 +115,45 @@ const MONTH_DAY = {
135
115
 
136
116
  const ActivityMapChart = memo(
137
117
  ({
138
- data = [],
118
+ series = [],
139
119
  title,
140
120
  onExport,
121
+ options: optionOverride,
141
122
  theme: chartTheme = "activity",
142
123
  heatmapPalette,
143
124
  highLabels,
144
125
  lowLabels,
126
+ showThemeSelector,
127
+ showTypeSelector,
145
128
  time = "day-hour",
146
129
  type: chartType = "scatter",
147
130
  onClick,
148
131
  }: ActivityMapProps) => {
149
132
  const chartRef = useRef<EChartsType>(null);
150
- const [type, setType] = useState(chartType);
151
- const [theme, setTheme] = useState(chartTheme);
133
+ const [type, setType] = useState(() => chartType);
134
+ const [theme, setTheme] = useState(() => chartTheme);
152
135
 
153
136
  useEffect(() => {
137
+ // eslint-disable-next-line @eslint-react/set-state-in-effect
154
138
  setType(chartType);
155
139
  }, [chartType]);
140
+
156
141
  useEffect(() => {
142
+ // eslint-disable-next-line @eslint-react/set-state-in-effect
157
143
  setTheme(chartTheme);
158
144
  }, [chartTheme]);
159
145
 
160
- const options = useMemoDebugger<EChartOption>(
146
+ const options = useMemoDebugger<EChartsOption>(
161
147
  () => {
162
148
  const defaultMap = time === "day-hour" ? DAY_HOUR : MONTH_DAY;
163
149
 
164
- const options: KeyValue = {
150
+ const options: EChartsOption = {
165
151
  series: [],
166
152
  };
167
153
 
168
- if (isEmpty(data)) return {};
154
+ if (isEmpty(series)) return {};
169
155
 
170
- options.xAxis = {
156
+ options.xAxis = Object.assign({}, optionOverride?.xAxis, {
171
157
  type: "category",
172
158
  data: lowLabels ?? defaultMap.low,
173
159
  boundaryGap: type === "heatmap",
@@ -177,8 +163,8 @@ const ActivityMapChart = memo(
177
163
  axisLine: {
178
164
  show: false,
179
165
  },
180
- };
181
- options.yAxis = {
166
+ });
167
+ options.yAxis = Object.assign({}, optionOverride?.yAxis, {
182
168
  type: "category",
183
169
  axisLabel: {
184
170
  margin: 16,
@@ -187,42 +173,47 @@ const ActivityMapChart = memo(
187
173
  axisLine: {
188
174
  show: false,
189
175
  },
190
- };
191
- options.series = data.map((points, index) => {
176
+ });
177
+ options.series = series.map((points, index) => {
192
178
  const total = points.reduce((t, p) => t + p[2], 0);
193
179
  return {
194
- id: index,
180
+ id: `${index}`,
195
181
  name: defaultMap.high[index],
196
182
  type,
197
183
  symbolSize: function (dataItem: number[]) {
198
- return dataItem[2] === 0
199
- ? 0
200
- : Math.min(Math.max(4, (dataItem[2] / total) * 256), 48);
184
+ return dataItem[2] === 0 ? 0 : Math.min(Math.max(4, (dataItem[2] / total) * 256), 48);
201
185
  },
202
186
  data: points,
203
187
  universalTransition: true,
204
- };
188
+ } as ScatterSeriesOption;
205
189
  });
206
190
 
207
191
  if (type === "heatmap") {
208
- const dataPoints = data.map((points) =>
209
- Math.max(...points.map((pt) => pt[2])),
210
- );
192
+ const dataPoints = series.map((points) => Math.max(...points.map((pt) => pt[2])));
211
193
  options.grid = {
212
194
  top: 32,
213
- bottom: 128,
195
+ bottom: 64,
214
196
  };
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,
197
+ options.legend = {
198
+ show: false,
225
199
  };
200
+ options.visualMap = [
201
+ {
202
+ type: "piecewise",
203
+ min: 1,
204
+ show: type === "heatmap",
205
+ max: Math.max(...dataPoints),
206
+ // calculable: true,inRange:
207
+ orient: "horizontal",
208
+ left: "center",
209
+ bottom: 4,
210
+ },
211
+ ];
212
+ if (heatmapPalette) {
213
+ options.visualMap[0].inRange = {
214
+ color: heatmapPalette,
215
+ };
216
+ }
226
217
  }
227
218
 
228
219
  return {
@@ -230,18 +221,23 @@ const ActivityMapChart = memo(
230
221
  top: 32,
231
222
  bottom: 64,
232
223
  },
233
- tooltip: {
234
- trigger: "item",
235
- confine: true,
236
- position: "top",
237
- valueFormatter: (c: AnyObject) => {
238
- return isArray(c) ? c[2] : c;
224
+ tooltip: Object.assign(
225
+ {
226
+ trigger: "item",
227
+ confine: true,
228
+ position: "top",
229
+ appendToBody: true,
230
+ valueFormatter: (c: string | string[]) => {
231
+ return isArray(c) ? c[2] : c;
232
+ },
239
233
  },
240
- } as AnyObject,
234
+ optionOverride?.tooltip,
235
+ ),
236
+ ...optionOverride,
241
237
  ...options,
242
238
  };
243
239
  },
244
- [data, title, type],
240
+ [series, title, type, heatmapPalette, optionOverride],
245
241
  "ActivityChart options",
246
242
  );
247
243
 
@@ -252,33 +248,19 @@ const ActivityMapChart = memo(
252
248
  theme={theme}
253
249
  options={options}
254
250
  chartRef={chartRef}
255
- isEmpty={isEmpty(data)}
256
- emptyIcon={CoreIcons.chartActivityScatter}
251
+ isEmpty={isEmpty(series)}
252
+ emptyIcon="icon-[mdi--chart-scatter-plot]"
257
253
  dataTableRenderer={activityRenderer}
258
- onClick={(e) => onClick?.(e.seriesIndex, e.dataIndex)}
254
+ onClick={(e) => onClick?.(e.seriesIndex ?? 0, e.dataIndex)}
259
255
  >
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
- />
256
+ {showTypeSelector && (
257
+ <ToggleButtonGroup value={type} onChange={setType}>
258
+ <Button size="sm" variant="link" value="scatter" aria-label="scatter" icon="icon-[mdi--chart-scatter-plot]" />
259
+ <Button size="sm" variant="link" value="heatmap" aria-label="heatmap" icon="icon-[mdi--checkerboard]" />
260
+ </ToggleButtonGroup>
261
+ )}
262
+ {showTypeSelector && showThemeSelector && <Divider vertical />}
263
+ {showThemeSelector && <PaletteSelect theme={theme} onClick={setTheme} defaultTheme="activity" />}
282
264
  </ChartContainer>
283
265
  );
284
266
  },
@@ -286,10 +268,29 @@ const ActivityMapChart = memo(
286
268
  ActivityMapChart.displayName = "ActivityMapChart";
287
269
 
288
270
  /**
289
- * Chart showing activity map
271
+ * ActivityMap component renders a chart that visualizes activity data
272
+ * over time, using either a scatter plot or a heatmap.
273
+ * It allows users to switch between different types of visualizations
274
+ * and customize the color palette used in the chart.
275
+ *
276
+ * @example
277
+ * ```jsx
278
+ * <ActivityMap
279
+ * data={activityData}
280
+ * time="day-hour"
281
+ * type="heatmap"
282
+ * heatmapPalette={["#f0f8ff", "#faebd7", "#00ffff", "#7fffd4"]}
283
+ * highLabels={["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"]}
284
+ * lowLabels={["12a", "1a", "2a", "3a", "4a", "5a", "6a", "7a", "8a", "9a", "10a", "11a", "12p", "1p", "2p", "3p", "4p", "5p", "6p", "7p", "8p", "9p", "10p", "11p"]}
285
+ * onClick={(highIndex, lowIndex) => console.log(`Clicked on: ${highIndex}, ${lowIndex}`)}
286
+ * />
287
+ * ```
288
+ * @see {@link https://echarts.apache.org/en/option.html#series-scatter} for more details on the ECharts scatter series.
290
289
  */
291
- export const ActivityMap: FC<ActivityMapProps> = (props) => (
292
- <ChartWrapper>
293
- <ActivityMapChart {...props} />
294
- </ChartWrapper>
295
- );
290
+ export function ActivityMap(props: ActivityMapProps) {
291
+ return (
292
+ <ChartWrapper>
293
+ <ActivityMapChart {...props} />
294
+ </ChartWrapper>
295
+ );
296
+ }
@@ -21,15 +21,9 @@
21
21
  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22
22
  */
23
23
 
24
- import {
25
- Button,
26
- CoreIcons,
27
- Divider,
28
- ToggleButtonGroup,
29
- useMemoDebugger,
30
- } from "@react-fabric/core";
24
+ import { Button, Divider, ToggleButtonGroup, useMemoDebugger } from "@react-fabric/core";
31
25
  import { compareValues, Format, isEmpty } from "@react-fabric/utilities";
32
- import { type EChartOption, type EChartsType } from "echarts";
26
+ import { type EChartsOption, type EChartsType } from "echarts";
33
27
  import { memo, useEffect, useRef, useState, type FC } from "react";
34
28
  import { type BaseChart, type CountType } from "../types";
35
29
  import { countRenderer } from "../types/utils";
@@ -38,70 +32,77 @@ import { ChartWrapper } from "../wrapper/ChartWrapper";
38
32
  import { PaletteSelect } from "../wrapper/PaletteSelect";
39
33
 
40
34
  export interface CountSeriesProps extends BaseChart, CountType {
35
+ seriesName?: string;
41
36
  type?: "pie" | "bar" | "column";
42
37
  onClick?: (key: string) => void;
43
38
  }
44
39
 
45
40
  const CountSeriesChart: FC<CountSeriesProps> = memo(
46
41
  ({
47
- data,
42
+ series: _series,
48
43
  title,
44
+ seriesName,
49
45
  onExport,
50
46
  theme: chartTheme,
51
47
  type: chartType = "pie",
48
+ options: optionOverride,
49
+ showThemeSelector,
50
+ showTypeSelector,
52
51
  onClick,
53
52
  }: CountSeriesProps) => {
54
53
  const chartRef = useRef<EChartsType>(null);
55
- const [type, setType] = useState(chartType);
56
- const [theme, setTheme] = useState(chartTheme);
54
+ const [type, setType] = useState(() => chartType);
55
+ const [theme, setTheme] = useState(() => chartTheme);
57
56
 
58
57
  useEffect(() => {
58
+ // eslint-disable-next-line @eslint-react/set-state-in-effect
59
59
  setType(chartType);
60
60
  }, [chartType]);
61
61
  useEffect(() => {
62
+ // eslint-disable-next-line @eslint-react/set-state-in-effect
62
63
  setTheme(chartTheme);
63
64
  }, [chartTheme]);
64
65
 
65
- const options = useMemoDebugger<EChartOption>(
66
+ const options = useMemoDebugger<EChartsOption>(
66
67
  () => {
67
- if (isEmpty(data)) {
68
+ if (isEmpty(_series)) {
68
69
  chartRef.current?.clear();
69
70
  return {};
70
71
  }
71
72
 
72
- const sorted = data?.sort(compareValues("desc", "count"));
73
+ const sorted = _series?.sort(compareValues("desc", "count"));
73
74
 
74
- const categoryAxis: AnyObject = {
75
+ const categoryAxis = Object.assign({}, optionOverride?.xAxis, {
75
76
  type: "category",
76
- name: title ?? "Count Series",
77
+ name: seriesName ?? "Count Series",
77
78
  nameLocation: "center",
78
79
  axisTick: {
79
80
  show: false,
80
81
  },
81
82
  data: [],
82
- } as EChartOption.XAxis;
83
- const valueAxis: AnyObject = {
83
+ } as EChartsOption["xAxis"]);
84
+ const valueAxis = Object.assign({}, optionOverride?.yAxis, {
84
85
  type: "value",
85
86
  axisLabel: {
86
- formatter: (val: AnyObject) => Format.number(val),
87
+ formatter: (val) => Format.number(val),
87
88
  },
88
- } as EChartOption.YAxis;
89
+ } as EChartsOption["yAxis"]);
89
90
 
90
91
  const series =
91
92
  type === "pie"
92
93
  ? [
93
94
  {
94
95
  type: "pie",
95
- name: title ?? "Count Series",
96
- data: sorted.map((item, index) => ({
96
+ name: seriesName ?? "Count Series",
97
+ data: sorted.map((item) => ({
97
98
  id: item.id,
98
99
  value: item.count,
99
- name: item.label ?? item.id,
100
+ name: item.name ?? item.id,
100
101
  })),
101
102
  universalTransition: true,
102
103
  },
103
104
  ]
104
- : sorted.map((item, index) => ({
105
+ : sorted.map((item) => ({
105
106
  id: item.id,
106
107
  type: "bar",
107
108
  label: {
@@ -113,23 +114,22 @@ const CountSeriesChart: FC<CountSeriesProps> = memo(
113
114
  },
114
115
  data: [
115
116
  {
116
- name: title ?? "Count Series",
117
+ name: seriesName ?? "Count Series",
117
118
  value: item.count,
118
119
  },
119
120
  ],
120
- name: item.label ?? item.id,
121
+ name: item.name ?? item.id,
121
122
  universalTransition: true,
122
123
  }));
123
124
  return {
125
+ ...optionOverride,
124
126
  series,
125
127
  countSeries: true,
126
- xAxis:
127
- type === "pie" ? null : type === "bar" ? valueAxis : categoryAxis,
128
- yAxis:
129
- type === "pie" ? null : type === "bar" ? categoryAxis : valueAxis,
128
+ xAxis: type === "pie" ? null : type === "bar" ? valueAxis : categoryAxis,
129
+ yAxis: type === "pie" ? null : type === "bar" ? categoryAxis : valueAxis,
130
130
  };
131
131
  },
132
- [data, title, type],
132
+ [_series, seriesName, type, optionOverride],
133
133
  "CountChart options",
134
134
  );
135
135
 
@@ -140,36 +140,20 @@ const CountSeriesChart: FC<CountSeriesProps> = memo(
140
140
  theme={theme}
141
141
  options={options}
142
142
  chartRef={chartRef}
143
- isEmpty={isEmpty(data)}
144
- emptyIcon={CoreIcons.chartPie}
143
+ isEmpty={isEmpty(_series)}
144
+ emptyIcon="icon-[mdi--chart-pie]"
145
145
  dataTableRenderer={countRenderer}
146
- onClick={(e) => onClick?.(e.data.id ?? e.seriesId)}
146
+ onClick={(e) => onClick?.((e?.data as KeyValue<string>)?.id ?? e.seriesId ?? "")}
147
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} />
148
+ {showTypeSelector && (
149
+ <ToggleButtonGroup value={type} onChange={setType}>
150
+ <Button size="sm" variant="link" value="pie" aria-label="pie" icon="icon-[mdi--chart-pie]" />
151
+ <Button size="sm" variant="link" value="column" aria-label="column" icon="icon-[mdi--chart-bar]" />
152
+ <Button size="sm" variant="link" value="bar" aria-label="bar" icon="icon-[mdi--chart-bar] rotate-90 -scale-x-100" />
153
+ </ToggleButtonGroup>
154
+ )}
155
+ {showTypeSelector && showThemeSelector && <Divider vertical />}
156
+ {showThemeSelector && <PaletteSelect theme={theme} onClick={setTheme} />}
173
157
  </ChartContainer>
174
158
  );
175
159
  },
@@ -177,10 +161,34 @@ const CountSeriesChart: FC<CountSeriesProps> = memo(
177
161
  CountSeriesChart.displayName = "CountSeriesChart";
178
162
 
179
163
  /**
180
- * Chart to display simple data count series
164
+ * CountSeries component renders a chart that displays counts of items in various formats.
165
+ * It supports pie, bar, and column charts.
166
+ * It can be used to visualize the distribution of counts across different categories.
167
+ * It allows users to switch between different types of visualizations and customize the color palette used in the chart.
168
+ *
169
+ * @example
170
+ * ```jsx
171
+ * <CountSeries
172
+ * data={[
173
+ * { id: "A", label: "Category A", count: 10 },
174
+ * { id: "B", label: "Category B", count: 20 },
175
+ * { id: "C", label: "Category C", count: 30 },
176
+ * ]}
177
+ * title="Count Distribution"
178
+ * onExport={() => console.log("Exporting chart data")}
179
+ * theme="light"
180
+ * type="pie"
181
+ * onClick={(key) => console.log("Clicked on:", key)}
182
+ * />
183
+ * ```
184
+ *
185
+ * @see {@link https://echarts.apache.org/en/option.html#series-pie} for more details on the ECharts pie series options.
186
+ * @see {@link https://echarts.apache.org/en/option.html#series-bar} for more details on the ECharts bar series options.
181
187
  */
182
- export const CountSeries: FC<CountSeriesProps> = (props) => (
183
- <ChartWrapper>
184
- <CountSeriesChart {...props} />
185
- </ChartWrapper>
186
- );
188
+ export function CountSeries(props: CountSeriesProps) {
189
+ return (
190
+ <ChartWrapper>
191
+ <CountSeriesChart {...props} />
192
+ </ChartWrapper>
193
+ );
194
+ }