Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

typescript type incorrect for chart.update(mode) #11487

Closed
Cazka opened this issue Sep 6, 2023 · 1 comment
Closed

typescript type incorrect for chart.update(mode) #11487

Cazka opened this issue Sep 6, 2023 · 1 comment
Labels
type: bug type: types Typescript type changes
Milestone

Comments

@Cazka
Copy link
Contributor

Cazka commented Sep 6, 2023

Expected behavior

the type of the parameter mode here:

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.

this._updateDataset(i, isFunction(mode) ? mode({datasetIndex: i}) : mode);

Current behavior

The current type definition of the parameter mode is UpdateMode:

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

https://www.typescriptlang.org/play?#code/JYWwDg9gTgLgBAbzgYQBYENZwL5wGZQQhwDkAxhrAHQBWAziQNwCwAUGwG6ZxkwAecALxwAJhDIBXEAFMAdjCoBzaTACiAG2kz5AIQCeASREAKAEQg9aTDFMBKOOjpwAEgBUAsgBlk6WVzoaWnIwLKxcUHAWVljCstIA7iiUMMa8fAA0iGxwOXAwemDSAFyk6sBxJOnZuSLoMOglCNW5ueroAEbS6nQlANqmAErSIqaZpjrqEtKjcKYAml3qEPEzpgDiUNJyqwAKElBgmqsA8lC+yqYAulWsLS219XQqPXC9Tbd3n22d6iUkAMRwCB4OAANQgMGkDBun0+DwarwAjAAmTKIgCcmQAzJkAKyZVFwLHXZp3bCXUnYG7YWyhNhRZJUCRgB7SYypfiNUR1RwqAyyETSPglWRSToRGlCAB8cAA9AAqOCyCFweLQADW5UU8tltKAA

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

@Cazka Cazka added the type: bug label Sep 6, 2023
@etimberg etimberg added this to the Version 4.4.1 milestone Sep 6, 2023
@etimberg etimberg added the type: types Typescript type changes label Sep 6, 2023
@LeeLenaleee
Copy link
Collaborator

Resolved in #11488

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: bug type: types Typescript type changes
Projects
None yet
Development

No branches or pull requests

3 participants