From 2153080b017a9823468d695a70a41dd4066984ee Mon Sep 17 00:00:00 2001 From: Evert Timberg Date: Sun, 21 Jan 2018 16:47:50 -0500 Subject: [PATCH] Update custom tooltip documentation and samples (#5166) --- docs/configuration/tooltip.md | 18 +++++++++++------- samples/tooltips/custom-line.html | 6 +++--- samples/tooltips/custom-pie.html | 6 +++--- 3 files changed, 17 insertions(+), 13 deletions(-) diff --git a/docs/configuration/tooltip.md b/docs/configuration/tooltip.md index 9990fc9161f..93db40147b7 100644 --- a/docs/configuration/tooltip.md +++ b/docs/configuration/tooltip.md @@ -6,7 +6,7 @@ The tooltip configuration is passed into the `options.tooltips` namespace. The g | Name | Type | Default | Description | -----| ---- | --------| ----------- -| `enabled` | `Boolean` | `true` | Are tooltips enabled +| `enabled` | `Boolean` | `true` | Are on-canvas tooltips enabled | `custom` | `Function` | `null` | See [custom tooltip](#external-custom-tooltips) section. | `mode` | `String` | `'nearest'` | Sets which elements appear in the tooltip. [more...](../general/interactions/modes.md#interaction-modes). | `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, { data: data, options: { tooltips: { + // Disable the on-canvas tooltip + enabled: false, + custom: function(tooltipModel) { // Tooltip Element var tooltipEl = document.getElementById('chartjs-tooltip'); @@ -199,7 +202,7 @@ var myPieChart = new Chart(ctx, { if (!tooltipEl) { tooltipEl = document.createElement('div'); tooltipEl.id = 'chartjs-tooltip'; - tooltipEl.innerHTML = "
" + tooltipEl.innerHTML = "
"; document.body.appendChild(tooltipEl); } @@ -238,7 +241,7 @@ var myPieChart = new Chart(ctx, { var style = 'background:' + colors.backgroundColor; style += '; border-color:' + colors.borderColor; style += '; border-width: 2px'; - var span = ''; + var span = ''; innerHtml += '' + span + body + ''; }); innerHtml += ''; @@ -252,11 +255,12 @@ var myPieChart = new Chart(ctx, { // Display, position, and set styles for font tooltipEl.style.opacity = 1; + tooltipEl.style.position = 'absolute'; tooltipEl.style.left = position.left + tooltipModel.caretX + 'px'; tooltipEl.style.top = position.top + tooltipModel.caretY + 'px'; - tooltipEl.style.fontFamily = tooltipModel._fontFamily; - tooltipEl.style.fontSize = tooltipModel.fontSize; - tooltipEl.style.fontStyle = tooltipModel._fontStyle; + tooltipEl.style.fontFamily = tooltipModel._bodyFontFamily; + tooltipEl.style.fontSize = tooltipModel.bodyFontSize + 'px'; + tooltipEl.style.fontStyle = tooltipModel._bodyFontStyle; tooltipEl.style.padding = tooltipModel.yPadding + 'px ' + tooltipModel.xPadding + 'px'; } } @@ -264,7 +268,7 @@ var myPieChart = new Chart(ctx, { }); ``` -See `samples/tooltips/line-customTooltips.html` for examples on how to get started. +See [samples](http://www.chartjs.org/samples/) for examples on how to get started with custom tooltips. ## Tooltip Model The tooltip model contains parameters that can be used to render the tooltip. diff --git a/samples/tooltips/custom-line.html b/samples/tooltips/custom-line.html index 801b12df680..011762907c4 100644 --- a/samples/tooltips/custom-line.html +++ b/samples/tooltips/custom-line.html @@ -102,9 +102,9 @@ tooltipEl.style.opacity = 1; tooltipEl.style.left = positionX + tooltip.caretX + 'px'; tooltipEl.style.top = positionY + tooltip.caretY + 'px'; - tooltipEl.style.fontFamily = tooltip._fontFamily; - tooltipEl.style.fontSize = tooltip.fontSize; - tooltipEl.style.fontStyle = tooltip._fontStyle; + tooltipEl.style.fontFamily = tooltip._bodyFontFamily; + tooltipEl.style.fontSize = tooltip.bodyFontSize + 'px'; + tooltipEl.style.fontStyle = tooltip._bodyFontStyle; tooltipEl.style.padding = tooltip.yPadding + 'px ' + tooltip.xPadding + 'px'; }; diff --git a/samples/tooltips/custom-pie.html b/samples/tooltips/custom-pie.html index 9c3cd0aa608..7bfbc56f1e9 100644 --- a/samples/tooltips/custom-pie.html +++ b/samples/tooltips/custom-pie.html @@ -98,9 +98,9 @@ tooltipEl.style.opacity = 1; tooltipEl.style.left = positionX + tooltip.caretX + 'px'; tooltipEl.style.top = positionY + tooltip.caretY + 'px'; - tooltipEl.style.fontFamily = tooltip._fontFamily; - tooltipEl.style.fontSize = tooltip.fontSize; - tooltipEl.style.fontStyle = tooltip._fontStyle; + tooltipEl.style.fontFamily = tooltip._bodyFontFamily; + tooltipEl.style.fontSize = tooltip.bodyFontSize; + tooltipEl.style.fontStyle = tooltip._bodyFontStyle; tooltipEl.style.padding = tooltip.yPadding + 'px ' + tooltip.xPadding + 'px'; };