Rahul Desai Rahul Desai - 1 year ago 179
Javascript Question

How to assign random colors to D3 bar chart?

I am working on a D3 bar chart as per the mock-up below:

Bar Chart

How do I make the bars to have random colors?



.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")

Answer Source

d3 has 4 built in color palettes.

Here's the link for the built in color palettes.

This tutorial is good on using specific colors for specific element.

Another tutorial by Jerome Cukier.

And the official site for d3 colors.

Fiddle - Note: In the fiddle I've passed the colors by adding colors in the data.

It can even be done by passing the colors from a different variables.

Hope this helps.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download