From: Ben McCann <322311+benmccann@users.noreply.github.com> Date: Mon, 29 Jun 2020 11:51:20 +0000 (-0700) Subject: Add parameters to tooltip filter option (#7556) X-Git-Tag: v3.0.0-beta.2~67 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=2ca155cce16b9bdc327030e169f8e69f3f5b627a;p=thirdparty%2FChart.js.git Add parameters to tooltip filter option (#7556) --- diff --git a/docs/docs/configuration/tooltip.md b/docs/docs/configuration/tooltip.md index cd98fdca9..5647095bc 100644 --- a/docs/docs/configuration/tooltip.md +++ b/docs/docs/configuration/tooltip.md @@ -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 diff --git a/docs/docs/getting-started/v3-migration.md b/docs/docs/getting-started/v3-migration.md index 3f0d3f5f5..a5974aea2 100644 --- a/docs/docs/getting-started/v3-migration.md +++ b/docs/docs/getting-started/v3-migration.md @@ -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 diff --git a/src/plugins/plugin.tooltip.js b/src/plugins/plugin.tooltip.js index 6f239f64b..2ca517b5d 100644 --- a/src/plugins/plugin.tooltip.js +++ b/src/plugins/plugin.tooltip.js @@ -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 diff --git a/test/specs/plugin.tooltip.tests.js b/test/specs/plugin.tooltip.tests.js index 3478102b2..1e1aa8c17 100644 --- a/test/specs/plugin.tooltip.tests.js +++ b/test/specs/plugin.tooltip.tests.js @@ -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; }