Skip to content

Commit f5c9a65

Browse files
authored
Configurable tooltip box padding (#9625)
1 parent 3f23aab commit f5c9a65

File tree

5 files changed

+84
-6
lines changed

5 files changed

+84
-6
lines changed

docs/configuration/tooltip.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ Namespace: `options.plugins.tooltip`, the global options for the chart tooltips
3737
| `displayColors` | `boolean` | `true` | If true, color boxes are shown in the tooltip.
3838
| `boxWidth` | `number` | `bodyFont.size` | Width of the color box if displayColors is true.
3939
| `boxHeight` | `number` | `bodyFont.size` | Height of the color box if displayColors is true.
40+
| `boxPadding` | `number` | `1` | Padding between the color box and the text.
4041
| `usePointStyle` | `boolean` | `false` | Use the corresponding point style (from dataset options) instead of color boxes, ex: star, triangle etc. (size is based on the minimum value between boxWidth and boxHeight).
4142
| `borderColor` | [`Color`](../general/colors.md) | `'rgba(0, 0, 0, 0)'` | Color of the border.
4243
| `borderWidth` | `number` | `0` | Size of the border.

src/plugins/plugin.tooltip.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ function getTooltipSize(tooltip, options) {
187187
each(tooltip.beforeBody.concat(tooltip.afterBody), maxLineWidth);
188188

189189
// Body lines may include some extra width due to the color box
190-
widthPadding = options.displayColors ? (boxWidth + 2) : 0;
190+
widthPadding = options.displayColors ? (boxWidth + 2 + options.boxPadding) : 0;
191191
each(body, (bodyItem) => {
192192
each(bodyItem.before, maxLineWidth);
193193
each(bodyItem.lines, maxLineWidth);
@@ -681,7 +681,7 @@ export class Tooltip extends Element {
681681
const me = this;
682682
const labelColors = me.labelColors[i];
683683
const labelPointStyle = me.labelPointStyles[i];
684-
const {boxHeight, boxWidth} = options;
684+
const {boxHeight, boxWidth, boxPadding} = options;
685685
const bodyFont = toFont(options.bodyFont);
686686
const colorX = getAlignedX(me, 'left', options);
687687
const rtlColorX = rtlHelper.x(colorX);
@@ -717,8 +717,8 @@ export class Tooltip extends Element {
717717
ctx.lineDashOffset = labelColors.borderDashOffset || 0;
718718

719719
// Fill a white rect so that colours merge nicely if the opacity is < 1
720-
const outerX = rtlHelper.leftForLtr(rtlColorX, boxWidth);
721-
const innerX = rtlHelper.leftForLtr(rtlHelper.xPlus(rtlColorX, 1), boxWidth - 2);
720+
const outerX = rtlHelper.leftForLtr(rtlColorX, boxWidth - boxPadding);
721+
const innerX = rtlHelper.leftForLtr(rtlHelper.xPlus(rtlColorX, 1), boxWidth - boxPadding - 2);
722722
const borderRadius = toTRBLCorners(labelColors.borderRadius);
723723

724724
if (Object.values(borderRadius).some(v => v !== 0)) {
@@ -763,7 +763,7 @@ export class Tooltip extends Element {
763763
drawBody(pt, ctx, options) {
764764
const me = this;
765765
const {body} = me;
766-
const {bodySpacing, bodyAlign, displayColors, boxHeight, boxWidth} = options;
766+
const {bodySpacing, bodyAlign, displayColors, boxHeight, boxWidth, boxPadding} = options;
767767
const bodyFont = toFont(options.bodyFont);
768768
let bodyLineHeight = bodyFont.lineHeight;
769769
let xLinePadding = 0;
@@ -789,7 +789,7 @@ export class Tooltip extends Element {
789789
each(me.beforeBody, fillLineOfText);
790790

791791
xLinePadding = displayColors && bodyAlignForCalculation !== 'right'
792-
? bodyAlign === 'center' ? (boxWidth / 2 + 1) : (boxWidth + 2)
792+
? bodyAlign === 'center' ? (boxWidth / 2 + boxPadding) : (boxWidth + 2 + boxPadding)
793793
: 0;
794794

795795
// Draw body lines now
@@ -1166,6 +1166,7 @@ export default {
11661166
boxWidth: (ctx, opts) => opts.bodyFont.size,
11671167
multiKeyBackground: '#fff',
11681168
displayColors: true,
1169+
boxPadding: 0,
11691170
borderColor: 'rgba(0,0,0,0)',
11701171
borderWidth: 0,
11711172
animation: {
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
const data = [];
2+
for (let x = 0; x < 3; x++) {
3+
for (let y = 0; y < 3; y++) {
4+
data.push({x, y});
5+
}
6+
}
7+
8+
module.exports = {
9+
config: {
10+
type: 'scatter',
11+
data: {
12+
datasets: [{
13+
data,
14+
backgroundColor: 'red',
15+
radius: 1,
16+
hoverRadius: 0
17+
}],
18+
},
19+
options: {
20+
scales: {
21+
x: {display: false},
22+
y: {display: false}
23+
},
24+
plugins: {
25+
legend: false,
26+
title: false,
27+
filler: false,
28+
tooltip: {
29+
mode: 'point',
30+
intersect: true,
31+
// spriteText: use white background to hide any gaps between fonts
32+
backgroundColor: 'white',
33+
borderColor: 'black',
34+
borderWidth: 1,
35+
callbacks: {
36+
label: () => 'label',
37+
},
38+
boxPadding: 30
39+
},
40+
},
41+
},
42+
plugins: [{
43+
afterDraw: function(chart) {
44+
const canvas = chart.canvas;
45+
const rect = canvas.getBoundingClientRect();
46+
const meta = chart.getDatasetMeta(0);
47+
let point, event;
48+
49+
for (let i = 0; i < data.length; i++) {
50+
point = meta.data[i];
51+
event = {
52+
type: 'mousemove',
53+
target: canvas,
54+
clientX: rect.left + point.x,
55+
clientY: rect.top + point.y
56+
};
57+
chart._handleEvent(event);
58+
chart.tooltip.handleEvent(event);
59+
chart.tooltip.draw(chart.ctx);
60+
}
61+
}
62+
}]
63+
},
64+
options: {
65+
spriteText: true,
66+
canvas: {
67+
height: 400,
68+
width: 500
69+
}
70+
}
71+
};
14.3 KB
Loading

types/index.esm.d.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2216,6 +2216,11 @@ export interface LegendOptions<TType extends ChartType> {
22162216
* @default fontSize
22172217
*/
22182218
boxHeight: number;
2219+
/**
2220+
* Padding between the color box and the text
2221+
* @default 1
2222+
*/
2223+
boxPadding: number;
22192224
/**
22202225
* Color of label
22212226
* @see Defaults.color

0 commit comments

Comments
 (0)