Simple Pattern to use Highchart and React<!-- --> | <!-- -->Patrick Desjardins Blog
Patrick Desjardins Blog
Patrick Desjardins picture from a conference

Simple Pattern to use Highchart and React

Posted on: June 12, 2018

At the time I am compositing this entry, Highchart doesn't collaborate naturally in a React project. There is an open source project available but it is not part of the official release. However, using directly the library from Highchart into your TypeScript and React project is not a huge burden. In this article, we will see how to create a React component that uses Highchart.

The first step is to get the official library and the definition file. The types are not in the main repository and are a major version late. However, it was enough in my experience to work. You need to NPM install "highcharts" and "@types/highcharts".

You will need to create a new TSX file and import one or many packages. The number of packages depends on what you want to do with HighChart. If you need to use the exporting capability, you will need to import an additional module. If you need the offline flavor of exporting, you will need also to import a package. For the completeness of the example, let's get them all.

1import Highcharts, { CSSObject, Options, SeriesObject, PlotBands } from "highcharts";
2import Exporter from "highcharts/modules/exporting";
3import OE from "highcharts/modules/offline-exporting";

The Exporting and OE are functions. They must be applied to the HightCharts function right below you imports.


The next step is to create a React component that will have a componentDidUpdate, a render, and a componentDidMount. Also, the component must have one reference to Highchart and one other reference to the HTML container. The container may be used for adding dynamic animation with CSS and also to indicate where to inject the Highchart component. The Highchart component reference will be used to push data or user's options during the lifetime of the component.

1private hightChartContainer: HTMLDivElement | null = null;
2private highChartControl: Highcharts.ChartObject | undefined = undefined;

The render function creates the container that hosts the Highchart control.

1public render(): JSX.Element {
2 const isChartVisible = this.props.source.graphData !== undefined;
3 const chart = <div style={{ display: (isChartVisible ? "block" : "none") }} ref={(div) => { this.hightChartContainer = div; }} />;
4 return <> <div
5 style={{ display: (isChartVisible ? "none" : "flex"), }}> Loading your data
6 </div> {chart}
7 </>; }

The render will be executed once and then the componentDidMount function is executed once as well. Meanwhile, the componentDidMount function does the heavy lifting by instantiating the Highchart component. In reality, this function is way bigger than what I am providing. It contains all the configuration you can apply to a Highchart.

1public componentDidMount() {
2 const that = this;
3 if (this.hightChartContainer !== null) {
4 Highcharts.setOptions(theme);
5 const options: Options = {
6 chart: {
7 type: "line", },
8 series: [],
9 };
10 this.highChartControl = Highcharts.chart(this.hightChartContainer, options);
11 if (this.props.source.graphData !== undefined) {
12 this.componentDidUpdate();
13 }
14 }

Finally, the componentDidUpdate has the role to push the values to the chart. The componentDidMount sets the series to an empty array because the data might not yet be available. However, the componentDidUpdate will be called every time new properties change which make it a good place to update the chart.

1public componentDidUpdate() {
2 if (this.hightChartContainer !== null && this.highChartControl !== undefined && this.props.source.graphData !== undefined) {
3 const data = this.props... // Get your data
4 const series: GraphData[] = [];
5 while (this.highChartControl.series.length > 0) {
6 this.highChartControl.series[0].remove(false);
7 }
9 // Values dimension represent each series
10 scenarioUpdate.addMarker("CreateSeries");
11 rawValues.forEach((value: number[], index: number) => {
12 const time = data.start + index * data.step;
13 for (let iSerie = 0; iSerie < value.length; iSerie++) {
14 if (index === 0) { // Only first time we create series
15 series.push({ seriesName: data.legend[iSerie], seriesValues: [] });
16 }
17 series[iSerie].seriesValues.push({ time: time, value: value[iSerie] });
18 }
19 });
21 let loopCount = 0;
22 series.forEach((serieData: GraphData, index: number) => {
23 if (this.highChartControl !== undefined) {
24 const values = serieData.seriesValues
25 .map((d: GraphDataValue) => { loopCount++; return [d.time, d.value]; });
26 this.highChartControl.addSeries({}, false, false);
27 this.highChartControl.series[index].setData(values, false, false, false);
28 this.highChartControl.series[index].update({ name: serieData.seriesName }, false);
29 }
30 });
32 window.requestAnimationFrame(() => {
33 window.requestAnimationFrame(() => {
34 if (this.highChartControl !== undefined) {
35 this.highChartControl.reflow();
36 this.highChartControl.redraw();
37 }
38 });
39 });
40 }

Once again, I removed a lot of the code from which I have been inspired. The goal here is to create all your series but before creating series, we remove all the existing one. In the end, we reflow and redraw which animated the graph. Indeed, a good "shouldComponentUpdate" is required to avoid updating for no reason.

It might look like a lot of code but in reality, it is so close to the Highcharts documentation that it is easy to maintain. The key concept is to have the container that host Highchart build one time and to update the existing one.