chartjs / chartjs/chartjs-plugin-datalabels
Add labelIndex to context so functions can know which label for a given datapoint is referenced
- Dominant language
- JavaScript
- Stars
- 920
- Forks
- 507
- PR merge metrics
- No merged PRs in 30d
Description
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`
```javascript
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.
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.