react-use-echarts 0.0.9 → 0.0.11
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/README.md +23 -13
- package/dist/index.es.js +49 -46
- package/dist/index.umd.js +1 -1
- package/package.json +23 -23
package/README.md
CHANGED
|
@@ -16,7 +16,8 @@ A powerful React hooks library for Apache ECharts, making it easy to use ECharts
|
|
|
16
16
|
- 📦 **Lightweight** - Zero dependencies except for React and ECharts
|
|
17
17
|
- 🛠 **Flexible** - Full access to ECharts instance and options
|
|
18
18
|
- ⚡ **Auto-updating** - Automatically updates chart when data or options change
|
|
19
|
-
- 📱 **Responsive** - Handles container resizing automatically
|
|
19
|
+
- 📱 **Responsive** - Handles container resizing automatically with ResizeObserver
|
|
20
|
+
- 🎯 **Event handling** - Easy to use event system with flexible configuration
|
|
20
21
|
|
|
21
22
|
## 📦 Installation
|
|
22
23
|
|
|
@@ -34,10 +35,15 @@ pnpm add react-use-echarts echarts
|
|
|
34
35
|
## 🔨 Usage
|
|
35
36
|
|
|
36
37
|
```tsx
|
|
37
|
-
import
|
|
38
|
+
import React from 'react';
|
|
39
|
+
import { useEcharts } from 'react-use-echarts';
|
|
40
|
+
import type { EChartsOption } from 'echarts';
|
|
38
41
|
|
|
39
42
|
function MyChart() {
|
|
40
|
-
const options:
|
|
43
|
+
const options: EChartsOption = {
|
|
44
|
+
title: {
|
|
45
|
+
text: 'Basic Line Chart Example'
|
|
46
|
+
},
|
|
41
47
|
xAxis: {
|
|
42
48
|
type: 'category',
|
|
43
49
|
data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
|
|
@@ -47,7 +53,8 @@ function MyChart() {
|
|
|
47
53
|
},
|
|
48
54
|
series: [{
|
|
49
55
|
data: [820, 932, 901, 934, 1290, 1330, 1320],
|
|
50
|
-
type: 'line'
|
|
56
|
+
type: 'line',
|
|
57
|
+
smooth: true
|
|
51
58
|
}]
|
|
52
59
|
};
|
|
53
60
|
|
|
@@ -55,7 +62,7 @@ function MyChart() {
|
|
|
55
62
|
option: options
|
|
56
63
|
});
|
|
57
64
|
|
|
58
|
-
return <div ref={chartRef} style={{ width: '100%', height: '
|
|
65
|
+
return <div ref={chartRef} style={{ width: '100%', height: '400px' }} />;
|
|
59
66
|
}
|
|
60
67
|
```
|
|
61
68
|
|
|
@@ -66,14 +73,17 @@ function MyChart() {
|
|
|
66
73
|
The main hook for using ECharts in React components.
|
|
67
74
|
|
|
68
75
|
```tsx
|
|
76
|
+
import type { EChartsOption } from 'echarts';
|
|
77
|
+
import type { UseEchartsOptions } from 'react-use-echarts';
|
|
78
|
+
|
|
69
79
|
const { chartRef, setOption, getInstance } = useEcharts({
|
|
70
|
-
option:
|
|
71
|
-
theme?: string | object;
|
|
72
|
-
notMerge?: boolean;
|
|
73
|
-
lazyUpdate?: boolean;
|
|
74
|
-
showLoading?: boolean;
|
|
75
|
-
loadingOption?: object;
|
|
76
|
-
onEvents?: {
|
|
80
|
+
option: EChartsOption; // ECharts options configuration (required)
|
|
81
|
+
theme?: string | object; // ECharts theme name or configuration
|
|
82
|
+
notMerge?: boolean; // Whether to not merge with previous options
|
|
83
|
+
lazyUpdate?: boolean; // Whether to update chart lazily
|
|
84
|
+
showLoading?: boolean; // Whether to display loading animation
|
|
85
|
+
loadingOption?: object; // Loading animation configuration
|
|
86
|
+
onEvents?: { // Event handlers
|
|
77
87
|
[eventName: string]: {
|
|
78
88
|
handler: (params: any) => void;
|
|
79
89
|
query?: string | object;
|
|
@@ -99,4 +109,4 @@ Detailed changes for each release are documented in the [release notes](https://
|
|
|
99
109
|
|
|
100
110
|
## 📄 License
|
|
101
111
|
|
|
102
|
-
[MIT](./LICENSE.txt) © [
|
|
112
|
+
[MIT](./LICENSE.txt) © [Ethan](https://github.com/chensid)
|
package/dist/index.es.js
CHANGED
|
@@ -1,45 +1,40 @@
|
|
|
1
|
-
import { useRef as
|
|
2
|
-
import * as
|
|
3
|
-
const
|
|
1
|
+
import { useRef as v, useCallback as a, useEffect as u } from "react";
|
|
2
|
+
import * as k from "echarts";
|
|
3
|
+
const M = ({
|
|
4
4
|
/** Chart configuration */
|
|
5
|
-
option:
|
|
5
|
+
option: f,
|
|
6
6
|
/** Theme name */
|
|
7
7
|
theme: i,
|
|
8
8
|
/** Skip merging with previous options */
|
|
9
|
-
notMerge:
|
|
9
|
+
notMerge: l = !1,
|
|
10
10
|
/** Enable lazy update mode */
|
|
11
|
-
lazyUpdate:
|
|
11
|
+
lazyUpdate: b = !1,
|
|
12
12
|
/** Display loading animation */
|
|
13
13
|
showLoading: p = !1,
|
|
14
14
|
/** Loading animation config */
|
|
15
15
|
loadingOption: O,
|
|
16
16
|
/** Event handlers map */
|
|
17
|
-
onEvents:
|
|
17
|
+
onEvents: s
|
|
18
18
|
}) => {
|
|
19
|
-
const
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
i,
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
O,
|
|
34
|
-
c
|
|
35
|
-
]), o = f(() => {
|
|
36
|
-
if (s.current && !n.current) {
|
|
37
|
-
const e = R.init(s.current, i);
|
|
19
|
+
const c = v(null), n = v(void 0), C = () => n.current, d = a(
|
|
20
|
+
() => ({
|
|
21
|
+
option: f,
|
|
22
|
+
theme: i,
|
|
23
|
+
notMerge: l,
|
|
24
|
+
lazyUpdate: b,
|
|
25
|
+
showLoading: p,
|
|
26
|
+
loadingOption: O,
|
|
27
|
+
onEvents: s
|
|
28
|
+
}),
|
|
29
|
+
[f, i, l, b, p, O, s]
|
|
30
|
+
), o = a(() => {
|
|
31
|
+
if (c.current && !n.current) {
|
|
32
|
+
const e = k.init(c.current, i);
|
|
38
33
|
n.current = e;
|
|
39
|
-
const r =
|
|
34
|
+
const r = d();
|
|
40
35
|
return r.onEvents && Object.entries(r.onEvents).forEach(
|
|
41
|
-
([t, { handler:
|
|
42
|
-
g ? e.on(t, g,
|
|
36
|
+
([t, { handler: h, query: g, context: z }]) => {
|
|
37
|
+
g ? e.on(t, g, h, z) : e.on(t, h, z);
|
|
43
38
|
}
|
|
44
39
|
), r.showLoading ? e.showLoading(r.loadingOption) : e.hideLoading(), e.setOption(r.option, {
|
|
45
40
|
notMerge: r.notMerge,
|
|
@@ -47,7 +42,7 @@ const w = ({
|
|
|
47
42
|
}), e;
|
|
48
43
|
}
|
|
49
44
|
return n.current;
|
|
50
|
-
}, [
|
|
45
|
+
}, [d, i]), R = a(
|
|
51
46
|
(e, r) => {
|
|
52
47
|
queueMicrotask(() => {
|
|
53
48
|
const t = n.current || o();
|
|
@@ -56,25 +51,33 @@ const w = ({
|
|
|
56
51
|
},
|
|
57
52
|
[o]
|
|
58
53
|
);
|
|
59
|
-
return
|
|
54
|
+
return u(() => {
|
|
60
55
|
const e = n.current;
|
|
61
|
-
if (!e) return;
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
56
|
+
if (!e || !c.current) return;
|
|
57
|
+
let r;
|
|
58
|
+
try {
|
|
59
|
+
r = new ResizeObserver(() => {
|
|
60
|
+
e.resize();
|
|
61
|
+
}), r.observe(c.current);
|
|
62
|
+
} catch (t) {
|
|
63
|
+
console.warn("ResizeObserver not available:", t);
|
|
64
|
+
}
|
|
65
|
+
return () => {
|
|
66
|
+
r?.disconnect();
|
|
69
67
|
};
|
|
70
|
-
}, [
|
|
71
|
-
|
|
72
|
-
}, [
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
68
|
+
}, []), u(() => {
|
|
69
|
+
c.current && queueMicrotask(o);
|
|
70
|
+
}, [c, o]), u(() => () => {
|
|
71
|
+
const e = n.current;
|
|
72
|
+
e && (s && Object.entries(s).forEach(([r, { handler: t }]) => {
|
|
73
|
+
e.off(r, t);
|
|
74
|
+
}), e.dispose(), n.current = void 0);
|
|
75
|
+
}, [s]), {
|
|
76
|
+
chartRef: c,
|
|
77
|
+
setOption: R,
|
|
78
|
+
getInstance: C
|
|
76
79
|
};
|
|
77
80
|
};
|
|
78
81
|
export {
|
|
79
|
-
|
|
82
|
+
M as useEcharts
|
|
80
83
|
};
|
package/dist/index.umd.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
(function(
|
|
1
|
+
(function(s,n){typeof exports=="object"&&typeof module<"u"?n(exports,require("react"),require("echarts")):typeof define=="function"&&define.amd?define(["exports","react","echarts"],n):(s=typeof globalThis<"u"?globalThis:s||self,n(s["react-use-echarts"]={},s.React,s.echarts))})(this,function(s,n,y){"use strict";function k(c){const i=Object.create(null,{[Symbol.toStringTag]:{value:"Module"}});if(c){for(const u in c)if(u!=="default"){const a=Object.getOwnPropertyDescriptor(c,u);Object.defineProperty(i,u,a.get?a:{enumerable:!0,get:()=>c[u]})}}return i.default=c,Object.freeze(i)}const v=k(y),z=({option:c,theme:i,notMerge:u=!1,lazyUpdate:a=!1,showLoading:p=!1,loadingOption:b,onEvents:l})=>{const f=n.useRef(null),o=n.useRef(void 0),C=()=>o.current,h=n.useCallback(()=>({option:c,theme:i,notMerge:u,lazyUpdate:a,showLoading:p,loadingOption:b,onEvents:l}),[c,i,u,a,p,b,l]),d=n.useCallback(()=>{if(f.current&&!o.current){const e=v.init(f.current,i);o.current=e;const t=h();return t.onEvents&&Object.entries(t.onEvents).forEach(([r,{handler:O,query:g,context:j}])=>{g?e.on(r,g,O,j):e.on(r,O,j)}),t.showLoading?e.showLoading(t.loadingOption):e.hideLoading(),e.setOption(t.option,{notMerge:t.notMerge,lazyUpdate:t.lazyUpdate}),e}return o.current},[h,i]),R=n.useCallback((e,t)=>{queueMicrotask(()=>{const r=o.current||d();r&&r.setOption(e,t)})},[d]);return n.useEffect(()=>{const e=o.current;if(!e||!f.current)return;let t;try{t=new ResizeObserver(()=>{e.resize()}),t.observe(f.current)}catch(r){console.warn("ResizeObserver not available:",r)}return()=>{t?.disconnect()}},[]),n.useEffect(()=>{f.current&&queueMicrotask(d)},[f,d]),n.useEffect(()=>()=>{const e=o.current;e&&(l&&Object.entries(l).forEach(([t,{handler:r}])=>{e.off(t,r)}),e.dispose(),o.current=void 0)},[l]),{chartRef:f,setOption:R,getInstance:C}};s.useEcharts=z,Object.defineProperty(s,Symbol.toStringTag,{value:"Module"})});
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "react-use-echarts",
|
|
3
3
|
"private": false,
|
|
4
|
-
"version": "0.0.
|
|
4
|
+
"version": "0.0.11",
|
|
5
5
|
"description": "A powerful React hooks library for Apache ECharts",
|
|
6
6
|
"keywords": [
|
|
7
7
|
"react",
|
|
@@ -12,7 +12,7 @@
|
|
|
12
12
|
"typescript"
|
|
13
13
|
],
|
|
14
14
|
"type": "module",
|
|
15
|
-
"author": "
|
|
15
|
+
"author": "Ethan",
|
|
16
16
|
"license": "MIT",
|
|
17
17
|
"publishConfig": {
|
|
18
18
|
"access": "public",
|
|
@@ -41,29 +41,29 @@
|
|
|
41
41
|
"dist"
|
|
42
42
|
],
|
|
43
43
|
"devDependencies": {
|
|
44
|
-
"@eslint/js": "^9.
|
|
45
|
-
"@testing-library/react": "^16.
|
|
46
|
-
"@types/node": "^
|
|
47
|
-
"@types/react": "^19.
|
|
48
|
-
"@types/react-dom": "^19.
|
|
49
|
-
"@vitejs/plugin-react-swc": "^
|
|
50
|
-
"@vitest/coverage-v8": "3.
|
|
51
|
-
"echarts": "^
|
|
52
|
-
"eslint": "^9.
|
|
53
|
-
"eslint-plugin-react-hooks": "^
|
|
54
|
-
"eslint-plugin-react-refresh": "^0.4.
|
|
55
|
-
"globals": "^16.
|
|
56
|
-
"jsdom": "^
|
|
57
|
-
"react": "^19.
|
|
58
|
-
"react-dom": "^19.
|
|
59
|
-
"typescript": "~5.
|
|
60
|
-
"typescript-eslint": "^8.
|
|
61
|
-
"vite": "^
|
|
62
|
-
"vite-plugin-dts": "^4.5.
|
|
63
|
-
"vitest": "^3.
|
|
44
|
+
"@eslint/js": "^9.38.0",
|
|
45
|
+
"@testing-library/react": "^16.3.0",
|
|
46
|
+
"@types/node": "^24.9.1",
|
|
47
|
+
"@types/react": "^19.2.2",
|
|
48
|
+
"@types/react-dom": "^19.2.2",
|
|
49
|
+
"@vitejs/plugin-react-swc": "^4.1.0",
|
|
50
|
+
"@vitest/coverage-v8": "3.2.4",
|
|
51
|
+
"echarts": "^6.0.0",
|
|
52
|
+
"eslint": "^9.38.0",
|
|
53
|
+
"eslint-plugin-react-hooks": "^7.0.0",
|
|
54
|
+
"eslint-plugin-react-refresh": "^0.4.24",
|
|
55
|
+
"globals": "^16.4.0",
|
|
56
|
+
"jsdom": "^27.0.1",
|
|
57
|
+
"react": "^19.2.0",
|
|
58
|
+
"react-dom": "^19.2.0",
|
|
59
|
+
"typescript": "~5.9.3",
|
|
60
|
+
"typescript-eslint": "^8.46.2",
|
|
61
|
+
"vite": "^7.1.11",
|
|
62
|
+
"vite-plugin-dts": "^4.5.4",
|
|
63
|
+
"vitest": "^3.2.4"
|
|
64
64
|
},
|
|
65
65
|
"peerDependencies": {
|
|
66
|
-
"echarts": "^5.
|
|
66
|
+
"echarts": "^5.6.0 || ^6.0.0",
|
|
67
67
|
"react": "^18.3.1 || ^19.0.0",
|
|
68
68
|
"react-dom": "^18.3.1 || ^19.0.0"
|
|
69
69
|
},
|