react-use-echarts 1.0.1 → 1.0.3
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 +72 -176
- package/README.zh-CN.md +523 -0
- package/dist/index.es.js +194 -160
- package/dist/index.umd.js +1 -1
- package/package.json +33 -26
package/README.md
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
# react-use-echarts
|
|
2
2
|
|
|
3
|
+
> [中文](./README.zh-CN.md) | [English](./README.md)
|
|
4
|
+
|
|
3
5
|
[](https://www.npmjs.com/package/react-use-echarts)
|
|
4
6
|
[](https://www.npmjs.com/package/react-use-echarts)
|
|
5
7
|
[](https://github.com/chensid/react-use-echarts/actions/workflows/npm-publish.yml)
|
|
@@ -52,26 +54,12 @@ import type { EChartsOption } from 'echarts';
|
|
|
52
54
|
function MyChart() {
|
|
53
55
|
const chartRef = useRef<HTMLDivElement>(null);
|
|
54
56
|
|
|
55
|
-
const options: EChartsOption = {
|
|
56
|
-
title: {
|
|
57
|
-
text: 'Basic Line Chart Example'
|
|
58
|
-
},
|
|
59
|
-
xAxis: {
|
|
60
|
-
type: 'category',
|
|
61
|
-
data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
|
|
62
|
-
},
|
|
63
|
-
yAxis: {
|
|
64
|
-
type: 'value'
|
|
65
|
-
},
|
|
66
|
-
series: [{
|
|
67
|
-
data: [820, 932, 901, 934, 1290, 1330, 1320],
|
|
68
|
-
type: 'line',
|
|
69
|
-
smooth: true
|
|
70
|
-
}]
|
|
71
|
-
};
|
|
72
|
-
|
|
73
57
|
useEcharts(chartRef, {
|
|
74
|
-
option:
|
|
58
|
+
option: {
|
|
59
|
+
xAxis: { type: 'category', data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'] },
|
|
60
|
+
yAxis: { type: 'value' },
|
|
61
|
+
series: [{ data: [820, 932, 901, 934, 1290, 1330, 1320], type: 'line' }]
|
|
62
|
+
}
|
|
75
63
|
});
|
|
76
64
|
|
|
77
65
|
return <div ref={chartRef} style={{ width: '100%', height: '400px' }} />;
|
|
@@ -184,15 +172,16 @@ function DynamicChart() {
|
|
|
184
172
|
}
|
|
185
173
|
```
|
|
186
174
|
|
|
187
|
-
###
|
|
175
|
+
### Themes
|
|
188
176
|
|
|
189
|
-
|
|
177
|
+
Built-in themes: `light`, `dark`, `macarons`, or pass a custom theme object.
|
|
190
178
|
|
|
191
179
|
```tsx
|
|
192
|
-
import { useRef } from 'react';
|
|
180
|
+
import { useRef, useMemo } from 'react';
|
|
193
181
|
import { useEcharts } from 'react-use-echarts';
|
|
194
182
|
|
|
195
|
-
|
|
183
|
+
// Using built-in theme
|
|
184
|
+
function BuiltinThemeChart() {
|
|
196
185
|
const chartRef = useRef<HTMLDivElement>(null);
|
|
197
186
|
|
|
198
187
|
useEcharts(chartRef, {
|
|
@@ -206,21 +195,14 @@ function ThemedChart() {
|
|
|
206
195
|
|
|
207
196
|
return <div ref={chartRef} style={{ width: '100%', height: '400px' }} />;
|
|
208
197
|
}
|
|
209
|
-
```
|
|
210
|
-
|
|
211
|
-
### Custom Theme
|
|
212
|
-
|
|
213
|
-
```tsx
|
|
214
|
-
import { useRef } from 'react';
|
|
215
|
-
import { useEcharts } from 'react-use-echarts';
|
|
216
198
|
|
|
217
|
-
|
|
199
|
+
// Using custom theme (recommend using useMemo to avoid unnecessary re-renders)
|
|
200
|
+
function CustomThemeChart() {
|
|
218
201
|
const chartRef = useRef<HTMLDivElement>(null);
|
|
219
|
-
|
|
220
|
-
const customTheme = {
|
|
202
|
+
const customTheme = useMemo(() => ({
|
|
221
203
|
color: ['#fc8452', '#9a60b4', '#ea7ccc'],
|
|
222
204
|
backgroundColor: '#1e1e1e'
|
|
223
|
-
};
|
|
205
|
+
}), []);
|
|
224
206
|
|
|
225
207
|
useEcharts(chartRef, {
|
|
226
208
|
option: {
|
|
@@ -237,7 +219,7 @@ function CustomThemedChart() {
|
|
|
237
219
|
|
|
238
220
|
### Chart Linkage
|
|
239
221
|
|
|
240
|
-
Connect multiple charts to
|
|
222
|
+
Connect multiple charts using the `group` option to enable synchronized interactions (e.g., tooltip, highlight).
|
|
241
223
|
|
|
242
224
|
```tsx
|
|
243
225
|
import { useRef } from 'react';
|
|
@@ -246,7 +228,6 @@ import { useEcharts } from 'react-use-echarts';
|
|
|
246
228
|
function LinkedCharts() {
|
|
247
229
|
const chartRef1 = useRef<HTMLDivElement>(null);
|
|
248
230
|
const chartRef2 = useRef<HTMLDivElement>(null);
|
|
249
|
-
|
|
250
231
|
const xAxisData = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'];
|
|
251
232
|
|
|
252
233
|
useEcharts(chartRef1, {
|
|
@@ -280,12 +261,13 @@ function LinkedCharts() {
|
|
|
280
261
|
|
|
281
262
|
### Lazy Initialization
|
|
282
263
|
|
|
283
|
-
|
|
264
|
+
Initialize charts only when they enter the viewport. Suitable for pages with multiple charts. Default parameters: `rootMargin: '50px'`, `threshold: 0.1`.
|
|
284
265
|
|
|
285
266
|
```tsx
|
|
286
267
|
import { useRef } from 'react';
|
|
287
268
|
import { useEcharts } from 'react-use-echarts';
|
|
288
269
|
|
|
270
|
+
// Using default configuration
|
|
289
271
|
function LazyChart() {
|
|
290
272
|
const chartRef = useRef<HTMLDivElement>(null);
|
|
291
273
|
|
|
@@ -295,13 +277,13 @@ function LazyChart() {
|
|
|
295
277
|
yAxis: { type: 'value' },
|
|
296
278
|
series: [{ data: [120, 200, 150], type: 'bar' }]
|
|
297
279
|
},
|
|
298
|
-
lazyInit: true
|
|
280
|
+
lazyInit: true
|
|
299
281
|
});
|
|
300
282
|
|
|
301
283
|
return <div ref={chartRef} style={{ width: '100%', height: '400px' }} />;
|
|
302
284
|
}
|
|
303
285
|
|
|
304
|
-
//
|
|
286
|
+
// Custom IntersectionObserver configuration
|
|
305
287
|
function LazyChartWithOptions() {
|
|
306
288
|
const chartRef = useRef<HTMLDivElement>(null);
|
|
307
289
|
|
|
@@ -312,7 +294,7 @@ function LazyChartWithOptions() {
|
|
|
312
294
|
series: [{ data: [120, 200, 150], type: 'bar' }]
|
|
313
295
|
},
|
|
314
296
|
lazyInit: {
|
|
315
|
-
rootMargin: '100px',
|
|
297
|
+
rootMargin: '100px',
|
|
316
298
|
threshold: 0.1
|
|
317
299
|
}
|
|
318
300
|
});
|
|
@@ -323,7 +305,7 @@ function LazyChartWithOptions() {
|
|
|
323
305
|
|
|
324
306
|
### SVG Renderer
|
|
325
307
|
|
|
326
|
-
Use SVG renderer
|
|
308
|
+
Use SVG renderer for better accessibility and print quality.
|
|
327
309
|
|
|
328
310
|
```tsx
|
|
329
311
|
import { useRef } from 'react';
|
|
@@ -338,7 +320,7 @@ function SVGChart() {
|
|
|
338
320
|
yAxis: { type: 'value' },
|
|
339
321
|
series: [{ data: [120, 200, 150], type: 'bar' }]
|
|
340
322
|
},
|
|
341
|
-
renderer: 'svg'
|
|
323
|
+
renderer: 'svg' // Default is 'canvas'
|
|
342
324
|
});
|
|
343
325
|
|
|
344
326
|
return <div ref={chartRef} style={{ width: '100%', height: '400px' }} />;
|
|
@@ -347,6 +329,8 @@ function SVGChart() {
|
|
|
347
329
|
|
|
348
330
|
### Accessing ECharts Instance
|
|
349
331
|
|
|
332
|
+
Access the ECharts instance via `getInstance()` to perform advanced operations like exporting images.
|
|
333
|
+
|
|
350
334
|
```tsx
|
|
351
335
|
import { useRef } from 'react';
|
|
352
336
|
import { useEcharts } from 'react-use-echarts';
|
|
@@ -365,12 +349,7 @@ function ChartWithInstance() {
|
|
|
365
349
|
const exportImage = () => {
|
|
366
350
|
const instance = getInstance();
|
|
367
351
|
if (instance) {
|
|
368
|
-
const url = instance.getDataURL({
|
|
369
|
-
type: 'png',
|
|
370
|
-
pixelRatio: 2,
|
|
371
|
-
backgroundColor: '#fff'
|
|
372
|
-
});
|
|
373
|
-
// Download or use the image URL
|
|
352
|
+
const url = instance.getDataURL({ type: 'png', pixelRatio: 2, backgroundColor: '#fff' });
|
|
374
353
|
const link = document.createElement('a');
|
|
375
354
|
link.download = 'chart.png';
|
|
376
355
|
link.href = url;
|
|
@@ -389,6 +368,8 @@ function ChartWithInstance() {
|
|
|
389
368
|
|
|
390
369
|
### Manual Resize
|
|
391
370
|
|
|
371
|
+
Manually trigger chart resize (usually handled automatically by ResizeObserver).
|
|
372
|
+
|
|
392
373
|
```tsx
|
|
393
374
|
import { useRef } from 'react';
|
|
394
375
|
import { useEcharts } from 'react-use-echarts';
|
|
@@ -404,75 +385,73 @@ function ResizableChart() {
|
|
|
404
385
|
}
|
|
405
386
|
});
|
|
406
387
|
|
|
407
|
-
// Manually trigger resize after some container size change
|
|
408
|
-
const handleContainerResize = () => {
|
|
409
|
-
resize();
|
|
410
|
-
};
|
|
411
|
-
|
|
412
388
|
return (
|
|
413
389
|
<div>
|
|
414
|
-
<button onClick={
|
|
390
|
+
<button onClick={resize}>Trigger Resize</button>
|
|
415
391
|
<div ref={chartRef} style={{ width: '100%', height: '400px' }} />
|
|
416
392
|
</div>
|
|
417
393
|
);
|
|
418
394
|
}
|
|
419
395
|
```
|
|
420
396
|
|
|
397
|
+
### Utilities
|
|
398
|
+
|
|
399
|
+
Advanced scenarios can directly use exported utility functions:
|
|
400
|
+
|
|
401
|
+
```tsx
|
|
402
|
+
import {
|
|
403
|
+
getCachedInstance,
|
|
404
|
+
clearInstanceCache,
|
|
405
|
+
getGroupInstances,
|
|
406
|
+
updateGroup,
|
|
407
|
+
addToGroup,
|
|
408
|
+
removeFromGroup,
|
|
409
|
+
} from 'react-use-echarts';
|
|
410
|
+
```
|
|
411
|
+
|
|
412
|
+
- `getCachedInstance` / `clearInstanceCache`: Query or clear internal instance cache
|
|
413
|
+
- `getGroupInstances` / `addToGroup` / `removeFromGroup` / `updateGroup`: Manually manage ECharts group linkage
|
|
414
|
+
|
|
421
415
|
## 📖 API
|
|
422
416
|
|
|
423
417
|
### useEcharts
|
|
424
418
|
|
|
425
|
-
The main
|
|
419
|
+
The main Hook for using ECharts in React components.
|
|
426
420
|
|
|
427
421
|
#### Parameters
|
|
428
422
|
|
|
429
423
|
```tsx
|
|
430
|
-
import { useRef } from 'react';
|
|
431
|
-
import type { EChartsOption } from 'echarts';
|
|
432
|
-
import type { UseEchartsOptions } from 'react-use-echarts';
|
|
433
|
-
import { useEcharts } from 'react-use-echarts';
|
|
434
|
-
|
|
435
424
|
const chartRef = useRef<HTMLDivElement>(null);
|
|
436
425
|
|
|
437
|
-
const
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
renderer: 'canvas', // Optional: 'canvas' | 'svg' (default: 'canvas')
|
|
447
|
-
lazyInit: false, // Optional: boolean | IntersectionObserverInit
|
|
448
|
-
group: 'my-group', // Optional: Group ID for chart linkage
|
|
449
|
-
setOptionOpts: { notMerge: false }, // Optional: Default setOption options
|
|
450
|
-
showLoading: false, // Optional: Show loading state
|
|
451
|
-
loadingOption: { text: 'Loading…' }, // Optional: Loading options
|
|
426
|
+
const { setOption, getInstance, resize } = useEcharts(chartRef, {
|
|
427
|
+
option: { /* EChartsOption */ }, // Required
|
|
428
|
+
theme: 'dark', // 'light' | 'dark' | 'macarons' | custom object | null
|
|
429
|
+
renderer: 'canvas', // 'canvas' | 'svg', default 'canvas'
|
|
430
|
+
lazyInit: false, // boolean | IntersectionObserverInit
|
|
431
|
+
group: 'my-group', // Group ID for chart linkage
|
|
432
|
+
setOptionOpts: { notMerge: false }, // Default options for setOption
|
|
433
|
+
showLoading: false, // Whether to show loading state
|
|
434
|
+
loadingOption: { text: 'Loading…' }, // Loading configuration
|
|
452
435
|
onEvents: {
|
|
453
436
|
click: {
|
|
454
|
-
handler: (params) =>
|
|
455
|
-
|
|
456
|
-
},
|
|
457
|
-
query: 'series', // Optional: Event query
|
|
437
|
+
handler: (params) => console.log(params),
|
|
438
|
+
query: 'series', // Optional: event query condition
|
|
458
439
|
},
|
|
459
440
|
},
|
|
460
|
-
};
|
|
461
|
-
|
|
462
|
-
const { setOption, getInstance, resize } = useEcharts(chartRef, options);
|
|
441
|
+
});
|
|
463
442
|
```
|
|
464
443
|
|
|
465
444
|
#### Options
|
|
466
445
|
|
|
467
446
|
| Property | Type | Default | Description |
|
|
468
447
|
|----------|------|---------|-------------|
|
|
469
|
-
| `option` | `EChartsOption` | **
|
|
448
|
+
| `option` | `EChartsOption` | **Required** | ECharts configuration option |
|
|
470
449
|
| `theme` | `'light' \| 'dark' \| 'macarons' \| object \| null` | `null` | Theme name or custom theme object |
|
|
471
450
|
| `renderer` | `'canvas' \| 'svg'` | `'canvas'` | Renderer type |
|
|
472
|
-
| `lazyInit` | `boolean \| IntersectionObserverInit` | `false` | Lazy initialization
|
|
473
|
-
| `group` | `string` | - |
|
|
451
|
+
| `lazyInit` | `boolean \| IntersectionObserverInit` | `false` | Lazy initialization configuration |
|
|
452
|
+
| `group` | `string` | - | Chart linkage group ID |
|
|
474
453
|
| `setOptionOpts` | `SetOptionOpts` | - | Default options for setOption |
|
|
475
|
-
| `showLoading` | `boolean` | `false` |
|
|
454
|
+
| `showLoading` | `boolean` | `false` | Whether to show loading state |
|
|
476
455
|
| `loadingOption` | `object` | - | Loading configuration |
|
|
477
456
|
| `onEvents` | `EChartsEvents` | - | Event handlers |
|
|
478
457
|
|
|
@@ -486,8 +465,8 @@ const { setOption, getInstance, resize } = useEcharts(chartRef, options);
|
|
|
486
465
|
}
|
|
487
466
|
```
|
|
488
467
|
|
|
489
|
-
- **`setOption`**:
|
|
490
|
-
- **`getInstance`**: Get
|
|
468
|
+
- **`setOption`**: Dynamically update chart configuration
|
|
469
|
+
- **`getInstance`**: Get ECharts instance (returns `undefined` before initialization)
|
|
491
470
|
- **`resize`**: Manually trigger chart resize
|
|
492
471
|
|
|
493
472
|
### Theme Utilities
|
|
@@ -501,26 +480,16 @@ import {
|
|
|
501
480
|
registerBuiltinThemes,
|
|
502
481
|
} from 'react-use-echarts';
|
|
503
482
|
|
|
504
|
-
//
|
|
505
|
-
const themes = getAvailableThemes(); // ['light', 'dark', 'macarons']
|
|
506
|
-
|
|
507
|
-
// Check if a theme name is built-in
|
|
483
|
+
getAvailableThemes(); // ['light', 'dark', 'macarons']
|
|
508
484
|
isBuiltinTheme('dark'); // true
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
//
|
|
512
|
-
const darkTheme = getBuiltinTheme('dark');
|
|
513
|
-
|
|
514
|
-
// Register a custom theme globally
|
|
515
|
-
registerCustomTheme('my-theme', { color: ['#ff0000', '#00ff00'] });
|
|
516
|
-
|
|
517
|
-
// Register built-in themes to ECharts (auto-called on module load, rarely needed manually)
|
|
518
|
-
registerBuiltinThemes();
|
|
485
|
+
getBuiltinTheme('dark'); // Get built-in theme configuration
|
|
486
|
+
registerCustomTheme('my-theme', { color: ['#ff0000', '#00ff00'] }); // Register custom theme
|
|
487
|
+
registerBuiltinThemes(); // Register built-in themes (automatically called on module load, usually no need to call manually)
|
|
519
488
|
```
|
|
520
489
|
|
|
521
490
|
### useLazyInit
|
|
522
491
|
|
|
523
|
-
|
|
492
|
+
Standalone lazy initialization Hook based on IntersectionObserver.
|
|
524
493
|
|
|
525
494
|
```tsx
|
|
526
495
|
import { useRef } from 'react';
|
|
@@ -528,8 +497,6 @@ import { useLazyInit } from 'react-use-echarts';
|
|
|
528
497
|
|
|
529
498
|
function MyComponent() {
|
|
530
499
|
const elementRef = useRef<HTMLDivElement>(null);
|
|
531
|
-
|
|
532
|
-
// Returns true when element enters viewport
|
|
533
500
|
const isInView = useLazyInit(elementRef, {
|
|
534
501
|
rootMargin: '50px',
|
|
535
502
|
threshold: 0.1
|
|
@@ -547,77 +514,6 @@ function MyComponent() {
|
|
|
547
514
|
|
|
548
515
|
We welcome all contributions. Please read our [contributing guidelines](CONTRIBUTING.md) first. You can submit any ideas as [pull requests](https://github.com/chensid/react-use-echarts/pulls) or as [GitHub issues](https://github.com/chensid/react-use-echarts/issues).
|
|
549
516
|
|
|
550
|
-
## 🔄 Migration Guide
|
|
551
|
-
|
|
552
|
-
### From v0.0.11 to v1.0
|
|
553
|
-
|
|
554
|
-
#### Breaking Change: External Ref Management
|
|
555
|
-
|
|
556
|
-
The `useEcharts` hook no longer returns a `chartRef`. Instead, you now create and manage the ref externally:
|
|
557
|
-
|
|
558
|
-
**Before (v0.0.11):**
|
|
559
|
-
```tsx
|
|
560
|
-
import { useEcharts } from 'react-use-echarts';
|
|
561
|
-
|
|
562
|
-
function MyChart() {
|
|
563
|
-
const { chartRef, setOption, getInstance } = useEcharts({
|
|
564
|
-
option: { /* ... */ }
|
|
565
|
-
});
|
|
566
|
-
|
|
567
|
-
return <div ref={chartRef} style={{ width: '100%', height: '400px' }} />;
|
|
568
|
-
}
|
|
569
|
-
```
|
|
570
|
-
|
|
571
|
-
**After (v1.0):**
|
|
572
|
-
```tsx
|
|
573
|
-
import { useRef } from 'react';
|
|
574
|
-
import { useEcharts } from 'react-use-echarts';
|
|
575
|
-
|
|
576
|
-
function MyChart() {
|
|
577
|
-
const chartRef = useRef<HTMLDivElement>(null);
|
|
578
|
-
|
|
579
|
-
const { setOption, getInstance, resize } = useEcharts(chartRef, {
|
|
580
|
-
option: { /* ... */ }
|
|
581
|
-
});
|
|
582
|
-
|
|
583
|
-
return <div ref={chartRef} style={{ width: '100%', height: '400px' }} />;
|
|
584
|
-
}
|
|
585
|
-
```
|
|
586
|
-
|
|
587
|
-
#### New Features in v1.0
|
|
588
|
-
|
|
589
|
-
- **Built-in themes**: Use `theme: 'light' | 'dark' | 'macarons'` or pass a custom theme object
|
|
590
|
-
- **Chart linkage**: Connect charts using the `group` option
|
|
591
|
-
- **Lazy initialization**: Use `lazyInit: true` or custom `IntersectionObserverInit` options
|
|
592
|
-
- **SVG renderer**: Use `renderer: 'svg'` for better accessibility and print quality
|
|
593
|
-
- **Manual resize**: New `resize()` function returned from the hook
|
|
594
|
-
|
|
595
|
-
#### Important Notes for Custom Themes
|
|
596
|
-
|
|
597
|
-
When using custom theme objects, **memoize them** to avoid unnecessary chart recreation:
|
|
598
|
-
|
|
599
|
-
```tsx
|
|
600
|
-
import { useRef, useMemo } from 'react';
|
|
601
|
-
import { useEcharts } from 'react-use-echarts';
|
|
602
|
-
|
|
603
|
-
function MyChart() {
|
|
604
|
-
const chartRef = useRef<HTMLDivElement>(null);
|
|
605
|
-
|
|
606
|
-
// ✅ Good: Memoized theme object
|
|
607
|
-
const customTheme = useMemo(() => ({
|
|
608
|
-
color: ['#fc8452', '#9a60b4', '#ea7ccc'],
|
|
609
|
-
backgroundColor: '#1e1e1e'
|
|
610
|
-
}), []);
|
|
611
|
-
|
|
612
|
-
useEcharts(chartRef, {
|
|
613
|
-
option: { /* ... */ },
|
|
614
|
-
theme: customTheme
|
|
615
|
-
});
|
|
616
|
-
|
|
617
|
-
return <div ref={chartRef} style={{ width: '100%', height: '400px' }} />;
|
|
618
|
-
}
|
|
619
|
-
```
|
|
620
|
-
|
|
621
517
|
## 📝 Changelog
|
|
622
518
|
|
|
623
519
|
Detailed changes for each release are documented in the [release notes](https://github.com/chensid/react-use-echarts/releases).
|