Skip to content

Commit

Permalink
Format numbers in tooltip (#7004)
Browse files Browse the repository at this point in the history
* Format numbers in tooltip
* Add check for typeof number
* Implement only for linear and log scales
  • Loading branch information
benmccann authored and etimberg committed Jan 27, 2020
1 parent b59dd50 commit 1ae11c4
Show file tree
Hide file tree
Showing 6 changed files with 12 additions and 7 deletions.
7 changes: 4 additions & 3 deletions src/plugins/plugin.tooltip.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,13 +77,14 @@ defaults._set('tooltips', {
// Args are: (tooltipItem, data)
beforeLabel: helpers.noop,
label: function(tooltipItem, data) {
var label = data.datasets[tooltipItem.datasetIndex].label || '';
let label = data.datasets[tooltipItem.datasetIndex].label || '';

if (label) {
label += ': ';
}
if (!helpers.isNullOrUndef(tooltipItem.value)) {
label += tooltipItem.value;
const value = tooltipItem.value;
if (!helpers.isNullOrUndef(value)) {
label += value;
}
return label;
},
Expand Down
4 changes: 4 additions & 0 deletions src/scales/scale.linearbase.js
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,10 @@ class LinearScaleBase extends Scale {
me._endValue = end;
me._valueRange = end - start;
}

getLabelForValue(value) {
return new Intl.NumberFormat().format(value);
}
}

export default LinearScaleBase;
2 changes: 1 addition & 1 deletion src/scales/scale.logarithmic.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ class LogarithmicScale extends Scale {
}

getLabelForValue(value) {
return value === undefined ? 0 : value;
return value === undefined ? 0 : new Intl.NumberFormat().format(value);
}

getPixelForTick(index) {
Expand Down
2 changes: 1 addition & 1 deletion test/specs/scale.linear.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@ describe('Linear Scale', function() {
});
chart.update();

expect(chart.scales.y.getLabelForValue(7)).toBe(7);
expect(chart.scales.y.getLabelForValue(7)).toBe('7');
});

it('Should correctly determine the min and max data values when stacked mode is turned on', function() {
Expand Down
2 changes: 1 addition & 1 deletion test/specs/scale.logarithmic.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -738,7 +738,7 @@ describe('Logarithmic Scale tests', function() {
}
});

expect(chart.scales.y.getLabelForValue(150)).toBe(150);
expect(chart.scales.y.getLabelForValue(150)).toBe('150');
});

describe('when', function() {
Expand Down
2 changes: 1 addition & 1 deletion test/specs/scale.radialLinear.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -414,7 +414,7 @@ describe('Test the radial linear scale', function() {
}
}
});
expect(chart.scales.r.getLabelForValue(5)).toBe(5);
expect(chart.scales.r.getLabelForValue(5)).toBe('5');
});

it('should get the correct distance from the center point', function() {
Expand Down

0 comments on commit 1ae11c4

Please sign in to comment.