-
Notifications
You must be signed in to change notification settings - Fork 12k
Description
Expected behavior
the type of the parameter mode here:
Line 519 in 44b97b8
| update(mode?: UpdateMode): void; |
should allow a function aswell.
In this method which is called by chart.update, the parameter mode is checked if it is a function.
Chart.js/src/core/core.controller.js
Line 676 in 44b97b8
| this._updateDataset(i, isFunction(mode) ? mode({datasetIndex: i}) : mode); |
Current behavior
The current type definition of the parameter mode is UpdateMode:
Lines 570 to 580 in 44b97b8
| export declare enum UpdateModeEnum { | |
| resize = 'resize', | |
| reset = 'reset', | |
| none = 'none', | |
| hide = 'hide', | |
| show = 'show', | |
| default = 'default', | |
| active = 'active' | |
| } | |
| export type UpdateMode = keyof typeof UpdateModeEnum; |
Reproducible sample
Optional extra steps/info to reproduce
No response
Possible solution
change the type of mode to a union type: UpdateMode | ((ctx: { datasetIndex: number }) => UpdateMode)
Context
I stumpled across this issue when trying to change the visibility of multiple datasets with one legend label.
my solution required passing a function to chart.update in the overwritten plugins.legend.onClick method:
onClick(e, legendItem, legend) {
const isNowVisible = !legend.chart.isDatasetVisible(legendItem.datasetIndex);
const mode = isNowVisible ? 'show' : 'hide';
if (isNowVisible) {
legend.chart.show(legendItem.datasetIndex);
legend.chart.show(anotherIndex);
} else {
legend.chart.hide(legendItem.datasetIndex);
legend.chart.hide(anotherIndex);
}
(legend.chart as any).update((ctx: { datasetIndex: number }) => [legendItem.datasetIndex, anotherIndex].includes(ctx.datasetIndex) ? mode : undefined);
}chart.js version
v4.4.0
Browser name and version
No response
Link to your project
No response