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:
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.
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!
Comments
DLT | 6 years ago
I like this approach. Could you help me understand how I would update a chart’s data dynamically? Assume additional data points are available every x minutes.
Linda Svendsen | 6 years ago
Hi,
Maybe this demo could help you? It updates with new data every second! https://www.highcharts.com/demo/dynamic-update
Will Hawker | 6 years ago
Hi DLT, I have an example of that here: https://whawker.github.io/react-jsx-highcharts/examples/LiveUpdate/index.html
Marcelo Baez | 5 years ago
It is impossible to click inside of rangeselector.input, either on my project or in the examples you provide
Jeremy Long | 5 years ago
Thanks for the contribution. Have you done any performance profiling to quantify what the gains are by not having to pass highcharts config objects around? …Or at least compared to, for example, react-highcharts ?
Also, it is worth noting that on the react.js website, they mention that “If you want your application to be stable, don’t use context. It is an experimental API and it is likely to break in future releases of React.”
Will Hawker | 5 years ago
I have yet to do any performance comparisons with react-highcharts, as my main goal was simplifying the means of updating Highcharts internal state using standard React practices. It is definitely on my to-do list though.
Whilst context is supposedly “unstable” it is utilised by many widely used React libraries, i.e. react-router and react-redux.
Context is considered appropriate for libraries to use internally, as long as the don’t force library users to use context directly themselves, but provide HOCs instead. React JSX Highcharts abides by these context best practices.
If the context API were to change, the burden would be on the library to re-implement rather than the downstream library user.
Sources:
https://twitter.com/dan_abramov/status/749715530454622208?lang=en
https://stackoverflow.com/questions/36428355/react-with-redux-what-about-the-context-issue#answer-36431583
Kevin | 9 months ago
Hi not sure if this post is still open but I have a quick question about plotOptions. Currently I am trying to make the data points draggable like in this example https://jsfiddle.net/gh/get/library/pure/highcharts/highcharts/tree/master/samples/highcharts/dragdrop/resize-column
but it’s not working. I have the plotOptions as a variable and assigning it to plotOptions in similar to the example in this post.
Mustapha Mekhatria | 8 months ago
Hi,
Feel free to get in touch with our tech support, and don’t forget to add a link to your demo 🙂
Want to leave a comment?
Comments are moderated. They will publish only if they add to the discussion in a constructive way. Please be polite.