d3 if / else设置颜色

时间:2015-08-03 13:31:28

标签: javascript html d3.js

我是js中的最新版本,无法使用d3设置文本颜色:

if let indexPath = sender as NSIndexPath

函数在console.log中返回int但未设置颜色

1 个答案:

答案 0 :(得分:1)

看来你错过了关键字" return"在if语句中确定要使用的颜色......

if(parseInt(x.AMOUNT_PA)<=30)
                    { return "red"; }
                else {}
                 }))

查看以下JSFiddle

http://jsfiddle.net/heavyhorse/4p0sewhe/2/

body.selectAll('h1')
.data(dataset)
.enter()
.append('h1')
.classed('data-label',true)
.text(function(d){ return d.val; })
.style('color',function(d) {
    if(d.val > 30) {
        return "black";
    } else {
        return "red";
    }
});