Given a bar graph with 2 data sets. How can I give each bar its own color?
svg.selectAll(".bar")
.data(data)
.enter().append("rect")
.style("fill", function(d,i) { console.log(i.fill) } // <- first red, second blue
I'm too lazy to set up a demo for what I think might work, but I imagine you want an array of colors like:
var colors = ['red', 'blue'];
then in your callback for fill
you can do like this:
return colors[i % colors.length];
If I'm correct in assuming i
is an index for the bar
you're on.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.