Ng2-charts: How to set colors?

Created on 19 Nov 2016  路  6Comments  路  Source: valor-software/ng2-charts

If my colors variable is:

[
    {backgroundColor: '#e84351'}, // red
    {backgroundColor: '#434a54'}, //grey
    {backgroundColor: '#3ebf9b'}, //green
    {backgroundColor: '#4d86dc'}, 
    {backgroundColor: '#f3af37'}
]

My graph looks like so:
image

What is the way to set colors in the latest version?

Most helpful comment

Answer:
You need to do like:

[{backgroundColor: ["#e84351", "#434a54", "#3ebf9b", "#4d86dc", "#f3af37"]}]

Not documented, so I won't close yet

All 6 comments

Answer:
You need to do like:

[{backgroundColor: ["#e84351", "#434a54", "#3ebf9b", "#4d86dc", "#f3af37"]}]

Not documented, so I won't close yet

I'm not sure why ng2-charts doesn't just follow the Chart.js API and define the colors in the dataset. At the very least if the colors attribute isn't specified when declaring the component then the Chart.js API should still work instead of ng2-charts selecting default colors of you.

Took me way too long to figure this out, thankfully I found this, I was just about to create an issue here stating that colors in the dataset are ignored.
I agree that it would be ideal if the chartjs API still worked as documented

@beck24 So, what solution did you find?

See the comment above by @AmitMY
That's the solution

Full answer:
For a single data series, the coloring is:
[{backgroundColor: ["#e84351", "#434a54", "#3ebf9b", "#4d86dc", "#f3af37"]}]

For multiple data series, the coloring is:

[
    {backgroundColor: '#e84351'},
    {backgroundColor: '#434a54'}, 
    {backgroundColor: '#3ebf9b'},
    {backgroundColor: '#4d86dc'}, 
    {backgroundColor: '#f3af37'}
]
Was this page helpful?
0 / 5 - 0 ratings

Related issues

raychenfj picture raychenfj  路  3Comments

mrpotato3 picture mrpotato3  路  5Comments

shyamal890 picture shyamal890  路  4Comments

grahammutter picture grahammutter  路  4Comments

Maistho picture Maistho  路  3Comments