How do you load millions of points in Highcharts?
The browsers have limitations when it comes to great amounts of data. Think about loading 1 million points of OHLC data – with three and four values each point, it will mean megabytes of data and heavy looping to process it. Perhaps some modern browsers can cope with it, but legacy browsers won’t get anywhere near it.
Enter the server. Backed by a database, we can load just the resolution we want to look at. In our new lazy loading demo, we set up a backend in PHP and MySQL. The original data is minute data for the AAPL stock, in total 1.7 million records. We wanted to display a candlestick chart with open, high, low and close data values, so we hit a limitation in MySQL. Since it lacks the concept of first and last in groups (open/close), we needed a nested select, which made it slow. So instead, we created four data tables holding data for minutes, hours, days and months respectively. If we wanted to display a simple line chart with average values, we wouldn’t need more than the original minute table.
Furthermore, to prevent the Highstock navigator from adjusting to the new data and sending off another setExtremes event, we needed to introduce a new option, navigator.adaptToUpdatedData. Now we can do whatever we want with the data in the main chart without affecting the navigator series, which always displays the entire data range.
Now as you can see in the source code below, the client server connection happens through jQuery.getJSON. We hook up to the X axis afterSetExtremes event, which unlike setExtremes fires after the new extremes have been adjusted for the X axis minRange and other variables. In the event handler we show a loading screen, then send off the new extremes to the server. The server then chews through this information, decides which resolution it should go for, selects the data and sends it back. Client receives it, hides the loading screen and sets new data to the series.
Comments
Kristopher Larson | 6 years ago
the chart is broken for anything but a year
highcharts | 6 years ago
Hi Kristopher, thanks for letting us know. We have updated the demo and it should be fixed now
Martin Hammer | 6 years ago
Great, you could improve the quality of the sample a lot by putting a small delay in the setExtremes function to prevent spamming your API resource.
Dillibabu | 2 years ago
hi hicharts taking more loading time when more than 500 records
Mustapha Mekhatria | 2 years ago
There is a new demo now, feel free to test it 🙂
Benny Neugebauer | 2 years ago
Your article says “in total 1.7 records”. I guess you meant “in total 1.7 million records”? 😉 Best, Benny
Mustapha Mekhatria | 2 years ago
Thx. Done 🙂
Joe | 1 year ago
Extremely helpful article, solved my issue! I’m curious about the MySQL limitation mentioned.
“Since it lacks the concept of first and last in groups (open/close), we needed a nested select, which made it slow.”
What is meant by the concept of first and last and how did/does that apply to this example. I feel it is something I should be aware of as I shall be storing OHLC data is a SQL database eventually.
Mustapha Mekhatria | 1 year ago
Hi,
Feel free to use the boost module to handle a big amount of data: https://www.highcharts.com/docs/advanced-chart-features/boost-module
Want to leave a comment?
Comments are moderated. They will publish only if they add to the discussion in a constructive way. Please be polite.