What is highcharts-react-official?
The highcharts-react-official package is a React wrapper for Highcharts, a popular JavaScript charting library. It allows you to create interactive and responsive charts in your React applications with ease.
What are highcharts-react-official's main functionalities?
Basic Chart
This code demonstrates how to create a basic chart using the highcharts-react-official package. It sets up a simple line chart with a title and a series of data points.
import React from 'react';
import Highcharts from 'highcharts';
import HighchartsReact from 'highcharts-react-official';
const options = {
title: {
text: 'My chart'
},
series: [{
data: [1, 2, 3, 4, 5]
}]
};
const BasicChart = () => (
<HighchartsReact
highcharts={Highcharts}
options={options}
/>
);
export default BasicChart;
Updating Chart Data
This example shows how to update the chart data dynamically. The chart data is stored in a state variable, and a button is provided to update the data, which in turn updates the chart.
import React, { useState } from 'react';
import Highcharts from 'highcharts';
import HighchartsReact from 'highcharts-react-official';
const DynamicChart = () => {
const [data, setData] = useState([1, 2, 3, 4, 5]);
const options = {
title: {
text: 'Dynamic Data Chart'
},
series: [{
data: data
}]
};
const updateData = () => {
setData(data.map(d => d + 1));
};
return (
<div>
<HighchartsReact
highcharts={Highcharts}
options={options}
/>
<button onClick={updateData}>Update Data</button>
</div>
);
};
export default DynamicChart;
Custom Chart Types
This code demonstrates how to create a custom chart type, specifically a bubble chart, using the highcharts-react-official package. It includes the HighchartsMore module to enable additional chart types.
import React from 'react';
import Highcharts from 'highcharts';
import HighchartsReact from 'highcharts-react-official';
import HighchartsMore from 'highcharts/highcharts-more';
HighchartsMore(Highcharts);
const options = {
chart: {
type: 'bubble'
},
title: {
text: 'Bubble Chart'
},
series: [{
data: [
[9, 81, 63],
[98, 5, 89],
[51, 50, 73],
[41, 22, 14],
[58, 24, 20],
[78, 37, 34],
[55, 56, 53],
[18, 45, 70],
[42, 44, 28],
[3, 52, 59],
[31, 18, 97],
[79, 91, 63],
[93, 23, 23],
[44, 83, 22]
]
}]
};
const BubbleChart = () => (
<HighchartsReact
highcharts={Highcharts}
options={options}
/>
);
export default BubbleChart;
Other packages similar to highcharts-react-official
react-chartjs-2
react-chartjs-2 is a React wrapper for Chart.js, another popular charting library. It offers a wide range of chart types and is known for its simplicity and ease of use. Compared to highcharts-react-official, react-chartjs-2 might be more suitable for simpler charting needs and has a smaller bundle size.
recharts
Recharts is a composable charting library built on React components. It provides a highly customizable and flexible way to create charts using React. Recharts is known for its declarative approach and ease of integration with React applications. It offers a different API and design philosophy compared to highcharts-react-official.
victory
Victory is a modular charting library for React and React Native. It provides a wide range of chart types and is designed to be highly customizable and extensible. Victory's API is more component-based, which can be more intuitive for React developers. It offers a different set of features and customization options compared to highcharts-react-official.
Highcharts React
Official minimal Highcharts wrapper for React.
Table of Contents
Getting Started
General prerequisites
Make sure you have node, NPM and React up to date.
Tested and required versions:
- node 8.11.3+
- npm 6.4.1+ or similar package manager
- React 16.4+
Installing
Get package from NPM in your React app:
npm install highcharts-react-official
Using
Basic usage example
Import into your React project and render a chart:
Highcharts chart
import React from 'react'
import { render } from 'react-dom'
import Highcharts from 'highcharts'
import HighchartsReact from 'highcharts-react-official'
const options = {
title: {
text: 'My chart'
},
series: [{
data: [1, 2, 3]
}]
}
const App = () => <div>
<HighchartsReact
highcharts={Highcharts}
options={options}
/>
</div>
render(<App />, document.getElementById('root'))
Highstock chart
import React from 'react'
import { render } from 'react-dom'
import Highcharts from 'highcharts/highstock'
import HighchartsReact from 'highcharts-react-official'
const options = {
title: {
text: 'My stock chart'
},
series: [{
data: [1, 2, 3]
}]
}
const App = () => <div>
<HighchartsReact
highcharts={Highcharts}
constructorType={'stockChart'}
options={options}
/>
</div>
render(<App />, document.getElementById('root'))
Highmaps chart
import React from 'react'
import { render } from 'react-dom'
import Highcharts from 'highcharts'
import HC_map from 'highcharts/modules/map'
import HighchartsReact from 'highcharts-react-official'
HC_map(Highcharts)
const options = {
title: {
text: 'My map chart'
},
series: [{
data: [1, 2, 3]
}]
}
const App = () => <div>
<HighchartsReact
highcharts={Highcharts}
constructorType={'mapChart'}
options={options}
/>
</div>
render(<App />, document.getElementById('root'))
Highcharts with TypeScript
import React from 'react';
import { render } from 'react-dom';
import * as Highcharts from 'highcharts';
import HighchartsReact from 'highcharts-react-official';
const options: Highcharts.Options = {
title: {
text: 'My chart'
},
series: [{
type: 'line',
data: [1, 2, 3]
}]
}
const App = () => <div>
<HighchartsReact
highcharts={Highcharts}
options={options}
/>
</div>
render(<App />, document.getElementById('root'));
Options details
Available options:
<HighchartsReact
options={options}
highcharts={Highcharts}
constructorType={'mapChart'}
allowChartUpdate={update}
updateArgs={[true, true, true]}
containerProps={{className: 'chartContainer'}}
callback={this.chartCallback}
/>
options
Highcharts chart configuration object. Please refer to the Highcharts API documentation. This option is required.
highcharts
Used to pass the Highcharts instance after modules are initialized.
If not set the component will try to get the Highcharts from window.
constructorType
String for constructor method, defaults to 'chart'
. Other official constructors are:
'stockChart'
for Highstock charts
'mapChart'
for Highmaps charts
If you have added a module or a plugin that adds new constructor then you can use it and set using this property.
allowChartUpdate
This wrapper uses chart.update()
method to apply new options to the chart when changing the parent component.
Option allowChartUpdate
allow to turn off the updating. This options is optional, defaults to true
.
updateArgs
Array of update()
's function optional arguments. Parameters should be defined in the same order like in native Highcharts function: [redraw, oneToOne, animation]
, in this wrapper defaults to [true, true, true]
. Here is a more specific description of the parameters. This option is optional.
containerProps
The props object passed to the chart container in React.createElement
method. Useful for adding styles or class.
callback
A callback function for the created chart. First argument for the function will hold the created chart
. Default this
in the function points to the chart
. This option is optional.
Example with custom chart component
Create custom component ./components/MyStockChart.jsx
:
import React from 'react'
import Highcharts from 'highcharts/highstock'
import HighchartsReact from 'highcharts-react-official'
const options = {
title: {
text: 'My stock chart'
},
series: [{
data: [1, 2, 3]
}]
}
const MyStockChart = () => <HighchartsReact
highcharts={Highcharts}
constructorType={'stockChart'}
options={options}
/>
export default MyStockChart
Render your custom chart component like below:
import React from 'react'
import { render } from 'react-dom'
import MyStockChart from './components/MyStockChart.jsx'
const App = () => <div>
<MyStockChart />
</div>
render(<App />, document.getElementById('root'))
Get repository
Clone github repository and install dependencies:
git clone https://github.com/highcharts/highcharts-react
cd highcharts-react
npm install
Examples and tests require Highcharts library, don't forget to:
npm install highcharts
Examples
There are several interesting examples in the demo folder that use all available constructors and several modules.
Bundle these with:
npm run build-demo
Demo is located under demo/index.html
Live example on codesandbox: https://codesandbox.io/s/rmjw8347po
Tests
This wrapper contains tests for: testing environment, chart rendering and passing down container props.
To run tests, type:
npm run test
FAQ
Where to look for help?
Technical support will help you with Highcharts and with the wrapper.
If you have a bug to report or an enhancement suggestion please submit Issues in this repository.
Why highcharts-react-official, and not highcharts-react, is used?
The NPM package is registered as highcharts-react-official
because highcharts-react
was already taken.
How to get the chart instance?
Use the React.createRef
method:
componentDidMount() {
this.chartRef = React.createRef();
}
render() {
return (
<HighchartsReact
highcharts={ Highcharts }
options={ options }
ref={ this.chartRef }
/>
);
}
or store it by the callback function:
constructor(props) {
super(props);
this.afterChartCreated = this.afterChartCreated.bind(this);
}
afterChartCreated(chart) {
this.internalChart = chart;
}
componentDidMount() {
this.internalChart.addSeries({ data: [1, 2, 3] })
}
render() {
return (
<div>
<h2>Highcharts</h2>
<HighchartsReact
highcharts={ Highcharts }
options={ options }
callback={ this.afterChartCreated }
/>
</div>
);
}
How to add a module?
To add a module, import and initialize it:
import Highcharts from 'highcharts'
import highchartsGantt from "highcharts/modules/gantt";
import HighchartsReact from 'highcharts-react-official'
highchartsGantt(Highcharts);
or use require
:
import Highcharts from 'highcharts'
import HighchartsReact from 'highcharts-react-official'
require("highcharts/modules/variwide")(Highcharts);