Update: Since the original publication of this post, Highsoft has released a React wrapper.
React has taken the world by storm, and once you first try it out it is easy to understand why. It is simple, fast and facilitates reusable code. This article will show how to create your favorite charts in React. It presumes that you have a basic knowledge about React. If this is not the case then you should start by getting familiar with the React documentation.
INSTALL THE HIGHCHARTS NPM PACKAGE
First install Highcharts in your project. Open a command line terminal and run the following: npm install highcharts --save
It will install Highcharts as a node module, and save highcharts as a dependency in package.json. If you are planning to use Highcharts only in your development environment, then you can use –save-dev as an alternative to –save.
REQUIRE AND LOAD HIGHCHARTS IN NPM
React is isomorphic and can therefore be run both on a server and the client. Because of this there is two different procedures to load Highcharts in React.
On a client side React application Highcharts can be loaded as the following example:
// Load Highcharts var Highcharts = require('highcharts'); // Load a module require('highcharts/modules/funnel')(Highcharts);
However with an isomorphic application the code will also be run on a server. Highcharts relies on a window to function properly, and on the server it does not exist. In this case Highcharts will wait to execute until the code runs on the client.
With an isomorphic application Highcharts should be loaded like in this example:
var Highcharts = require('highcharts'), addFunnel = require('highcharts/modules/funnel'); // After window is present, normally on componentDidMount or in the callback of ReactDOM.render addFunnel(Highcharts);
You will learn more about how to use Highcharts in an isomorphic React application later on.
CREATE YOUR FIRST CHART IN REACT
There are two different methods of rendering a chart in React. The simplest is using ReactDOM.render with a callback. Second method is about creating a React component to hold the logic for your charts. We will show you an example of both cases.
CREATE A CHART WITH CALLBACK IN REACTDOM.RENDER
This example will show how to create a funnel chart in an isomorphic React application.
var React = require('react'), ReactDOM = require('react-dom'), Highcharts = require('highcharts'), addFunnel = require('highcharts/modules/funnel'); var element = React.createElement('div', { id: 'chart' }); ReactDOM.render(element, document.getElementById('react-app'), function () { // Apply funnel after window is present addFunnel(Highcharts); // Construct the chart Highcharts.chart('chart', { /*Options*/ }); });
CREATE A CHART WITH A SIMPLE REACT COMPONENT
The following example will show how it is possible to incorporate Highcharts in a React component. It is a very basic example and will only show you how to get started:
var React = require('react'), Highcharts = require('highcharts'); module.exports = React.createClass({ // When the DOM is ready, create the chart. componentDidMount: function () { // Extend Highcharts with modules if (this.props.modules) { this.props.modules.forEach(function (module) { module(Highcharts); }); } // Set container which the chart should render to. this.chart = new Highcharts[this.props.type || "Chart"]( this.props.container, this.props.options ); }, //Destroy chart before unmount. componentWillUnmount: function () { this.chart.destroy(); }, //Create the div which the chart will be rendered to. render: function () { return React.createElement('div', { id: this.props.container }); } });
Afterwards you are able to use this component in your React application:
var React = require('react'), ReactDOM = require('react-dom'), Chart = require('./components/Highcharts.react'), options = { /*Highcharts options*/ }, element; // Create and render element element = React.createElement(Chart, { container: 'chart', options: options }); ReactDOM.render(element, document.getElementById('react-app'));
GOING A STEP FURTHER WITH COMPONENTS
Something which can be practical and help keep your code cleaner, is to put all your charts including their options and data in separate components. Continuing on what is previously done in this article, then a chart component could look something like this:
var React = require('react'), Chart = require('./Highcharts.react'), Funnel = require('highcharts/modules/funnel.src'), options = { /*Highcharts options*/}; // Add additional module required to render a treemap. module.exports = React.createClass({ render: function () { return React.createElement(Chart, { container: 'chart-funnel', options: options, modules: [Funnel] }); } });
As mentioned previously this is just a simple use case. It might not suit all your needs, but it will get you started.
Comments
mothsin | 8 years ago
I followed your example, but I’m stuck in this error
Highcharts error #13: http://www.highcharts.com/errors/13
Can you help me?
Jon Arild (Highsoft) | 8 years ago
Hi, it’s not easy to know for certain what happens exactly in your case, but my best guess is that the name of the container is not set in the component properties. Here’s an example of what I am referring to: https://github.com/jon-a-nygaard/highcharts-react/blob/master/app.js#L9
I also have an example to get you up and running quickly, or which might help you discover the error: https://github.com/jon-a-nygaard/highcharts-react
Should this not work out, please share some instructions on your setup and how to recreate the issue, prefrerably a demo, and I will have a look at it.
Best regards
Jon
mothsin | 8 years ago
Hi Jon, I discover that my version was 4.0.9. I upgraded and it’s working now!
Thanks
Elise Chant | 8 years ago
Error 13 happens when Highcharts can’t find a DOM node. Highcharts depends on DOM existing to construct and then mount it’s chart instances. This is a common problem with using Highcharts with React.
A work around is to use an element like this as your mounting node:
div ref={el => this.chartEl = el} />
and then do chart rendering within
React.Component().componentDidMount
Check that you instantiate Highcharts new Highcharts.chart after DOM is loaded.
wiesson | 8 years ago
Could you share an example how I could use boost with es6 (and react)? Here is an example, how I use highcharts with react-create-app: https://github.com/wiesson/react-highcharts/blob/master/src/App.js
Jon Arild (Highsoft) | 8 years ago
Hi wiesson, my apologies for the delayed reply.
You should be able to load boost by doing the following:
import Highcharts from 'highcharts';
import HighchartsBoost from 'highcharts/modules/boost';
HighchartsBoost(Highcharts);
Best regards
Jon
Elise Chant | 8 years ago
Hi @wiesson:disqus. As well as needing to explicitly load the module, I think another issue you might be facing in your example is that your component is referring to itself recursively and you don’t define the ref used by Highcharts.chart.renderTo.
class Chart extends Component {
render() {
// https://github.com/wiesson/react-highcharts/blob/master/src/App.js#L72
}
}
You need to change line 72 to:
div ref={el => this.chartEl = el} /> // make this change
and your ComponentDidMount method to:
componentDidMount() {
this.chart = new Highcharts[this.props.type || ‘Chart’](
this.chartEl, // make this change
this.props.options
);
}
wiesson | 8 years ago
Could you please format the code? If my example referring to itself, by is it working then? I did define the ref in line 21 and referring to that.
It would be nice if you fork my repo and show me your example. 🙂
Elise Chant | 8 years ago
no worries https://github.com/wiesson/react-highcharts/pull/1/files
Michael Rojas | 8 years ago
Nice explanation. ¿Is is possible to add the commercial license if I use the module from npm?
Øystein | 8 years ago
Apologies for the delayed response. Highcharts JS has the same licensing model regardless of how you load it. You do not need to input a license key, as the license is trust based.
SeonWoo Chang | 8 years ago
Could you answer that if I would like to get polar chart, what “props.type” should I use? “Chart”?
Øystein | 8 years ago
Yes, “Chart” should do what you are looking for.
Mitchell | 8 years ago
Does HighCharts work with react-native?
Chris V | 8 years ago
Hi,
we don’t have an official react native wrapper, but this may change in the future.
There are a few unofficial wrappers out there though, but I can’t speak to their completeness or if they are production ready.
Hua Lu | 8 years ago
Nice example.
Could you please add more details to show how to update the chart dynamically with React ?
Jon Arild (Highsoft) | 8 years ago
Hi,
I don’t have a live example ready to showcase this for you, but to update the chart dynamically it should be sufficient to add the following to your chart component:
componentDidUpdate: function () {
this.chart.update(this.props.options)
}
It utilizes the Chart.update functionality, which you can read more about in our API: http://api.highcharts.com/highcharts/Chart.update
Best regards
Jon
Elise Chant | 8 years ago
Hi @jon_a_nygaard:disqus , nice question @disqus_MNw2Z6arPy:disqus. A fundamental problem with using Highcharts with React is that that have different rerendering paradigms and dynamic updates don’t “Just work”. You actually need to destroy the chart and then recreate it like how react-highcharts-wrapper does – https://github.com/beestripes/react-highcharts-wrapper/blob/master/src/services/highchartsApi.js#L16, The HIghcharts update API is not forgiving in JavaScript applications because Highcharts ties its lifecycle state to DOM entities which is _not_ how React likes to play,
Jon Arild (Highsoft) | 8 years ago
Hi Elise,
The Chart.update() has a third parameter named oneToOne, which should help you with the updating issues you are describing regarding Axis.
Best regards
Jon
Aditya | 8 years ago
Where is the chart-funnel defined? Is it div or class or alien?
Jon Arild (Highsoft) | 8 years ago
Hi Aditya,
In this articles example `chart-funnel` is passed as a property, which the Chart compenent collects, and use as an id for the div it creates in its render function. The id is later used for Highcharts to know where to render its chart.
Quick tip: Should you want to avoid the use of id’s, then you can modify the Chart compent to store a reference to the div it creates, and pass that reference to Highcharts in stead of the id.
Best regards
Jon Arild Nygård
krishna | 8 years ago
Great! Is this a free library, or do we need buy the licence??
Jon Arild (Highsoft) | 8 years ago
Hi Krishna,
Highcharts is free for non-commercial use, for commerical use you will have to purchase a license. You can find our different licenses here: https://shop.highsoft.com/highcharts
Best regards
Jon Arild Nygård
Sunny Gadia | 7 years ago
We are using highcharts with react. We are unable to use it for HeatMap. Can you help how do we implement a heatmap chart in react.
Jon Arild (Highsoft) | 7 years ago
Hi Sunny,
The following is very simplistic React application which is loading Highcharts and its Heatmap module:
'use strict';
import React from 'react';
import ReactDOM from 'react-dom';
import Highcharts from 'highcharts'
import addHeatmap from 'highcharts/modules/heatmap'
addHeatmap(Highcharts)
const options = {
series: [{
type: 'heatmap',
data: [1, 2 , 3]
}]
};
const onRender = () => {
// Create the chart.
Highcharts.chart('chart', options);
};
const chartContainer = React.createElement('div', { id: 'chart' });
ReactDOM.render(chartContainer, document.getElementById('app'), onRender);
This example presumes you are using Babel as a transpiler to load Highcharts as ES6 modules. You can find more about different ways of loading Highcharts in our Readme section
Hope it helps.
Best regards
Jon
Soni | 7 years ago
Could you please provide the same example in ES6. Basically I am looking zooming functionality which can support component in Web and React-native as well.
Thanks for your support
Ayumi | 7 years ago
Thank you for the tutorial! I used the highcharts for my application. I encountered an issue that I cannot pass array value from redux store to options. (eg:
` series: [
{
data: score
}`
The data is passed through store.
` if(categories){
_.map(categories, data=> {
return score.push(data.score_out_of_10)
})
}
console.log( score)// returns [7.721, 5.112, 2.668, 2.609000000000001, 6.5585, 4.3845, 8.418, 9.924, 9.081666666666665, 5.589, 5.1165, 3.7184999999999997, 5.990000000000001, 1.714, 7.2265, 6.5055, 4.7645]`
)
When I use a hardcoded array outside options, it worked. (eg.
`series: [
{
data: newScore
}`
`var newScore = [ 7.721, 5.112, 2.668, 2.609000000000001, 6.5585, 4.3845, 8.418, 9.924, 9.081666666666665, 5.589, 5.1165, 3.7184999999999997, 5.990000000000001, 1.714, 7.2265, 6.5055, 4.7645]` )
Do you know why it happened? It would be great to hear your advice if it’s possible. The code in my repository : https://github.com/aaayumi/Open_Data_API/blob/master/src/containers/data.js
Thank you.
Nisha | 7 years ago
Can we implement map by this package?
Jon Arild (Highsoft) | 7 years ago
Hi Nisha,
Yes, you can also implement maps by using the highcharts npm package.
Highmaps is available by either loading the map module from
'highcharts/modules/map'
and apply it to Highcharts, or you can replace Highcharts with the Higmaps source file from'highcharts/highmaps'
.The map collection is at the moment not available on npm, and will have to be downloaded as local files to be imported in to your application.
Hope it helps!
Best regards
Noor Woozeer | 7 years ago
Why not use the official library https://github.com/highcharts/highcharts-react
Ah, ok. This post is 2 yrs at this time of writing….
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.