DEV Community

Cover image for Using Apache ECharts with React and TypeScript

Using Apache ECharts with React and TypeScript

Maneet Goyal on July 03, 2021

What is Apache ECharts? It's a cool data-visualization library like Highcharts, Chart.js, amCharts, Vega-Lite, and numerous others. A lo...
Collapse
 
raymolla7 profile image
raymolla7 • Edited

This is great, thank you for writing this.

I am wondering how do you get tree shaking working with the above? (Minimal Option Type in TypeScript)

I found this documentation here: echarts.apache.org/en/tutorial.htm...

but I am having trouble incorporating it into your React-ECharts.tsx

Collapse
 
maneetgoyal profile image
Maneet Goyal • Edited

Didn't try it yet @raymolla7 . But thanks for pointing out that link. Seems like tree shaking can be useful in our case too.

Collapse
 
raymolla7 profile image
raymolla7

Ok let me know if you can figure out a way to incorporate it. I tried for a bit but I had no luck :(

The bundle size of ECharts is quite large and can be drastically reduced if we use the "Minimal Option Type in TypeScript" listed in the bottom of the link

Thread Thread
 
maneetgoyal profile image
Maneet Goyal

@raymolla7 If you were still looking for some info, I hope this helps: dev.to/maneetgoyal/using-apache-ec...

Thread Thread
 
raymolla7 profile image
raymolla7

this is incredibly helpful! Thank you so much for this well written article Maneet!

Collapse
 
captaint33mo profile image
Vibhor Sharma

This is great article, thanks.

I was wondering if you could also add the onclick and other events functionality in this component as well which is given in this example: echarts.apache.org/examples/en/edi....

I want to make use of this onclick even when I click on any of the bar elements in the bar graph or any other graph.

Collapse
 
maneetgoyal profile image
Maneet Goyal

You should be able to add any click events on the chart too:

const chart = getInstanceByDom(chartRef.current);
chart.on("click", ...);
Enter fullscreen mode Exit fullscreen mode

All the methods and properties mentioned here should be accessible via this approach.

Collapse
 
huydhoang profile image
Huy

Nice work! Underrated article. Perhaps it would be easier to follow if you incorporate the example dataset in the comment into the main article :-)

Collapse
 
maneetgoyal profile image
Maneet Goyal • Edited

Thanks Huy. Have made some updates. Hope it hasn't become too complicated now. The source code changed multiple times in our private repo since the article was first written, so updated the source here too.

Collapse
 
huydhoang profile image
Huy

+1 unicorn for the edit! This has been immensely helpful in my learning of echarts. The above article was much more comprehensive and easy to follow now.

Collapse
 
dcantu96 profile image
David Cantu • Edited

Hi there. First of all great article! I'm having some trouble myself with the width of the charts not setting correctly on first load. After I adjust the browser width, it is adjusted correctly. Is anyone else having trouble with this?

Collapse
 
dcantu96 profile image
David Cantu

I also noticed that the chart.resize() returns undefined every time the resize event listener is triggered

Collapse
 
edusidsan profile image
edusidsan

How do you call this component in app? I´m having problmes in type declaration.

Collapse
 
maneetgoyal profile image
Maneet Goyal • Edited

Have updated the component code in the first snippet since the article was first published. Please take a look.

Here's how I am using it inside an app:

<Grid item>
  <ReactECharts option={{.......whatever options object we want to add........}} />
</Grid>
Enter fullscreen mode Exit fullscreen mode

An valid option prop can be:

 {
    dataset: {
      source: [
        ["Commodity", "Owned", "Financed"],
        ["Commodity 1", 4, 1],
        ["Commodity 2", 2, 4],
        ["Commodity 3", 3, 6],
        ["Commodity 4", 5, 3],
      ],
    },
    tooltip: {
      trigger: "axis",
      axisPointer: {
        type: "shadow",
      },
    },
    legend: {
      data: ["Owned", "Financed"],
    },
    grid: {
      left: "10%",
      right: "0%",
      top: "20%",
      bottom: "20%",
    },
    xAxis: {
      type: "value",
    },
    yAxis: {
      type: "category",
    },
    series: [
      {
        type: "bar",
        stack: "total",
        label: {
          show: true,
        },
      },
      {
        type: "bar",
        stack: "total",
        label: {
          show: true,
        },
      },
    ],
  }
Enter fullscreen mode Exit fullscreen mode