Closed AHijner closed 2 months ago
@AHijner the dataset hiding is always a tough topic.
The setHidden(hideValue)
setter at dataset level should be used only when you are creating a chart, therefore to start with a hidden dataset.
This is because Chart.js is not using that options to track the dataset visibility and it should be used only at chart creation.
After the creation you should use the API at chart level to hide, show and check the dataset visibility.
Let's use https://pepstock-org.github.io/Charba-Wiki/docs/charts/Api#setdatasetvisibility
Let's use https://pepstock-org.github.io/Charba-Wiki/docs/charts/Api#isdatasetvisible
Coming back to your use case, splitting in store
and restore
the dataset visibility, I think you could:
read from storage --> dataset.setHidden(value) --> create chart
chart.isDatasetVisible(datasetIndex) --> write to storage
During the chart lifecycle, you could have use cases where you want to show/hide the datasets on top to the out-of-the-box way based on the chart legend.
Let me know if this can help you!
Hi, yes previously we used the dataset.setHidden(value)
to restore, this worked well. However since Charba version 4.2 it was my understanding that this method is no longer available to use. Is there another alternative for it?
Yeah, you are right! Apologize... I'm getting old and my memory is not so persistent... forgive me.
Well, yes, setHidden
method was changed as protected
and not public
anymore.
This was done because, as explained above, the hidden options is used only during the chart init and couldn't be used to hide/show the dataset.
For this reason, the hidden flag has been moved in the dataset constructor.
For instance, for line dataset:
or by the chart:
Coming back to your use case, to restore the user preferences for dataset:
read from storage --> create chart --> create dataset by chart.newDataset(hiddenUserPreference)
or
read from storage --> create chart --> new dataset (hiddenUserPreference)
Let me always know if it will work for you. Thank you!
Hi, alright that makes a lot of sense! We will have to reorganize our code a bit (we now try to do the set hidden generically, so we can't easily change the specific dataset creations), but we have some ideas already of how to make it work!
Thank you for your extensive responses!
We have been happily using charba for a while now, but recently discovered a small bug in our software. We have some feature where we 'remember' which datasets have been hidden by the user, to persist them after recreating the chart. We used to do this by setting the value using
dataSetet.setHidden(hideValue)
, however since the update to Charba 4.2 this method has been replace bysetDatasetVisibility(Position i, Boolean hideValue)
and so we replaced it. However we now noticed it is not working as desired, the value does not seem to have any influence. ThegetDatasetVisibility
works well, but the value is the same right before and after setting it. When we are setting these values the chart has not yet been drawn.We are wondering, are we using the
setDatasetVisibility
in the wrong way? Ie. should we only use it after the chart has been drawn. Or is there a small bug in either Charba or ChartJS with thesetDatasetVisibility
and is there a possible workaround?We've tried using
chart.hide(datasetIndex)
but this does not seem to do what we want either.