Saturday, November 24, 2012

Detect kissing and overlapping points in Highcharts

formatter: function() {
    
   // console.log(this);

   // Michael Buen is here                    
 
 var search = this.series.chart.series[0].data;

 var a = this.point;

 var overlapCount = 0;                    
 for(var i in search) {
        
  var b = search[i];
  var d = getDistance(a, b);
  
  // choose an overlap threshold
  var kiss = 5 * 2;
  var halfOverlap = 5;
  var fullOverlap = 2.5; 
  if (d <= kiss) {
   ++overlapCount;
  }
        
 }
 
      
 return this.x +' cm, '+ this.y +' kg' + a.plotX + ' <br/><b>Overlaps:</b> ' + overlapCount;
}



function getDistance(point1,point2)
{
  var xs = 0;
  var ys = 0;
  xs = point2.plotX - point1.plotX;
  xs = xs * xs;
  ys = point2.plotY - point1.plotY;
  ys = ys * ys;
  return Math.sqrt( xs + ys );
}



Live code: http://jsfiddle.net/HxTjK/

No comments:

Post a Comment