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

Check colors definition in the chart options #11003

Merged
merged 5 commits into from Jan 18, 2023

Conversation

stockiNail
Copy link
Contributor

Fix #11002

src/plugins/plugin.colors.ts Outdated Show resolved Hide resolved
src/plugins/plugin.colors.ts Outdated Show resolved Hide resolved
src/plugins/plugin.colors.ts Outdated Show resolved Hide resolved
stockiNail and others added 4 commits December 19, 2022 17:48
Co-authored-by: Dan Onoshko <danon0404@gmail.com>
Co-authored-by: Dan Onoshko <danon0404@gmail.com>
Co-authored-by: Dan Onoshko <danon0404@gmail.com>
@stockiNail
Copy link
Contributor Author

@dangreen Thank you for review! Applied and a bit changed (options is the reference to plugin options, provided to the hook).

@LeeLenaleee LeeLenaleee added this to the Version 4.1.1 milestone Dec 19, 2022
Copy link
Member

@kurkle kurkle left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is still room for improvement after this.

This could be checked by iterating the chart.configdatasetElementScopeKeys(type, elType)

image

But that would still leave out the defaults.datasets and defaults.elements etc.

Yet another option could be to use the dataset options resolver, but also check if the value is the value of defaults.color
and consider it undefined in that case too.

IMO best option overall would be to improve the options resolver to be able to tell you where the option is resolved from.

@DavideViolante
Copy link

It seems fine to me, but 2 functions with almost same name is a bit odd: containsColorsDefinitions, containsColorsDefinition.
If one is checking datasets and the other one options, I would call them containsColorsDefinitionsInDatasets, containsColorsDefinitionsInOptions. Or make one function for both.

@etimberg etimberg modified the milestones: Version 4.1.1, Version 4.2.0 Dec 20, 2022
@stockiNail
Copy link
Contributor Author

IMO best option overall would be to improve the options resolver to be able to tell you where the option is resolved from.

@kurkle Can I try to implement it or do you want to do it?

@kurkle
Copy link
Member

kurkle commented Dec 21, 2022

It can be done later, if ever. This should be ok for most users already.

@stockiNail
Copy link
Contributor Author

yes, not for this PR. I'm curious to understand how to expose a method (or whatever else) to a Proxy in order to get the scope where the prop is resolved.

@stockiNail
Copy link
Contributor Author

@kurkle just FYI (just a prototype):

Added a property to the cache of the proxy

    _search: () => function(key) {
      for (const scope of scopes) {
        if (!scope) {
          continue;
        }
        const value = scope[key];
        if (defined(value)) {
          return scope;
        }
      }
    },
  chart.options._search('backgroundColor'); // ==> returns the scope, for instance Chart.defaults.

@kurkle
Copy link
Member

kurkle commented Dec 21, 2022

Looks simple enough 😃
Could also refactor the scopes to contain a name..

@stockiNail
Copy link
Contributor Author

Could also refactor the scopes to contain a name..

This is more complex.... The "name" should be stored when a scope is calculated but in a different object (I don't think we could add a property to the scope or maybe yes with a special value, i.e. "_name"). We could also "wrap" the scope in an object, with a name property, but this could be quite disruptive, where the scopes are created and used. Let me spend some days to have a look.

@kurkle
Copy link
Member

kurkle commented Dec 22, 2022

I think it could be a WeakMap usink the scope object as key and name as value.

@stockiNail
Copy link
Contributor Author

stockiNail commented Dec 22, 2022

I think it could be a WeakMap usink the scope object as key and name as value.

yes, even if I need to understand how to relate the key (scope) to the chart in order to keep the map "updated" otherwise I see the risk that it can grow maintaining useless reference. But not sure about that.

EDIT: That is, an object's presence as a key in a WeakMap does not prevent the object from being garbage collected. Once an object used as a key has been collected, its corresponding values in any WeakMap become candidates for garbage collection as well — as long as they aren't strongly referred to elsewhere.

@LeeLenaleee LeeLenaleee merged commit ab55f6c into chartjs:master Jan 18, 2023
@stockiNail stockiNail deleted the colorsOptions branch January 18, 2023 13:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

backgroundColor doesn't work since v4?
7 participants