yikenman/use-echarts-react

Repository files navigation

NPM Version NPM Licensecodecov

A modernized React hook for integrating ECharts.


  • Hook based modern APIs.
  • Supports tree-shakeable ECharts usages by default.
  • Supports declarative/imperative mode for different scenarios.
  • Supports deep comparison.
  • Supports all ECharts APIs.
  • Works with SSR.
  • Written in Typescript.
  • 100% test coverage.
$ npm install --save use-echarts-react echarts
import { LineChart } from 'echarts/charts';
import { GridComponent, LegendComponent, TitleComponent, TooltipComponent } from 'echarts/components';
import { use } from 'echarts/core';
import { CanvasRenderer } from 'echarts/renderers';
import { useECharts, useEChartsEvent } from "use-echarts-react";

// Tree-shakeable ECharts usage, see https://echarts.apache.org/handbook/en/basics/import/#shrinking-bundle-size.
use([
  CanvasRenderer,
  GridComponent,
  LineChart,
  TooltipComponent,
  TitleComponent,
  LegendComponent
]);

const App = () => {
  const ref = useECharts<HTMLDivElement>({
    // echarts instance option
    xAxis: {
      type: 'category',
      data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
    },
    yAxis: {
      type: 'value'
    },
    series: [
      {
        data: [150, 230, 224, 218, 135, 147, 260],
        type: 'line'
      }
    ]
  }, {
    width: 100,
    height: 100
  });

  // binding event
  useEChartsEvent(ref, 'click', () => {
    console.log('triggered');
  });

  return (
    <div ref={ref}></div>
  );
};

Configures the ECharts instance with the provided options.

This parameter is used for configuring the ECharts instance with the options that would be passed to the setOption method. It can include the following:

PropertyDescription
... ECharts OptionsAccepts all options from setOption method. Refers ECharts Configuration Items Manual for all available options.
groupSpecifies the group of the ECharts instance and automatically connects the chart group. See echartsInstance.group.
loadingControls the loading state of the ECharts instance. See showLoading method.

This is used to initialize the ECharts instance. It can only be set once during the initialization and includes the following options:

PropertyDescription
themeThe theme for the ECharts instance, see echarts.init - theme.
localeThe locale for the ECharts instance, see echarts.init - locale.
rendererThe renderer for ECharts (canvas or SVG), see echarts.init - renderer.
devicePixelRatioThe pixel ratio for the instance, see echarts.init - devicePixelRatio.
useDirtyRectWhether to use the dirty rectangle technique to optimize performance, see echarts.init - useDirtyRect.
useCoarsePointerWhether to use a coarse pointer for event handling, see echarts.init - useCoarsePointer.
pointerSizeThe size of the pointer for interactions, see echarts.init - pointerSize.
ssrEnable server-side rendering for the instance, see echarts.init - ssr.
widthThe width of the chart, see echarts.init - width.
heightThe height of the chart, see echarts.init - height.
resizeAutomatically resizes the chart when the bound element’s size changes. Defaults to false.
imperativeModeEnables direct control of the ECharts instance. When enabled, the option parameter is ignored. Defaults to false.
deepCompareEnables deep comparison for option before updating the instance. Defaults to false.

The hook returns a React ref object used to bind the DOM element. This ref contains two properties:

  • current: The current DOM element that the ref is bound to.
  • chart: The current created ECharts instance with restricted APIs. If imperativeMode is enabled, this will return the complete ECharts instance, allowing direct interaction with all methods of ECharts.

Automatically handling event binding to ECharts instance.

The return value of the useECharts hook.

The event to listen for. This is the same as the eventName parameter in the ECharts echartsInstance.on method.

This is the query parameter for the event listener. It is the same as the query parameter in the ECharts echartsInstance.on method.

Same as the handler parameter in the ECharts echartsInstance.on method.

By default, useECharts will return an ECharts instance with restricted APIs like setOption, on... and user should update ECharts instance by updating the hook parameters. Also user can get a complete ECharts instance via enabling imperativeMode.

import { useECharts } from "use-echarts-react";

const App = () => {
  const ref = useECharts<HTMLDivElement>({
    // can only use hook parameters to update ECharts instance.
    xAxis: {
      type: 'category',
      data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
    },
    yAxis: {
      type: 'value'
    },
    series: [
      {
        data: [150, 230, 224, 218, 135, 147, 260],
        type: 'line'
      }
    ]
  }, {
    width: 100,
    height: 100
  });

  return (
    <div ref={ref}></div>
  );
};
import { useECharts } from "use-echarts-react";

const App = () => {
  const ref = useECharts<HTMLDivElement>({
    // option will be ignored under imperative mode.
  }, {
    imperativeMode: true,
    width: 100,
    height: 100
  });

  return (
    <div>
      <button
        onClick={() => {
          // `setOption` is available to call now.
          ref.chart?.setOption({
            xAxis: {
              type: 'category',
              data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
            },
            yAxis: {
              type: 'value'
            },
            series: [
              {
                data: [150, 230, 224, 218, 135, 147, 260],
                type: 'line',
              }
            ]
          });
        }}
      >
        update
      </button>
      <div ref={ref}></div>
    </div>
  );
};

useEcharts supports using ECharts SSR option. But this is not as the same as React SSR. ECharts SSR is used for returning SVG string instead of manipulating a dom element.

import { useECharts } from "use-echarts-react";

const App = () => {
  const ref = useECharts<HTMLDivElement>({
    xAxis: {
      type: 'category',
      data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
    },
    yAxis: {
      type: 'value'
    },
    series: [
      {
        data: [150, 230, 224, 218, 135, 147, 260],
        type: 'line'
      }
    ]
  }, {
    renderer: 'svg',
    ssr: true,
    width: 100,
    height: 100
  });

  return (
    <div
      dangerouslySetInnerHTML={{
        __html: ref.chart?.renderToSVGString() ?? ''
      }}
    ></div>
  );
};

MIT License

About

A modernized React hook for integrating ECharts.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 2

  •  
  •