The provided content offers a comprehensive guide to mastering Highcharts in React applications, showcasing examples of various chart types and discussing accessibility features.
Abstract
The web content serves as an in-depth tutorial for integrating Highcharts, an enterprise-grade JavaScript charting library, into React applications. It begins by setting up the development environment using Create React App and proceeds to demonstrate how to create and customize a range of chart types, including basic types like line, spline, and pie charts, as well as more complex ones like 3D charts, heatmaps, treemaps, and gauge charts. The guide emphasizes the importance of chart accessibility and how to implement it using Highcharts' accessibility module. Additionally, it addresses common issues, such as edge lines being cut off, and provides solutions to enhance the visual quality of charts. The article concludes by recommending Highcharts for its robust charting capabilities, especially for those with the budget to invest in it.
Opinions
The author suggests that Highcharts, despite its proprietary license, is a powerful tool for charting in React applications, especially for enterprise-grade solutions.
Highcharts' extensive documentation and examples are highlighted as beneficial for developers.
The inclusion of the accessibility module is presented as a critical feature for making web applications more inclusive and usable for individuals with disabilities.
The article implies that the cost of Highcharts is justified by its comprehensive features and the quality of the charts it produces.
The author provides a workaround for the common issue of edge lines being cut off in Highcharts, indicating a known limitation that requires additional configuration.
The recommendation of Highcharts for those who can afford it suggests that while it is a valuable tool, budget constraints might limit its adoption for some users.
Highcharts is an enterprise-grade JavaScript charting library based on SVG. It was first released in 2009 by Highsoft in Vik, Norway.
Highcharts supports a long list of different chart types, including line, spline, area, areaspline, column, bar, pie, scatter, scatter3d, heatmap, treemap, gauge, and almost all chart types. It comes with excellent documentation and innumerable examples. It is powerful with a low learning curve. The only drawback is that the license is proprietary. It is free for personal/non-commercial uses, but you have to pay for commercial applications.
Along with D3.js, Highcharts dominates the charting application. Let’s take a look at how it works.
Set Up The Working Environment
We are going to use Create React App to explore Highcharts. The following command creates a React project:
% yarn create react-app react-highcharts% cd react-highcharts
Install highcharts and highcharts-react-official.
highcharts: It is the JavaScript charting library.
highcharts-react-official: It is the official Highcharts-supported wrapper for React.
% yarn add highcharts highcharts-react-official
After the installation, these packages become part of dependencies in package.json:
Modify src/App.js to plot eight charts of the type line, spline, area, areaspline, column, bar, pie, and scatter.
At lines 6–28, a chart configuration is created.
– At lines 7–11, the chart’s type, width, and height are defined.
– At lines 12–14, the chart title is composed from the type. As lodash comes with Create React App, _.startCase is called to make each initial letter capitalized.
– At lines 15–19, yAxis, the Y axis, or value axis, is configured with a title.
– At lines 20–27, two data series are created.
At lines 33–40, each line creates a chart with the specified type:
– line (line 33): The line chart is represented by a series of data points connected with a straight line.
– spline (line 34): The spline chart draws a curved line between the points in a data series.
– area (line 35): The area chart functions the same way as a line chart, except that it fills the area between the line and the threshold, which is 0 by default.
– areaspline (line 36): The areaspline chart is the same as the area chart, except that the line is a spline instead of a straight line.
– column (line 37): The column chart displays data as vertical bars.
– bar (line 38): The bar chart is the same as the column chart, except that the X-axis and Y-axis are switched.
– pie (line 39): The pie chart is a circular chart divided into sectors, which is proportional to the quantity it represents.
– scatter (line 40): The scatter chart draws a dot for each data point without connecting them.
Execute yarn start, and we see eight charts, each being a specific type.
Image by author
Example 2: Chart Credits and Export Menu
Look at the screenshot above. By default, Highcharts puts a credits label/link in the lower right corner of the chart.
The text and link can be configured. And here is an example:
Highcharts’ exporting module provides a menu with export-related menu items to View in full screen, Print chart, Download PNG image, Download JPEG image, Download PDF document, and Download SVG vector image.
Configuring a 3D chart is similar to configuring a regular chart, except for the following options:
options3d configures the 3D option. The flag, enabled, enables a 3D chart. alpha and beta are the angles to rotate the view of the chart and depth defines the total depth with a default value of 100. Here is an example:
plotOptions.[type].depth defines the depth of each plot.
The chart’s data might be in a different format for each type. xAxis defines X axis or category axis. For our 3D examples, three xAxis are defined for plots at different depths, although these xAxis themselves are not visible.
Here is the modified src/App.js:
At lines 6 and 8, Highcharts’ 3D module is loaded.
At lines 13–18, options3d enables the 3D charts and configure the rotation angles and depth.
At lines 28–38, three xAxis are set to invisible.
At lines 39–43, plotOptions.[type].depth is set to 100.
At lines 44–57, series defines plots for xAxis 0, xAxis 1, and xAxis 2. Each plot has six points.
Execute yarn start, and we see eight charts in 3D view.
Image by author
Here are some observations from the screenshot above:
line, spline, area, column and bar charts are displayed nicely in 3D view.
The areaspline does not rotate the view, as 3D type is not officially supported.
The pie chart works, but three pies stacking on each other is clumsy. Simplifying it to one pie makes it look nicer:
series: [
{
data: [2, 2, 1, 4, 3, 2],
},
]
Image by author
The scatter chart does not show up. The common options that we have used do not work for the scatter chart. The data needs to be coded in [x, y, z] format. Also, it should use the type, scatter3d, instead of scatter. Here is the fixed configuration:
In the screenshot above, the xAxis ending label, 5, is jammed with the zAxis starting label, 0. The two numbers next to each other look like 50. To avoid confusion, we turn off the first label of zAxis, which stands for the Z axis or depth axis for 3D plots.
zAxis: {
showFirstLabel: false
}
It looks better now.
Image by author
options3d positions the chart in a good angle. However, it could be more helpful if we can view it from various angles, via 3D rotation. This can be achieved by click and drag of a mouse, or through a touch screen.
Here is the modified src/App.js:
Execute yarn start, and we see the 3D scatter chart that can be rotated:
Image by author
Example 4: Heatmap Charts
A heatmap is a graphical representation of data where the individual values contained in a matrix are represented as colors.
The heatmap chart requires the heatmap module to be imported. It can be accomplished by one line of code:
colorAxis is a color axis for series. Visually, it appears as a gradient or as separate items inside the legend. Here is colorAxis that defines a gradient from '#FF0000' (red) to '#0000FF' (blue)
At line 13, add plotBorderWidth to separate the heatmap areas.
At lines 18–20, xAxis is defined with 6 categories.
At lines 21–25, yAxis is defined with 3 categories. title is turned off (line 23), and categories is displayed in a reversed order.
At lines 26–30, colorAxis is defined as a gradient color from white (line 28) to the color of the first series (line 29). colorAxis replaces the default labels for series.
At lines 31–45, data is defined and dataLables enables showing the heatmap value at each area.
At line 53, the heatmap chart is created.
Execute yarn start, and we see a heatmap chart.
Image by author
Example 5: Treemap Charts
A treemap is a graphical representation of data where hierarchical data is displayed using nested figures, usually rectangles.
The treemap chart requires the treemap module to be imported. It can be done by one line of code:
At line 16, type is specified in series, instead of chart.
At line 17, layoutAlgorithm is set to 'sliceAndDice’, which is the default behavior of dividing the area into smaller parts.
At lines 18–79, data defines the hierarchical data, which defines three parents, Red (lines 19–23), Green (lines 24–28), and Blue (lines 29–33). Red has thee children (lines 34–48), Green has two children (lines 49–58), and Blue has four children (lines 59–78).
At line 89, the treemap chart is created.
Execute yarn start, and we see a treemap chart.
Image by author
Change layoutAlgorithm to 'stripes’, and we see the areas are divided into strips:
Image by author
Change layoutAlgorithm to 'squarified’ or 'strip’, and we see the areas are divided into the following shapes:
Image by author
Example 6: Gauge Charts
A gauge chart is also called a speedometer chart or a dial chart. The chart has a needle that points to a certain value over the pivot point. It is an effective visualization to assess performance, track progress, and facilitate decision making.
The gauge chart requires the highcharts-more module to be imported. It can be done by one line of code:
At lines 17–23, yAxis sets min and max to 0 and 100. yAxis.title.text is set to 'Speed'.
At lines 24–28, series.data is set to 80.
At line 36, the gauge chart is created.
Execute yarn start, and we see a gauge chart.
Image by author
A gauge chart can be defined for any arc with pane.startAngle and pane.endAngle. When it is not a full circle, pane.background should be set to null. It is also a good idea to adjust the center to position [x, y].
Execute chart examples, and we see the following warning in the console.
highcharts.src.js:223 Highcharts warning: Consider including the “accessibility.js” module to make your chart more usable for people with disabilities. Set the “accessibility.enabled” option to false to remove this warning. See https://www.highcharts.com/docs/accessibility/accessibility-module.
Inspect chart elements, and we see no accessibility information.
Image by author
Accessibility makes web applications usable for people whose abilities are limited in some way, such as vision (using a screen reader) and mobility (using the keyboard only). In addition, limitations could come from the device’s capability, such as mobile devices. It is very important to make Enterprise software accessible.
The accessibility module can be imported by one line of code:
Simply importing this module, the chart becomes accessible. In addition, there is accessibility option to configure description and more features, such as announcing new data to screen reader users, configuring special keyboard navigation, etc.
At lines 7 and 10, the accessibility module is loaded.
At lines 19–22, accessibility defines a description.
Execute yarn start, and recheck the chart elements.
Image by author
There are many of accessibility information in the screenshot above, including the accessibility description that we have specified:
<divaria-hidden="false"style="position: absolute; width: 1px; height: 1px; overflow: hidden; white-space: nowrap; clip: rect(1px, 1px, 1px, 1px); margin-top: -3px; opacity: 0.01;"><p>Gauge Chart</p><div>Chart with 1 data point.</div><div>The gauge chart shows the performance speed between 0 and 100.</div><div>The chart has 1 Y axis displaying Speed. Data ranges from 80 to 80.</div></div>
If you pay a little attention, you will notice that each chart is an SVG file.
Image by author
Bonus Example: Draw a Line With Full Width on Axis X
We give this bonus example, as you may encounter this issue in product development. Put this simple line chart in src/App.js, where the series lineWidth is set to 10 (line 21):
Execute yarn start, and we see the line chart.
Image by author
Have you noticed that the blue line on axis X is thinner?
It is a well-known issue in Highcharts that edge lines are cut off. There are many forums that have discussed similar problems, such as this one. We can adopt the solution from the linked article to set min to a value below 0.
yAxis: {
title: {
text: 'Values',
},
min: -0.2,
}
Execute yarn start, and we see the line on axis X fully displayed:
Image by author
Conclusion
We have shown seven examples of Highcharts in React, which include 12 chart types of line, spline, area, areaspline, column, bar, pie, scatter, scatter3d, heatmap, treemap, and gauge.
We have also written articles on other Highcharts types:
In this article, bubble, packedbubble, streamgraph, and cylinder.
In this article, sankeydiagram, arcdiagram, dependencywheel, and networkgraph.