简体   繁体   English

为d3创建两种不同类型的热图

[英]Creating a heatmap of two different types for d3

Absolutely going insane trying to work this out, should have studied harder at school! 绝对疯狂尝试解决这个问题,应该在学校努力学习!

I'm wanting to achieve something that I'm convinced is quite simple to do, yet having a hard time understanding the terminology of it all. 我想实现我认为很容易实现的目标,但是很难理解所有术语。

I'm wanting to create a heatmap based on a choice of 1 of 2 settings. 我想基于2个设置中的1个创建热图。

First, if I have 5 countries, eg: 首先,如果我有5个国家/地区,例如:

  1. UK - 10000 英国-10000
  2. USA - 5000 美国-5000
  3. Brazil - 4000 巴西-4000
  4. Germany - 1000 德国-1000
  5. China - 500 中国-500

I'm trying to colours based on the ranking. 我正在尝试根据排名为颜色上色。 So for example if I had Red, Yellow, Green, Blue and Purple, then the following would be true: 因此,例如,如果我有红色,黄色,绿色,蓝色和紫色,则以下内容为真:

UK=Red, USA=Yellow, Brazil=Green, Germany=Blue and China=Purple. 英国=红色,美国=黄色,巴西=绿色,德国=蓝色,中国=紫色。

Secondly, I want to colour them on value, so in that case the following one apply: 其次,我想按值对它们进行着色,因此在这种情况下,适用以下条件:

UK=Red, USA=Green, Brazil=Greeny Blue, Germany=Bluey Purple, China=Purple. 英国=红色,美国=绿色,巴西=绿色蓝色,德国=蓝色紫色,中国=紫色。

But I've absolutely no idea if this is a linear, ordinal, quantile or what-have-you! 但是,我绝对不知道这是线性的,有序的,分位数的还是您所拥有的!

I've read the instructions and it looks like the Domain is the input and the Range is the output. 我已经阅读了说明,看起来域是输入,范围是输出。

I've got the following code: 我有以下代码:

 var colours = ['#FF0000', '#FFFF00', '#00FF00', '#0000FF', '#FF00FF'];
 var heatmapColour = d3.scale.linear().domain([0, sortedArray.length]).range(colours);

And in that example, for the 'Ranking' I'd expect each heatMapColour to return Red, Yellow, Green, Blue and Purple but I get 5 colours ranging from Red (first position) to Yellow (last position). 在该示例中,对于“排名”,我希望每个heatMapColour都返回红色,黄色,绿色,蓝色和紫色,但是我得到5种颜色,从红色(第一个位置)到黄色(最后一个位置)。

Any pointers? 有指针吗?

edit 编辑

As an example, this 'almost works' perfectly. 例如,此“几乎可以正常工作”。

var colours = ['#FF0000', '#FFFF00', '#00FF00', '#0000FF', '#FF00FF'];
var heatmapColour = d3.scale.quantize().domain([0, sortedArray.length-1]).range(colours);

If my array has 5 items, it produces the results one colour for each item, but if I have 10 items in my array, I have 2 red, 2 yellows, 2 greens, etc, rather than Red, Red->Orange, Orange->Yellow etc. 如果我的数组有5个项目,则它为每个项目产生一种颜色,但是如果我的数组中有10个项目,则我有2个红色,2个黄色,2个绿色等,而不是红色,红色->橙色,橙色->黄色等

Likewise if I have 3 items in the array, I get Red, Green, Purple then nothing. 同样,如果阵列中有3个项目,则得到红色,绿色,紫色,然后什么也没有。 It makes no sense! 这个不成立!

For this i like to use the RainbowVis.js [ https://github.com/anomal/RainbowVis-JS] to map numeric values to colors: 为此,我喜欢使用RainbowVis.js [ https://github.com/anomal/RainbowVis-JS]将数字值映射为颜色:

something like this: 像这样的东西:

var rainbow = new Rainbow();
rainbow.setSpectrum('green', 'red');
rainbow.setNumberRange(minValue, maxCalue);
return rainbow.colourAt(CountryValue);

You can add more colors to the gradient 您可以为渐变添加更多颜色

Still not sure I understand, but it sounds like you want a quantile scale: 仍然不确定我是否理解,但听起来您想要分位数刻度:

var x = d3.scale.quantile()
  .domain([0,500,1000,4000,5000,10000])
  .range(["#FF00FF", "#0000FF", "#00FF00", "#FFFF00", "#FF0000"]);

> x(400)
"#FF00FF"
> x(600)
"#0000FF"
> x(10001)
"#FF0000"

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

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