Chart.js icon indicating copy to clipboard operation
Chart.js copied to clipboard

backgroundColor to allow type string[] and scriptable string[]

Open LovelyAndy opened this issue 3 years ago • 3 comments

Feature Proposal

I've been attempting to get variable backgroundcolors for a doughnut charts based on their value. Each would have a variable color, followed by a base color (grey). The idea I had was to have a few if statements to check the value and then render the correct array of colors, but the current typing does not allow for an array. It would be a great fix to allow you to use arrays as the background color with ts support.

There's a type mistake in the source code where it doesn't allow something that's possible with just JS. It renders correctly if I cast to any but TS doesn't allow it:

ChartOptions<'doughnut'>
// throws a Type error at backgroundColor
{
  backgroundColor: ((ctx) => {
    return ['blue', 'grey']
  })
}

Looking at the source code, I found that now it says:

backgroundColor: Scriptable<Color, ScriptableContext<TType>>;
// only accepts string or a fn that returns a string

it will allow a string array if I change it to

backgroundColor: ScriptableAndArray<Color, ScriptableContext<TType>>;

but what I need is to allow a function that returns a string array. Wasn't sure how to do that!

Possible Implementation

No response

LovelyAndy avatar Feb 03 '22 15:02 LovelyAndy

~~The scriptable function is evaluated for each individual part, and it is not supported to return an array of colors for each individual slice so the current behaviour is correnct~~

Seems I am wrong

LeeLenaleee avatar Feb 03 '22 15:02 LeeLenaleee