Replace tooltip item xLabel and yLabel with label and value (#5996)

This commit is contained in:
Jukka Kurkela 2019-01-30 12:41:19 +02:00 committed by Simon Brunel
parent fee44cfeef
commit 29f7fa23bb
3 changed files with 23 additions and 27 deletions

View File

@ -162,10 +162,18 @@ The tooltip items passed to the tooltip callbacks implement the following interf
```javascript
{
// X Value of the tooltip as a string
// Label for the tooltip
label: string,
// Value for the tooltip
value: string,
// X Value of the tooltip
// (deprecated) use `value` or `label` instead
xLabel: string,
// Y value of the tooltip as a string
// Y value of the tooltip
// (deprecated) use `value` or `label` instead
yLabel: string,
// Index of the dataset the item comes from

View File

@ -35,27 +35,6 @@ defaults._set('horizontalBar', {
},
tooltips: {
callbacks: {
title: function(item, data) {
// Pick first xLabel for now
var title = '';
if (item.length > 0) {
if (item[0].yLabel) {
title = item[0].yLabel;
} else if (data.labels.length > 0 && item[0].index < data.labels.length) {
title = data.labels[item[0].index];
}
}
return title;
},
label: function(item, data) {
var datasetLabel = data.datasets[item.datasetIndex].label || '';
return datasetLabel + ': ' + item.xLabel;
}
},
mode: 'index',
axis: 'y'
}

View File

@ -40,15 +40,15 @@ defaults._set('global', {
// Args are: (tooltipItems, data)
beforeTitle: helpers.noop,
title: function(tooltipItems, data) {
// Pick first xLabel for now
var title = '';
var labels = data.labels;
var labelCount = labels ? labels.length : 0;
if (tooltipItems.length > 0) {
var item = tooltipItems[0];
if (item.xLabel) {
if (item.label) {
title = item.label;
} else if (item.xLabel) {
title = item.xLabel;
} else if (labelCount > 0 && item.index < labelCount) {
title = labels[item.index];
@ -70,7 +70,11 @@ defaults._set('global', {
if (label) {
label += ': ';
}
if (!helpers.isNullOrUndef(tooltipItem.value)) {
label += tooltipItem.value;
} else {
label += tooltipItem.yLabel;
}
return label;
},
labelColor: function(tooltipItem, chart) {
@ -206,10 +210,15 @@ function createTooltipItem(element) {
var yScale = element._yScale || element._scale; // handle radar || polarArea charts
var index = element._index;
var datasetIndex = element._datasetIndex;
var controller = element._chart.getDatasetMeta(datasetIndex).controller;
var indexScale = controller._getIndexScale();
var valueScale = controller._getValueScale();
return {
xLabel: xScale ? xScale.getLabelForIndex(index, datasetIndex) : '',
yLabel: yScale ? yScale.getLabelForIndex(index, datasetIndex) : '',
label: indexScale ? indexScale.getLabelForIndex(index, datasetIndex) : '',
value: valueScale ? valueScale.getLabelForIndex(index, datasetIndex) : '',
index: index,
datasetIndex: datasetIndex,
x: element._model.x,