pepstock-org / Charba

J2CL and GWT Charts library based on CHART.JS
https://pepstock-org.github.io/Charba-Wiki/docs
Apache License 2.0
62 stars 6 forks source link

setDatasetVisibility does not have any effect #103

Closed AHijner closed 2 months ago

AHijner commented 2 months ago

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 by setDatasetVisibility(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. The getDatasetVisibility 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 the setDatasetVisibility and is there a possible workaround?

We've tried using chart.hide(datasetIndex) but this does not seem to do what we want either.

stockiNail commented 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.

Set dataset visibility by API after chart creation

Let's use https://pepstock-org.github.io/Charba-Wiki/docs/charts/Api#setdatasetvisibility

Get dataset visibility by API after chart creation

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:

Restore

read from storage --> dataset.setHidden(value) --> create chart

Store

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!

AHijner commented 2 months ago

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?

stockiNail commented 2 months ago

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:

https://github.com/pepstock-org/Charba/blob/a04bb9a2ba2f4254b68e2428ee9416110340d4f3/src/org/pepstock/charba/client/data/LineDataset.java#L115-L123

or by the chart:

https://github.com/pepstock-org/Charba/blob/a04bb9a2ba2f4254b68e2428ee9416110340d4f3/src/org/pepstock/charba/client/LineChart.java#L69-L77

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!

AHijner commented 2 months ago

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!