A Highcharts’ abstraction in React Components: React JSX Highcharts

Highcharts and React logo


Update: Since the original publication of this post, Highsoft has released a React wrapper.

In December 2016, I began a project in my workplace to rewrite our legacy Knockout web application using React. As we provide data analytics to the utilities sector, charts are the central piece of the application delivered to the clients.

Highcharts had long proved itself as an excellent solution to our graphing requirements, so naturally, developers and stakeholders alike were keen that Highcharts remained a key part of the new application going forward.

I was in charge of the Highcharts integration within the React App, and after some research, it became evident that many of the existing solutions require passing large Highcharts configuration objects to a single React component.

I’m not particularly keen on this approach, as a React component might not re-render as expected when this configuration object is updated. Consider this (admittedly contrived) example:

Consent for marketing cookies needs to be given to view code samples

In the above example, the reference (pointer) to myObj is maintained despite the number property being modified. When React compares the new state to the previous state, by shallow comparison, the state is considered equal, and no re-render occurs. These React specific issues are usually avoided by ensuring that the user clones the object before modifying it – this principle is the basis of ImmutableJS.

Aside from these technical concerns, the existing approaches don’t really feel like “React” to me. In the beginning, I preferred the approach of Recharts, but I quickly realized that Recharts lacks the comprehensive feature set Highcharts provides.

This motivated me to begin a side project, to investigate whether I could create an abstraction of Highcharts using React components, like Recharts.

The result is what I am sharing with the community today – React JSX Highcharts (GitHub / NPM).

The goal of this project is to provide a thin-as-possible abstraction, so anyone reading the Highcharts documentation could easily figure out how to achieve the same via React JSX Highcharts.

Consent for marketing cookies needs to be given to view code samples

React JSX Highcharts works by first creating a chart instance with all features disabled, then, as its’ child components are mounted, it updates the chart instance with the appropriate configuration.

To avoid the previously mentioned problems with passing complex objects as props, each configuration option is passed as a separate component prop.

Let’s check this example to render a line series:

<LineSeries id="my-series" data={[1, 2, 3, 4]} color="#0FF" step />

Passing props individually allows React JSX Highcharts to process and select the optimal Highcharts method – for example, if the series data prop were to change, React JSX Highcharts can follow Highcharts best practices and use the setData method rather than update.
There are a few instances where React conventions have taken precedence over Highcharts configuration – for instance, rather than passing an object of event handles, React JSX Highcharts uses the onEventName convention, commonly seen in React code.

<SplineSeries id="my-series" data={myData} onHide={this.handleHide} onShow={this.handleShow} />

This would be equivalent to the Highcharts configuration:

series: [{
  type: 'spline',
  data: myData,
  events: { hide: this.handleHide, show: this.handleShow }

To avoid passing around the chart instance between all the components, React JSX Highcharts utilizes React’s context feature – then using Higher Order Components, it injects Highcharts methods into the wrapped component as props. The components then interact with the chart instance via these injected methods on the application author’s behalf.

I am obviously biased, but I believe that it is quite a powerful way to interacts with Highcharts. Therefore React JSX Highcharts exposes these Higher Order Components publicly, allowing application authors to write custom React components which can interact with the chart too.

For instance, in this example, I use React Day Picker as a drop in replacement for Highstocks Range Selector date pickers.

I would love to get some feedback (and contributions ?) from the Highcharts community to advance the project. Going forward I’m planning to add Heat and Tree Map functionality, and ImmutableJS support, specifically for series data structures, but I’m open to suggestions!