简体   繁体   English

d3 Choropleth映射-正确的域/范围配置

[英]d3 choropleth map - proper domain/range config

I am currently working on a d3 choropleth map. 我目前正在制作d3弦图。 I am more specifically working to configure the scale based on incoming data. 我更具体地工作是根据传入数据配置比例。 I have two middle data points expressing where to set the color separation. 我有两个中间数据点,表示要在哪里设置分色。

For example, I have the domain of [0,1]. 例如,我的域为[0,1]。 Color scale of 3 elements, ['#FF9489', '#FFED89', '#83F5A2'](red, yellow, green). 3个元素的色阶,['#FF9489','#FFED89','#83F5A2'](红色,黄色,绿色)。 The data point given to divide the colors properly are good_bound, bad_bound(green = good, red = bad). 正确划分颜色的数据点是good_bound,bad_bound(绿色=良好,红色=不良)。 good_bound = 0.9, bad_bound = 0.8. good_bound = 0.9,bad_bound = 0.8。 I want the map to show bound(red) < 0.8, 0.8 <= bound(yellow) < 0.9, 0.9 < bound(green). 我希望地图显示bound(红色)<0.8、0.8 <= bound(黄色)<0.9、0.9 <bound(绿色)。

I am understanding that the range method called off of d3.scale.quantize(this is the method I am using instead of threshold) works off of the domain given and range specified, dividing it evenly. 我了解到,从d3.scale.quantize(这是我使用的方法,而不是阈值)中调用的range方法在给定的域和指定的范围内工作,将其平均划分。

options.color = ['#FF9489', '#FFED89', '#83F5A2']
var colorScale = d3.scale.threshold()
  .domain([1,0].concat())
  .range(options.color.concat().reverse().slice(0, 3))

Right now I am only able to get it to work by passing in the domain as [good_bound,bad_bound] and it ends up with a domain of .8 to .9, which is obviously not what is required. 现在,我只能通过将域传入[good_bound,bad_bound]来使其正常工作,并且最终以.8到.9的域结束,这显然不是必需的。 I was trying to figure out a way to override the d3.scale.range method but I am not understanding the structure. 我试图找出一种方法来覆盖d3.scale.range方法,但我不了解其结构。

The end correct result needs to be 0%-80% - red, 80%-90% - yellow, 90%-100% - green. 最终正确结果需要为0%-80%-红色,80%-90%-黄色,90%-100%-绿色。

I am not sure what other code snippets I might need to include without it being irrelevant to the issue, please let me know and I can include it. 我不确定在与问题无关的情况下可能需要包括哪些其他代码片段,请让我知道,我可以将其包括在内。

Scales domains aren't limited to two inputs. 比例域不限于两个输入。 If you want to map ranges you just specify them out: 如果要映射范围,只需将其指定出来:

 var options = {}; options.color = ['red', 'yellow', 'green']; var colorScale = d3.scale.threshold() .domain([80,90,101]) .range(options.color); console.log('0 is red - ' + colorScale(0)); console.log('50 is red - ' + colorScale(50)); console.log('80 is yellow - ' + colorScale(80)); console.log('88 is yellow - ' + colorScale(88)); console.log('90 is green - ' + colorScale(90)); console.log('95 is green - ' + colorScale(95)); console.log('100 is green - ' + colorScale(100)); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js"></script> 

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

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