Add parameters to tooltip filter option (#7556)

This commit is contained in:
Ben McCann
2020-06-29 04:51:20 -07:00
committed by Evert Timberg
parent aa523da832
commit 49f18c262c
4 changed files with 4 additions and 3 deletions

View File

@@ -93,7 +93,7 @@ Allows sorting of [tooltip items](#tooltip-item-interface). Must implement at mi
### Filter Callback
Allows filtering of [tooltip items](#tooltip-item-interface). Must implement at minimum a function that can be passed to [Array.prototype.filter](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/filter). This function can also accept a second parameter that is the data object passed to the chart.
Allows filtering of [tooltip items](#tooltip-item-interface). Must implement at minimum a function that can be passed to [Array.prototype.filter](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/filter). This function can also accept a fourth parameter that is the data object passed to the chart.
## Tooltip Callbacks

View File

@@ -185,6 +185,7 @@ Animation system was completely rewritten in Chart.js v3. Each property can now
#### Tooltip
* `xLabel` and `yLabel` were removed. Please use `index` and `value`
* The `filter` option will now be passed additional parameters when called and should have the method signature `function(tooltipItem, index, tooltipItems, data)`
## Developer migration

View File

@@ -602,7 +602,7 @@ export class Tooltip extends Element {
// If the user provided a filter function, use it to modify the tooltip items
if (options.filter) {
tooltipItems = tooltipItems.filter((a) => options.filter(a, data));
tooltipItems = tooltipItems.filter((element, index, array) => options.filter(element, index, array, data));
}
// If the user provided a sorting function, use it to modify the tooltip items

View File

@@ -706,7 +706,7 @@ describe('Core.Tooltip', function() {
options: {
tooltips: {
mode: 'index',
filter: function(tooltipItem, data) {
filter: function(tooltipItem, index, tooltipItems, data) {
// For testing purposes remove the first dataset that has a tooltipHidden property
return !data.datasets[tooltipItem.datasetIndex].tooltipHidden;
}