npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

echarts-react-wrapper

v0.2.4

Published

Echarts wrap as React component

Downloads

10

Readme

React component of Echarts. It requires ECharts version 5.x. The development is done with React 18.

How to install

npm -i echarts react echarts-react-wrapper

It requires peer two dependencies, react and echarts. I use React 17 and Echarts 5.3 for testing this component.

How to use

import { EchartsComponent } from 'echarts-react-wrapper'

export function ChartContainer() {
  return <EchartsComponent 
    style={{
      width: 500, 
      height: 500
    }} 
    options={{
      yAxis={
        type: "value"
      },
      xAxis={
        type: "category", 
        data: [0, 1, 2, 3]
      }
      series=[{
        type: "line",
        data: [1, 2, 3, 4]
      }]
    }}
    eventsHandler={{
      click: (params, context) => {
        console.log(params, context)
      }
    }}
  />
}

Supported props

  • option - See Echarts' option.
  • opts - See init function opts prop.
  • notMerge, replaceMerge, lazyUpdate - See Echarts' setOption's opts prop
  • onInit - A callback function which when ECharts is initialized, it will call this callback with Echarts instance as argument.
  • eventsHandler - An object where each key can be one of event name from here and object value must be a function that took params and context as it arguments. For more details, see this tutorial

How to fire an action/event

Use onInit callback to obtains Echarts instance. After that, you can follow this tutorial to fire an action/event.

Declarative option

This component support option as an object and merging strategy that Echarts 5 provide. For example, replaceMerge: true will allow user to remove an entry from existing option that Echarts is using. If original option look like this:

import { EchartsComponent } from 'echarts-react-wrapper'

export function ChartContainer({option}) {
  return <EchartsComponent 
    style={{
      width: 500, 
      height: 500
    }} 
    option={option}
    eventsHandler={{
      click: (params, context) => {
        console.log(params, context)
      }
    }}
    replaceMerge={["series"]}
  />
}

When first render, option is

{
  yAxis={
    type: "value"
  },
  xAxis={
    type: "category", 
    data: [0, 1, 2, 3]
  }
  series=[{
    id: "data1",
    type: "line",
    data: [1, 2, 3, 4]
  }]
}

Then sometime later, option is

{
  yAxis={
    type: "value"
  },
  xAxis={
    type: "category", 
    data: [0, 1, 2, 3]
  }
  series=[{
    id: "data2",
    type: "line",
    data: [4, 3, 2, 1]
  }]
}

The chart will remove series with id "data1" out and put series with id "data2" in place.

Programmatically option

There's a use case where each update to option may need complex logic. It's possible to obtain echarts instance without any option supplied. For example:

import { EchartsComponent } from 'echarts-react-wrapper'

export function ChartContainer({/* list of props that may involve option logic */}) {
  const [echarts, setEcharts] = useState()
  if (echarts !== undefined) {
    // perform some complex logic to generate option
    echarts.setOption(/* option obtain from logic above */, {replaceMerge: [/* field to be modify */]})
  }
  return <EchartsComponent 
    style={{
      width: 500, 
      height: 500
    }} 
    onInit={instance => setEcharts(instance)}
    eventsHandler={{
      click: (params, context) => {
        console.log(params, context)
      }
    }}
  />
}

This usage style may be much more efficient if it involve constructing large and complex data or dataset for chart.

Breaking change

0.1.x to 0.2

  • Rename eventHandlers to eventsHandler