Skip to content

Commit 222479c

Browse files
committed
Update the tooltip with a new caretPadding setting. Previously this value was essentially hard coded to
2 because of a typo that read it from the positioner output. Based on #3599 we agreed to make this into a config setting.
1 parent 6f99157 commit 222479c

File tree

3 files changed

+55
-1
lines changed

3 files changed

+55
-1
lines changed

docs/configuration/tooltip.md

+1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ The tooltip configuration is passed into the `options.tooltips` namespace. The g
3434
| `footerMarginTop` | `Number` | `6` | Margin to add before drawing the footer.
3535
| `xPadding` | `Number` | `6` | Padding to add on left and right of tooltip.
3636
| `yPadding` | `Number` | `6` | Padding to add on top and bottom of tooltip.
37+
| `caretPadding` | `Number` | `2` | Extra distance to move the end of the tooltip arrow away from the tooltip point.
3738
| `caretSize` | `Number` | `5` | Size, in px, of the tooltip arrow.
3839
| `cornerRadius` | `Number` | `6` | Radius of tooltip corner curves.
3940
| `multiKeyBackground` | Color | `'#fff'` | Color to draw behind the colored boxes when multiple items are in the tooltip

src/core/core.tooltip.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ module.exports = function(Chart) {
3434
footerAlign: 'left',
3535
yPadding: 6,
3636
xPadding: 6,
37+
caretPadding: 2,
3738
caretSize: 5,
3839
cornerRadius: 6,
3940
multiKeyBackground: '#fff',
@@ -522,7 +523,7 @@ module.exports = function(Chart) {
522523
// Initial positioning and colors
523524
model.x = Math.round(tooltipPosition.x);
524525
model.y = Math.round(tooltipPosition.y);
525-
model.caretPadding = helpers.getValueOrDefault(tooltipPosition.padding, 2);
526+
model.caretPadding = opts.caretPadding;
526527
model.labelColors = labelColors;
527528

528529
// data points

test/specs/core.tooltip.tests.js

+52
Original file line numberDiff line numberDiff line change
@@ -641,6 +641,58 @@ describe('Core.Tooltip', function() {
641641
}));
642642
});
643643

644+
it('should set the caretPadding based on a config setting', function() {
645+
var chart = window.acquireChart({
646+
type: 'line',
647+
data: {
648+
datasets: [{
649+
label: 'Dataset 1',
650+
data: [10, 20, 30],
651+
pointHoverBorderColor: 'rgb(255, 0, 0)',
652+
pointHoverBackgroundColor: 'rgb(0, 255, 0)',
653+
tooltipHidden: true
654+
}, {
655+
label: 'Dataset 2',
656+
data: [40, 40, 40],
657+
pointHoverBorderColor: 'rgb(0, 0, 255)',
658+
pointHoverBackgroundColor: 'rgb(0, 255, 255)'
659+
}],
660+
labels: ['Point 1', 'Point 2', 'Point 3']
661+
},
662+
options: {
663+
tooltips: {
664+
caretPadding: 10
665+
}
666+
}
667+
});
668+
669+
// Trigger an event over top of the
670+
var meta0 = chart.getDatasetMeta(0);
671+
var point0 = meta0.data[1];
672+
673+
var node = chart.canvas;
674+
var rect = node.getBoundingClientRect();
675+
676+
var evt = new MouseEvent('mousemove', {
677+
view: window,
678+
bubbles: true,
679+
cancelable: true,
680+
clientX: rect.left + point0._model.x,
681+
clientY: rect.top + point0._model.y
682+
});
683+
684+
// Manually trigger rather than having an async test
685+
node.dispatchEvent(evt);
686+
687+
// Check and see if tooltip was displayed
688+
var tooltip = chart.tooltip;
689+
690+
expect(tooltip._model).toEqual(jasmine.objectContaining({
691+
// Positioning
692+
caretPadding: 10,
693+
}));
694+
});
695+
644696
it('Should have dataPoints', function() {
645697
var chart = window.acquireChart({
646698
type: 'line',

0 commit comments

Comments
 (0)