{"id":12977,"date":"2017-07-17T14:34:55","date_gmt":"2017-07-17T12:34:55","guid":{"rendered":"http:\/\/www.highcharts.com\/blog\/?p=12977"},"modified":"2026-01-12T09:28:01","modified_gmt":"2026-01-12T09:28:01","slug":"highcharts-abstraction-react-components-react-jsx-highcharts","status":"publish","type":"post","link":"https:\/\/www.highcharts.com\/blog\/integration\/highcharts-abstraction-react-components-react-jsx-highcharts\/","title":{"rendered":"A Highcharts&#8217; abstraction in React Components: React JSX Highcharts"},"content":{"rendered":"<p>&nbsp;<\/p>\n<p><i>Update: Since the original publication of this post, <a href=\"\/blog\/tutorials\/highcharts-react-wrapper\/\">Highsoft has released a React wrapper<\/a>. <\/i><\/p>\n<p>In December 2016, I began a project in my workplace to rewrite our legacy Knockout web application using React. As we provide data analytics to the utilities sector, charts are the central piece of the application delivered to the clients.<\/p>\n<p>Highcharts had long proved itself as an excellent solution to our graphing requirements, so naturally, developers and stakeholders alike were keen that Highcharts remained a key part of the new application going forward.<\/p>\n<p>I was in charge of the Highcharts integration within the React App, and after some research, it became evident that many of the existing solutions require passing large Highcharts configuration objects to a single React component.<\/p>\n<p>I&#8217;m not particularly keen on this approach, as a React component might not re-render as expected when this configuration object is updated. Consider this (admittedly contrived) example:<br \/>\n<iframe style=\"width: 100%; height: 250px; border: 0; border-radius: 4px; overflow: hidden;\" width=\"300\" height=\"150\" sandbox=\"allow-modals allow-forms allow-popups allow-scripts allow-same-origin\" data-src=\"https:\/\/codesandbox.io\/embed\/R6GX0LYvR\" data-cookieconsent=\"marketing\"><\/iframe><\/p>\n<div class=\"cookieconsent-optout-marketing\"><a>Consent for marketing cookies<\/a> needs to be given to view code samples<\/div>\n<p>In the above example, the reference (pointer) to <code>myObj <\/code>is maintained despite the number property being modified. When React compares the new state to the previous state, by shallow comparison, the state is considered equal, and no re-render occurs. These React specific issues are usually avoided by ensuring that the user clones the object before modifying it &#8211; this principle is the basis of <a href=\"https:\/\/facebook.github.io\/immutable-js\/\" target=\"_blank\" rel=\"noopener noreferrer\">ImmutableJS<\/a>.<\/p>\n<p>Aside from these technical concerns, the existing approaches don\u2019t really feel like \u201cReact\u201d to me. In the beginning, I preferred the approach of <a href=\"http:\/\/recharts.org\/#\/en-US\" target=\"_blank\" rel=\"noopener noreferrer\">Recharts<\/a>, but I quickly realized that Recharts lacks the comprehensive feature set Highcharts provides.<\/p>\n<p>This motivated me to begin a side project, to investigate whether I could create an abstraction of Highcharts using React components, like Recharts.<\/p>\n<p>The result is what I am sharing with the community today \u2013 React JSX Highcharts (<a href=\"https:\/\/github.com\/whawker\/react-jsx-highcharts\" target=\"_blank\" rel=\"noopener noreferrer\">GitHub <\/a>\/ <a href=\"https:\/\/www.npmjs.com\/package\/react-jsx-highcharts\" target=\"_blank\" rel=\"noopener noreferrer\">NPM<\/a>).<\/p>\n<p>The goal of this project is to provide a thin-as-possible abstraction, so anyone reading the Highcharts documentation could easily figure out how to achieve the same via React JSX Highcharts.<br \/>\n<iframe style=\"width: 100%; height: 525px; border: 0; border-radius: 4px; overflow: hidden;\" width=\"300\" height=\"150\" sandbox=\"allow-modals allow-forms allow-popups allow-scripts allow-same-origin\" data-src=\"https:\/\/codesandbox.io\/embed\/xvx4JJGX3\" data-cookieconsent=\"marketing\"><\/iframe><\/p>\n<div class=\"cookieconsent-optout-marketing\"><a>Consent for marketing cookies<\/a> needs to be given to view code samples<\/div>\n<p>React JSX Highcharts works by first creating a chart instance with all features disabled, then, as its\u2019 child components are mounted, it updates the chart instance with the appropriate configuration.<\/p>\n<p>To avoid the previously mentioned problems with passing complex objects as props, each configuration option is passed as a separate component prop.<\/p>\n<p>Let\u2019s check this example to render a line series:<\/p>\n<pre>&lt;LineSeries id=\"my-series\" data={[1, 2, 3, 4]} color=\"#0FF\" step \/&gt;<\/pre>\n<p>Passing props individually allows React JSX Highcharts to process and select the optimal Highcharts method &#8211; for example, if the series data prop were to change, React JSX Highcharts can follow Highcharts best practices and use the <a href=\"http:\/\/api.highcharts.com\/highstock\/Series.setData\" target=\"_blank\" rel=\"noopener noreferrer\">setData <\/a>method rather than update.<br \/>\nThere are a few instances where React conventions have taken precedence over Highcharts configuration &#8211; for instance, rather than passing an object of event handles, React JSX Highcharts uses the <code>onEventName <\/code>convention, commonly seen in React code.<\/p>\n<pre>&lt;SplineSeries id=\"my-series\" data={myData} onHide={this.handleHide} onShow={this.handleShow} \/&gt;<\/pre>\n<p>This would be equivalent to the Highcharts configuration:<\/p>\n<pre>series: [{\r\n  type: 'spline',\r\n  data: myData,\r\n  events: { hide: this.handleHide, show: this.handleShow }\r\n}]<\/pre>\n<p>To avoid passing around the chart instance between all the components, React JSX Highcharts utilizes <a href=\"https:\/\/facebook.github.io\/react\/docs\/context.html\" target=\"_blank\" rel=\"noopener noreferrer\">React\u2019s context feature<\/a> &#8211; then using <a href=\"https:\/\/facebook.github.io\/react\/docs\/higher-order-components.html\" target=\"_blank\" rel=\"noopener noreferrer\">Higher Order Components<\/a>, it injects Highcharts methods into the wrapped component as props. The components then interact with the chart instance via these injected methods on the application author\u2019s behalf.<\/p>\n<p>I am obviously biased, but I believe that it is quite a powerful way to interacts with Highcharts. Therefore React JSX Highcharts exposes these Higher Order Components publicly, allowing application authors to write custom React components which can interact with the chart too.<\/p>\n<p>For instance, in <a href=\"https:\/\/whawker.github.io\/react-jsx-highcharts\/examples\/CustomComponent\/index.html\" target=\"_blank\" rel=\"noopener noreferrer\">this example<\/a>, I use<a href=\"http:\/\/react-day-picker.js.org\/\" target=\"_blank\" rel=\"noopener noreferrer\"> React Day Picker<\/a> as a drop in replacement for Highstocks Range Selector date pickers.<\/p>\n<p>I would love to get some feedback (and contributions ?) from the Highcharts community to advance the project. Going forward I\u2019m planning to add <a href=\"https:\/\/www.highcharts.com\/demo\/treemap-with-levels\" target=\"_blank\" rel=\"noopener noreferrer\">Heat and Tree Map<\/a> functionality, and ImmutableJS support, specifically for series data structures, but I\u2019m open to suggestions!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Introducing a new open source React component library for building Highcharts charts<\/p>\n","protected":false},"author":215,"featured_media":10591,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"meta_title":"","meta_description":"","hc_selected_options":[],"footnotes":""},"categories":[1105],"tags":[1094,1031,824],"coauthors":[756],"class_list":["post-12977","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-integration","tag-highcharts-core","tag-javascript","tag-react"],"_links":{"self":[{"href":"https:\/\/www.highcharts.com\/blog\/wp-json\/wp\/v2\/posts\/12977","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.highcharts.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.highcharts.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.highcharts.com\/blog\/wp-json\/wp\/v2\/users\/215"}],"replies":[{"embeddable":true,"href":"https:\/\/www.highcharts.com\/blog\/wp-json\/wp\/v2\/comments?post=12977"}],"version-history":[{"count":1,"href":"https:\/\/www.highcharts.com\/blog\/wp-json\/wp\/v2\/posts\/12977\/revisions"}],"predecessor-version":[{"id":29097,"href":"https:\/\/www.highcharts.com\/blog\/wp-json\/wp\/v2\/posts\/12977\/revisions\/29097"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.highcharts.com\/blog\/wp-json\/wp\/v2\/media\/10591"}],"wp:attachment":[{"href":"https:\/\/www.highcharts.com\/blog\/wp-json\/wp\/v2\/media?parent=12977"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.highcharts.com\/blog\/wp-json\/wp\/v2\/categories?post=12977"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.highcharts.com\/blog\/wp-json\/wp\/v2\/tags?post=12977"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/www.highcharts.com\/blog\/wp-json\/wp\/v2\/coauthors?post=12977"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}