Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 6 commits into from Jan 30, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 10 additions & 2 deletions docs/configuration/tooltip.md
Expand Up @@ -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
Expand Down
21 changes: 0 additions & 21 deletions src/controllers/controller.horizontalBar.js
Expand Up @@ -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'
}
Expand Down
17 changes: 13 additions & 4 deletions src/core/core.tooltip.js
Expand Up @@ -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];
Expand All @@ -70,7 +70,11 @@ defaults._set('global', {
if (label) {
label += ': ';
}
label += tooltipItem.yLabel;
if (!helpers.isNullOrUndef(tooltipItem.value)) {
label += tooltipItem.value;
} else {
label += tooltipItem.yLabel;
}
return label;
},
labelColor: function(tooltipItem, chart) {
Expand Down Expand Up @@ -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,
Expand Down