Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NVD3.js coloring specific bars in graph

Tags:

svg

d3.js

nvd3.js

Is there a way to color specific bars? If a bar is less than the line, color it red.

Code: https://github.com/tvinci/webs/blob/gh-pages/lineplusbar.html

Example: http://tvinci.github.io/webs/lineplusbar.html

I'd like to do something like this but the value for i is not the y value that I send in. It has been modified:

d3.selectAll("rect.nv-bar")
    .style("fill", function(d, i){
        return i > 50 ? "red":"blue";
    });

Data:

var overview_data=[
     {
        "key" : "Achieved",
        "bar": true,
        "values" : [ [ "1x" , 30] , [ "2x" , 70] , [ "3x" , 200] ]
     },
     {
        "key" : "Required",
        "values" : [ [ "1x" , 50] , [ "2x" , 100] , [ "3x" , 150] ]
     }
].map(function(series) {
    series.values = series.values.map(function(d) { return {x: d[0], y: d[1] } });
    return series;
});
like image 687
user2265754 Avatar asked Dec 21 '25 17:12

user2265754


1 Answers

You can do:

d3.selectAll("rect.nv-bar")
    .style("fill", function(d, i){
        return d.y > 50 ? "red":"blue";
    });
like image 181
Cihan Keser Avatar answered Dec 24 '25 10:12

Cihan Keser



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!