Top Related Projects
📊 React Component for ApexCharts
Redefined chart library built with React and D3
nivo provides a rich set of dataviz components, built on top of the awesome d3 and React libraries
A collection of composable React components for building interactive data visualizations
React components for Chart.js, the most popular charting library
Data Visualization Components
Quick Overview
Highcharts-react is an official React wrapper for the Highcharts charting library. It provides a seamless integration of Highcharts into React applications, allowing developers to create interactive and responsive charts with ease.
Pros
- Easy integration with React applications
- Supports all Highcharts modules and chart types
- Automatic chart updates when data or options change
- TypeScript support for improved development experience
Cons
- Requires a separate Highcharts license for commercial use
- Learning curve for developers new to Highcharts
- Limited customization options compared to using Highcharts directly
- Potential performance issues with large datasets or complex charts
Code Examples
- Basic line chart:
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] }]
};
const LineChart = () => (
<HighchartsReact highcharts={Highcharts} options={options} />
);
- Updating chart data:
import React, { useState } from 'react';
import Highcharts from 'highcharts';
import HighchartsReact from 'highcharts-react-official';
const DynamicChart = () => {
const [chartOptions, setChartOptions] = useState({
series: [{ data: [1, 2, 3] }]
});
const updateData = () => {
setChartOptions({
series: [{ data: [Math.random(), Math.random(), Math.random()] }]
});
};
return (
<>
<HighchartsReact highcharts={Highcharts} options={chartOptions} />
<button onClick={updateData}>Update Data</button>
</>
);
};
- Using Highcharts modules:
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' },
series: [{ data: [[1, 2, 3], [4, 5, 6], [7, 8, 9]] }]
};
const BubbleChart = () => (
<HighchartsReact highcharts={Highcharts} options={options} />
);
Getting Started
-
Install the required packages:
npm install highcharts highcharts-react-official
-
Import and use the HighchartsReact component in your React application:
import React from 'react'; import Highcharts from 'highcharts'; import HighchartsReact from 'highcharts-react-official'; const options = { title: { text: 'My First Chart' }, series: [{ data: [1, 2, 3, 4, 5] }] }; const App = () => ( <div> <h1>Highcharts React Demo</h1> <HighchartsReact highcharts={Highcharts} options={options} /> </div> ); export default App;
-
Render your React component as usual.
Competitor Comparisons
📊 React Component for ApexCharts
Pros of react-apexcharts
- Free and open-source, suitable for commercial projects without licensing fees
- Lightweight and performant, with a smaller bundle size
- Extensive customization options and interactive features out-of-the-box
Cons of react-apexcharts
- Less mature and established compared to Highcharts
- Smaller community and ecosystem, potentially leading to fewer resources and third-party extensions
- May lack some advanced features or chart types available in Highcharts
Code Comparison
react-apexcharts:
import Chart from 'react-apexcharts'
const App = () => (
<Chart
options={options}
series={series}
type="bar"
width={500}
height={320}
/>
)
highcharts-react:
import Highcharts from 'highcharts'
import HighchartsReact from 'highcharts-react-official'
const App = () => (
<HighchartsReact
highcharts={Highcharts}
options={options}
/>
)
Both libraries offer React wrappers for their respective charting solutions, with similar ease of use. The main differences lie in the underlying charting libraries' features, performance, and licensing models.
Redefined chart library built with React and D3
Pros of Recharts
- Free and open-source, suitable for commercial use without licensing fees
- Built specifically for React, offering seamless integration and performance
- Lightweight and fast, with a smaller bundle size
Cons of Recharts
- Less extensive documentation and community support
- Fewer chart types and customization options out of the box
- May require more custom coding for complex visualizations
Code Comparison
Recharts:
import { LineChart, Line, XAxis, YAxis, CartesianGrid, Tooltip, Legend } from 'recharts';
<LineChart width={600} height={300} data={data}>
<Line type="monotone" dataKey="value" stroke="#8884d8" />
<CartesianGrid stroke="#ccc" />
<XAxis dataKey="name" />
<YAxis />
</LineChart>
Highcharts React:
import Highcharts from 'highcharts';
import HighchartsReact from 'highcharts-react-official';
const options = {
series: [{ data: [1, 2, 3] }]
};
<HighchartsReact highcharts={Highcharts} options={options} />
Both libraries offer React-friendly chart components, but Recharts provides a more React-centric API with individual components for chart elements. Highcharts React uses a configuration object approach, which may be more familiar to those with experience in traditional Highcharts usage.
nivo provides a rich set of dataviz components, built on top of the awesome d3 and React libraries
Pros of nivo
- Free and open-source, suitable for commercial projects without licensing costs
- Highly customizable with a wide range of chart types and options
- Built with React and D3, offering smooth integration with React applications
Cons of nivo
- Less extensive documentation compared to Highcharts
- Smaller community and ecosystem, potentially leading to fewer resources and third-party extensions
- May require more setup and configuration for complex visualizations
Code Comparison
nivo example:
import { ResponsiveLine } from '@nivo/line'
const MyChart = ({ data }) => (
<ResponsiveLine
data={data}
margin={{ top: 50, right: 110, bottom: 50, left: 60 }}
xScale={{ type: 'point' }}
yScale={{ type: 'linear', min: 'auto', max: 'auto', stacked: true, reverse: false }}
/>
)
Highcharts React example:
import Highcharts from 'highcharts'
import HighchartsReact from 'highcharts-react-official'
const options = {
series: [{ data: [1, 2, 3] }]
}
const MyChart = () => <HighchartsReact highcharts={Highcharts} options={options} />
Both libraries offer React components for creating charts, but nivo's API is more React-centric, while Highcharts React wraps the traditional Highcharts library.
A collection of composable React components for building interactive data visualizations
Pros of Victory
- Fully open-source and free to use, unlike Highcharts which requires a license for commercial use
- Built specifically for React, offering a more React-centric API and integration
- Highly customizable with a modular architecture, allowing for fine-grained control over chart components
Cons of Victory
- Smaller community and ecosystem compared to Highcharts, potentially leading to fewer resources and third-party extensions
- Less comprehensive documentation and fewer examples than Highcharts
- May require more code to create complex charts, as it favors flexibility over pre-built solutions
Code Comparison
Victory:
import { VictoryBar, VictoryChart, VictoryAxis } from 'victory';
<VictoryChart>
<VictoryBar data={data} x="quarter" y="earnings" />
<VictoryAxis tickValues={[1, 2, 3, 4]} tickFormat={["Q1", "Q2", "Q3", "Q4"]} />
</VictoryChart>
Highcharts React:
import Highcharts from 'highcharts';
import HighchartsReact from 'highcharts-react-official';
const options = {
chart: { type: 'bar' },
series: [{ data: [1, 2, 3, 4] }],
xAxis: { categories: ['Q1', 'Q2', 'Q3', 'Q4'] }
};
<HighchartsReact highcharts={Highcharts} options={options} />
React components for Chart.js, the most popular charting library
Pros of react-chartjs-2
- Free and open-source, suitable for commercial projects without licensing costs
- Lightweight and fast, with a smaller bundle size
- Extensive community support and regular updates
Cons of react-chartjs-2
- Less feature-rich compared to Highcharts, with fewer chart types and customization options
- Limited built-in responsiveness and may require additional work for complex responsive designs
Code Comparison
react-chartjs-2:
import { Line } from 'react-chartjs-2';
const data = {
labels: ['January', 'February', 'March'],
datasets: [{ data: [65, 59, 80], label: 'Sales' }]
};
<Line data={data} />
highcharts-react:
import Highcharts from 'highcharts';
import HighchartsReact from 'highcharts-react-official';
const options = {
series: [{ data: [65, 59, 80], name: 'Sales' }]
};
<HighchartsReact highcharts={Highcharts} options={options} />
Both libraries offer React-specific wrappers for their respective charting libraries, making integration into React applications straightforward. react-chartjs-2 uses a more declarative approach with predefined chart components, while highcharts-react relies on a configuration object for chart setup.
Data Visualization Components
Pros of react-vis
- Free and open-source, suitable for commercial projects without licensing costs
- Lightweight and focused on React integration, potentially easier for React developers
- More customizable and flexible for creating unique visualizations
Cons of react-vis
- Less comprehensive documentation and smaller community compared to Highcharts
- Fewer out-of-the-box chart types and features
- Less frequent updates and maintenance
Code Comparison
react-vis:
import {XYPlot, LineSeries} from 'react-vis';
<XYPlot width={300} height={300}>
<LineSeries data={[{x: 1, y: 10}, {x: 2, y: 5}, {x: 3, y: 15}]} />
</XYPlot>
highcharts-react:
import Highcharts from 'highcharts';
import HighchartsReact from 'highcharts-react-official';
const options = {
series: [{data: [1, 2, 3]}]
};
<HighchartsReact highcharts={Highcharts} options={options} />
Both libraries offer React-specific implementations for creating charts, but react-vis uses a more declarative approach with individual components for chart elements, while highcharts-react relies on a configuration object passed to a single component.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
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:
npm install highcharts-react-official
If Highcharts is not already installed, get the package with Highcharts:
npm install highcharts highcharts-react-official
Using
Basic usage example
Import into your React project and render a 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'))
Highcharts with TypeScript
Live example: https://stackblitz.com/edit/react-starter-typescript-cfcznt
import React, { useRef } from 'react';
import * as Highcharts from 'highcharts';
import { HighchartsReact } from 'highcharts-react-official';
// The integration exports only a default component that at the same time is a
// namespace for the related Props interface (HighchartsReact.Props) and
// RefObject interface (HighchartsReact.RefObject). All other interfaces
// like Options come from the Highcharts module itself.
const options: Highcharts.Options = {
title: {
text: 'My chart'
},
series: [{
type: 'line',
data: [1, 2, 3]
}]
};
const App = (props: HighchartsReact.Props) => {
const chartComponentRef = useRef<HighchartsReact.RefObject>(null);
return (
<HighchartsReact
highcharts={Highcharts}
options={options}
ref={chartComponentRef}
{...props}
/>
);
};
// Render your App component into the #root element of the document.
ReactDOM.render(<App />, document.getElementById('root'));
Since version 3.2.1 it is also possible to import types for props
and ref
independently:
import 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:
import React from 'react'
import Highcharts from 'highcharts'
import HighchartsExporting from 'highcharts/modules/exporting'
import HighchartsReact from 'highcharts-react-official'
if (typeof Highcharts === 'object') {
HighchartsExporting(Highcharts)
}
...
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
import React, { Component } from 'react';
import { render } from 'react-dom';
import HighchartsReact from 'highcharts-react-official';
import Highcharts from 'highcharts';
class LineChart extends Component {
constructor(props) {
super(props);
this.state = {
// To avoid unnecessary update keep all options in the state.
chartOptions: {
xAxis: {
categories: ['A', 'B', 'C'],
},
series: [
{ data: [1, 2, 3] }
],
plotOptions: {
series: {
point: {
events: {
mouseOver: this.setHoverData.bind(this)
}
}
}
}
},
hoverData: null
};
}
setHoverData = (e) => {
// The chart is not updated because `chartOptions` has not changed.
this.setState({ hoverData: e.target.category })
}
updateSeries = () => {
// The chart is updated only with new options.
this.setState({
chartOptions: {
series: [
{ data: [Math.random() * 5, 2, 1]}
]
}
});
}
render() {
const { chartOptions, hoverData } = this.state;
return (
<div>
<HighchartsReact
highcharts={Highcharts}
options={chartOptions}
/>
<h3>Hovering over {hoverData}</h3>
<button onClick={this.updateSeries.bind(this)}>Update Series</button>
</div>
)
}
}
render(<LineChart />, document.getElementById('root'));
Options details
Available options with example values:
<HighchartsReact
options = { this.state.chartOptions }
highcharts = { Highcharts }
constructorType = { 'mapChart' }
allowChartUpdate = { true }
immutable = { false }
updateArgs = { [true, true, true] }
containerProps = {{ className: 'chartContainer' }}
callback = { this.chartCallback }
/>
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
:
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: 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:
npm 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
:
constructor(props) {
super(props)
this.chartRef = React.createRef();
}
render() {
return (
<HighchartsReact
highcharts={ Highcharts }
options={ options }
ref={ this.chartRef }
/>
);
}
For functional components and version 3.0.0 and later use useRef
hook:
const chartComponent = useRef(null);
const [options] = useState({...});
useEffect(() => {
const chart = chartComponent.current.chart;
...
}, []);
return <HighchartsReact ref={chartComponent} highcharts={Highcharts} options={options} />;
Alternatively store a chart reference in a callback function:
afterChartCreated = (chart) => {
// Highcharts creates a separate chart instance during export
if (!chart.options.chart.forExport) {
this.internalChart = chart;
}
}
componentDidMount() {
// example of use
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'
// init the module
highchartsGantt(Highcharts);
alternative with require
:
import Highcharts from 'highcharts'
import HighchartsReact from 'highcharts-react-official'
require("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
Top Related Projects
📊 React Component for ApexCharts
Redefined chart library built with React and D3
nivo provides a rich set of dataviz components, built on top of the awesome d3 and React libraries
A collection of composable React components for building interactive data visualizations
React components for Chart.js, the most popular charting library
Data Visualization Components
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot