简体   繁体   English

如何使用我自己的颜色表而不是使用 d3 颜色集( interpolateviridis )来创建图例

[英]How to use my own color table instead of using d3 color set ( interpolateviridis ) to create legend

I have following set of color table where first value in the first array refers to point and following 3 values are RGB points.我有以下一组颜色表,其中第一个数组中的第一个值是指点,以下 3 个值是 RGB 点。 Ex: For the point 0.00, the RGB value should be 1.00, 0.00, 0.00.例如:对于点 0.00,RGB 值应为 1.00、0.00、0.00。 Currently I am using d3 interpolateViridis colors for me legend and that is not recommended and I am supposed to use my own points and color set.目前我正在为我的图例使用 d3 interpolateViridis 颜色,不推荐这样做,我应该使用我自己的点和颜色集。 Can anyone guide me on this.任何人都可以指导我。 I have also shared Fiddle link of my code.我还分享了我的代码的 Fiddle 链接。

"colorsTable": [
    [ 0.00, 1.00, 0.00, 0.00 ],
    [ 0.25, 0.71, 0.71, 0.00 ],
    [ 0.50, 0.00, 1.00, 0.00 ],
    [ 0.75, 0.00, 0.71, 0.71 ],
    [ 1.00, 0.00, 0.00, 1.00 ]
]

// defining d3 color set
var colorScale2 = d3.scaleSequential(d3.interpolateViridis).domain([0,0.38]);

// calling function and passing values
continuous("#legend2", colorScale2);

function continuous(selector_id, colorscale) {
// defining canvas and so on
// main logic
var legendscale = d3.scaleLinear()
.range([0, legendheight - margin.top - margin.bottom])
.domain(colorscale.domain());

var image = ctx.createImageData(1, legendheight);
d3.range(legendheight).forEach(function(i) {
var c = d3.rgb(colorscale(legendscale.invert(i)));
image.data[4*i] = c.r;
image.data[4*i + 1] = c.g;
image.data[4*i + 2] = c.b;
image.data[4*i + 3] = 255;
});
ctx.putImageData(image, 0, 0);
}

Fiddle link: https://jsfiddle.net/um863e5o/3/小提琴链接: https : //jsfiddle.net/um863e5o/3/

I may be misinterpreting your needs, but this should get you on the right track.我可能误解了您的需求,但这应该会让您走上正轨。 Since your "values" are evenly spaced, you can create an array of colors and then pass that to d3.interpolateRgbBasis .由于您的“值”间隔均匀,您可以创建一个颜色数组,然后将其传递给d3.interpolateRgbBasis You can then use this interpolator to create a color scale with your desired domain.然后,您可以使用此插值器创建具有所需域的色标。

// create an array of the colors
const colors = colorsTable
  .map(([value, ...color]) => d3.rgb(...color.map(d => d * 255)));

// create color scale
const colorScale = d3.scaleSequential()
  .domain([0, .38])
  .interpolator(d3.interpolateRgbBasis(colors));

The result looks like this:结果如下所示:

在此处输入图片说明

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

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