react-use-echarts 1.0.2 → 1.0.4

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 CHANGED
@@ -1,5 +1,7 @@
1
1
  # react-use-echarts
2
2
 
3
+ > [中文](./README.zh-CN.md) | [English](./README.md)
4
+
3
5
  [![NPM version](https://img.shields.io/npm/v/react-use-echarts.svg)](https://www.npmjs.com/package/react-use-echarts)
4
6
  [![NPM downloads](https://img.shields.io/npm/dm/react-use-echarts.svg)](https://www.npmjs.com/package/react-use-echarts)
5
7
  [![GitHub Actions Workflow Status](https://img.shields.io/github/actions/workflow/status/chensid/react-use-echarts/npm-publish.yml)](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: options
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
- ### Built-in Themes
175
+ ### Themes
188
176
 
189
- The library includes three built-in themes: `light`, `dark`, and `macarons`.
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
- function ThemedChart() {
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
198
 
211
- ### Custom Theme
212
-
213
- ```tsx
214
- import { useRef } from 'react';
215
- import { useEcharts } from 'react-use-echarts';
216
-
217
- function CustomThemedChart() {
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 synchronize their interactions (e.g., tooltip, highlight).
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,14 +261,13 @@ function LinkedCharts() {
280
261
 
281
262
  ### Lazy Initialization
282
263
 
283
- Only initialize charts when they enter the viewport, great for pages with many charts.
284
-
285
- > 默认懒加载参数:`rootMargin: '50px'`,`threshold: 0.1`
264
+ Initialize charts only when they enter the viewport. Suitable for pages with multiple charts. Default parameters: `rootMargin: '50px'`, `threshold: 0.1`.
286
265
 
287
266
  ```tsx
288
267
  import { useRef } from 'react';
289
268
  import { useEcharts } from 'react-use-echarts';
290
269
 
270
+ // Using default configuration
291
271
  function LazyChart() {
292
272
  const chartRef = useRef<HTMLDivElement>(null);
293
273
 
@@ -297,13 +277,13 @@ function LazyChart() {
297
277
  yAxis: { type: 'value' },
298
278
  series: [{ data: [120, 200, 150], type: 'bar' }]
299
279
  },
300
- lazyInit: true // Chart will only initialize when scrolled into view
280
+ lazyInit: true
301
281
  });
302
282
 
303
283
  return <div ref={chartRef} style={{ width: '100%', height: '400px' }} />;
304
284
  }
305
285
 
306
- // With custom IntersectionObserver options
286
+ // Custom IntersectionObserver configuration
307
287
  function LazyChartWithOptions() {
308
288
  const chartRef = useRef<HTMLDivElement>(null);
309
289
 
@@ -314,7 +294,7 @@ function LazyChartWithOptions() {
314
294
  series: [{ data: [120, 200, 150], type: 'bar' }]
315
295
  },
316
296
  lazyInit: {
317
- rootMargin: '100px', // Pre-load when 100px away from viewport
297
+ rootMargin: '100px',
318
298
  threshold: 0.1
319
299
  }
320
300
  });
@@ -325,7 +305,7 @@ function LazyChartWithOptions() {
325
305
 
326
306
  ### SVG Renderer
327
307
 
328
- Use SVG renderer instead of Canvas for better accessibility and print quality.
308
+ Use SVG renderer for better accessibility and print quality.
329
309
 
330
310
  ```tsx
331
311
  import { useRef } from 'react';
@@ -340,7 +320,7 @@ function SVGChart() {
340
320
  yAxis: { type: 'value' },
341
321
  series: [{ data: [120, 200, 150], type: 'bar' }]
342
322
  },
343
- renderer: 'svg'
323
+ renderer: 'svg' // Default is 'canvas'
344
324
  });
345
325
 
346
326
  return <div ref={chartRef} style={{ width: '100%', height: '400px' }} />;
@@ -349,6 +329,8 @@ function SVGChart() {
349
329
 
350
330
  ### Accessing ECharts Instance
351
331
 
332
+ Access the ECharts instance via `getInstance()` to perform advanced operations like exporting images.
333
+
352
334
  ```tsx
353
335
  import { useRef } from 'react';
354
336
  import { useEcharts } from 'react-use-echarts';
@@ -367,12 +349,7 @@ function ChartWithInstance() {
367
349
  const exportImage = () => {
368
350
  const instance = getInstance();
369
351
  if (instance) {
370
- const url = instance.getDataURL({
371
- type: 'png',
372
- pixelRatio: 2,
373
- backgroundColor: '#fff'
374
- });
375
- // Download or use the image URL
352
+ const url = instance.getDataURL({ type: 'png', pixelRatio: 2, backgroundColor: '#fff' });
376
353
  const link = document.createElement('a');
377
354
  link.download = 'chart.png';
378
355
  link.href = url;
@@ -387,28 +364,12 @@ function ChartWithInstance() {
387
364
  </div>
388
365
  );
389
366
  }
390
-
391
- ### Utilities
392
-
393
- 高级场景可直接使用导出的实例缓存与组联动工具:
394
-
395
- ```tsx
396
- import {
397
- getCachedInstance,
398
- clearInstanceCache,
399
- getGroupInstances,
400
- updateGroup,
401
- addToGroup,
402
- removeFromGroup,
403
- } from 'react-use-echarts';
404
- ```
405
-
406
- - `getCachedInstance` / `clearInstanceCache`:查询或清理内部实例缓存
407
- - `getGroupInstances` / `addToGroup` / `removeFromGroup` / `updateGroup`:手动管理 ECharts 组联动
408
367
  ```
409
368
 
410
369
  ### Manual Resize
411
370
 
371
+ Manually trigger chart resize (usually handled automatically by ResizeObserver).
372
+
412
373
  ```tsx
413
374
  import { useRef } from 'react';
414
375
  import { useEcharts } from 'react-use-echarts';
@@ -424,75 +385,73 @@ function ResizableChart() {
424
385
  }
425
386
  });
426
387
 
427
- // Manually trigger resize after some container size change
428
- const handleContainerResize = () => {
429
- resize();
430
- };
431
-
432
388
  return (
433
389
  <div>
434
- <button onClick={handleContainerResize}>Trigger Resize</button>
390
+ <button onClick={resize}>Trigger Resize</button>
435
391
  <div ref={chartRef} style={{ width: '100%', height: '400px' }} />
436
392
  </div>
437
393
  );
438
394
  }
439
395
  ```
440
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
+
441
415
  ## 📖 API
442
416
 
443
417
  ### useEcharts
444
418
 
445
- The main hook for using ECharts in React components.
419
+ The main Hook for using ECharts in React components.
446
420
 
447
421
  #### Parameters
448
422
 
449
423
  ```tsx
450
- import { useRef } from 'react';
451
- import type { EChartsOption } from 'echarts';
452
- import type { UseEchartsOptions } from 'react-use-echarts';
453
- import { useEcharts } from 'react-use-echarts';
454
-
455
424
  const chartRef = useRef<HTMLDivElement>(null);
456
425
 
457
- const someEchartsOption: EChartsOption = {
458
- xAxis: { type: 'category', data: ['Mon', 'Tue', 'Wed'] },
459
- yAxis: { type: 'value' },
460
- series: [{ type: 'line', data: [120, 200, 150] }],
461
- };
462
-
463
- const options: UseEchartsOptions = {
464
- option: someEchartsOption, // Required: ECharts options
465
- theme: 'dark', // Optional: 'light' | 'dark' | 'macarons' | custom object | null
466
- renderer: 'canvas', // Optional: 'canvas' | 'svg' (default: 'canvas')
467
- lazyInit: false, // Optional: boolean | IntersectionObserverInit
468
- group: 'my-group', // Optional: Group ID for chart linkage
469
- setOptionOpts: { notMerge: false }, // Optional: Default setOption options
470
- showLoading: false, // Optional: Show loading state
471
- 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
472
435
  onEvents: {
473
436
  click: {
474
- handler: (params) => {
475
- console.log(params);
476
- },
477
- query: 'series', // Optional: Event query
437
+ handler: (params) => console.log(params),
438
+ query: 'series', // Optional: event query condition
478
439
  },
479
440
  },
480
- };
481
-
482
- const { setOption, getInstance, resize } = useEcharts(chartRef, options);
441
+ });
483
442
  ```
484
443
 
485
444
  #### Options
486
445
 
487
446
  | Property | Type | Default | Description |
488
447
  |----------|------|---------|-------------|
489
- | `option` | `EChartsOption` | **required** | ECharts configuration options |
448
+ | `option` | `EChartsOption` | **Required** | ECharts configuration option |
490
449
  | `theme` | `'light' \| 'dark' \| 'macarons' \| object \| null` | `null` | Theme name or custom theme object |
491
450
  | `renderer` | `'canvas' \| 'svg'` | `'canvas'` | Renderer type |
492
- | `lazyInit` | `boolean \| IntersectionObserverInit` | `false` | Lazy initialization config |
493
- | `group` | `string` | - | Group ID for chart linkage |
451
+ | `lazyInit` | `boolean \| IntersectionObserverInit` | `false` | Lazy initialization configuration |
452
+ | `group` | `string` | - | Chart linkage group ID |
494
453
  | `setOptionOpts` | `SetOptionOpts` | - | Default options for setOption |
495
- | `showLoading` | `boolean` | `false` | Show loading state |
454
+ | `showLoading` | `boolean` | `false` | Whether to show loading state |
496
455
  | `loadingOption` | `object` | - | Loading configuration |
497
456
  | `onEvents` | `EChartsEvents` | - | Event handlers |
498
457
 
@@ -506,8 +465,8 @@ const { setOption, getInstance, resize } = useEcharts(chartRef, options);
506
465
  }
507
466
  ```
508
467
 
509
- - **`setOption`**: Update chart options dynamically
510
- - **`getInstance`**: Get the ECharts instance (returns `undefined` before initialization)
468
+ - **`setOption`**: Dynamically update chart configuration
469
+ - **`getInstance`**: Get ECharts instance (returns `undefined` before initialization)
511
470
  - **`resize`**: Manually trigger chart resize
512
471
 
513
472
  ### Theme Utilities
@@ -521,26 +480,16 @@ import {
521
480
  registerBuiltinThemes,
522
481
  } from 'react-use-echarts';
523
482
 
524
- // Get all available built-in theme names
525
- const themes = getAvailableThemes(); // ['light', 'dark', 'macarons']
526
-
527
- // Check if a theme name is built-in
483
+ getAvailableThemes(); // ['light', 'dark', 'macarons']
528
484
  isBuiltinTheme('dark'); // true
529
- isBuiltinTheme('custom'); // false
530
-
531
- // Get built-in theme configuration
532
- const darkTheme = getBuiltinTheme('dark');
533
-
534
- // Register a custom theme globally
535
- registerCustomTheme('my-theme', { color: ['#ff0000', '#00ff00'] });
536
-
537
- // Register built-in themes to ECharts (auto-called on module load, rarely needed manually)
538
- 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)
539
488
  ```
540
489
 
541
490
  ### useLazyInit
542
491
 
543
- A standalone hook for lazy initialization using IntersectionObserver.
492
+ Standalone lazy initialization Hook based on IntersectionObserver.
544
493
 
545
494
  ```tsx
546
495
  import { useRef } from 'react';
@@ -548,8 +497,6 @@ import { useLazyInit } from 'react-use-echarts';
548
497
 
549
498
  function MyComponent() {
550
499
  const elementRef = useRef<HTMLDivElement>(null);
551
-
552
- // Returns true when element enters viewport
553
500
  const isInView = useLazyInit(elementRef, {
554
501
  rootMargin: '50px',
555
502
  threshold: 0.1
@@ -567,84 +514,6 @@ function MyComponent() {
567
514
 
568
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).
569
516
 
570
- ## 🔄 Migration Guide
571
-
572
- ### From v0.0.11 to v1.0
573
-
574
- #### Breaking Change: External Ref Management
575
-
576
- The `useEcharts` hook no longer returns a `chartRef`. Instead, you now create and manage the ref externally:
577
-
578
- **Before (v0.0.11):**
579
- ```tsx
580
- import { useEcharts } from 'react-use-echarts';
581
-
582
- function MyChart() {
583
- const { chartRef, setOption, getInstance } = useEcharts({
584
- option: { /* ... */ }
585
- });
586
-
587
- return <div ref={chartRef} style={{ width: '100%', height: '400px' }} />;
588
- }
589
- ```
590
-
591
- **After (v1.0):**
592
- ```tsx
593
- import { useRef } from 'react';
594
- import { useEcharts } from 'react-use-echarts';
595
-
596
- function MyChart() {
597
- const chartRef = useRef<HTMLDivElement>(null);
598
-
599
- const { setOption, getInstance, resize } = useEcharts(chartRef, {
600
- option: { /* ... */ }
601
- });
602
-
603
- return <div ref={chartRef} style={{ width: '100%', height: '400px' }} />;
604
- }
605
- ```
606
-
607
- #### New Features in v1.0
608
-
609
- - **Built-in themes**: Use `theme: 'light' | 'dark' | 'macarons'` or pass a custom theme object
610
- - **Chart linkage**: Connect charts using the `group` option
611
- - **Lazy initialization**: Use `lazyInit: true` or custom `IntersectionObserverInit` options
612
- - **SVG renderer**: Use `renderer: 'svg'` for better accessibility and print quality
613
- - **Manual resize**: New `resize()` function returned from the hook
614
-
615
- #### Important Notes for Custom Themes
616
-
617
- When using custom theme objects, **memoize them** to avoid unnecessary chart recreation:
618
-
619
- ```tsx
620
- import { useRef, useMemo } from 'react';
621
- import { useEcharts } from 'react-use-echarts';
622
-
623
- function MyChart() {
624
- const chartRef = useRef<HTMLDivElement>(null);
625
-
626
- // ✅ Good: Memoized theme object
627
- const customTheme = useMemo(() => ({
628
- color: ['#fc8452', '#9a60b4', '#ea7ccc'],
629
- backgroundColor: '#1e1e1e'
630
- }), []);
631
-
632
- useEcharts(chartRef, {
633
- option: { /* ... */ },
634
- theme: customTheme
635
- });
636
-
637
- return <div ref={chartRef} style={{ width: '100%', height: '400px' }} />;
638
- }
639
- ```
640
-
641
- ### 1.0.2
642
-
643
- - 主题切换后保留组联动与 loading 状态
644
- - 懒加载完成后正确加入组
645
- - onEvents 变更时自动重绑事件
646
- - 文档补充实例缓存与组联动工具导出
647
-
648
517
  ## 📝 Changelog
649
518
 
650
519
  Detailed changes for each release are documented in the [release notes](https://github.com/chensid/react-use-echarts/releases).