Skip to content

Commit 274fca6

Browse files
authored
Update custom tooltip documentation and samples (#5166)
1 parent f82c8ad commit 274fca6

File tree

3 files changed

+17
-13
lines changed

3 files changed

+17
-13
lines changed

docs/configuration/tooltip.md

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ The tooltip configuration is passed into the `options.tooltips` namespace. The g
66

77
| Name | Type | Default | Description
88
| -----| ---- | --------| -----------
9-
| `enabled` | `Boolean` | `true` | Are tooltips enabled
9+
| `enabled` | `Boolean` | `true` | Are on-canvas tooltips enabled
1010
| `custom` | `Function` | `null` | See [custom tooltip](#external-custom-tooltips) section.
1111
| `mode` | `String` | `'nearest'` | Sets which elements appear in the tooltip. [more...](../general/interactions/modes.md#interaction-modes).
1212
| `intersect` | `Boolean` | `true` | if true, the tooltip mode applies only when the mouse position intersects with an element. If false, the mode will be applied at all times.
@@ -191,6 +191,9 @@ var myPieChart = new Chart(ctx, {
191191
data: data,
192192
options: {
193193
tooltips: {
194+
// Disable the on-canvas tooltip
195+
enabled: false,
196+
194197
custom: function(tooltipModel) {
195198
// Tooltip Element
196199
var tooltipEl = document.getElementById('chartjs-tooltip');
@@ -199,7 +202,7 @@ var myPieChart = new Chart(ctx, {
199202
if (!tooltipEl) {
200203
tooltipEl = document.createElement('div');
201204
tooltipEl.id = 'chartjs-tooltip';
202-
tooltipEl.innerHTML = "<table></table>"
205+
tooltipEl.innerHTML = "<table></table>";
203206
document.body.appendChild(tooltipEl);
204207
}
205208

@@ -238,7 +241,7 @@ var myPieChart = new Chart(ctx, {
238241
var style = 'background:' + colors.backgroundColor;
239242
style += '; border-color:' + colors.borderColor;
240243
style += '; border-width: 2px';
241-
var span = '<span class="chartjs-tooltip-key" style="' + style + '"></span>';
244+
var span = '<span style="' + style + '"></span>';
242245
innerHtml += '<tr><td>' + span + body + '</td></tr>';
243246
});
244247
innerHtml += '</tbody>';
@@ -252,19 +255,20 @@ var myPieChart = new Chart(ctx, {
252255

253256
// Display, position, and set styles for font
254257
tooltipEl.style.opacity = 1;
258+
tooltipEl.style.position = 'absolute';
255259
tooltipEl.style.left = position.left + tooltipModel.caretX + 'px';
256260
tooltipEl.style.top = position.top + tooltipModel.caretY + 'px';
257-
tooltipEl.style.fontFamily = tooltipModel._fontFamily;
258-
tooltipEl.style.fontSize = tooltipModel.fontSize;
259-
tooltipEl.style.fontStyle = tooltipModel._fontStyle;
261+
tooltipEl.style.fontFamily = tooltipModel._bodyFontFamily;
262+
tooltipEl.style.fontSize = tooltipModel.bodyFontSize + 'px';
263+
tooltipEl.style.fontStyle = tooltipModel._bodyFontStyle;
260264
tooltipEl.style.padding = tooltipModel.yPadding + 'px ' + tooltipModel.xPadding + 'px';
261265
}
262266
}
263267
}
264268
});
265269
```
266270

267-
See `samples/tooltips/line-customTooltips.html` for examples on how to get started.
271+
See [samples](http://www.chartjs.org/samples/) for examples on how to get started with custom tooltips.
268272

269273
## Tooltip Model
270274
The tooltip model contains parameters that can be used to render the tooltip.

samples/tooltips/custom-line.html

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,9 +102,9 @@
102102
tooltipEl.style.opacity = 1;
103103
tooltipEl.style.left = positionX + tooltip.caretX + 'px';
104104
tooltipEl.style.top = positionY + tooltip.caretY + 'px';
105-
tooltipEl.style.fontFamily = tooltip._fontFamily;
106-
tooltipEl.style.fontSize = tooltip.fontSize;
107-
tooltipEl.style.fontStyle = tooltip._fontStyle;
105+
tooltipEl.style.fontFamily = tooltip._bodyFontFamily;
106+
tooltipEl.style.fontSize = tooltip.bodyFontSize + 'px';
107+
tooltipEl.style.fontStyle = tooltip._bodyFontStyle;
108108
tooltipEl.style.padding = tooltip.yPadding + 'px ' + tooltip.xPadding + 'px';
109109
};
110110

samples/tooltips/custom-pie.html

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,9 +98,9 @@
9898
tooltipEl.style.opacity = 1;
9999
tooltipEl.style.left = positionX + tooltip.caretX + 'px';
100100
tooltipEl.style.top = positionY + tooltip.caretY + 'px';
101-
tooltipEl.style.fontFamily = tooltip._fontFamily;
102-
tooltipEl.style.fontSize = tooltip.fontSize;
103-
tooltipEl.style.fontStyle = tooltip._fontStyle;
101+
tooltipEl.style.fontFamily = tooltip._bodyFontFamily;
102+
tooltipEl.style.fontSize = tooltip.bodyFontSize;
103+
tooltipEl.style.fontStyle = tooltip._bodyFontStyle;
104104
tooltipEl.style.padding = tooltip.yPadding + 'px ' + tooltip.xPadding + 'px';
105105
};
106106

0 commit comments

Comments
 (0)