├── .eslintrc.js
├── .gitignore
├── CHANGELOG.md
├── LICENSE
├── README.md
├── babel.config.js
├── demo
├── components
│ ├── Chart.jsx
│ ├── Container.jsx
│ ├── Map.jsx
│ └── Stock.jsx
├── data
│ └── mapData.js
├── demo.jsx
└── index.html
├── dist
├── highcharts-react.d.ts
├── highcharts-react.js
├── highcharts-react.js.map
├── highcharts-react.min.d.ts
├── highcharts-react.min.js
└── highcharts-react.min.js.map
├── index.js
├── package.json
├── src
├── HighchartsReact.d.ts
└── HighchartsReact.js
├── tests
├── ParentComponent.jsx
├── __tests__
│ ├── component
│ │ ├── addModule.test.js
│ │ ├── chartReference.test.js
│ │ ├── componentWithoutProps.test.js
│ │ ├── dynamics.test.js
│ │ ├── importComponent.test.js
│ │ └── multipleCharts.test.js
│ └── props
│ │ ├── allowChartUpdate.test.js
│ │ ├── callback.test.js
│ │ ├── constructorType.test.js
│ │ ├── containerProps.test.js
│ │ ├── highcharts.test.js
│ │ ├── immutable.test.js
│ │ └── updateArgs.test.js
└── setupTests.js
└── webpack.config.js
/.eslintrc.js:
--------------------------------------------------------------------------------
1 | module.exports = {
2 | "env": {
3 | "browser": true,
4 | "jest": true,
5 | "node": true,
6 | "es6": true
7 | },
8 | "globals": {
9 | "Atomics": "readonly",
10 | "SharedArrayBuffer": "readonly"
11 | },
12 | "parserOptions": {
13 | "ecmaFeatures": {
14 | "jsx": true
15 | },
16 | "ecmaVersion": 2018,
17 | "sourceType": "module"
18 | },
19 | "plugins": [
20 | "react"
21 | ],
22 | "extends": [
23 | "eslint:recommended",
24 | "plugin:react/recommended"
25 | ],
26 | "rules": {
27 | "indent": ["error", 2],
28 | "camelcase": [2, {"properties": "always"}],
29 | "comma-dangle": [2, "never"],
30 | "dot-location": [2, "property"],
31 | "lines-around-comment": 0,
32 | "newline-after-var": 0,
33 | "no-alert": 2,
34 | "no-debugger": 2,
35 | "no-else-return": 2,
36 | "no-nested-ternary": 0,
37 | "no-param-reassign": 0,
38 | "no-restricted-properties": 0,
39 | "no-unmodified-loop-condition": 0,
40 | "no-useless-escape": 0,
41 | "no-var": 2,
42 | "object-curly-spacing": [2, "always"],
43 | "operator-assignment": 0,
44 | "operator-linebreak": [2, "after"],
45 | "padding-line-between-statements": 0,
46 | "prefer-arrow-callback": 0,
47 | "prefer-spread": 0,
48 | "react/jsx-curly-spacing": [2, "always"],
49 | "react/prop-types": 0,
50 | "semi": [2, "always"],
51 | "space-before-function-paren": [2, {"anonymous": "always", "named": "never"}],
52 | "strict": 0,
53 | "valid-jsdoc": ["error", { prefer: { "returns": "return"}}],
54 | "quotes": [2, "single"]
55 | }
56 | };
57 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | node_modules
2 | demo/bundle.js
3 | package-lock.json
4 | yarn.lock
5 | .source.*
6 | .DS_Store
7 |
--------------------------------------------------------------------------------
/CHANGELOG.md:
--------------------------------------------------------------------------------
1 | # Changelog
2 |
3 | All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.
4 |
5 | ### [3.2.1](https://github.com/highcharts/highcharts-react/compare/v3.0.0...v3.2.1) (2023-08-31)
6 |
7 | ### Features
8 |
9 | * adapted TS definitions for version 5.2.2 ([c24b667](https://github.com/highcharts/highcharts-react/commit/c24b667))
10 | * exported HighchartsReactProps HighchartsReactRefObject types ([774c47b](https://github.com/highcharts/highcharts-react/commit/774c47b))
11 |
12 | # [3.2.0](https://github.com/highcharts/highcharts-react/compare/v3.1.0...v3.2.0) (2023-03-01)
13 |
14 | ### Features
15 |
16 | * restricted chart update by adding props: `options`, `allowChartUpdate`, `updateArgs`, `containerProps`, `highcharts`, `constructorType` to useEffect dependency array ([30a9e1f](https://github.com/highcharts/highcharts-react/commit/30a9e1f))
17 | * updated Highcharts with TypeScript section in README ([7c32a50](https://github.com/highcharts/highcharts-react/commit/7c32a50))
18 | * added HighchartsReact named export ([ceadaf1](https://github.com/highcharts/highcharts-react/commit/ceadaf1))
19 | * fixed build-demo script ([d9a82b7](https://github.com/highcharts/highcharts-react/commit/d9a82b7))
20 | * fixed #366 - recreate a chart on constructorType or highcharts props change ([30a9e1f](https://github.com/highcharts/highcharts-react/commit/30a9e1f))
21 |
22 | # [3.1.0](https://github.com/highcharts/highcharts-react/compare/v3.0.0...v3.1.0) (2021-10-14)
23 |
24 | ### Features
25 |
26 | * increase Highcharts dev dependency to >=9.0.0 ([f7f9efc](https://github.com/highcharts/highcharts-react/commit/f7f9efc))
27 | * updated docs ([589c096](https://github.com/highcharts/highcharts-react/commit/589c096))
28 | * removed Highcharts import in HighchartsReact component ([8344597](https://github.com/highcharts/highcharts-react/commit/8344597))
29 | * improved component types([746ccb8](https://github.com/highcharts/highcharts-react/commit/746ccb8))
30 |
31 |
32 | # [3.0.0](https://github.com/highcharts/highcharts-react/compare/v2.2.2...v3.0.0) (2020-02-17)
33 |
34 |
35 | ### Features
36 |
37 | * increase React peer dependency to v16.8.0 ([030ac4e](https://github.com/highcharts/highcharts-react/commit/030ac4e))
38 | * refactor component with React Hooks ([c8b1e55](https://github.com/highcharts/highcharts-react/commit/c8b1e55))
39 | * refactor tests ([634a7e7](https://github.com/highcharts/highcharts-react/commit/25f5ecb))
40 |
41 |
42 | ### BREAKING CHANGES
43 |
44 | * To upgrade, run `yarn add react@^16.8.0 react-dom@^16.8.0` or `npm install --save react@^16.8.0 react-dom@^16.8.0`.
45 | If you're using `react-test-renderer`, you'll need to bump it to `^16.8.0` as well.
46 |
47 |
48 |
49 |
50 | ## [2.2.2](https://github.com/highcharts/highcharts-react/compare/v2.2.1...v2.2.2) (2019-06-07)
51 |
52 |
53 | ### Bug Fixes
54 |
55 | * Changed the way of importing Highcharts package. ([e7c314b](https://github.com/highcharts/highcharts-react/commit/e7c314b))
56 |
57 |
58 | ### Features
59 |
60 | * **husky:** Husky added. ([1d8234a](https://github.com/highcharts/highcharts-react/commit/1d8234a))
61 |
62 |
63 |
64 |
65 | ## [2.2.1](https://github.com/highcharts/highcharts-react/compare/v2.2.0...v2.2.1) (2019-06-07)
66 |
67 |
68 | ### Bug Fixes
69 |
70 | * **webpack:** Externals extended. Closes [#132](https://github.com/highcharts/highcharts-react/issues/132) ([193b3d2](https://github.com/highcharts/highcharts-react/commit/193b3d2))
71 |
72 |
73 |
74 |
75 | # [2.2.0](https://github.com/highcharts/highcharts-react/compare/v2.1.3...v2.2.0) (2019-06-07)
76 |
77 |
78 | ### Bug Fixes
79 |
80 | * Fixed externals [#114](https://github.com/highcharts/highcharts-react/issues/114). ([ee27ed0](https://github.com/highcharts/highcharts-react/commit/ee27ed0))
81 |
82 |
83 | ### Features
84 |
85 | * **debugger:** Added warning messages. ([3b3ff83](https://github.com/highcharts/highcharts-react/commit/3b3ff83))
86 | * **immutable:** Support for immutable prop - rerendering entire chart on componentDidUpdate. ([6464894](https://github.com/highcharts/highcharts-react/commit/6464894))
87 |
88 |
89 |
90 |
91 | ## [2.1.3](https://github.com/highcharts/highcharts-react/compare/v2.1.2...v2.1.3) (2019-03-27)
92 |
93 |
94 | ## Features
95 |
96 | * Fixed [#106](https://github.com/highcharts/highcharts-react/issues/106) - incorrect typescript typing ([4ffad38](https://github.com/highcharts/highcharts-react/commit/4ffad38))
97 |
98 |
99 |
100 |
101 | ## [2.1.2](https://github.com/highcharts/highcharts-react/compare/v2.1.1...v2.1.2) (2019-03-20)
102 |
103 |
104 | ## Features
105 |
106 | * Fixed [#102](https://github.com/highcharts/highcharts-react/issues/102) - tsconfig incompatibility ([4e27d0d](https://github.com/highcharts/highcharts-react/commit/4e27d0d))
107 | * Corrections in the documentation.
108 |
109 |
110 |
111 |
112 | ## [2.1.1](https://github.com/highcharts/highcharts-react/compare/v2.1.0...v2.1.1) (2019-03-07)
113 |
114 |
115 | ## Features
116 |
117 | * Fixed [#20](https://github.com/highcharts/highcharts-react/issues/20) - invalid indexer type ([1345d32](https://github.com/highcharts/highcharts-react/commit/1345d32))
118 |
119 |
120 |
121 |
122 | # [2.1.0](https://github.com/highcharts/highcharts-react/compare/v2.0.0...v2.1.0) (2019-03-07)
123 |
124 |
125 | ### Features
126 |
127 | * Added `containerProps` option and corrections to the docs. ([f14a969](https://github.com/highcharts/highcharts-react/commit/f14a969))
128 | * Added TypeScript declarations. ([b64eb01](https://github.com/highcharts/highcharts-react/commit/b64eb01))
129 |
130 |
131 |
132 |
133 | # [2.0.0](https://github.com/highcharts/highcharts-react/compare/v1.5.1...v2.0.0) (2018-11-08)
134 |
135 |
136 | ### Features
137 |
138 | * **webpack-and-sourcemap-added:** Code compiled to ES5, added sourcemaps. ([cb5083a](https://github.com/highcharts/highcharts-react/commit/cb5083a))
139 | * Updated for React v16 ([20c7ef9](https://github.com/highcharts/highcharts-react/commit/20c7ef9))
140 |
141 |
142 |
143 |
144 | ## [1.4.1](https://github.com/highcharts/highcharts-react/compare/v1.4.0...v1.4.1) (2018-10-26)
145 |
146 |
147 |
148 |
149 | # [1.4.0](https://github.com/highcharts/highcharts-react/compare/v1.3.2...v1.4.0) (2018-08-24)
150 |
151 |
152 | ### Features
153 |
154 | * Added changelog. ([097ca9a](https://github.com/highcharts/highcharts-react/commit/097ca9a))
155 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | Highcharts React
2 |
3 | Copyright (c) 2016-2017, Highsoft
4 |
5 | The software in Highcharts React repository is free and open source,
6 | but as highcharts-react relies on Highcharts.js, it requires a valid
7 | Highcharts license for commercial use.
8 |
9 | Permission is hereby granted, free of charge, to any person obtaining
10 | a copy of this software and associated documentation files (the
11 | "Software"), to deal in the Software without restriction, including
12 | without limitation the rights to use, copy, modify, merge, publish,
13 | distribute, sublicense, and/or sell copies of the Software, and to
14 | permit persons to whom the Software is furnished to do so, subject to
15 | the following conditions:
16 |
17 | The above copyright notice and this permission notice shall be
18 | included in all copies or substantial portions of the Software.
19 |
20 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
23 | IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
24 | CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
25 | TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
26 | SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # Highcharts React
2 | Official minimal [Highcharts](https://www.highcharts.com/) integration for React.
3 |
4 | **Note: the beta of the upcoming v4 of the Highcharts React integration is
5 | now available at [`@highcharts/react@next`](https://www.npmjs.com/package/@highcharts/react/)**
6 |
7 | ## Table of Contents
8 | 1. [Getting started](#getting-started)
9 | 1. [General prerequisites](#general-prerequisites)
10 | 2. [Installing](#installing)
11 | 3. [Using](#using)
12 | 1. [Basic usage example](#basic-usage-example)
13 | 2. [Highcharts with NextJS](#highcharts-with-nextjs)
14 | 3. [Highcharts with TypeScript](#highcharts-with-typescript)
15 | 4. [Optimal way to update](#optimal-way-to-update)
16 | 2. [Options details](#options-details)
17 | 3. [Example with custom chart component](#example-with-custom-chart-component)
18 | 4. [Get repository](#get-repository)
19 | 5. [Examples](#examples)
20 | 6. [Tests](#tests)
21 | 7. [Changelog](#changelog)
22 | 8. [FAQ](#faq)
23 | 1. [Where to look for help?](#where-to-look-for-help)
24 | 2. [Why highcharts-react-official and not highcharts-react is used?](#why-highcharts-react-official-and-not-highcharts-react-is-used)
25 | 3. [How to get a chart instance?](#how-to-get-a-chart-instance)
26 | 4. [How to add a module?](#how-to-add-a-module)
27 | 5. [How to add React component to a chart's element?](#how-to-add-react-component-to-a-charts-element)
28 | 6. [Why Highcharts mutates my data?](#why-highcharts-mutates-my-data)
29 |
30 | ## Getting Started
31 |
32 | ### General prerequisites
33 |
34 | Make sure you have **node**, **NPM** and **React** up to date.
35 | Tested and required versions:
36 |
37 | * `node` 8.11.3+
38 | * `npm` 6.4.1+ or similar package manager
39 |
40 | This integration also requires highcharts and react packages with the following versions installed in your project:
41 |
42 | For version **2.x.x** :
43 |
44 | * `react` 16.4+
45 | * `highcharts` 5.0.0+
46 |
47 | For version **3.x.x** :
48 |
49 | * `react` 16.8+
50 | * `highcharts` 6.0.0+
51 |
52 | ### Installing
53 |
54 | Get the package from NPM in your React app:
55 |
56 | ```bash
57 | npm install highcharts-react-official
58 | ```
59 |
60 | If Highcharts is not already installed, get the package with Highcharts:
61 |
62 | ```bash
63 | npm install highcharts highcharts-react-official
64 | ```
65 |
66 | ### Using
67 |
68 | #### Basic usage example
69 |
70 | Import into your React project and render a chart:
71 |
72 | ```jsx
73 | import React from 'react'
74 | import { render } from 'react-dom'
75 | import Highcharts from 'highcharts'
76 | import HighchartsReact from 'highcharts-react-official'
77 |
78 | const options = {
79 | title: {
80 | text: 'My chart'
81 | },
82 | series: [{
83 | data: [1, 2, 3]
84 | }]
85 | }
86 |
87 | const App = () =>
88 |
92 |
93 |
94 | render(, document.getElementById('root'))
95 | ```
96 |
97 | ### Highcharts with TypeScript
98 |
99 | Live example: https://stackblitz.com/edit/react-starter-typescript-cfcznt
100 |
101 | ```tsx
102 | import React, { useRef } from 'react';
103 | import * as Highcharts from 'highcharts';
104 | import { HighchartsReact } from 'highcharts-react-official';
105 |
106 | // The integration exports only a default component that at the same time is a
107 | // namespace for the related Props interface (HighchartsReact.Props) and
108 | // RefObject interface (HighchartsReact.RefObject). All other interfaces
109 | // like Options come from the Highcharts module itself.
110 |
111 | const options: Highcharts.Options = {
112 | title: {
113 | text: 'My chart'
114 | },
115 | series: [{
116 | type: 'line',
117 | data: [1, 2, 3]
118 | }]
119 | };
120 |
121 | const App = (props: HighchartsReact.Props) => {
122 | const chartComponentRef = useRef(null);
123 |
124 | return (
125 |
131 | );
132 | };
133 | // Render your App component into the #root element of the document.
134 | ReactDOM.render(, document.getElementById('root'));
135 | ```
136 |
137 | Since version 3.2.1 it is also possible to import types for `props` and `ref` independently:
138 |
139 | ```tsx
140 | import HighchartsReact, { HighchartsReactRefObject, HighchartsReactProps } from 'highcharts-react-official';
141 | ```
142 |
143 | ### Highcharts with NextJS
144 |
145 | 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:
146 |
147 | ```jsx
148 | import React from 'react'
149 | import Highcharts from 'highcharts'
150 | import HighchartsExporting from 'highcharts/modules/exporting'
151 | import HighchartsReact from 'highcharts-react-official'
152 |
153 | if (typeof Highcharts === 'object') {
154 | HighchartsExporting(Highcharts)
155 | }
156 |
157 | ...
158 | ```
159 |
160 | This is a know issue with NextJS and is covered here: https://github.com/vercel/next.js/issues/5354
161 |
162 | ### Optimal way to update
163 |
164 | 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.
165 |
166 | **Live example:** https://stackblitz.com/edit/react-hketvd?file=index.js
167 |
168 | **Optimal way to update with React Hooks:** https://stackblitz.com/edit/react-nwseym?file=index.js
169 |
170 | ```jsx
171 | import React, { Component } from 'react';
172 | import { render } from 'react-dom';
173 | import HighchartsReact from 'highcharts-react-official';
174 | import Highcharts from 'highcharts';
175 |
176 | class LineChart extends Component {
177 | constructor(props) {
178 | super(props);
179 |
180 | this.state = {
181 | // To avoid unnecessary update keep all options in the state.
182 | chartOptions: {
183 | xAxis: {
184 | categories: ['A', 'B', 'C'],
185 | },
186 | series: [
187 | { data: [1, 2, 3] }
188 | ],
189 | plotOptions: {
190 | series: {
191 | point: {
192 | events: {
193 | mouseOver: this.setHoverData.bind(this)
194 | }
195 | }
196 | }
197 | }
198 | },
199 | hoverData: null
200 | };
201 | }
202 |
203 | setHoverData = (e) => {
204 | // The chart is not updated because `chartOptions` has not changed.
205 | this.setState({ hoverData: e.target.category })
206 | }
207 |
208 | updateSeries = () => {
209 | // The chart is updated only with new options.
210 | this.setState({
211 | chartOptions: {
212 | series: [
213 | { data: [Math.random() * 5, 2, 1]}
214 | ]
215 | }
216 | });
217 | }
218 |
219 | render() {
220 | const { chartOptions, hoverData } = this.state;
221 |
222 | return (
223 |
224 |
228 |
Hovering over {hoverData}
229 |
230 |
231 | )
232 | }
233 | }
234 |
235 | render(, document.getElementById('root'));
236 | ```
237 |
238 | ## Options details
239 |
240 | Available options with example values:
241 |
242 | ```jsx
243 |
253 | ```
254 |
255 | | Parameter | Type | Required | Defaults | Description |
256 | | --------- | :----: | :--------: | :--------: | ----------- |
257 | | `options` | Object | yes | - | Highcharts chart configuration object. Please refer to the Highcharts [API documentation](https://api.highcharts.com/highcharts/). |
258 | | `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. |
259 | | `constructorType` | String | no | `'chart'` | String for [constructor method](https://www.highcharts.com/docs/getting-started/your-first-chart). Official constructors: - `'chart'` for Highcharts charts - `'stockChart'` for Highstock charts - `'mapChart'` for Highmaps charts - `'ganttChart'` for Gantt charts|
260 | | `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. |
261 | | `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.|
262 | | `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](https://api.highcharts.com/class-reference/Highcharts.Chart#update) is a more specific description of the parameters.|
263 | | `containerProps` | Object | no | - | The props object passed to the chart container in `React.createElement` method. Useful for adding styles or class.|
264 | | `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. |
265 |
266 |
267 | ## Example with custom chart component
268 |
269 | Create custom component `./components/MyStockChart.jsx`:
270 |
271 | ```jsx
272 | import React from 'react'
273 | import Highcharts from 'highcharts/highstock'
274 | import HighchartsReact from 'highcharts-react-official'
275 |
276 | const options = {
277 | title: {
278 | text: 'My stock chart'
279 | },
280 | series: [{
281 | data: [1, 2, 3]
282 | }]
283 | }
284 |
285 | const MyStockChart = () =>
290 |
291 | export default MyStockChart
292 | ```
293 |
294 | Render your custom chart component like below:
295 |
296 | ```jsx
297 | import React from 'react'
298 | import { render } from 'react-dom'
299 | import MyStockChart from './components/MyStockChart.jsx'
300 |
301 | const App = () =>
302 |
303 |
304 |
305 | render(, document.getElementById('root'))
306 | ```
307 |
308 | ## Get repository
309 |
310 | Clone github repository and install dependencies:
311 |
312 | ```bash
313 | git clone https://github.com/highcharts/highcharts-react
314 | cd highcharts-react
315 | npm install
316 | ```
317 |
318 | Examples and tests require Highcharts library, don't forget to:
319 |
320 | ```bash
321 | npm install highcharts
322 | ```
323 |
324 | ## Examples
325 |
326 | There are several interesting examples in the demo folder that use all available constructors and several modules.
327 |
328 | Bundle these with:
329 |
330 | ```bash
331 | npm run build-demo
332 | ```
333 |
334 | Demo is located under demo/index.html
335 |
336 | Live example: https://stackblitz.com/edit/react-4ded5d?file=index.js
337 |
338 | ## Tests
339 |
340 | This integration contains tests for: testing environment, chart rendering and passing down container props.
341 | To run tests, type:
342 |
343 | ```bash
344 | npm run test
345 | ```
346 |
347 | ## Changelog
348 | The changelog is available [here](https://github.com/highcharts/highcharts-react/blob/master/CHANGELOG.md).
349 |
350 | ## FAQ
351 |
352 | ### Where to look for help?
353 |
354 | [Technical support](https://www.highcharts.com/support) will help you with Highcharts and with the integration.
355 |
356 | If you have a bug to report or an enhancement suggestion please submit [Issues](https://github.com/highcharts/highcharts-react/issues) in this repository.
357 |
358 | ### Why highcharts-react-official and not highcharts-react is used?
359 |
360 | The NPM package is registered as `highcharts-react-official` because `highcharts-react` was already taken.
361 |
362 | ### How to get a chart instance?
363 |
364 | For class components and version prior to 3.0.0 use `React.createRef`:
365 |
366 | ```jsx
367 | constructor(props) {
368 | super(props)
369 | this.chartRef = React.createRef();
370 | }
371 |
372 | render() {
373 | return (
374 |
379 | );
380 | }
381 | ```
382 |
383 | For functional components and version 3.0.0 and later use `useRef` hook:
384 |
385 | ```jsx
386 | const chartComponent = useRef(null);
387 | const [options] = useState({...});
388 |
389 | useEffect(() => {
390 | const chart = chartComponent.current.chart;
391 | ...
392 | }, []);
393 |
394 | return ;
395 | ```
396 |
397 | Alternatively store a chart reference in a callback function:
398 |
399 | ```jsx
400 | afterChartCreated = (chart) => {
401 | // Highcharts creates a separate chart instance during export
402 | if (!chart.options.chart.forExport) {
403 | this.internalChart = chart;
404 | }
405 | }
406 |
407 | componentDidMount() {
408 | // example of use
409 | this.internalChart.addSeries({ data: [1, 2, 3] })
410 | }
411 |
412 | render() {
413 | return (
414 |
415 |
Highcharts
416 |
421 |
422 | );
423 | }
424 | ```
425 |
426 | ### How to add a module?
427 |
428 | To add a module, import it like so:
429 |
430 | ```jsx
431 | import Highcharts from 'highcharts'
432 | import highchartsGantt from "highcharts/modules/gantt"; // The Gantt module
433 | import HighchartsReact from 'highcharts-react-official'
434 |
435 | // Init the module (only for Highcharts v < 12)
436 | if (typeof highchartsGantt === 'function') {
437 | highchartsGantt(Highcharts);
438 | }
439 | ```
440 |
441 |
442 | ### How to add React component to a chart's element?
443 |
444 | By using [Portals](https://en.reactjs.org/docs/portals.html) it is possible to add a component to every HTML chart element.
445 |
446 | Live example: https://codesandbox.io/s/1o5y7r31k3
447 |
448 | ### Why Highcharts mutates my data?
449 |
450 | 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.
451 |
452 | Issue: https://github.com/highcharts/highcharts-react/issues/326
453 | More discussion here: https://github.com/highcharts/highcharts/issues/4259
454 |
--------------------------------------------------------------------------------
/babel.config.js:
--------------------------------------------------------------------------------
1 | module.exports = {
2 | presets: ['@babel/preset-env', '@babel/preset-react']
3 | };
4 |
--------------------------------------------------------------------------------
/demo/components/Chart.jsx:
--------------------------------------------------------------------------------
1 | import React from 'react'
2 | import HighchartsReact from '../../'
3 |
4 | const Chart = ({ options, highcharts }) =>
9 |
10 | export default Chart
11 |
--------------------------------------------------------------------------------
/demo/components/Container.jsx:
--------------------------------------------------------------------------------
1 | import React from 'react'
2 | import HighchartsReact from '../../'
3 | import Highcharts from 'highcharts'
4 |
5 | export default class Container extends React.Component {
6 | constructor () {
7 | super()
8 | this.state = { data: [] }
9 | setInterval(() => this.setState({ data: [...Array(3)].map(Math.random) }), 1500)
10 | }
11 |
12 | render () {
13 | const cb = function () {}
14 | return (
15 |