@fanciers/echarts-react 0.0.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.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025-present, Tmk
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,3 @@
1
+ # @fanciers/echarts-react
2
+
3
+ > Using ECharts in React the Idiomatic Way, with Treeshaking Support! 🍃
@@ -0,0 +1,50 @@
1
+ import React from "react";
2
+ import { LineSeriesOption, PieSeriesOption } from "echarts/charts";
3
+ import { GridComponentOption } from "echarts/components";
4
+ import * as echarts from "echarts/core";
5
+ import { ComposeOption } from "echarts/core";
6
+ import { ComponentOption } from "echarts/types/src/util/types.js";
7
+ import { ComposeOption as ComposeOption$1, DataZoomComponentOption, DatasetComponentOption, GraphicComponentOption, LegendComponentOption, MarkAreaComponentOption, MarkLineComponentOption, MarkPointComponentOption, MatrixComponentOption, PolarComponentOption, TitleComponentOption, ToolboxComponentOption, TooltipComponentOption, VisualMapComponentOption } from "echarts";
8
+
9
+ //#region src/shared.d.ts
10
+
11
+ type EChartExt = Parameters<(typeof echarts)['use']>[0];
12
+ type Simplify<T> = { [K in keyof T]: T[K] } & {};
13
+ //#endregion
14
+ //#region src/charts.d.ts
15
+ interface ChartBaseProps<T extends readonly ChartComponentType<any>[]> {
16
+ className?: string;
17
+ style?: React.CSSProperties;
18
+ containerProps?: React.HTMLAttributes<HTMLDivElement>;
19
+ compose?: [...T];
20
+ children?: React.ReactNode;
21
+ }
22
+ type InferChartComponentOption<T extends readonly ChartComponentType<any>[], U = T[number]> = U extends ChartComponentType<infer P> ? P : never;
23
+ interface ChartComponentType<T extends ComponentOption> {
24
+ <U extends readonly ChartComponentType<any>[] = []>(props: ChartBaseProps<U> & echarts.EChartsCoreOption & Simplify<ComposeOption<T | InferChartComponentOption<U>>> & {
25
+ ref?: React.Ref<echarts.ECharts>;
26
+ }): React.JSX.Element;
27
+ ext: EChartExt;
28
+ }
29
+ declare const LineChart: ChartComponentType<LineSeriesOption | GridComponentOption>;
30
+ declare const PieChart: ChartComponentType<PieSeriesOption>;
31
+ //#endregion
32
+ //#region src/components.d.ts
33
+ declare const Dataset: (props: ComposeOption$1<DatasetComponentOption>) => null;
34
+ declare const DataZoom: (props: ComposeOption$1<DataZoomComponentOption>) => null;
35
+ declare const Graphic: (props: ComposeOption$1<GraphicComponentOption>) => null;
36
+ declare const Legend: (props: ComposeOption$1<LegendComponentOption>) => null;
37
+ declare const MarkArea: (props: ComposeOption$1<MarkAreaComponentOption>) => null;
38
+ declare const MarkLine: (props: ComposeOption$1<MarkLineComponentOption>) => null;
39
+ declare const MarkPoint: (props: ComposeOption$1<MarkPointComponentOption>) => null;
40
+ declare const Matrix: (props: ComposeOption$1<MatrixComponentOption>) => null;
41
+ declare const Polar: (props: ComposeOption$1<PolarComponentOption>) => null;
42
+ declare const Title: (props: ComposeOption$1<TitleComponentOption>) => null;
43
+ declare const Toolbox: (props: ComposeOption$1<ToolboxComponentOption>) => null;
44
+ declare const Tooltip: (props: ComposeOption$1<TooltipComponentOption>) => null;
45
+ declare const VisualMap: (props: ComposeOption$1<VisualMapComponentOption>) => null;
46
+ //#endregion
47
+ //#region src/features.d.ts
48
+ declare function AxisBreak(): null;
49
+ //#endregion
50
+ export { AxisBreak, DataZoom, Dataset, Graphic, Legend, LineChart, MarkArea, MarkLine, MarkPoint, Matrix, PieChart, Polar, Title, Toolbox, Tooltip, VisualMap, echarts };
package/dist/index.js ADDED
@@ -0,0 +1,115 @@
1
+ import React, { forwardRef, useLayoutEffect, useRef } from "react";
2
+ import { LineChart as LineChart$1, PieChart as PieChart$1 } from "echarts/charts";
3
+ import { DataZoomComponent, DatasetComponent, GraphicComponent, GridComponent, LegendComponent, MarkAreaComponent, MarkLineComponent, MarkPointComponent, MatrixComponent, PolarComponent, TitleComponent, ToolboxComponent, TooltipComponent, TransformComponent, VisualMapComponent } from "echarts/components";
4
+ import * as echarts from "echarts/core";
5
+ import { jsx, jsxs } from "react/jsx-runtime";
6
+ import { AxisBreak as AxisBreak$1 } from "echarts/features";
7
+
8
+ //#region src/shared.tsx
9
+ const defaultSetOptionOpt = {
10
+ notMerge: false,
11
+ lazyUpdate: true
12
+ };
13
+ const ChartContext = React.createContext(null);
14
+ const useInitialChartContext = () => {
15
+ const [chartContextValue] = React.useState(() => ({ options: [] }));
16
+ return chartContextValue;
17
+ };
18
+ const useChartContext = () => {
19
+ const ctx = React.useContext(ChartContext);
20
+ if (process.env.NODE_ENV === "development" && !ctx) throw new Error("The component should render within ChartContext");
21
+ return ctx;
22
+ };
23
+ function useRegister(fn) {
24
+ const isExecuted = React.useRef(false);
25
+ if (isExecuted.current) return;
26
+ isExecuted.current = true;
27
+ fn(echarts);
28
+ }
29
+
30
+ //#endregion
31
+ //#region src/charts.tsx
32
+ function assignForwardedRef(ref, value) {
33
+ if (!ref) return;
34
+ if (typeof ref === "function") ref(value);
35
+ else ref.current = value;
36
+ }
37
+ function defineChart(ext) {
38
+ const ChartComponent = forwardRef(({ className, style, containerProps, compose, children,...props }, ref) => {
39
+ const containerRef = useRef(null);
40
+ const chartRef = useRef(null);
41
+ const ctx = useInitialChartContext();
42
+ useRegister((echarts$1) => {
43
+ echarts$1.use([ext, compose?.map((comp) => comp.ext).flat() || []].flat());
44
+ });
45
+ useLayoutEffect(() => {
46
+ const chart = chartRef.current = echarts.init(containerRef.current, null);
47
+ assignForwardedRef(ref, chart);
48
+ return () => {
49
+ chart.dispose();
50
+ assignForwardedRef(ref, null);
51
+ };
52
+ }, []);
53
+ useLayoutEffect(() => {
54
+ const chart = chartRef.current;
55
+ if (!chart) return;
56
+ for (const opt of ctx.options) chart.setOption(opt, defaultSetOptionOpt);
57
+ chart.setOption(props, defaultSetOptionOpt);
58
+ chart.setOption(chart.getOption(), { notMerge: true });
59
+ ctx.options.length = 0;
60
+ });
61
+ return /* @__PURE__ */ jsxs(ChartContext.Provider, {
62
+ value: ctx,
63
+ children: [/* @__PURE__ */ jsx("div", {
64
+ className,
65
+ style,
66
+ ...containerProps,
67
+ ref: containerRef
68
+ }), children]
69
+ });
70
+ });
71
+ ChartComponent.ext = ext;
72
+ return ChartComponent;
73
+ }
74
+ const LineChart = /* @__PURE__ */ defineChart([LineChart$1, GridComponent]);
75
+ const PieChart = /* @__PURE__ */ defineChart([PieChart$1]);
76
+
77
+ //#endregion
78
+ //#region src/components.tsx
79
+ function defineComponent(ext) {
80
+ return function ChartComponent(props) {
81
+ const ctx = useChartContext();
82
+ useRegister((echarts$1) => {
83
+ echarts$1.use(ext);
84
+ });
85
+ React.useLayoutEffect(() => {
86
+ ctx.options.push(props);
87
+ });
88
+ return null;
89
+ };
90
+ }
91
+ const Dataset = /* @__PURE__ */ defineComponent([DatasetComponent, TransformComponent]);
92
+ const DataZoom = /* @__PURE__ */ defineComponent([DataZoomComponent]);
93
+ const Graphic = /* @__PURE__ */ defineComponent([GraphicComponent]);
94
+ const Legend = /* @__PURE__ */ defineComponent(LegendComponent);
95
+ const MarkArea = /* @__PURE__ */ defineComponent(MarkAreaComponent);
96
+ const MarkLine = /* @__PURE__ */ defineComponent(MarkLineComponent);
97
+ const MarkPoint = /* @__PURE__ */ defineComponent(MarkPointComponent);
98
+ const Matrix = /* @__PURE__ */ defineComponent(MatrixComponent);
99
+ const Polar = /* @__PURE__ */ defineComponent(PolarComponent);
100
+ const Title = /* @__PURE__ */ defineComponent(TitleComponent);
101
+ const Toolbox = /* @__PURE__ */ defineComponent(ToolboxComponent);
102
+ const Tooltip = /* @__PURE__ */ defineComponent(TooltipComponent);
103
+ const VisualMap = /* @__PURE__ */ defineComponent(VisualMapComponent);
104
+
105
+ //#endregion
106
+ //#region src/features.tsx
107
+ function AxisBreak() {
108
+ useRegister((echarts$1) => {
109
+ echarts$1.use(AxisBreak$1);
110
+ });
111
+ return null;
112
+ }
113
+
114
+ //#endregion
115
+ export { AxisBreak, DataZoom, Dataset, Graphic, Legend, LineChart, MarkArea, MarkLine, MarkPoint, Matrix, PieChart, Polar, Title, Toolbox, Tooltip, VisualMap, echarts };
package/package.json ADDED
@@ -0,0 +1,52 @@
1
+ {
2
+ "name": "@fanciers/echarts-react",
3
+ "version": "0.0.0",
4
+ "description": "ECharts <3 React",
5
+ "author": "Tmk <i@tmk.im>",
6
+ "keywords": [
7
+ "echarts",
8
+ "react",
9
+ "component",
10
+ "chart",
11
+ "visualization"
12
+ ],
13
+ "license": "MIT",
14
+ "type": "module",
15
+ "sideEffects": false,
16
+ "files": [
17
+ "dist"
18
+ ],
19
+ "exports": {
20
+ ".": {
21
+ "types": "./dist/index.d.ts",
22
+ "default": "./dist/index.js"
23
+ }
24
+ },
25
+ "typings": "./dist/index.d.ts",
26
+ "devDependencies": {
27
+ "@rsbuild/core": "^1.4.15",
28
+ "@rsbuild/plugin-react": "^1.3.5",
29
+ "@storybook/addon-docs": "^9.1.2",
30
+ "@storybook/react": "^9.1.2",
31
+ "@types/node": "^24.3.0",
32
+ "@types/react": "^19.1.10",
33
+ "@types/react-dom": "^19.1.7",
34
+ "echarts": "^6.0.0",
35
+ "react": "^19.1.1",
36
+ "react-dom": "^19.1.1",
37
+ "storybook": "^9.1.2",
38
+ "storybook-react-rsbuild": "^2.0.4",
39
+ "swr": "^2.3.6",
40
+ "tsdown": "^0.14.1",
41
+ "typescript": "^5.9.2"
42
+ },
43
+ "peerDependencies": {
44
+ "echarts": "^6.0.0",
45
+ "react": ">=18.0.0",
46
+ "react-dom": ">=18.0.0"
47
+ },
48
+ "scripts": {
49
+ "dev": "storybook dev",
50
+ "lint": "tsc --noEmit"
51
+ }
52
+ }