Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
550 views
in Technique[技术] by (71.8m points)

javascript - d3 mapping 4 colors to 4 numeric ranges to look a bit like a very basic heatmap

I am trying to get this to work as such:

  • 0 = gray
  • 1-33.33 = green
  • 33.34-66.66 = yellow
  • 66.67-100 = red

It is currently not working but I do have a code example here: http://jsfiddle.net/JLSt4/1/

This is the specific block of code:

    var colors = ["#CCCCCC","#55AF29","#FFD017","#B72E00"];

        var heatmapColor = d3.scale.linear()
            .domain(d3.range(0, 1, 1.0 / (colors.length - 1)))
          .range(colors);

        var getData = d3.extent(jsondata.kpi, function (d) { return +d.status });
        var c = d3.scale.linear().domain(getData).range([0, 1]); 

and the display:

    .style("fill", function(d) { return heatmapColor(c(d))});

I've been working with the answer to this question but it isn't quite what I'm looking for since this maps dynamic values to a color set: D3: Create a continuous color scale with many strings/inputs for the range and dynamically changing values of the domain

Any help is appreciated!!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

It sounds like what you want is a quantize scale or a threshold scale -- a scale that takes a continuous input value and splits it into bins for conversion to a discrete set of output values.

For quantize scales, you specify the domain as normal ([min,max]), and the size of the bins is determined by the number of values in the range, such that each bin is the same size.

For threshold scales, you specify the domain as the set of threshold values above which the next value from the range should be used; there should be one fewer threshold values then there are range values.

The version that @FernOfTheAndes got working for you is a polylinear scale: a linear scale with different domain/range blocks. Values in between two points of the domain are converted to a value that is the same distance in between the corresponding two points of the range.

So for a polylinear scale with domain [0,33,66,100] and range [gray,green,yellow,red] a value of 0 gets mapped to gray, a value of 33 gets mapped to green, but a value of 15 gets mapped to a gray-green colour halfway in between.

In contrast, for a threshold scale with domain [1,33,66] and range[gray,green,yellow,red], any value greater or equal to 1 but less than 33 will get mapped to pure green.

Alternately, you could use a quantize scale with domain [0,100], range [green,yellow,red], and use a separate test to assign the colour gray when the value is 0.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...