DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
  1. DZone
  2. Coding
  3. JavaScript
  4. A Highcharts’ Abstraction in React Components: React JSX Highcharts

A Highcharts’ Abstraction in React Components: React JSX Highcharts

Read on to see how one team of devs tackled an issue they encountered while working with React, and how you can use this open-source tool.

Will Hawker user avatar by
Will Hawker
·
Jul. 20, 17 · Opinion
Like (1)
Save
Tweet
Share
6.33K Views

Join the DZone community and get the full member experience.

Join For Free

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.

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.

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.

I’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:

In the above example, the reference (pointer) to myObj 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 – this principle is the basis of ImmutableJS.

Aside from these technical concerns, the existing approaches don’t really feel like “React” to me. In the beginning, I preferred the approach of Recharts, but I quickly realized that Recharts lacks the comprehensive feature set Highcharts provides.

This motivated me to begin a side project, to investigate whether I could create an abstraction of Highcharts using React components, like Recharts.

The result is what I am sharing with the community today – React JSX Highcharts (GitHub / NPM).

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, as demonstrated in the following example.


React JSX Highcharts works by first creating a chart instance with all features disabled, then, as its child components are mounted, it updates the chart instance with the appropriate configuration.

To avoid the previously mentioned problems with passing complex objects as props, each configuration option is passed as a separate component prop.

Let’s check this example to render a line series:

<LineSeries id="my-series" data={[1, 2, 3, 4]} color="#0FF" step />

Passing props individually allows React JSX Highcharts to process and select the optimal Highcharts method – for example, if the series data prop were to change, React JSX Highcharts can follow Highcharts best practices and use the setData method rather than update.
There are a few instances where React conventions have taken precedence over Highcharts configuration – for instance, rather than passing an object of event handles, React JSX Highcharts uses the onEventName convention, commonly seen in React code.

<SplineSeries id="my-series" data={myData} onHide={this.handleHide} onShow={this.handleShow} />

This would be equivalent to the Highcharts configuration:

series: [{
  type: 'spline',
  data: myData,
  events: { hide: this.handleHide, show: this.handleShow }
}]

To avoid passing around the chart instance between all the components, React JSX Highcharts utilizes React’s context feature – then using Higher Order Components, 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’s behalf.

I am obviously biased, but I believe that it is quite a powerful way to interact 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.

For instance, in this example, I use React Day Picker as a drop in replacement for Highstocks Range Selector date pickers.

I would love to get some feedback (and contributions) from the Highcharts community to advance the project. Going forward I’m planning to add Heat and Tree Map functionality, and ImmutableJS support, specifically for series data structures, but I’m open to suggestions!

Highcharts React (JavaScript library) Abstraction (computer science)

Published at DZone with permission of Will Hawker, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Streamlining Your Workflow With the Jenkins HTTP Request Plugin: A Guide to Replacing CURL in Scripts
  • Spring Cloud: How To Deal With Microservice Configuration (Part 1)
  • Top Authentication Trends to Watch Out for in 2023
  • Kubernetes vs Docker: Differences Explained

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: