D3: Create a continuous color scale with many strings/inputs for the range and dynamically changing values of the domain

后端 未结 3 785
心在旅途
心在旅途 2021-01-31 10:19

I am trying to create a linear color scale for a heatmap. I want to color scale to go through a large set of specific colors, where the first color corresponds to the min of the

3条回答
  •  栀梦
    栀梦 (楼主)
    2021-01-31 11:02

    Use a Quantitative Scale plus Color Brewer

    // pick any number [3-9]
    var numColors = 9;
    
    var heatmapColour = d3.scale.quantize()
      .domain(d3.extent(dataset))
      .range(colorbrewer.Reds[numColors]);
    
    // use the heatmap to fill in a canvas or whatever you want to do...
    canvas.append("svg:rect")
      .data(dataset)
      .enter()
      // snip...
      .style("fill", function(d) {return heatmapColour(d);})
    

提交回复
热议问题