Monday, December 30, 2013

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,
  

No comments:

Post a Comment