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

Add labelIndex to context so functions can know which label for a given datapoint is referenced #265

Open
mloit opened this issue Jul 28, 2021 · 1 comment

Comments

@mloit
Copy link

mloit commented Jul 28, 2021

Currently if you have multiple labels for a given data point [say a "label" and "value"] there is no easy way for code to know how to respond. ie a formatter doesn't know if it is to return a label or a value, a display function doesn't know if it should show or hide. etc. At least not without adding adding a fair amount of fragile external code to try and track which label it is by assuming call order. Externally adding a property to context suffers the same problem, as you still need to know which label the context applies to when you first set the value. As it stands the formatter or display function [in my test case on a doughnut chart] are called twice, once for each label, with the exact same context content, with no discernible way to distinguish which label is being referenced without externally tracking the call order.

Luckily the internal solution is easy... just one line in the definition of context

          label.$context = {
            active: false,
            chart: chart,
            dataIndex: i,
            labelIndex: j,  // <---- added to indicate which label this context applies to 
            dataset: dataset,
            datasetIndex: datasetIndex
          };

This is the simplest solution, which works, but certainly more elegant options exist as well.

@mloit
Copy link
Author

mloit commented Jul 29, 2021

Just to add some further context since I've since discovered the way it was intended.

Looking through the past issues on similar themes it stems from a critical hole in the documentation. It isn't clear in the documentation that you can specify a custom formatter (in fact you must for it to be useful) per label within the labels section. Yet the documentation and the corresponding code shown make no mention of this critical fact. It was only when I stumbled across the sample for multiple labels that it became clearer. So I highly recommend improving that section of the documentation. At a minimum the displayed code snippits should include the bare minimum to accomplish the goal, and that particular one falls short IMHO.

My perspective coming in was that I could only define the one formatter, yet had no mechanism to determine which label the formatter was being called for. So while I have found 'the way' now, i can still see some use cases where having the label index in the context for use by a common formatter may be useful. I see no harm in adding it, and it allows for multiple ways to accomplish a given goal depending on the users requirements.

I'll leave this open with the intent as a feature request rather than a bug as I still do see some benefit in having the additional meta-data within context.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant