HighCharts: Accessing Zoomed Series Data
HighCharts: Accessing Zoomed Series Data
If i zoom an HighStock multi series chart (using either the Navigator or the Range Selector), is there a way to fetch the point.y data only for the zoomed series?
For example, if I am showing the sales information for a period across multiple branches (each branch plotted as series), and if zoomed to a week, I would like to know the total of sales for the week across the branches. Does highstock provide access to the zoomed dataset?
2 Answers
2
A very brute force approach to this is to loop over your data upon zooming and evaluate if it is visible. That is, is a given point within the range of the x-axis (and y-axis, if you allow zoom in that direction)?
chart:
events:
redraw: function()
sum = 0;
xAxis = this.xAxis[0];
// Loop over your series
this.series[0].data.forEach(function(point)
// Add to sum if within x-axis visible range
if(point.x >= xAxis.min && point.x <= xAxis.max)
sum += point.y;
// If too large, stop summing
if(point.x > xAxis.max)
return;
);
// Print sum
console.log('Visible sum: '+sum);
See this JSFiddle demonstration of it in use.
You might evaluate xAxis.events.setExtremes
, but it offers min
and max
values that might change after it takes effect, which might give some unexpected results.
xAxis.events.setExtremes
min
max
Thanks Halvor, or may be we could just directly filter the series dataset for the zoom period and then sum it up. (which will not involve checking the point values) I will check that.
– thanikkal
Sep 17 '18 at 6:45
You can check isInside
flag on every point:
isInside
redraw: function()
var sum = 0;
Highcharts.each(this.series[0].points, function(point)
if (point.isInside)
sum += point.y;
);
console.log(sum)
Live demo: http://jsfiddle.net/BlackLabel/7kmzhecy/
This doesnt work for highstock chart reliably (havent tried for hicharts alone), using Navigator or the Range Selector. I need to debug and see where its getting off.
– thanikkal
Sep 17 '18 at 14:28
It should work fine with Highstock: jsfiddle.net/BlackLabel/ab6192v7
– ppotaczek
Sep 17 '18 at 15:06
Thanks for contributing an answer to Stack Overflow!
But avoid …
To learn more, see our tips on writing great answers.
Required, but never shown
Required, but never shown
By clicking "Post Your Answer", you agree to our terms of service, privacy policy and cookie policy
This seems like an unnecessarily expensive operation. Hopefully someone can bring something with better performance
– Halvor Holsten Strand
Sep 16 '18 at 20:25