[Performance] Drawing 300 stacked series with 60 000 points in Highcharts’s Highstock charts
Tags: performance, highcharts, javascript
Published: 2022-08-14 13:48:00

## Intro

Highcharts is a charting library for JavaScript projects ( [see more](https://www.highcharts.com/) ). It offers various types of charts, the API is very intuitive to use, the documentation is also clear and the Highcharts Team is always ready to help.
Although, working with huge data sets can be quite demanding. In this article, let me introduce some general performance improvement solutions and one for Highstock stacked charts.

## General performance improvement solutions

Performance can be improved by:

- Switching off animations
- Switching off series in navigator
- Using Data grouping
- Using Boost module

For React users, don’t forget to control re-rendering - use shouldComponentUpdate / useMemo / useEffect etc. Re-rendering the whole chart with a huge data set can be quite time consuming 🙂 

## Highstock stacked AREA chart performance problem

#### Requirements
My task was to create a chart with following requirements:
- Use Highcharts stock stacked chart
- Display about 300 series (about 60 000 points)
- Series with various lengths (some series have about 4 000 data points, some can have about 10)

I was quite sad to find out it’s barely usable - it takes about 10 seconds to navigate in the bottom navigation bar ( [Live demo](https://codesandbox.io/s/area-chart-300-series-no-performance-improvements-final-m9kddc?file=/src/index.js) ).

#### Data Grouping

> [Data grouping replaces a sequence of data points in a series with one grouped point.](https://www.highcharts.com/docs/stock/data-grouping) 

I tried to improve the performance with data grouping but I wasn't satisfied with the results. Here's why ( [Live demo](https://codesandbox.io/s/area-chart-300-series-data-grouping-final-cq6g4c?file=/src/series_serials.js:0-4930484) ) :

- Navigation with the bottom navigation bar works much faster but it still freezes for about 2 seconds. 
- The data is flatter which leads to hiding spikes in data.
Exemple 1: Before - without data grouping:
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/o6efslfllueay0jpy917.png)
Exemple 1: After - with data grouping:
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ocwgliohgyfe7y29uv4x.png)
Example 2 (data spike): Before - without data grouping - the spike is at about 1000M 
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hgnlpul3iqayt8bnn25b.png)
Example 2 (data spike): After - with data grouping - the spike doesn’t reach 1000M
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/478ij3y0klp8kkkatq9m.png) 
- Also, with data grouping I have observed weird shapes that didn’t stack properly, but I didn’t dig into it further.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qea7gfowmizwwkjb0wxe.png)

I needed some faster and more precise visualization so I gave up on data grouping and tried out the Boost module.

## Boost module

> [focuses on handling and rendering as many data points as possible as quickly as possible](https://www.highcharts.com/docs/advanced-chart-features/boost-module)

Also boost module didn't fit my data set:
- The rendering time was’t perfect as well, as you can see in the [Live example](https://codesandbox.io/s/highstock-area-chart-300-series-boost-module-final-sgnlol) 
- Sadly, as you read in [the documentation](https://www.highcharts.com/docs/advanced-chart-features/boost-module#caveats) 

> The area of area and areaspline series are drawn as 1px columns.

It’s not perfectly suited to area charts. In short - my data set looked like that:
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wffji2228hpq4co383fc.png)
- Because my series had various lengths it was hard to adjust the boost parameters. There are two parameters to do so:

[seriesThreshold](https://api.highcharts.com/highcharts/boost.seriesThreshold)- a threshold which describe the minimal number of series to start rendering in boost mode, eg. if you have it set to 20 and you have 10 series the chart will never render with the boost module (it seems like it also counts series in navigator).

[boostThreshold](https://api.highcharts.com/highcharts/plotOptions.series.boost) - a threshold which for a single series describes when to enter the boost mode. For example if you have displayed a part of series _a_ with 100 points, second series _b_ with 10 points, and boostThreshold = 50, the boost mode will be active only for the series with _a_ with 100 points. 
In my case most of the series were short, so I had to use a low value of boostThreshold to enable it on the short series and increase performance. This way, when I zoomed in the chart with the navigator, the boost mode was enabled constantly (because the displayed part of the series always had more than the boostThreshold number) showing the 1px width columns with a lot of blank space.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/55917nrqdrzqxrzgkaiq.png)

For my dataset it was a useless module but it works great with line charts.

## Final solution - switching to stacked LINE chart

I decided to change the approach. I changed the area chart to a line chart and the performance problem was fixed ([Live demo](https://codesandbox.io/s/highstock-line-stacked-chart-result-final-0nyi8q?file=/src/index.js))!

But… another problem approached me 🙂

#### Disorder of series problem

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yq3ftyevei04gjoikcq8.png)

As you can see in the [live demo](https://codesandbox.io/s/highstock-line-stacked-chart-sudden-change-in-order-of-series-final-opzhs4?file=/src/problematicSeries.js) and the picture above, the order of series is wrong (it's changed) at Jun 10, 14:15:18 - green series with name “b” goes under yellow series “c” instead of stacking on the top of them giving an illusion that the value drops suddenly. 

Why is it happening? Because of a lack of a data point in series “a” at Jun 10, 14:15:18. 

Solution: add a point that has a null value at Jun 10, 14:15:18. This will prevent the series from changing order ( [Live demo](https://codesandbox.io/s/highstock-line-stacked-chart-sudden-change-in-order-of-series-solved-final-hr5jwx?file=/src/problematicSeries.js) ).

## Final word

#### Note about initial loading time of each chart type
- [Stacked Area chart without improvements](https://codesandbox.io/s/area-chart-300-series-no-performance-improvements-final-m9kddc?file=/src/index.js:1306-1392): about **7 468 ms** (7s is way too long!)
- [Stacked Area chart with Data Grouping](https://codesandbox.io/s/area-chart-300-series-data-grouping-final-cq6g4c?file=/src/index.js): 885 ms
- [Stacked Area chart with Boost Module](https://codesandbox.io/s/highstock-area-chart-300-series-boost-module-final-sgnlol?file=/src/index.js): 745 ms
- [Stacked Line chart without improvements](https://codesandbox.io/s/highstock-line-stacked-chart-result-final-0nyi8q?file=/src/index.js): 716 ms

The fastest is definitely the Line chart, also it's visible that navigating on this chart is the fastest. 


Performance is a very important matter and it was worth spending some time on investigating possible improvement methods. I hope you can find some hints for your data visualizations here!

Also, I would like to thank the Highcharts Team for their support in resolving the issue with wrong series ordering in stacked line chart.