The official Highcharts supported wrapper for React
Installations
npm install highcharts-react-official
Developer
highcharts
Developer Guide
Module System
CommonJS
Min. Node Version
Typescript Support
Yes
Node Version
16.14.2
NPM Version
8.5.0
Statistics
1,080 Stars
164 Commits
108 Forks
24 Watching
9 Branches
58 Contributors
Updated on 22 Nov 2024
Languages
JavaScript (100%)
Total Downloads
Cumulative downloads
Total Downloads
77,961,283
Last day
-4%
110,903
Compared to previous day
Last week
3%
593,745
Compared to previous week
Last month
9%
2,494,856
Compared to previous month
Last year
33.8%
24,823,954
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
Highcharts React
Official minimal Highcharts integration for React.
Table of Contents
- Getting started
- Options details
- Example with custom chart component
- Get repository
- Examples
- Tests
- Changelog
- FAQ
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
This integration also requires highcharts and react packages with the following versions installed in your project:
For version 2.x.x :
react
16.4+highcharts
5.0.0+
For version 3.x.x :
react
16.8+highcharts
6.0.0+
Installing
Get the package from NPM in your React app:
1npm install highcharts-react-official
If Highcharts is not already installed, get the package with Highcharts:
1npm install highcharts highcharts-react-official
Using
Basic usage example
Import into your React project and render a chart:
1import React from 'react' 2import { render } from 'react-dom' 3import Highcharts from 'highcharts' 4import HighchartsReact from 'highcharts-react-official' 5 6const options = { 7 title: { 8 text: 'My chart' 9 }, 10 series: [{ 11 data: [1, 2, 3] 12 }] 13} 14 15const App = () => <div> 16 <HighchartsReact 17 highcharts={Highcharts} 18 options={options} 19 /> 20</div> 21 22render(<App />, document.getElementById('root'))
Highcharts with TypeScript
Live example: https://stackblitz.com/edit/react-starter-typescript-cfcznt
1import React, { useRef } from 'react'; 2import * as Highcharts from 'highcharts'; 3import { HighchartsReact } from 'highcharts-react-official'; 4 5// The integration exports only a default component that at the same time is a 6// namespace for the related Props interface (HighchartsReact.Props) and 7// RefObject interface (HighchartsReact.RefObject). All other interfaces 8// like Options come from the Highcharts module itself. 9 10const options: Highcharts.Options = { 11 title: { 12 text: 'My chart' 13 }, 14 series: [{ 15 type: 'line', 16 data: [1, 2, 3] 17 }] 18}; 19 20const App = (props: HighchartsReact.Props) => { 21 const chartComponentRef = useRef<HighchartsReact.RefObject>(null); 22 23 return ( 24 <HighchartsReact 25 highcharts={Highcharts} 26 options={options} 27 ref={chartComponentRef} 28 {...props} 29 /> 30 ); 31}; 32// Render your App component into the #root element of the document. 33ReactDOM.render(<App />, document.getElementById('root'));
Since version 3.2.1 it is also possible to import types for props
and ref
independently:
1import HighchartsReact, { HighchartsReactRefObject, HighchartsReactProps } from 'highcharts-react-official';
Highcharts with NextJS
Next.js executes code twice - on server-side and then client-side. First run is done in an environment that lacks window
and causes Highcharts to be loaded, but not initialized. Easy fix is to place all modules inits in a if
checking if Highcharts is an object or a function. It should be an object for modules initialization to work without any errors, so code like below is an easy fix:
1import React from 'react' 2import Highcharts from 'highcharts' 3import HighchartsExporting from 'highcharts/modules/exporting' 4import HighchartsReact from 'highcharts-react-official' 5 6if (typeof Highcharts === 'object') { 7 HighchartsExporting(Highcharts) 8} 9 10...
This is a know issue with NextJS and is covered here: https://github.com/vercel/next.js/issues/5354
Optimal way to update
A good practice is to keep all chart options in the state. When setState
is called, the options are overwritten and only the new ones are passed to the chart.update
method.
Live example: https://stackblitz.com/edit/react-hketvd?file=index.js
Optimal way to update with React Hooks: https://stackblitz.com/edit/react-nwseym?file=index.js
1import React, { Component } from 'react'; 2import { render } from 'react-dom'; 3import HighchartsReact from 'highcharts-react-official'; 4import Highcharts from 'highcharts'; 5 6class LineChart extends Component { 7 constructor(props) { 8 super(props); 9 10 this.state = { 11 // To avoid unnecessary update keep all options in the state. 12 chartOptions: { 13 xAxis: { 14 categories: ['A', 'B', 'C'], 15 }, 16 series: [ 17 { data: [1, 2, 3] } 18 ], 19 plotOptions: { 20 series: { 21 point: { 22 events: { 23 mouseOver: this.setHoverData.bind(this) 24 } 25 } 26 } 27 } 28 }, 29 hoverData: null 30 }; 31 } 32 33 setHoverData = (e) => { 34 // The chart is not updated because `chartOptions` has not changed. 35 this.setState({ hoverData: e.target.category }) 36 } 37 38 updateSeries = () => { 39 // The chart is updated only with new options. 40 this.setState({ 41 chartOptions: { 42 series: [ 43 { data: [Math.random() * 5, 2, 1]} 44 ] 45 } 46 }); 47 } 48 49 render() { 50 const { chartOptions, hoverData } = this.state; 51 52 return ( 53 <div> 54 <HighchartsReact 55 highcharts={Highcharts} 56 options={chartOptions} 57 /> 58 <h3>Hovering over {hoverData}</h3> 59 <button onClick={this.updateSeries.bind(this)}>Update Series</button> 60 </div> 61 ) 62 } 63} 64 65render(<LineChart />, document.getElementById('root'));
Options details
Available options with example values:
1 <HighchartsReact 2 options = { this.state.chartOptions } 3 highcharts = { Highcharts } 4 constructorType = { 'mapChart' } 5 allowChartUpdate = { true } 6 immutable = { false } 7 updateArgs = { [true, true, true] } 8 containerProps = {{ className: 'chartContainer' }} 9 callback = { this.chartCallback } 10 />
Parameter | Type | Required | Defaults | Description |
---|---|---|---|---|
options | Object | yes | - | Highcharts chart configuration object. Please refer to the Highcharts API documentation. |
highcharts | Object | yes | - | 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 | no | 'chart' | String for constructor method. Official constructors: - 'chart' for Highcharts charts - 'stockChart' for Highstock charts - 'mapChart' for Highmaps charts - 'ganttChart' for Gantt charts |
allowChartUpdate | Boolean | no | true | This integration uses chart.update() method to apply new options to the chart when changing the parent component. This option allow to turn off the updating. |
immutable | Boolean | no | false | Reinitialises the chart on prop update (as oppose to chart.update() ) - useful in some cases but slower than a regular update. |
updateArgs | Array | no | [true, true, true] | Array of update() 's function optional arguments. Parameters should be defined in the same order like in native Highcharts function: [redraw, oneToOne, animation] . Here is a more specific description of the parameters. |
containerProps | Object | no | - | The props object passed to the chart container in React.createElement method. Useful for adding styles or class. |
callback | Function | no | - | 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
:
1import React from 'react' 2import Highcharts from 'highcharts/highstock' 3import HighchartsReact from 'highcharts-react-official' 4 5const options = { 6 title: { 7 text: 'My stock chart' 8 }, 9 series: [{ 10 data: [1, 2, 3] 11 }] 12} 13 14const MyStockChart = () => <HighchartsReact 15 highcharts={Highcharts} 16 constructorType={'stockChart'} 17 options={options} 18/> 19 20export default MyStockChart
Render your custom chart component like below:
1import React from 'react' 2import { render } from 'react-dom' 3import MyStockChart from './components/MyStockChart.jsx' 4 5const App = () => <div> 6 <MyStockChart /> 7</div> 8 9render(<App />, document.getElementById('root'))
Get repository
Clone github repository and install dependencies:
1git clone https://github.com/highcharts/highcharts-react 2cd highcharts-react 3npm install
Examples and tests require Highcharts library, don't forget to:
1npm install highcharts
Examples
There are several interesting examples in the demo folder that use all available constructors and several modules.
Bundle these with:
1npm run build-demo
Demo is located under demo/index.html
Live example: https://stackblitz.com/edit/react-4ded5d?file=index.js
Tests
This integration contains tests for: testing environment, chart rendering and passing down container props. To run tests, type:
1npm run test
Changelog
The changelog is available here.
FAQ
Where to look for help?
Technical support will help you with Highcharts and with the integration.
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 a chart instance?
For class components and version prior to 3.0.0 use React.createRef
:
1constructor(props) { 2 super(props) 3 this.chartRef = React.createRef(); 4} 5 6render() { 7 return ( 8 <HighchartsReact 9 highcharts={ Highcharts } 10 options={ options } 11 ref={ this.chartRef } 12 /> 13 ); 14}
For functional components and version 3.0.0 and later use useRef
hook:
1 const chartComponent = useRef(null); 2 const [options] = useState({...}); 3 4 useEffect(() => { 5 const chart = chartComponent.current.chart; 6 ... 7 }, []); 8 9 return <HighchartsReact ref={chartComponent} highcharts={Highcharts} options={options} />;
Alternatively store a chart reference in a callback function:
1afterChartCreated = (chart) => { 2 // Highcharts creates a separate chart instance during export 3 if (!chart.options.chart.forExport) { 4 this.internalChart = chart; 5 } 6} 7 8componentDidMount() { 9 // example of use 10 this.internalChart.addSeries({ data: [1, 2, 3] }) 11} 12 13render() { 14 return ( 15 <div> 16 <h2>Highcharts</h2> 17 <HighchartsReact 18 highcharts={ Highcharts } 19 options={ options } 20 callback={ this.afterChartCreated } 21 /> 22 </div> 23 ); 24}
How to add a module?
To add a module, import and initialize it:
1import Highcharts from 'highcharts' 2import highchartsGantt from "highcharts/modules/gantt"; 3import HighchartsReact from 'highcharts-react-official' 4 5// init the module 6highchartsGantt(Highcharts);
alternative with require
:
1import Highcharts from 'highcharts' 2import HighchartsReact from 'highcharts-react-official' 3 4require("highcharts/modules/variwide")(Highcharts);
How to add React component to a chart's element?
By using Portals it is possible to add a component to every HTML chart element.
Live example: https://codesandbox.io/s/1o5y7r31k3
Why Highcharts mutates my data?
It can be confusing, since React props are read-only, but Highcharts for performance reasons mutates the original data array. This behaviour is NOT changed by our integration. You need to pass a copy of your data to the integration if you want to prevent mutations.
Issue: https://github.com/highcharts/highcharts-react/issues/326
More discussion here: https://github.com/highcharts/highcharts/issues/4259
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
0 existing vulnerabilities detected
Reason
license file detected
Details
- Info: project has a license file: LICENSE:0
- Warn: project license file does not contain an FSF or OSI license.
Reason
Found 5/21 approved changesets -- score normalized to 2
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
- Warn: no security policy file detected
- Warn: no security file to analyze
- Warn: no security file to analyze
- Warn: no security file to analyze
Reason
project is not fuzzed
Details
- Warn: no fuzzer integrations found
Reason
branch protection not enabled on development/release branches
Details
- Warn: branch protection not enabled for branch 'master'
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
- Warn: 0 commits out of 16 are checked with a SAST tool
Score
3.3
/10
Last Scanned on 2024-11-18
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More