仅有两个选项的barChart dc.js中的.colors函数 [英] .colors function in barChart dc.js with only two options

查看:76
本文介绍了仅有两个选项的barChart dc.js中的.colors函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个颜色函数,当该值为负数时显示红色,否则在条形图中显示为绿色.

I want to create a color function which shows red when the value is negative and green otherwise in a barChart.

这是到目前为止我要提出的内容:

Here is what I've come up with so far :

var colorChoice = d3.scale.ordinal().domain(["positive","negative"])
                                    .range(["#00FF00","#FF0000"]);
hitsPerDaybarChart
.colors(function(d) {return(colorChoice((d.hits>0) ? "positive":"negative"));})

但是我收到以下错误消息:TypeError: Object function (d) {return(colorChoice((d.pnl>0) ? "positive":"negative"));} has no method 'range'.

But I get the following error message : TypeError: Object function (d) {return(colorChoice((d.pnl>0) ? "positive":"negative"));} has no method 'range'.

欢迎所有帮助.谢谢.

推荐答案

您将要使用

You will want to use the colorAccessor function.

1)定义您的颜色范围和域:

1) Define your color range and domain:

.colors(d3.scale.ordinal().domain(["positive", "negative"])
                          .range(["#00FF00", "#FF0000"]))

2)定义您的颜色访问器:

2) Define your color accessor:

.colorAccessor(function(d) {
  if (d.value > 0) {
    return "positive";
  }
  return "negative";
})

希望这个JSFiddle帮助: http://jsfiddle.net/djmartin_umich/9ELWV/

Hopefully this JSFiddle helps: http://jsfiddle.net/djmartin_umich/9ELWV/

注意:对于最新版本的d3.js,请使用d3.scaleOrdinal()而不是d3.scale.ordinal().

Note: with the latest versions of d3.js, use d3.scaleOrdinal() instead of d3.scale.ordinal().

这篇关于仅有两个选项的barChart dc.js中的.colors函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆