简体   繁体   English

基于 c3.js 中的值的条形图颜色

[英]Bar graph colors based on value in c3.js

I have the following bar chart in c3.js (here's a fiddle) :我在 c3.js 中有以下条形图(这是一个小提琴)

var chart = c3.generate({

  data: {
    x: 'Letter',
    columns:
    [
      ['Letter', 'A','B','C','D'],
      ['value', 25,50,75,100]
    ],

    type: 'bar',

        colors: {
        value: '#FF0000'
    },

  },
  axis: {
    x: {
      type: 'category'
    }
  },
  legend: {
    show: false
  }
});

This sets all the bars to red.这会将所有条形设置为红色。 What I would like is the colors of the bars to be this gradient from FF0000 to 10FF00, which I believe is just in increments of adding 4096 to the decimal value of the color.我想是吧的颜色是这个梯度从FF0000到10FF00,我相信这是刚刚加入4096色的十进制值的增量。

I'd like the gradient to start at 50 and end at 100 (that is, anything green and below would be solid red FF0000, and 100 would be green 10FF00).我希望渐变从 50 开始到 100 结束(也就是说,任何绿色及以下的都是纯红色 FF0000,而 100 是绿色的 10FF00)。

I was trying to follow this example to set the color of a data point on a c3.js graph based on its value.我试图按照这个例子来根据它的值在 c3.js 图表上设置数据点的颜色。 In the example, the value of data3 is darkened as its value goes up.在示例中, data3的值随着其值的增加而变暗。 I've been trying to modify the color: function (color, d) , but I can't seem to get it to do anything that I want it to.我一直在尝试修改color: function (color, d) ,但我似乎无法让它做任何我想做的事情。

Any help would be appreciated.任何帮助,将不胜感激。 Thanks!谢谢!

I'm not sure what you did inside color: function , but it is the correct way to set it up.我不确定您在color: function做了什么,但这是设置它的正确方法。 Regarding your desired gradient, it cannot be achieved by just increasing its hex value because going from yellow to green requires decreasing the value.关于您想要的渐变,仅通过增加其十六进制值是无法实现的,因为从黄色变为绿色需要减小该值。 The following example goes from red to yellow:以下示例从红色变为黄色:

var chart = c3.generate({
  data: {
    x: 'Letter',
    columns:
    [
      ['Letter', 'A','B','C','D'],
      ['value', 25,50,75,100]
    ],
    type: 'bar',
    colors: {
      value: function(d) {
        return '#'+(0xff0000+(d.value-25)*256*3).toString(16);
      }
    },
  },
  axis: {
    x: {
      type: 'category
    }
  },
  legend: {
    show: false
  }
});

fiddle小提琴

you can adding in your css like a你可以像这样添加你的css

.c3-bar-0 {
   fill:rgb(204, 0, 102)!important;
}
.c3-bar-1 {
   fill:rgb(51, 153, 255)!important;
}
.c3-bar-2 {
   fill: rgb(0, 255, 204)!important;
}

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

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