From 2894f7a1bcf8871880a6fd4349f2c972e5ee2d20 Mon Sep 17 00:00:00 2001 From: huarui Date: Mon, 18 Jun 2018 14:19:55 -0700 Subject: [PATCH 1/9] add option to have one tooltip element per dataset --- src/core/core.interaction.js | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/core/core.interaction.js b/src/core/core.interaction.js index be85a080f76..36110d69668 100644 --- a/src/core/core.interaction.js +++ b/src/core/core.interaction.js @@ -293,6 +293,15 @@ module.exports = { if (options.intersect && !intersectsItem) { items = []; } + if (options.onlyOne) { + const byIndex = {}; + items.forEach(element => { + if (!byIndex[element._datasetIndex]) { + byIndex[element._datasetIndex] = element; + } + }); + return Object.values(byIndex); + } return items; }, From 91a98f2e596953ad5c47e5759b0d756cdbe8e519 Mon Sep 17 00:00:00 2001 From: huarui Date: Mon, 18 Jun 2018 14:23:38 -0700 Subject: [PATCH 2/9] lint --- src/core/core.interaction.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/core/core.interaction.js b/src/core/core.interaction.js index 36110d69668..ce6b007ab3d 100644 --- a/src/core/core.interaction.js +++ b/src/core/core.interaction.js @@ -294,8 +294,8 @@ module.exports = { items = []; } if (options.onlyOne) { - const byIndex = {}; - items.forEach(element => { + var byIndex = {}; + items.forEach(function(element) { if (!byIndex[element._datasetIndex]) { byIndex[element._datasetIndex] = element; } From 3ec8caa427edbcc9cae4142577b55480f01c5bdd Mon Sep 17 00:00:00 2001 From: huarui Date: Tue, 19 Jun 2018 15:16:56 -0700 Subject: [PATCH 3/9] change option name to onePerDataset --- src/core/core.interaction.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/core/core.interaction.js b/src/core/core.interaction.js index ce6b007ab3d..b249a028a8b 100644 --- a/src/core/core.interaction.js +++ b/src/core/core.interaction.js @@ -293,7 +293,7 @@ module.exports = { if (options.intersect && !intersectsItem) { items = []; } - if (options.onlyOne) { + if (options.onePerDataset) { var byIndex = {}; items.forEach(function(element) { if (!byIndex[element._datasetIndex]) { From 28fdbc297affb3921b83058bf8edf3d9a01c6eec Mon Sep 17 00:00:00 2001 From: huarui Date: Mon, 25 Jun 2018 13:00:31 -0700 Subject: [PATCH 4/9] sort by distance --- src/core/core.interaction.js | 33 +++++++++++++++++++++++---------- 1 file changed, 23 insertions(+), 10 deletions(-) diff --git a/src/core/core.interaction.js b/src/core/core.interaction.js index b249a028a8b..6244b56d94b 100644 --- a/src/core/core.interaction.js +++ b/src/core/core.interaction.js @@ -293,16 +293,29 @@ module.exports = { if (options.intersect && !intersectsItem) { items = []; } - if (options.onePerDataset) { - var byIndex = {}; - items.forEach(function(element) { - if (!byIndex[element._datasetIndex]) { - byIndex[element._datasetIndex] = element; - } - }); - return Object.values(byIndex); - } - return items; + + // group by dataset + var byIndex = items.reduce(function(res, element) { + if (!res[element._datasetIndex]) { + res[element._datasetIndex] = [element]; + } else { + res[element._datasetIndex].push(element); + } + return res; + }, {}); + + // sort by distance + Object.values(byIndex).forEach(function(elements) { + elements.sort(function(a, b) { + var centerA = a.getCenterPoint(); + var centerB = b.getCenterPoint(); + var distanceA = distanceMetric(position, centerA); + var distanceB = distanceMetric(position, centerB); + return distanceA - distanceB; + }) + }); + + return Object.values(byIndex).map(els => els[0]); }, /** From 96b4def212134a22ee3881abc4ca22b5cbd351bd Mon Sep 17 00:00:00 2001 From: huarui Date: Mon, 25 Jun 2018 13:13:39 -0700 Subject: [PATCH 5/9] fix errors --- src/core/core.interaction.js | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/core/core.interaction.js b/src/core/core.interaction.js index 6244b56d94b..e8c7486cd1b 100644 --- a/src/core/core.interaction.js +++ b/src/core/core.interaction.js @@ -275,6 +275,7 @@ module.exports = { */ x: function(chart, e, options) { var position = getRelativePosition(e, chart); + var distanceMetric = getDistanceMetricForAxis('x'); var items = []; var intersectsItem = false; @@ -312,10 +313,14 @@ module.exports = { var distanceA = distanceMetric(position, centerA); var distanceB = distanceMetric(position, centerB); return distanceA - distanceB; - }) + }); }); - return Object.values(byIndex).map(els => els[0]); + return Object.values(byIndex).map( + function(els) { + return els[0]; + } + ); }, /** From ffb9f3ee4d09ac5bdbf9f62c638cf1ee5a1adbeb Mon Sep 17 00:00:00 2001 From: huarui Date: Tue, 26 Jun 2018 14:44:26 -0700 Subject: [PATCH 6/9] add test that tests one per dataset --- test/specs/core.interaction.tests.js | 47 +++++++++++++++++++++++----- 1 file changed, 39 insertions(+), 8 deletions(-) diff --git a/test/specs/core.interaction.tests.js b/test/specs/core.interaction.tests.js index 49cd3bd193d..45b83bc3152 100644 --- a/test/specs/core.interaction.tests.js +++ b/test/specs/core.interaction.tests.js @@ -556,22 +556,53 @@ describe('Core.Interaction', function() { describe('x mode', function() { beforeEach(function() { this.chart = window.acquireChart({ - type: 'line', + type: 'scatter', data: { datasets: [{ label: 'Dataset 1', - data: [10, 40, 30], - pointRadius: [5, 10, 5], + data: [ + { + x: 1, + y: 10 + }, + { + x: 2, + y: 40 + }, + { + x: 2, + y: 40 + }, + { + x: 3, + y: 30 + }], + pointRadius: [10, 5, 10, 5], pointHoverBorderColor: 'rgb(255, 0, 0)', pointHoverBackgroundColor: 'rgb(0, 255, 0)' }, { label: 'Dataset 2', - data: [40, 40, 40], - pointRadius: [10, 10, 10], + data: [ + { + x: 1, + y: 40 + }, + { + x: 2, + y: 40 + }, + { + x: 2, + y: 40 + }, + { + x: 3, + y: 40 + }], + pointRadius: [10, 5, 10, 5], pointHoverBorderColor: 'rgb(0, 0, 255)', pointHoverBackgroundColor: 'rgb(0, 255, 255)' - }], - labels: ['Point 1', 'Point 2', 'Point 3'] + }] } }); }); @@ -610,7 +641,7 @@ describe('Core.Interaction', function() { expect(elements).toEqual([]); }); - it('should return items at the same x value when intersect is true', function() { + it('should return one item per dataset at the same x value when intersect is true', function() { var chart = this.chart; var meta0 = chart.getDatasetMeta(0); var meta1 = chart.getDatasetMeta(1); From da3262b727de99031686cb8da022d0b4bc2929f3 Mon Sep 17 00:00:00 2001 From: huarui Date: Thu, 28 Jun 2018 11:54:38 -0700 Subject: [PATCH 7/9] fix check for undefined and change to support ie11 --- src/core/core.interaction.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/core/core.interaction.js b/src/core/core.interaction.js index e8c7486cd1b..c41ddcda029 100644 --- a/src/core/core.interaction.js +++ b/src/core/core.interaction.js @@ -297,7 +297,7 @@ module.exports = { // group by dataset var byIndex = items.reduce(function(res, element) { - if (!res[element._datasetIndex]) { + if (res[element._datasetIndex] === undefined) { res[element._datasetIndex] = [element]; } else { res[element._datasetIndex].push(element); @@ -306,8 +306,8 @@ module.exports = { }, {}); // sort by distance - Object.values(byIndex).forEach(function(elements) { - elements.sort(function(a, b) { + Object.keys(byIndex).forEach(function(key) { + byIndex[key].sort(function(a, b) { var centerA = a.getCenterPoint(); var centerB = b.getCenterPoint(); var distanceA = distanceMetric(position, centerA); From 762bb5e43db579aa5e8d5d9dc80afdddc8f4737e Mon Sep 17 00:00:00 2001 From: huarui Date: Fri, 29 Jun 2018 11:44:40 -0700 Subject: [PATCH 8/9] change object.values --- src/core/core.interaction.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/core/core.interaction.js b/src/core/core.interaction.js index c41ddcda029..3e4bc3e027b 100644 --- a/src/core/core.interaction.js +++ b/src/core/core.interaction.js @@ -316,9 +316,9 @@ module.exports = { }); }); - return Object.values(byIndex).map( - function(els) { - return els[0]; + return Object.keys(byIndex).map( + function(key) { + return byIndex[key][0]; } ); }, From bc82b3ba6f3a665c061afd3405a0be20cd2ec15d Mon Sep 17 00:00:00 2001 From: huarui Date: Wed, 25 Jul 2018 11:59:23 -0700 Subject: [PATCH 9/9] refactor to reduce loc --- src/core/core.interaction.js | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/src/core/core.interaction.js b/src/core/core.interaction.js index 3e4bc3e027b..59474a5b32b 100644 --- a/src/core/core.interaction.js +++ b/src/core/core.interaction.js @@ -308,11 +308,7 @@ module.exports = { // sort by distance Object.keys(byIndex).forEach(function(key) { byIndex[key].sort(function(a, b) { - var centerA = a.getCenterPoint(); - var centerB = b.getCenterPoint(); - var distanceA = distanceMetric(position, centerA); - var distanceB = distanceMetric(position, centerB); - return distanceA - distanceB; + return distanceMetric(position, a.getCenterPoint()) - distanceMetric(position, b.getCenterPoint()); }); });