lundi 2 mars 2015

Optimal algorithm for segmenting set of integers into labels for a chart axis?


Say you get values anywhere from 0 to 1,000,000,000, and you want to plot 30 days. So one particular chart may have a set like:



[1, 465, 123, 9, ...]


While another chart can have a set with much larger numbers:



[761010, 418781, ...]


Is there an "optimal algorithm" that can take those values and segment them into "clean" numbers? Sorry for the wording, don't know the right terminology, I will try to explain.


By "optimal algorithm", I mean both in terms of minimum number of computational steps, given that it creates labels (say for the y-axis) that are simplest from a human perspective.


For example, say you always want to divide the y-axis into 5 labels. You could do this:



var max = Math.max.apply(Math, values); // 465 (from the first set of values)
var interval = max / 5;
var labels = [ interval * 0, interval * 1, interval * 2, ... ];


But that creates labels like:



[ 0, 93, 186, ... ]


And that would be complex for humans to understand. What would be better (but still not ideal) is to create labels like:



[ 0, 125, 250, 375, 500 ]


But that's still to specific. Somehow it should figure out that a better segmentation is:



[ 0, 200, 400, 600, 800 ]


That way, it's divided into more intuitive chunks.


Is there a standard way to solve this problem? What algorithm works best?





Aucun commentaire:

Enregistrer un commentaire