简体   繁体   English

甜甜圈图-将域映射到颜色范围(过渡)

[英]Donut Chart - map domain to range of colors (transition)

I am trying to make a 'donut' chart and I am currently struggling with D3 scales and colors. 我正在尝试制作“甜甜圈”图表,而我目前正在努力使用D3比例和颜色。 If you open my current chart: https://jsfiddle.net/dtr7hrg2/ , you will notice that the values 0 and 5.26 share exactly the same color. 如果打开我当前的图表: https : //jsfiddle.net/dtr7hrg2/ ,您将注意到值0和5.26共享完全相同的颜色。 I guess that is because those two values fall within the same range. 我猜这是因为这两个值在同一范围内。 My domain is [0, 100]. 我的域名是[0,100]。 What I want to achieve is to map this domain into a range [0, 100] which corresponds to a linear transition between following colors ["#000000", "#5F192A", "#B12848", "#EC335C"] where 0.0 falls into #000 and 100.0 falls into #B12848 . 我要实现的是将该域映射到范围[ ["#000000", "#5F192A", "#B12848", "#EC335C"] ],该范围对应于以下颜色["#000000", "#5F192A", "#B12848", "#EC335C"]之间的线性过渡落入#000而100.0落入#B12848 Could somebody give me a hint what I am doing wrong? 有人可以提示我我做错了什么吗? Any help will be appreciated! 任何帮助将不胜感激!

If you want a linear range of colors between #000000 and #B12848 you can create an interpolation function and scale its input to be between 0.0 and 1.0: 如果要在#000000#B12848之间设置线性的颜色范围,则可以创建一个插值函数并将其输入缩放到0.0到1.0之间:

var chart = d3.select("#chart");

var color = d3.interpolate('#000000', '#B12848');

for (var i = 0; i <= 100; i++) {
    chart.append('span').attr('class', 'colorBlock').attr('style', function (d) {
        return 'background-color: ' + color(i / 100.0);
    });
}

Here's a working fiddle 这是一个工作的小提琴

But, as Tim B points out the colors between 0 and 5 are barely discernible. 但是,正如Tim B指出的那样,几乎看不到0到5之间的颜色。

Its not the exact same color, what you did is correct 它的颜色不完全相同,您所做的是正确的

console.log(heat_map_color(5)) // rgb(14, 4, 6)
console.log(heat_map_color(0)) // rgb(0, 0, 0)

Those colors are so close you can't see the difference. 这些颜色是如此接近,看不到差异。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM