-
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
Cache sorting order of dataset metas #6757
Conversation
@@ -446,8 +447,11 @@ helpers.extend(Chart.prototype, /** @lends Chart */ { | |||
meta.controller = new ControllerClass(me, i); | |||
newControllers.push(meta.controller); | |||
} | |||
sorted.push(meta); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder if we should check if the dataset is visible here so that we don't have to do it each time _getSortedDatasetMetas
is called
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think we necessarily need a new property visible
cause that's basically just !hidden
, right?
I was wondering if we could do the filtering of only visible datasets here to cache that as well, but on second look it seems like that's not possible because the legend shows the labels for visible and non-visible datasets
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Chart.js/src/core/core.controller.js
Lines 897 to 899 in eb0751c
// meta.hidden is a per chart dataset hidden flag override with 3 states: if true or false, | |
// the dataset.hidden value is ignored, else if null, the dataset hidden state is returned. | |
return typeof meta.hidden === 'boolean' ? !meta.hidden : !this.data.datasets[datasetIndex].hidden; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure about the use case where datasets are shared between charts, @etimberg any insights?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not quite sure where we modify the data object, but stopping that sounds like another potentially good thing to change
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perhaps 'modify' is the wrong word, but we attach extra info to the data object and so systems that treat that as immutable are unhappy
Chart.js/src/core/core.controller.js
Line 863 in e5b03a0
dataset._meta = {}; |
I used chart.js in a React project recently and tried writing my own wrapper. I had a look at the source code of react-chartjs-2 and they have to do a lot of work to handle the fact that the data object gets mutated. Dropping that would significantly simplify things
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder if we need to treat options
as immutable as well. Here's a PR for fixing dataset._meta
: #6778
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So the meta.hidden
and dataset.hidden
are both needed. dataset.hidden
is provided in dataset configuration (loaded from storage maybe) and meta.hidden
is used to override that, when clicking legend (avoiding touching the provided configuration).
Now this could be changed to read the dataset.hidden to meta.hidden at initializion and only use meta.hidden runtime. Provide hide() and show() for developer access. But not in the scope of this PR.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should visible
be made private (i.e. _visible
) since we may change it in the future? (maybe even worth adding a TODO to make the change you suggested)
Timings from uPlot bench, but with stacked y-scale.
Master
PR