Finding the Max and Min in a Line Chart widget
I would like to find the max and min of multiple series on line chart. What is the best way to do this?
-
Here is one way.
I'm not sure whether you want MAX/MIN for each series, or across all series. Here is JS code for both. This assumes all series have the same number of points on the X axis.
Overall...
// Get MAX and MIN across all series
widget.on('ready', function(widget) {
//Retrieve the chart
var chart = element.data('hc');
var queryResult = widget.queryResult;
var maxPoint = 0;
var minPoint = 0;
var reset=1;
// Scan Each Series
for (var i = 0; i < queryResult.series.length; i++){
// Scan Each Category (X-point) within the Series
for (var j = 0; j < queryResult.xAxis.categories.length ; j++){
thisPoint = queryResult.series[i].data[j].y;
if ( reset==1 ) {
maxPoint = thisPoint;
minPoint = thisPoint;
reset=0;
}
if(thisPoint > maxPoint){
maxPoint = thisPoint;
}
if(thisPoint < minPoint){
minPoint = thisPoint;
}
console.log("series"+i+" X point "+j+": "+thisPoint);
}
}
console.log("max: "+maxPoint);
console.log("min: "+minPoint);
});Each Series...
// Get MAX and MIN of each series
widget.on('ready', function(widget) {
//Retrieve the chart
var chart = element.data('hc');
var queryResult = widget.queryResult;
var maxPoint = 0;
var minPoint = 0;
var maximums = [];
var minimums = [];
// Scan Each Series
for (var i = 0; i < queryResult.series.length; i++){
var reset=1;
// Scan Each Category (X-point) within the Series
for (var j = 0; j < queryResult.xAxis.categories.length ; j++){
thisPoint = queryResult.series[i].data[j].y;
if ( reset==1 ) {
maxPoint = thisPoint;
minPoint = thisPoint;
reset=0;
}
if(thisPoint > maxPoint){
maxPoint = thisPoint;
}
if(thisPoint < minPoint){
minPoint = thisPoint;
}
console.log("series"+i+" X point "+j+": "+thisPoint);
}
maximums.push(maxPoint);
minimums.push(minPoint);
}
console.log("max: "+maximums);
console.log("min: "+minimums);
}); -
Hi,
Sharing Christopher's script which solves the issue:
var max = Number.MIN_VALUE;
var min = Number.MAX_VALUE;
widget.on('processresult', function(se, ev){
//var categories = ev.result.xAxis.categories;
//console.log("Custom script");
/*
Reset the max and min between
filter changes
*/
max = Number.MIN_VALUE;
min = Number.MAX_VALUE;
var data = ev.result.series;
//console.log(data.length);
for(i=0; i < data.length; i++){
for(j=0; j < data[i].data.length; j++){
//console.log(data[i].data[j].y);
if(max < data[i].data[j].y){
max = data[i].data[j].y;
}
if(min > data[i].data[j].y){
min = data[i].data[j].y;
}
}
}
})
widget.on('ready', function(se, ev){
var e = element;
var chart = e.data('hc');
chart.alignTicks =false;
chart.yAxis[0].update({max: max, min: min}, true);
})Thank you Christopher!
-
Hello there,
Unfortunately, the code above will not work if you use any dimension in the "Break by" field.
I wanted to share a modified version which deals with this issue:[quote]
widget.on('ready', function(se, ev) {
// Parameters
var seriesIndex_Count = se.queryResult.series.length
var adjustmentPercent = 0.25;var i= 0; //start from the first Column/line
// Initially there will be no Min or Max
Calculated_min = 100; //null,
Calculated_max = null;//Doing a while loop as line 1 may have max - $20 and line 2 may have max -$40... then out chart must show MAx-$55 or so
while(i< seriesIndex_Count)
{
debugger
$.each(se.queryResult.series[i].data, function() {
if (this.y !== null && Calculated_min > this.y ) {
debugger
Calculated_min = this.y;
}
if (!Calculated_max || Calculated_max < this.y) {
Calculated_max = this.y;
}
})
i++;
} //end whiledebugger
// adjust based on the delta
var delta = Calculated_max-Calculated_min;
Calculated_max = Calculated_max + (delta*adjustmentPercent);
Calculated_min = Calculated_min - (delta*adjustmentPercent);// Set the min/max on the chart
var e = element;
var chart = e.data('hc');
chart.alignTicks =false;
chart.yAxis[0].update({max:Calculated_max, min: Calculated_min}, true);
})[/quote]
Hope that helps!
Szymon
Please sign in to leave a comment.
Comments
5 comments