Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Normalize numbers to 100 using D3 range?

I'm trying to take a given array of numbers and normalize them be 0-100.

Clearly I'm doing something wrong here, but I can't for the life of me figure it out. Someone please help me find my dumb mistake?

var arr = [6345, 45678, 34572, 59867, 23517, 20938, 9872345, 2986];

arr.sort();

var scale = d3.scale.linear().domain([0, arr[0]]).range([0, 100]);

for ( var i in arr ){
    console.log(scale(arr[i]));
}

jsFiddle

like image 735
RavenHursT Avatar asked Jan 14 '14 01:01

RavenHursT


1 Answers

Try:

var max = d3.max(arr);
var scale = d3.scale.linear().domain([0, max]).range([0, 100]);
like image 191
bill automata Avatar answered Oct 14 '22 03:10

bill automata