From aa3e4c4d270c3e197519982c015a70363f7ad0cd Mon Sep 17 00:00:00 2001 From: Jukka Kurkela Date: Fri, 15 Nov 2019 18:51:45 +0200 Subject: [PATCH] Tooltip label and value via controller (#6744) --- src/controllers/controller.bar.js | 19 +++++++++++++++++++ src/controllers/controller.bubble.js | 24 +++++++++++++++++++----- src/controllers/controller.radar.js | 14 ++++++++++++++ src/core/core.controller.js | 1 + src/core/core.datasetController.js | 17 +++++++++++++++++ src/core/core.tooltip.js | 9 +++------ 6 files changed, 73 insertions(+), 11 deletions(-) diff --git a/src/controllers/controller.bar.js b/src/controllers/controller.bar.js index 212ff840e..8d7e01f71 100644 --- a/src/controllers/controller.bar.js +++ b/src/controllers/controller.bar.js @@ -239,6 +239,25 @@ module.exports = DatasetController.extend({ return parsed; }, + /** + * @private + */ + _getLabelAndValue: function(index) { + const me = this; + const indexScale = me._getIndexScale(); + const valueScale = me._getValueScale(); + const parsed = me._getParsed(index); + const custom = parsed._custom; + const value = custom + ? '[' + custom.start + ', ' + custom.end + ']' + : '' + valueScale.getLabelForValue(parsed[valueScale.id]); + + return { + label: '' + indexScale.getLabelForValue(parsed[indexScale.id]), + value: value + }; + }, + initialize: function() { var me = this; var meta; diff --git a/src/controllers/controller.bubble.js b/src/controllers/controller.bubble.js index d344f488e..726f86b81 100644 --- a/src/controllers/controller.bubble.js +++ b/src/controllers/controller.bubble.js @@ -27,11 +27,6 @@ defaults._set('bubble', { title: function() { // Title doesn't make sense for scatter since we format the data as a point return ''; - }, - label: function(item, data) { - var datasetLabel = data.datasets[item.datasetIndex].label || ''; - var dataPoint = data.datasets[item.datasetIndex].data[item.index] || {r: '?'}; - return datasetLabel + ': (' + item.label + ', ' + item.value + ', ' + dataPoint.r + ')'; } } } @@ -94,6 +89,25 @@ module.exports = DatasetController.extend({ return Math.max(firstPoint, lastPoint) / 2; }, + /** + * @private + */ + _getLabelAndValue: function(index) { + const me = this; + const meta = me._cachedMeta; + const xScale = me.getScaleForId(meta.xAxisID); + const yScale = me.getScaleForId(meta.yAxisID); + const parsed = me._getParsed(index); + const x = xScale.getLabelForValue(parsed[xScale.id]); + const y = yScale.getLabelForValue(parsed[yScale.id]); + const r = parsed._custom; + + return { + label: meta.label, + value: '(' + x + ', ' + y + (r ? ', ' + r : '') + ')' + }; + }, + /** * @protected */ diff --git a/src/controllers/controller.radar.js b/src/controllers/controller.radar.js index b31ceab04..8596e39b3 100644 --- a/src/controllers/controller.radar.js +++ b/src/controllers/controller.radar.js @@ -77,6 +77,20 @@ module.exports = DatasetController.extend({ return this.chart.scale.id; }, + /** + * @private + */ + _getLabelAndValue: function(index) { + const me = this; + const scale = me._getValueScale(); + const parsed = me._getParsed(index); + + return { + label: scale._getLabels()[index], + value: '' + scale.getLabelForValue(parsed[scale.id]) + }; + }, + update: function(reset) { var me = this; var meta = me.getMeta(); diff --git a/src/core/core.controller.js b/src/core/core.controller.js index 5b2f5230e..c1ca43a3d 100644 --- a/src/core/core.controller.js +++ b/src/core/core.controller.js @@ -432,6 +432,7 @@ helpers.extend(Chart.prototype, /** @lends Chart */ { meta.type = type; meta.order = dataset.order || 0; meta.index = i; + meta.label = '' + dataset.label; if (meta.controller) { meta.controller.updateIndex(i); diff --git a/src/core/core.datasetController.js b/src/core/core.datasetController.js index 3de5c5531..d9adc2ef9 100644 --- a/src/core/core.datasetController.js +++ b/src/core/core.datasetController.js @@ -717,6 +717,23 @@ helpers.extend(DatasetController.prototype, { return false; }, + /** + * @private + */ + _getLabelAndValue: function(index) { + const me = this; + const indexScale = me._getIndexScale(); + const valueScale = me._getValueScale(); + const parsed = me._getParsed(index); + return { + label: indexScale ? '' + indexScale.getLabelForValue(parsed[indexScale.id]) : '', + value: valueScale ? '' + valueScale.getLabelForValue(parsed[valueScale.id]) : '' + }; + }, + + /** + * @private + */ _update: function(reset) { var me = this; me._configure(); diff --git a/src/core/core.tooltip.js b/src/core/core.tooltip.js index 4b83d1f7e..e6a439f83 100644 --- a/src/core/core.tooltip.js +++ b/src/core/core.tooltip.js @@ -206,14 +206,11 @@ function splitNewlines(str) { */ 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); + const {label, value} = chart.getDatasetMeta(datasetIndex).controller._getLabelAndValue(index); return { - label: indexScale ? '' + indexScale.getLabelForValue(parsed[indexScale.id]) : '', - value: valueScale ? '' + valueScale.getLabelForValue(parsed[valueScale.id]) : '', + label: label, + value: value, index: index, datasetIndex: datasetIndex, x: element._model.x, -- 2.47.2