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

Format numbers in tooltip #7004

Merged
merged 3 commits into from
Jan 27, 2020
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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