From a3392e0e59ae77a9c081b4c14de2c95a35422e18 Mon Sep 17 00:00:00 2001 From: Ben McCann <322311+benmccann@users.noreply.github.com> Date: Sun, 10 Nov 2019 04:52:12 -0800 Subject: [PATCH] Remove index and datasetIndex from Element (#6688) Remove `index` and `datasetIndex` properties from elements. --- docs/getting-started/v3-migration.md | 14 ++++++ src/controllers/controller.bar.js | 2 - src/controllers/controller.bubble.js | 2 - src/controllers/controller.doughnut.js | 4 -- src/controllers/controller.line.js | 5 -- src/controllers/controller.polarArea.js | 4 -- src/controllers/controller.radar.js | 4 -- src/core/core.controller.js | 23 +++++---- src/core/core.datasetController.js | 5 +- src/core/core.element.js | 7 +-- src/core/core.interaction.js | 39 +++++++-------- src/core/core.tooltip.js | 21 ++++---- src/helpers/helpers.core.js | 25 ++++++++++ test/specs/controller.bar.tests.js | 10 ++-- test/specs/core.controller.tests.js | 8 ++-- test/specs/core.interaction.tests.js | 64 +++++++++++-------------- test/specs/global.defaults.tests.js | 16 +++---- test/specs/helpers.core.tests.js | 12 +++++ 18 files changed, 147 insertions(+), 118 deletions(-) diff --git a/docs/getting-started/v3-migration.md b/docs/getting-started/v3-migration.md index eab3a3c4833..a57b27da306 100644 --- a/docs/getting-started/v3-migration.md +++ b/docs/getting-started/v3-migration.md @@ -106,3 +106,17 @@ Chart.js is no longer providing the `Chart.bundle.js` and `Chart.bundle.min.js`. ##### Time Scale * `getValueForPixel` now returns milliseconds since the epoch + +#### Controllers + +##### Core Controller + +* The first parameter to `updateHoverStyle` is now an array of objects containing the `element`, `datasetIndex`, and `index` + +##### Dataset Controllers + +* `setHoverStyle` now additionally takes the `datasetIndex` and `index` + +#### Interactions + +* Interaction mode methods now return an array of objects containing the `element`, `datasetIndex`, and `index` diff --git a/src/controllers/controller.bar.js b/src/controllers/controller.bar.js index 6d89e0708b5..4242b145811 100644 --- a/src/controllers/controller.bar.js +++ b/src/controllers/controller.bar.js @@ -240,8 +240,6 @@ module.exports = DatasetController.extend({ var me = this; var options = me._resolveDataElementOptions(index); - rectangle._datasetIndex = me.index; - rectangle._index = index; rectangle._model = { backgroundColor: options.backgroundColor, borderColor: options.borderColor, diff --git a/src/controllers/controller.bubble.js b/src/controllers/controller.bubble.js index 50a9b2d0435..39cf25668b8 100644 --- a/src/controllers/controller.bubble.js +++ b/src/controllers/controller.bubble.js @@ -107,8 +107,6 @@ module.exports = DatasetController.extend({ var y = reset ? yScale.getBasePixel() : yScale.getPixelForValue(parsed[yScale.id]); point._options = options; - point._datasetIndex = me.index; - point._index = index; point._model = { backgroundColor: options.backgroundColor, borderColor: options.borderColor, diff --git a/src/controllers/controller.doughnut.js b/src/controllers/controller.doughnut.js index 4b29d422617..8fefa623fa1 100644 --- a/src/controllers/controller.doughnut.js +++ b/src/controllers/controller.doughnut.js @@ -238,10 +238,6 @@ module.exports = DatasetController.extend({ var options = arc._options || {}; helpers.extend(arc, { - // Utility - _datasetIndex: me.index, - _index: index, - // Desired view properties _model: { backgroundColor: options.backgroundColor, diff --git a/src/controllers/controller.line.js b/src/controllers/controller.line.js index d73332e7571..b7fc90c299e 100644 --- a/src/controllers/controller.line.js +++ b/src/controllers/controller.line.js @@ -82,8 +82,6 @@ module.exports = DatasetController.extend({ // Update Line if (showLine) { - // Utility - line._datasetIndex = me.index; // Data line._children = points; // Model @@ -110,7 +108,6 @@ module.exports = DatasetController.extend({ updateElement: function(point, index, reset) { var me = this; var meta = me.getMeta(); - var datasetIndex = me.index; var xScale = me._xScale; var yScale = me._yScale; var lineModel = meta.dataset._model; @@ -122,8 +119,6 @@ module.exports = DatasetController.extend({ // Utility point._options = options; - point._datasetIndex = datasetIndex; - point._index = index; // Desired view properties point._model = { diff --git a/src/controllers/controller.polarArea.js b/src/controllers/controller.polarArea.js index b4c5ba48015..0272d6e464a 100644 --- a/src/controllers/controller.polarArea.js +++ b/src/controllers/controller.polarArea.js @@ -201,10 +201,6 @@ module.exports = DatasetController.extend({ var options = arc._options || {}; helpers.extend(arc, { - // Utility - _datasetIndex: me.index, - _index: index, - // Desired view properties _model: { backgroundColor: options.backgroundColor, diff --git a/src/controllers/controller.radar.js b/src/controllers/controller.radar.js index 049c6cf8efa..e34dfc418e9 100644 --- a/src/controllers/controller.radar.js +++ b/src/controllers/controller.radar.js @@ -83,8 +83,6 @@ module.exports = DatasetController.extend({ config.lineTension = config.tension; } - // Utility - line._datasetIndex = me.index; // Data line._children = points; line._loop = true; @@ -119,8 +117,6 @@ module.exports = DatasetController.extend({ // Utility point._options = options; - point._datasetIndex = me.index; - point._index = index; // Desired view properties point._model = { diff --git a/src/core/core.controller.js b/src/core/core.controller.js index e3fce696504..db5391dd99e 100644 --- a/src/core/core.controller.js +++ b/src/core/core.controller.js @@ -980,19 +980,24 @@ helpers.extend(Chart.prototype, /** @lends Chart */ { }); }, - updateHoverStyle: function(elements, mode, enabled) { + updateHoverStyle: function(items, mode, enabled) { var prefix = enabled ? 'set' : 'remove'; - var element, i, ilen; + var meta, item, i, ilen; - for (i = 0, ilen = elements.length; i < ilen; ++i) { - element = elements[i]; - if (element) { - this.getDatasetMeta(element._datasetIndex).controller[prefix + 'HoverStyle'](element); + if (mode === 'dataset') { + meta = this.getDatasetMeta(items[0].datasetIndex); + meta.controller['_' + prefix + 'DatasetHoverStyle'](); + for (i = 0, ilen = meta.data.length; i < ilen; ++i) { + meta.controller[prefix + 'HoverStyle'](meta.data[i], items[0].datasetIndex, i); } + return; } - if (mode === 'dataset') { - this.getDatasetMeta(elements[0]._datasetIndex).controller['_' + prefix + 'DatasetHoverStyle'](); + for (i = 0, ilen = items.length; i < ilen; ++i) { + item = items[i]; + if (item) { + this.getDatasetMeta(item.datasetIndex).controller[prefix + 'HoverStyle'](item.element, item.datasetIndex, item.index); + } } }, @@ -1100,7 +1105,7 @@ helpers.extend(Chart.prototype, /** @lends Chart */ { } me._updateHoverStyles(); - changed = !helpers.arrayEquals(me.active, me.lastActive); + changed = !helpers._elementsEqual(me.active, me.lastActive); // Remember Last Actives me.lastActive = me.active; diff --git a/src/core/core.datasetController.js b/src/core/core.datasetController.js index cc9601ec357..a434d0c5ae4 100644 --- a/src/core/core.datasetController.js +++ b/src/core/core.datasetController.js @@ -793,9 +793,8 @@ helpers.extend(DatasetController.prototype, { delete element.$previousStyle; }, - setHoverStyle: function(element) { - var dataset = this.chart.data.datasets[element._datasetIndex]; - var index = element._index; + setHoverStyle: function(element, datasetIndex, index) { + var dataset = this.chart.data.datasets[datasetIndex]; var model = element._model; var getHoverColor = helpers.getHoverColor; diff --git a/src/core/core.element.js b/src/core/core.element.js index dc98270c866..799ec82a4ab 100644 --- a/src/core/core.element.js +++ b/src/core/core.element.js @@ -56,12 +56,13 @@ class Element { constructor(configuration) { helpers.extend(this, configuration); + + // this.hidden = false; we assume Element has an attribute called hidden, but do not initialize to save memory + this.initialize.apply(this, arguments); } - initialize() { - this.hidden = false; - } + initialize() {} pivot(animationsDisabled) { var me = this; diff --git a/src/core/core.interaction.js b/src/core/core.interaction.js index a11f0ae5421..a558d8b20c1 100644 --- a/src/core/core.interaction.js +++ b/src/core/core.interaction.js @@ -33,7 +33,7 @@ function parseVisibleItems(chart, handler) { for (j = 0, jlen = metadata.length; j < jlen; ++j) { element = metadata[j]; if (!element._view.skip) { - handler(element); + handler(element, i, j); } } } @@ -48,9 +48,9 @@ function parseVisibleItems(chart, handler) { function getIntersectItems(chart, position) { var elements = []; - parseVisibleItems(chart, function(element) { + parseVisibleItems(chart, function(element, datasetIndex, index) { if (element.inRange(position.x, position.y)) { - elements.push(element); + elements.push({element, datasetIndex, index}); } }); @@ -69,7 +69,7 @@ function getNearestItems(chart, position, intersect, distanceMetric) { var minDistance = Number.POSITIVE_INFINITY; var nearestItems = []; - parseVisibleItems(chart, function(element) { + parseVisibleItems(chart, function(element, datasetIndex, index) { if (intersect && !element.inRange(position.x, position.y)) { return; } @@ -77,11 +77,11 @@ function getNearestItems(chart, position, intersect, distanceMetric) { var center = element.getCenterPoint(); var distance = distanceMetric(position, center); if (distance < minDistance) { - nearestItems = [element]; + nearestItems = [{element, datasetIndex, index}]; minDistance = distance; } else if (distance === minDistance) { // Can have multiple items at the same distance in which case we sort by size - nearestItems.push(element); + nearestItems.push({element, datasetIndex, index}); } }); @@ -117,11 +117,12 @@ function indexMode(chart, e, options) { } chart._getSortedVisibleDatasetMetas().forEach(function(meta) { - var element = meta.data[items[0]._index]; + var index = items[0].index; + var element = meta.data[index]; // don't count items that are skipped (null data) if (element && !element._view.skip) { - elements.push(element); + elements.push({element, datasetIndex: meta.index, index}); } }); @@ -152,7 +153,7 @@ module.exports = { * @param {Chart} chart - the chart we are returning items from * @param {Event} e - the event we are find things at * @param {IInteractionOptions} options - options to use during interaction - * @return {Chart.Element[]} Array of elements that are under the point. If none are found, an empty array is returned + * @return {Object[]} Array of elements that are under the point. If none are found, an empty array is returned */ index: indexMode, @@ -163,7 +164,7 @@ module.exports = { * @param {Chart} chart - the chart we are returning items from * @param {Event} e - the event we are find things at * @param {IInteractionOptions} options - options to use during interaction - * @return {Chart.Element[]} Array of elements that are under the point. If none are found, an empty array is returned + * @return {Object[]} Array of elements that are under the point. If none are found, an empty array is returned */ dataset: function(chart, e, options) { var position = getRelativePosition(e, chart); @@ -172,7 +173,7 @@ module.exports = { var items = options.intersect ? getIntersectItems(chart, position) : getNearestItems(chart, position, false, distanceMetric); if (items.length > 0) { - items = chart.getDatasetMeta(items[0]._datasetIndex).data; + items = [{datasetIndex: items[0].datasetIndex}]; // when mode: 'dataset' we only need to return datasetIndex } return items; @@ -184,7 +185,7 @@ module.exports = { * @function Chart.Interaction.modes.intersect * @param {Chart} chart - the chart we are returning items from * @param {Event} e - the event we are find things at - * @return {Chart.Element[]} Array of elements that are under the point. If none are found, an empty array is returned + * @return {Object[]} Array of elements that are under the point. If none are found, an empty array is returned */ point: function(chart, e) { var position = getRelativePosition(e, chart); @@ -197,7 +198,7 @@ module.exports = { * @param {Chart} chart - the chart we are returning items from * @param {Event} e - the event we are find things at * @param {IInteractionOptions} options - options to use - * @return {Chart.Element[]} Array of elements that are under the point. If none are found, an empty array is returned + * @return {Object[]} Array of elements that are under the point. If none are found, an empty array is returned */ nearest: function(chart, e, options) { var position = getRelativePosition(e, chart); @@ -212,16 +213,16 @@ module.exports = { * @param {Chart} chart - the chart we are returning items from * @param {Event} e - the event we are find things at * @param {IInteractionOptions} options - options to use - * @return {Chart.Element[]} Array of elements that are under the point. If none are found, an empty array is returned + * @return {Object[]} Array of elements that are under the point. If none are found, an empty array is returned */ x: function(chart, e, options) { var position = getRelativePosition(e, chart); var items = []; var intersectsItem = false; - parseVisibleItems(chart, function(element) { + parseVisibleItems(chart, function(element, datasetIndex, index) { if (element.inXRange(position.x)) { - items.push(element); + items.push({element, datasetIndex, index}); } if (element.inRange(position.x, position.y)) { @@ -243,16 +244,16 @@ module.exports = { * @param {Chart} chart - the chart we are returning items from * @param {Event} e - the event we are find things at * @param {IInteractionOptions} options - options to use - * @return {Chart.Element[]} Array of elements that are under the point. If none are found, an empty array is returned + * @return {Object[]} Array of elements that are under the point. If none are found, an empty array is returned */ y: function(chart, e, options) { var position = getRelativePosition(e, chart); var items = []; var intersectsItem = false; - parseVisibleItems(chart, function(element) { + parseVisibleItems(chart, function(element, datasetIndex, index) { if (element.inYRange(position.y)) { - items.push(element); + items.push({element, datasetIndex, index}); } if (element.inRange(position.x, position.y)) { diff --git a/src/core/core.tooltip.js b/src/core/core.tooltip.js index cea6d93cde7..4b83d1f7e16 100644 --- a/src/core/core.tooltip.js +++ b/src/core/core.tooltip.js @@ -117,7 +117,7 @@ var positioners = { var count = 0; for (i = 0, len = elements.length; i < len; ++i) { - var el = elements[i]; + var el = elements[i].element; if (el && el.hasValue()) { var pos = el.tooltipPosition(); x += pos.x; @@ -146,7 +146,7 @@ var positioners = { var i, len, nearestElement; for (i = 0, len = elements.length; i < len; ++i) { - var el = elements[i]; + var el = elements[i].element; if (el && el.hasValue()) { var center = el.getCenterPoint(); var d = helpers.distanceBetweenPoints(eventPosition, center); @@ -201,16 +201,15 @@ function splitNewlines(str) { /** * Private helper to create a tooltip item model - * @param element - the chart element (point, arc, bar) to create the tooltip item for + * @param item - the chart element (point, arc, bar) to create the tooltip item for * @return new tooltip item */ -function createTooltipItem(chart, element) { - var datasetIndex = element._datasetIndex; - var index = element._index; - var controller = chart.getDatasetMeta(datasetIndex).controller; - var indexScale = controller._getIndexScale(); - var valueScale = controller._getValueScale(); - var parsed = controller._getParsed(index); +function createTooltipItem(chart, item) { + const {datasetIndex, element, index} = item; + const controller = chart.getDatasetMeta(datasetIndex).controller; + const indexScale = controller._getIndexScale(); + const valueScale = controller._getValueScale(); + const parsed = controller._getParsed(index); return { label: indexScale ? '' + indexScale.getLabelForValue(parsed[indexScale.id]) : '', @@ -1016,7 +1015,7 @@ class Tooltip extends Element { } // Remember Last Actives - changed = !helpers.arrayEquals(me._active, me._lastActive); + changed = !helpers._elementsEqual(me._active, me._lastActive); // Only handle target event on tooltip change if (changed) { diff --git a/src/helpers/helpers.core.js b/src/helpers/helpers.core.js index b68b62b358c..9276a18b883 100644 --- a/src/helpers/helpers.core.js +++ b/src/helpers/helpers.core.js @@ -164,6 +164,31 @@ var helpers = { return true; }, + /** + * Returns true if the `a0` and `a1` arrays have the same content, else returns false. + * @param {Array} a0 - The array to compare + * @param {Array} a1 - The array to compare + * @returns {boolean} + */ + _elementsEqual: function(a0, a1) { + let i, ilen, v0, v1; + + if (!a0 || !a1 || a0.length !== a1.length) { + return false; + } + + for (i = 0, ilen = a0.length; i < ilen; ++i) { + v0 = a0[i]; + v1 = a1[i]; + + if (v0.datasetIndex !== v1.datasetIndex || v0.index !== v1.index) { + return false; + } + } + + return true; + }, + /** * Returns a deep copy of `source` without keeping references on objects and arrays. * @param {*} source - The value to clone. diff --git a/test/specs/controller.bar.tests.js b/test/specs/controller.bar.tests.js index cb958d8373a..b46352d9c19 100644 --- a/test/specs/controller.bar.tests.js +++ b/test/specs/controller.bar.tests.js @@ -1379,7 +1379,7 @@ describe('Chart.controllers.bar', function() { var meta = chart.getDatasetMeta(1); var bar = meta.data[0]; - meta.controller.setHoverStyle(bar); + meta.controller.setHoverStyle(bar, 1, 0); expect(bar._model.backgroundColor).toBe('rgb(230, 0, 0)'); expect(bar._model.borderColor).toBe('rgb(0, 0, 230)'); expect(bar._model.borderWidth).toBe(2); @@ -1389,7 +1389,7 @@ describe('Chart.controllers.bar', function() { chart.data.datasets[1].hoverBorderColor = 'rgb(0, 0, 0)'; chart.data.datasets[1].hoverBorderWidth = 5; - meta.controller.setHoverStyle(bar); + meta.controller.setHoverStyle(bar, 1, 0); expect(bar._model.backgroundColor).toBe('rgb(128, 128, 128)'); expect(bar._model.borderColor).toBe('rgb(0, 0, 0)'); expect(bar._model.borderWidth).toBe(5); @@ -1399,7 +1399,7 @@ describe('Chart.controllers.bar', function() { chart.data.datasets[1].hoverBorderColor = ['rgb(9, 9, 9)', 'rgb(0, 0, 0)']; chart.data.datasets[1].hoverBorderWidth = [2.5, 5]; - meta.controller.setHoverStyle(bar); + meta.controller.setHoverStyle(bar, 1, 0); expect(bar._model.backgroundColor).toBe('rgb(255, 255, 255)'); expect(bar._model.borderColor).toBe('rgb(9, 9, 9)'); expect(bar._model.borderWidth).toBe(2.5); @@ -1441,7 +1441,7 @@ describe('Chart.controllers.bar', function() { expect(bar._model.backgroundColor).toBe('rgb(128, 128, 128)'); expect(bar._model.borderColor).toBe('rgb(15, 15, 15)'); expect(bar._model.borderWidth).toBe(3.14); - meta.controller.setHoverStyle(bar); + meta.controller.setHoverStyle(bar, 1, 0); expect(bar._model.backgroundColor).toBe(helpers.getHoverColor('rgb(128, 128, 128)')); expect(bar._model.borderColor).toBe(helpers.getHoverColor('rgb(15, 15, 15)')); expect(bar._model.borderWidth).toBe(3.14); @@ -1459,7 +1459,7 @@ describe('Chart.controllers.bar', function() { expect(bar._model.backgroundColor).toBe('rgb(255, 255, 255)'); expect(bar._model.borderColor).toBe('rgb(9, 9, 9)'); expect(bar._model.borderWidth).toBe(2.5); - meta.controller.setHoverStyle(bar); + meta.controller.setHoverStyle(bar, 1, 0); expect(bar._model.backgroundColor).toBe(helpers.getHoverColor('rgb(255, 255, 255)')); expect(bar._model.borderColor).toBe(helpers.getHoverColor('rgb(9, 9, 9)')); expect(bar._model.borderWidth).toBe(2.5); diff --git a/test/specs/core.controller.tests.js b/test/specs/core.controller.tests.js index 849d258a904..5dc8bf81a33 100644 --- a/test/specs/core.controller.tests.js +++ b/test/specs/core.controller.tests.js @@ -1168,13 +1168,13 @@ describe('Chart', function() { // Check and see if tooltip was displayed var tooltip = chart.tooltip; - expect(chart.lastActive).toEqual([point]); - expect(tooltip._lastActive).toEqual([point]); + expect(chart.lastActive[0].element).toEqual(point); + expect(tooltip._lastActive[0].element).toEqual(point); // Update and confirm tooltip is updated chart.update(); - expect(chart.lastActive).toEqual([point]); - expect(tooltip._lastActive).toEqual([point]); + expect(chart.lastActive[0].element).toEqual(point); + expect(tooltip._lastActive[0].element).toEqual(point); }); it ('should update the metadata', function() { diff --git a/test/specs/core.interaction.tests.js b/test/specs/core.interaction.tests.js index 356d0de95b1..d640e866572 100644 --- a/test/specs/core.interaction.tests.js +++ b/test/specs/core.interaction.tests.js @@ -37,7 +37,7 @@ describe('Core.Interaction', function() { y: point._model.y, }; - var elements = Chart.Interaction.modes.point(chart, evt); + var elements = Chart.Interaction.modes.point(chart, evt).map(item => item.element); expect(elements).toEqual([point, meta1.data[1]]); }); @@ -51,7 +51,7 @@ describe('Core.Interaction', function() { y: 0 }; - var elements = Chart.Interaction.modes.point(chart, evt); + var elements = Chart.Interaction.modes.point(chart, evt).map(item => item.element); expect(elements).toEqual([]); }); }); @@ -92,7 +92,7 @@ describe('Core.Interaction', function() { y: point._model.y, }; - var elements = Chart.Interaction.modes.index(chart, evt, {intersect: true}); + var elements = Chart.Interaction.modes.index(chart, evt, {intersect: true}).map(item => item.element); expect(elements).toEqual([point, meta1.data[1]]); }); @@ -106,7 +106,7 @@ describe('Core.Interaction', function() { y: 0, }; - var elements = Chart.Interaction.modes.index(chart, evt, {intersect: true}); + var elements = Chart.Interaction.modes.index(chart, evt, {intersect: true}).map(item => item.element); expect(elements).toEqual([]); }); }); @@ -147,7 +147,7 @@ describe('Core.Interaction', function() { y: 0 }; - var elements = Chart.Interaction.modes.index(chart, evt, {intersect: false}); + var elements = Chart.Interaction.modes.index(chart, evt, {intersect: false}).map(item => item.element); expect(elements).toEqual([meta0.data[0], meta1.data[0]]); }); @@ -169,7 +169,7 @@ describe('Core.Interaction', function() { y: center.y + 30, }; - var elements = Chart.Interaction.modes.index(chart, evt, {axis: 'y', intersect: false}); + var elements = Chart.Interaction.modes.index(chart, evt, {axis: 'y', intersect: false}).map(item => item.element); expect(elements).toEqual([meta0.data[0], meta1.data[0]]); }); @@ -186,7 +186,7 @@ describe('Core.Interaction', function() { y: 0 }; - var elements = Chart.Interaction.modes.index(chart, evt, {axis: 'xy', intersect: false}); + var elements = Chart.Interaction.modes.index(chart, evt, {axis: 'xy', intersect: false}).map(item => item.element); expect(elements).toEqual([meta0.data[0], meta1.data[0]]); }); }); @@ -228,7 +228,7 @@ describe('Core.Interaction', function() { }; var elements = Chart.Interaction.modes.dataset(chart, evt, {intersect: true}); - expect(elements).toEqual(meta.data); + expect(elements).toEqual([{datasetIndex: 0}]); }); it ('should return an empty array if nothing found', function() { @@ -284,9 +284,7 @@ describe('Core.Interaction', function() { }; var elements = Chart.Interaction.modes.dataset(chart, evt, {axis: 'x', intersect: false}); - - var meta = chart.getDatasetMeta(0); - expect(elements).toEqual(meta.data); + expect(elements).toEqual([{datasetIndex: 0}]); }); it ('axis: y gets correct items', function() { @@ -300,9 +298,7 @@ describe('Core.Interaction', function() { }; var elements = Chart.Interaction.modes.dataset(chart, evt, {axis: 'y', intersect: false}); - - var meta = chart.getDatasetMeta(1); - expect(elements).toEqual(meta.data); + expect(elements).toEqual([{datasetIndex: 1}]); }); it ('axis: xy gets correct items', function() { @@ -316,9 +312,7 @@ describe('Core.Interaction', function() { }; var elements = Chart.Interaction.modes.dataset(chart, evt, {intersect: false}); - - var meta = chart.getDatasetMeta(1); - expect(elements).toEqual(meta.data); + expect(elements).toEqual([{datasetIndex: 1}]); }); }); }); @@ -359,7 +353,7 @@ describe('Core.Interaction', function() { }; // Nearest to 0,0 (top left) will be first point of dataset 2 - var elements = Chart.Interaction.modes.nearest(chart, evt, {intersect: false}); + var elements = Chart.Interaction.modes.nearest(chart, evt, {intersect: false}).map(item => item.element); var meta = chart.getDatasetMeta(1); expect(elements).toEqual([meta.data[0]]); }); @@ -384,7 +378,7 @@ describe('Core.Interaction', function() { }; // Both points are nearest - var elements = Chart.Interaction.modes.nearest(chart, evt, {intersect: false}); + var elements = Chart.Interaction.modes.nearest(chart, evt, {intersect: false}).map(item => item.element); expect(elements).toEqual([meta0.data[1], meta1.data[1]]); }); }); @@ -410,7 +404,7 @@ describe('Core.Interaction', function() { }; // Middle point from both series are nearest - var elements = Chart.Interaction.modes.nearest(chart, evt, {axis: 'x', intersect: false}); + var elements = Chart.Interaction.modes.nearest(chart, evt, {axis: 'x', intersect: false}).map(item => item.element); expect(elements).toEqual([meta0.data[1], meta1.data[1]]); }); @@ -434,7 +428,7 @@ describe('Core.Interaction', function() { }; // Should return all (4) points from 'Point 1' and 'Point 2' - var elements = Chart.Interaction.modes.nearest(chart, evt, {axis: 'x', intersect: false}); + var elements = Chart.Interaction.modes.nearest(chart, evt, {axis: 'x', intersect: false}).map(item => item.element); expect(elements).toEqual([meta0.data[0], meta0.data[1], meta1.data[0], meta1.data[1]]); }); }); @@ -459,7 +453,7 @@ describe('Core.Interaction', function() { }; // Middle point from both series are nearest - var elements = Chart.Interaction.modes.nearest(chart, evt, {axis: 'y', intersect: false}); + var elements = Chart.Interaction.modes.nearest(chart, evt, {axis: 'y', intersect: false}).map(item => item.element); expect(elements).toEqual([meta0.data[2]]); }); @@ -483,7 +477,7 @@ describe('Core.Interaction', function() { }; // Should return points with value 40 - var elements = Chart.Interaction.modes.nearest(chart, evt, {axis: 'y', intersect: false}); + var elements = Chart.Interaction.modes.nearest(chart, evt, {axis: 'y', intersect: false}).map(item => item.element); expect(elements).toEqual([meta0.data[1], meta1.data[0], meta1.data[1], meta1.data[2]]); }); }); @@ -525,7 +519,7 @@ describe('Core.Interaction', function() { }; // Nothing intersects so find nothing - var elements = Chart.Interaction.modes.nearest(chart, evt, {intersect: true}); + var elements = Chart.Interaction.modes.nearest(chart, evt, {intersect: true}).map(item => item.element); expect(elements).toEqual([]); evt = { @@ -535,7 +529,7 @@ describe('Core.Interaction', function() { x: point._view.x, y: point._view.y }; - elements = Chart.Interaction.modes.nearest(chart, evt, {intersect: true}); + elements = Chart.Interaction.modes.nearest(chart, evt, {intersect: true}).map(item => item.element); expect(elements).toEqual([point]); }); @@ -565,7 +559,7 @@ describe('Core.Interaction', function() { y: pt.y }; - var elements = Chart.Interaction.modes.nearest(chart, evt, {intersect: true}); + var elements = Chart.Interaction.modes.nearest(chart, evt, {intersect: true}).map(item => item.element); expect(elements).toEqual([meta0.data[1]]); }); @@ -595,7 +589,7 @@ describe('Core.Interaction', function() { y: pt.y }; - var elements = Chart.Interaction.modes.nearest(chart, evt, {intersect: true}); + var elements = Chart.Interaction.modes.nearest(chart, evt, {intersect: true}).map(item => item.element); expect(elements).toEqual([meta0.data[1], meta1.data[1]]); }); }); @@ -644,7 +638,7 @@ describe('Core.Interaction', function() { y: 0 }; - var elements = Chart.Interaction.modes.x(chart, evt, {intersect: false}); + var elements = Chart.Interaction.modes.x(chart, evt, {intersect: false}).map(item => item.element); expect(elements).toEqual([meta0.data[1], meta1.data[1]]); evt = { @@ -655,7 +649,7 @@ describe('Core.Interaction', function() { y: 0 }; - elements = Chart.Interaction.modes.x(chart, evt, {intersect: false}); + elements = Chart.Interaction.modes.x(chart, evt, {intersect: false}).map(item => item.element); expect(elements).toEqual([]); }); @@ -678,7 +672,7 @@ describe('Core.Interaction', function() { y: 0 }; - var elements = Chart.Interaction.modes.x(chart, evt, {intersect: true}); + var elements = Chart.Interaction.modes.x(chart, evt, {intersect: true}).map(item => item.element); expect(elements).toEqual([]); // we don't intersect anything evt = { @@ -689,7 +683,7 @@ describe('Core.Interaction', function() { y: pt.y }; - elements = Chart.Interaction.modes.x(chart, evt, {intersect: true}); + elements = Chart.Interaction.modes.x(chart, evt, {intersect: true}).map(item => item.element); expect(elements).toEqual([meta0.data[1], meta1.data[1]]); }); }); @@ -736,7 +730,7 @@ describe('Core.Interaction', function() { y: pt.y, }; - var elements = Chart.Interaction.modes.y(chart, evt, {intersect: false}); + var elements = Chart.Interaction.modes.y(chart, evt, {intersect: false}).map(item => item.element); expect(elements).toEqual([meta0.data[1], meta1.data[0], meta1.data[1], meta1.data[2]]); evt = { @@ -747,7 +741,7 @@ describe('Core.Interaction', function() { y: pt.y + 20, // out of range }; - elements = Chart.Interaction.modes.y(chart, evt, {intersect: false}); + elements = Chart.Interaction.modes.y(chart, evt, {intersect: false}).map(item => item.element); expect(elements).toEqual([]); }); @@ -770,7 +764,7 @@ describe('Core.Interaction', function() { y: pt.y }; - var elements = Chart.Interaction.modes.y(chart, evt, {intersect: true}); + var elements = Chart.Interaction.modes.y(chart, evt, {intersect: true}).map(item => item.element); expect(elements).toEqual([]); // we don't intersect anything evt = { @@ -781,7 +775,7 @@ describe('Core.Interaction', function() { y: pt.y, }; - elements = Chart.Interaction.modes.y(chart, evt, {intersect: true}); + elements = Chart.Interaction.modes.y(chart, evt, {intersect: true}).map(item => item.element); expect(elements).toEqual([meta0.data[1], meta1.data[0], meta1.data[1], meta1.data[2]]); }); }); diff --git a/test/specs/global.defaults.tests.js b/test/specs/global.defaults.tests.js index afef1aabf2a..e1188458a91 100644 --- a/test/specs/global.defaults.tests.js +++ b/test/specs/global.defaults.tests.js @@ -18,7 +18,7 @@ describe('Default Configs', function() { }); // fake out the tooltip hover and force the tooltip to update - chart.tooltip._active = [chart.getDatasetMeta(0).data[0]]; + chart.tooltip._active = [{element: chart.getDatasetMeta(0).data[0], datasetIndex: 0, index: 0}]; chart.tooltip.update(); // Title is always blank @@ -46,7 +46,7 @@ describe('Default Configs', function() { }); // fake out the tooltip hover and force the tooltip to update - chart.tooltip._active = [chart.getDatasetMeta(0).data[1]]; + chart.tooltip._active = [{element: chart.getDatasetMeta(0).data[1], datasetIndex: 0, index: 1}]; chart.tooltip.update(); // Title is always blank @@ -72,7 +72,7 @@ describe('Default Configs', function() { }); // fake out the tooltip hover and force the tooltip to update - chart.tooltip._active = [chart.getDatasetMeta(0).data[1]]; + chart.tooltip._active = [{element: chart.getDatasetMeta(0).data[1], datasetIndex: 0, index: 1}]; chart.tooltip.update(); // Title is always blank @@ -125,14 +125,14 @@ describe('Default Configs', function() { var expected = [{ text: 'label1', fillStyle: 'red', - hidden: false, + hidden: undefined, index: 0, strokeStyle: '#000', lineWidth: 2 }, { text: 'label2', fillStyle: 'green', - hidden: false, + hidden: undefined, index: 1, strokeStyle: '#000', lineWidth: 2 @@ -192,7 +192,7 @@ describe('Default Configs', function() { }); // fake out the tooltip hover and force the tooltip to update - chart.tooltip._active = [chart.getDatasetMeta(0).data[1]]; + chart.tooltip._active = [{element: chart.getDatasetMeta(0).data[1], datasetIndex: 0, index: 1}]; chart.tooltip.update(); // Title is always blank @@ -241,14 +241,14 @@ describe('Default Configs', function() { var expected = [{ text: 'label1', fillStyle: 'red', - hidden: false, + hidden: undefined, index: 0, strokeStyle: '#000', lineWidth: 2 }, { text: 'label2', fillStyle: 'green', - hidden: false, + hidden: undefined, index: 1, strokeStyle: '#000', lineWidth: 2 diff --git a/test/specs/helpers.core.tests.js b/test/specs/helpers.core.tests.js index cdda01b9674..de3892a83ea 100644 --- a/test/specs/helpers.core.tests.js +++ b/test/specs/helpers.core.tests.js @@ -264,6 +264,18 @@ describe('Chart.helpers.core', function() { }); }); + describe('_elementsEqual', function() { + it('should return true if arrays are the same', function() { + expect(helpers._elementsEqual( + [{datasetIndex: 0, index: 1}, {datasetIndex: 0, index: 2}], + [{datasetIndex: 0, index: 1}, {datasetIndex: 0, index: 2}])).toBeTruthy(); + }); + it('should return false if arrays are not the same', function() { + expect(helpers._elementsEqual([], [{datasetIndex: 0, index: 1}])).toBeFalsy(); + expect(helpers._elementsEqual([{datasetIndex: 0, index: 2}], [{datasetIndex: 0, index: 1}])).toBeFalsy(); + }); + }); + describe('clone', function() { it('should clone primitive values', function() { expect(helpers.clone()).toBe(undefined);