Monday, December 30, 2013

Highcharts - Setting new minimum maximum values (extremes) for Y-axis


I use the following code to define min-max values of a chart:

    var min1 = Math.min.apply(Math, data1);  
    var min2 = Math.min.apply(Math, data2);  
    var min3 = Math.min.apply(Math, data3);  
    var min = Math.min(min1, min2, min3);
   
    var max1 = Math.max.apply(Math, data1);  
    var max2 = Math.max.apply(Math, data2);  
    var max3 = Math.max.apply(Math, data3);  
    var max = Math.max(max1, max2, max3);                       
    .
    .
    .



    yAxis: {
                min: min,
                max: max,


However, after modifications made to the chart like adding/removing points, these values need to be updated. Here is how you can do it:

   var max = chart1.yAxis[0].getExtremes().dataMax;
   var min = chart1.yAxis[0].getExtremes().dataMin;
   chart1.yAxis[0].setExtremes(min, max);
   chart1.redraw();

Highcharts - Removing selected point's neighbor (same time in other series) points




Hi,

I needed to remove selected lines and I found the following example:

http://www.highcharts.com/studies/dynamic.htm

 function removePoint() {
  var points = chart.getSelectedPoints();
   
  if (!points.length) alert ('No points selected. Click a point to select it. Control click to select multiple points');
  
  jQuery.each(points, function(i, point) {
   point.remove(false);
  });
   
  chart.redraw();
 }
 
  
 
It works perfectly. But later I needed something more: When I select one point, 
its neighbor points (which means the other series' related points) need to be removed too automatically
because they belong to the same time and removing only one of them doesn't cut it. It also distorts the chart.
 
Selecting the other points is another option but it's not efficient if you have charts like the following:
 

It feels like hell to select  three of those points if they are close to each other.

So I changed the code like the following:
 
             jQuery.each(points, function(i, point)
             {       
                      if (point.series!=null)
                     {
                                var index = point.series.data.indexOf(point);
                                var x1 = chart1.series[0].data[index];                              
                                var x2 = chart1.series[1].data[index];
                                var x3 = chart1.series[2].data[index];
                                x1.remove(false);
                                x2.remove(false);
                                x3.remove(false);                     
                      }                                              
              });

By the help of this code, whichever point you select in the series, it will remove the other neighbor points in other series too.

Note: You may need to alter this code if you have more or less than 3 series.

Cheers,