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

Global defaults have no effect for doughnuts and pie charts.

Open santam85 opened this issue 3 years ago • 14 comments

Expected behavior

Doughnut and pie charts use global datasets defaults or arc elements defaults for backgroundColor.

Polar area charts scriptable property dataIndex seems dependent on the property type.

Current behavior

The global defaults have no effect. Scriptable options funcitons are not called.

Reproducible sample

https://codepen.io/leelenaleee/pen/WNyJXEe

Optional extra steps/info to reproduce

No response

Possible solution

No response

Context

Testing new Chart.js version for ng2-charts

chart.js version

v4.0.1

Browser name and version

No response

Link to your project

No response

santam85 avatar Nov 30 '22 13:11 santam85

I think this is related to new color plugin of version 4, see https://www.chartjs.org/docs/latest/general/colors.html#per-dataset-color-settings.

I think it's a bug of the plugin, on the following code:

https://github.com/chartjs/Chart.js/blob/82395678024670b0a829b7780c73158ddac27062/src/plugins/plugin.colors.ts#L95-L99

I see 2 things:

  1. the plugin is using chart.config but it doesn't seem to be a proxy therefore doesn't have the fallback to the defaults. It's (AFAIK) the user configuration, ignoring defaults both on controller and elements.
  2. type is not used anymore after PR #10904

stockiNail avatar Nov 30 '22 14:11 stockiNail

Nice find, thanks! Given the addition of this new plugin in v4, as a maintainer of the Angular wrapper, I wonder if I should remove the color generation feature and just delegate to the new plugin, any opinions from the Chart.js maintainers?

santam85 avatar Nov 30 '22 14:11 santam85

@santam85 For tests you can disable this plugin by options.plugins.colors.enabled option.

dangreen avatar Nov 30 '22 14:11 dangreen

I don't think the colors plugin can resort to fallback colors, because then there always is a color already.

The plugin can also be disabled globally:

Chart.defaults.plugins.colors.enabled = false;

kurkle avatar Nov 30 '22 18:11 kurkle

@kurkle exactly what I'm thinking. No options apart disable it, globally.

stockiNail avatar Nov 30 '22 18:11 stockiNail

Yes that's what I did. I can confirm that it fixes my issue, so for now I'll go ahead and disable the color plugin when generating colors myself.

santam85 avatar Nov 30 '22 18:11 santam85

Another option, for the colors plugin, could be to attach as a scriptable global default. But it has performance impact: would always have scriptable options, so no option sharing between elements.

kurkle avatar Nov 30 '22 18:11 kurkle

@kurkle for my understanding, do you mean the plugin will set a scriptable option in the defaults? The plugin will just set the scriptable option and the logic is overthere, correct?

stockiNail avatar Nov 30 '22 18:11 stockiNail

Anyway, even if the color plugin is a feature and not a breaking change, I think something could be reported in the migration guide because this is a common use case which is broken. WDYT?

stockiNail avatar Nov 30 '22 18:11 stockiNail

Another option, for the colors plugin, could be to attach as a scriptable global default. But it has performance impact: would always have scriptable options, so no option sharing between elements.

That's what I've gone for in the Angular wrapper lib

santam85 avatar Nov 30 '22 18:11 santam85

I can make a fix for this case.

dangreen avatar Nov 30 '22 18:11 dangreen

It is impossible to detect whether the defaults changed or not on the plugin level. So I suggest accepting this as expected behavior and closing the issue.

I think something could be reported in the migration guide

It is a valid problem only for the UMD bundle, where all plugins are enabled by default. We have a notice about that in the docs:

https://www.chartjs.org/docs/latest/general/colors.html

If you are using the UMD version of Chart.js, this plugin will be enabled by default. You can disable it by setting the enabled option to false

dangreen avatar Dec 09 '22 11:12 dangreen

@santam85 @stockiNail @kurkle what do you think?

dangreen avatar Dec 14 '22 18:12 dangreen

@santam85 @stockiNail @kurkle what do you think?

Fine for me

stockiNail avatar Dec 14 '22 20:12 stockiNail