html - JavaScript, a good way to represent a float as color in a plot -


i trying create plot using javascript (d3.js) , in every second have new incomming point label, label float chosen randomly given interval.

i need represent label (float) 1 given color points gradient colors, something like:

enter image description here

i have tried far, don't give expected result.

function float2color( label ) {         var r = 255 - ( label / 8 * 255 | 0 );              g = label / 8 * 255 | 0;            return '#' +              ( r ? ( r = r.tostring(16), r.length == 2 ? r : '0' + r ) : '00' ) +             ( g ? ( g = g.tostring(16), g.length == 2 ? g : '0' + g ) : '00' ) + '00'     } 

result (not good))

enter image description here

it sounds want linear color scale. replace float2color function.

var color = d3.scale.linear()     .domain([min, max])     .range(['blue', 'yellow']); 

then color float,

color(float_value); 

note 'blue' , 'yellow' can given hex representations well. see https://github.com/mbostock/d3/wiki/quantitative-scales#linear-scales


Comments

Popular posts from this blog

html - Firefox flex bug applied to buttons? -

html - Missing border-right in select on Firefox -

c# - two queries in same method -