Cha*_*apo 4 colors bar-chart d3.js dc.js
我想创建一个颜色函数,当值为负时显示红色,否则在barChart中显示绿色.
这是我到目前为止所提出的:
var colorChoice = d3.scale.ordinal().domain(["positive","negative"])
.range(["#00FF00","#FF0000"]);
hitsPerDaybarChart
.colors(function(d) {return(colorChoice((d.hits>0) ? "positive":"negative"));})
Run Code Online (Sandbox Code Playgroud)
但是我收到以下错误消息:TypeError: Object function (d) {return(colorChoice((d.pnl>0) ? "positive":"negative"));} has no method 'range'.
欢迎所有帮助.谢谢.
DJ *_*tin 10
您将需要使用该colorAccessor功能.
1)定义您的颜色范围和域:
.colors(d3.scale.ordinal().domain(["positive", "negative"])
.range(["#00FF00", "#FF0000"]))
Run Code Online (Sandbox Code Playgroud)
2)定义颜色访问器:
.colorAccessor(function(d) {
if (d.value > 0) {
return "positive";
}
return "negative";
})
Run Code Online (Sandbox Code Playgroud)
希望这个JSFiddle帮助:http://jsfiddle.net/djmartin_umich/9ELWV/
注意:使用最新版本的d3.js,请使用d3.scaleOrdinal()而不是d3.scale.ordinal().