-
Notifications
You must be signed in to change notification settings - Fork 1.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Simple way to color bar chart #200
Comments
Hi, I ended up solving this using the css way. Here it is summarised for anyone else trying to do it: (Assumes trying to show stacks for three categories by year, totalling their "value", by year "yearDimension")
This means that it creates a bunch of zero values in a stack so that stack0 always remains the same colour, even if there are no values in that stack (the bottom stack would normally default to stack0, even if that botom stack was category1 rather than category2
And then in the css, this, where stack0 = category1, stack1= category2 and stack2=category3
If anyone would care to share an easier way to do it direct within dc.js I' be keen to hear it! |
The renderlet solution should work however you need to set the fill to color instead of css class: .renderlet(function(chart){
chart.selectAll("g.rect.stack").attr("fill", function(d){
if(d.key == "a) Free")
return "green";
else if(d.key == "b) Partly Free")
return "yellow";
else if(d.key == "c) Not Free")
return "red";
});}) Also make sure remove fill setting in your css class since in some browser the css class take precedence over fill attribute. |
In case anyone still runs into something like this, Nick's solution works fine for me but only operating on the chart selection with "style" rather than "attr", i.e. |
I was able to create multi-series bar chart with a twist of the renderlet technique explained above . renderlet(function (chart) {
chart.selectAll('rect.bar').each(function(d){
d3.select(this).attr("fill",
(function(d){
var colorcode ="grey"
if(d.x[1] === "Zone1")
colorcode ="#ff7373";
else if(d.x[1] === "Zone2")
colorcode ="#b0e0e6";
else if(d.x[1] === "Zone3")
colorcode ="#c0c0c0";
else if(d.x[1] === "Zone4")
colorcode ="#003366";
else if(d.x[1] === "Zone5")
colorcode ="#ffa500";
else if(d.x[1] === "Zone6")
colorcode ="#468499";
else if(d.x[1] === "Zone7")
colorcode ="#660066";
return colorcode;
}))
});
}); Note : I was using a dimension with 2 values for the series . Thanks to all comments and feel free to use this. I think with some development over this technique could yield us a multi-bar chart within the dc.charts library . |
Thank you @rajeshr6r for this great contribution. For some reason, it did not work for me. Do you have a working example? Thanks. |
Hi I do Hv one . Will share as soon as I get to my laptop . I'm hoping you are us dc 2.0 Sent from my iPhone
|
Thank you @gordonwoodhull! |
Hi again Rajesh, I will be very happy if you can share you working bar chart example. Thanks, Murat From: Rajesh Rajamani [mailto:[email protected]] Hi I do Hv one . Will share as soon as I get to my laptop . I'm hoping you are us dc 2.0 Sent from my iPhone
— |
The |
The below works instead :
|
|
Use pretransition instead of renderlet to avoid the flicker. |
Thanks @gordonwoodhull , works like a charm. Anyone looking, here's the no flickering solution with bar color, legend color on
|
With |
Here's the solution for clicking on a bar not dimming the other unselected stackedbars.
|
@nitinsurana what is chart.selectAll('g.dc-legend-item rect') for? |
Hi.
I'm trying to color a stacked bar chart, and I've looked at some other complicated solutions, but I need something simple!
I am trying this which seems to be a hotch-potch of code. I've tried the selectAll with about every combination: rect.bar, rect.stack, g.rect.bar etc.
Anyone help me get over this?
The text was updated successfully, but these errors were encountered: