如何使用d3量程?

时间:2016-01-13 01:48:30

标签: d3.js

在d3中是否可以使用自定义颜色范围值为对象着色?

我有两个json: 一个是有价值观:

staging_dir

另一个是范围:

[
  {
    "area": "amsterdamn",
    "KPIValue": 98.46
  },
  {
    "area": "brunei",
    "KPIValue": 95.98
  }
]

如何使用值在范围内使用颜色填充路径?

1 个答案:

答案 0 :(得分:2)

Quantile scales应该完成这项工作。

// define a variable thresholds as an array of threshold values
// using the lower range
var thresholds = range.map(function(rec) { return rec.lowerRange})

// define a variable colors as an array of color values
var colors = range.map(function(rec) { return rec.color})

// define the scale
var t = d3.scale.quantile().domain(thresholds).range(colors)

// use it
t(KPIValue) // returns the corresponding color

这是working demo