-
Notifications
You must be signed in to change notification settings - Fork 11.9k
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
Ability to configure visibility status of a dataset, to retain visibility state of datasets hidden by clicking Legend label #8451
Comments
Seems to be a problem in the react wrapper since as seen in this example when you hide a dataset and add a new one it stays hidden https://www.chartjs.org/samples/master/charts/line/basic.html Also if you want to implement this yourself you can do it by checking if a dataset is hidden by first getting the right legend item and checking its state (if its hidden or not): After you know its hidden or shown you can call the hide/show method for a dataset with the following line: EDIT: The show and hide are V3 notation, for V2 solution see #8451 (comment) |
Hello, just googling around and I came here looking for a way to programatically hide some information within the chart after has loaded. this instruction works but either of this two do not: there are no hide or show functions defined on the prototype. (I assume 'chart' happens to be the instance returned from new Chart( ... )) Thanks in advance |
@k1r0s guess you are using v2 of the lib, in that case you will have to do |
@LeeLenaleee I am not sure if this would be an issue in the React wrapper (@xr0master : any thoughts here?). I am currently implementing this logic by doing the bookkeeping in following manner:
I think a config option in dataset, to configure whether that dataset is hidden or not, would be of great value! The current logic we have to add for achieving this seems quite complicated. |
As you can see from the sample linked, the state is maintained internally. Is the wrapper destroying the chart on every update? |
@kurkle From what I observed during addition/removal of datasets, it did seem like the Chart was getting destroyed and re-created, because all the previously hidden lines are being re-displayed and there previous visibility state isn't being respected.
|
@kashifshamaz21 This is out of this project, but the chart will be re-created only if the canvas element has been unmounted/re-mounted. I think the Chart.js behavior is correct, since the datasets come again, the chart cannot know which of them was hidden before... @kurkle maybe it's possible to achieve by dataset id?
@kashifshamaz21 In the example, the reference to the dataset is saved and only pushed a new dataset inside, but not all data. It is not an easy task to keep the reference, especially for wrappers. It is necessary to get |
@xr0master Yeah I get your point. Hence my feature request to see if its possible for Chart.js to support a |
One option here is to wrap the datasets in |
You can also add |
Doesn't look like its documentated in the dataset properties for the charts https://www.chartjs.org/docs/master/charts/line#dataset-properties |
@kurkle Thanks for letting me know about this hidden property :D I tried using the Here's how I modified my code to try setting
And as pointed by @LeeLenaleee this property doesn't seem to be present in the Types: Any thoughts if the above y-axis showing up on the graph for hidden datasets is expected ? |
display: 'auto' on scales should hide the ones not having any datasets. |
@kurkle Awesome, your tips have solved this issue for me 👍 For anyone else who stumbles upon this issue, below is what is needed, to hide a dataset and its y-axis (if you are using separate y-axis per dataset)
Below are my final working code snippets: Method generating datasets (check "hidden" field being set on dataset):
Generating Chart options config:
@kurkle Thanks a lot for your help on this issue. One last thing, for fixing the |
Yeah, either that or create a PR for it :) |
Feature Proposal
I'm using Chart.js (v3.x.x) with React, using chartjs-react wrapper.
I would like to know if there is a way to configure a dataset to be
hidden
. The use case is described below, wherein, once a dataset is hidden by clicking on its legend label, the visibility of this dataset can't be retained when new datasets are added to the graph.Feature Use Case
When a dataset in a line chart gets hidden by clicking on its label in the legend, and then if a new dataset is added, the previously hidden dataset gets enabled/shown again as the component re-renders and I could not find a way to set the "visibility / hidden" status of a dataset in the docs.
Possible Implementation
Using the
generateLabels
andonClick
callbacks on Legend plugin, one can keep track of which fields are in hidden state.By exposing a hidden property in the dataset's config, consumers can configure which datasets they would like to be in hidden state.
The text was updated successfully, but these errors were encountered: