Skip to content

Instantly share code, notes, and snippets.

@jweir
Created August 29, 2014 14:35
Show Gist options
  • Save jweir/bfac545a1648620c1247 to your computer and use it in GitHub Desktop.
Save jweir/bfac545a1648620c1247 to your computer and use it in GitHub Desktop.
Weeding functions
// just a sketch to put away for now
// these are weeding functions
// the goal is to remove lables on a graph that are too close together (ie weed out overlapping labels)
function weed(array, x, y){
var f = array[0]
, l = array[array.length -1]
, ex = d3.extent(array, function(d){ return d.y })
, hiLo = array.filter(function(d){ return d.y == ex[0] || d.y == ex[1]});
return _.uniq([f,l].concat(hiLo));
}
// filters out labels which will be too close together
function weedValue(array, x, y){
var threshold = 0.1;
var ls = threshold+1 // last displayed x
, skipped = false;
return array.filter(function(d){
var cy = y(d.y);
var diffy = Math.abs(1 - (ls / cy));
if(diffy > threshold){
skipped = false;
ls = cy;
} else {
skipped = true;
}
return ! skipped;
})
}
// filters out labels which will be too close together
function weedSpatial(array, x, y){
var thresholdx = 24,
thresholdy = 24;
var lsx = thresholdx+1 // last displayed x
, lsy = thresholdy+1 // last displayed y
, skipped = false;
return array.filter(function(d){
var cx = x(d.x) // current positions
, cy = y(d.y);
var diffx = Math.abs(lsx - cx)
, diffy = Math.abs(lsy - cy);
if(diffx > thresholdx || diffy > thresholdy){
skipped = false;
lsx = cx;
lst = cy;
} else {
skipped = true;
}
return ! skipped;
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment