score:15
Accepted answer
The template does not recognize HTML. You have to use the customTooltips option. Below is an example adapted (but not optimized) from https://github.com/nnnick/Chart.js/blob/master/samples/line-customTooltips.html
HTML
<canvas id="myChart" width="400" height="200"></canvas>
<div id="chartjs-tooltip"></div>
CSS
#chartjs-tooltip {
opacity: 0;
position: absolute;
background: rgba(0, 0, 0, .7);
color: white;
padding: 3px;
border-radius: 3px;
-webkit-transition: all .1s ease;
transition: all .1s ease;
pointer-events: none;
-webkit-transform: translate(-50%, 0);
transform: translate(-50%, 0);
}
JS
var ctx = $("#myChart").get(0).getContext("2d");
var data = {
labels: ["January", "February", "March", "April", "May", "June", "July"],
datasets: [{
label: "My First dataset",
fillColor: "rgba(220,220,220,0.2)",
strokeColor: "rgba(220,220,220,1)",
pointColor: "rgba(220,220,220,1)",
pointStrokeColor: "#fff",
pointHighlightFill: "#fff",
pointHighlightStroke: "rgba(220,220,220,1)",
data: [65, 59, 80, 81, 56, 55, 40]
}]
};
var myLineChart = new Chart(ctx).Line(data, {
customTooltips: function (tooltip) {
var tooltipEl = $('#chartjs-tooltip');
if (!tooltip) {
tooltipEl.css({
opacity: 0
});
return;
}
tooltipEl.removeClass('above below');
tooltipEl.addClass(tooltip.yAlign);
// split out the label and value and make your own tooltip here
var parts = tooltip.text.split(":");
var innerHtml = '<span>' + parts[0].trim() + '</span> : <span><b>' + parts[1].trim() + '</b></span>';
tooltipEl.html(innerHtml);
tooltipEl.css({
opacity: 1,
left: tooltip.chart.canvas.offsetLeft + tooltip.x + 'px',
top: tooltip.chart.canvas.offsetTop + tooltip.y + 'px',
fontFamily: tooltip.fontFamily,
fontSize: tooltip.fontSize,
fontStyle: tooltip.fontStyle,
});
}
});
Fiddle - http://jsfiddle.net/6rxdo0c0/1/
score:2
I have an up-to-date answer using jquery and bootstrap 4 tooltips that creates the tooltip divs dynamically. Assume your html is
<canvas id="myPieChart" width="100" height="55"></canvas>
Then use this script:
<script>
$(document).ready(function() {
let canvas = $("#donutChart")[0];
let ctx = canvas.getContext("2d");
let donutChart = new Chart(ctx, {
type: 'doughnut',
data: data,
options: {
tooltips: {
callbacks: {
title: function(){
return null;
},
label: function(tooltipItem, data) {
var multiline = ['First Line', 'second line'];
return multiline;
},
},
enabled: false, // the builtin tooltips cannot extend beyond the canvas
custom: function(tooltip) {
var tooltipEl = $('#chartjs-tooltip');
if(tooltipEl.length == 0) { // if not exists, create it
tooltipEl = $('<div class="tooltip fade" id="chartjs-tooltip"></div>').appendTo('body');
}
if ( !tooltip.body ) { // Hide if no tooltip
tooltipEl.remove();
return;
}
tooltipEl.removeClass('bs-tooltip-top bs-tooltip-bottom');
tooltipEl.addClass( 'bs-tooltip-' + (tooltip.yAlign == "bottom" ? "top" : "bottom") ); // different naming in bootstrap
var innerHtml = '<div class="arrow" style="left: '+tooltip.caretX+'px;"></div>'
+ '<div class="tooltip-inner" style="max-width: none;">' + tooltip.body[0].join('<br>')
+ '</div>';
tooltipEl.html( innerHtml );
tooltipEl.css( {
opacity: 1,
left: $("#donutChart").offset().left + tooltip.x + 'px',
top: $("#donutChart").offset().top + tooltip.y + 'px'
} );
},
},
hover: {
mode: null,
},
}
});
});
</script>
Source: stackoverflow.com
Related Query
- Chart.js: changing tooltip template
- Chart JS: Use function in tooltip template
- ChartJS New Lines '\n' in X axis Labels or Displaying More Information Around Chart or Tooltip with ChartJS V2
- Chart JS custom tooltip option?
- Changing fontFamily on ChartJS bar chart
- Chart.js: Dynamic Changing of Chart Type (Line to Bar as Example)
- chart.js scatter chart - displaying label specific to point in tooltip
- show label in tooltip but not in x axis for chartjs line chart
- Chart.js doughnut chart tooltip size?
- ChartJS add tooltip to a grouped bar chart
- Chart JS Show HTML in Tooltip
- chart js tooltip how to control the data that show
- Changing cursor to pointer on Chart.js bar chart when hover (mousemove) event is disabled?
- Chart js v2 tooltip callback line breaks
- How to disable Chart JS tooltip when there is no value?
- Chart.js tooltip hover customization for mixed chart
- Add all data in the tooltip of Chart JS
- Changing x axis labels in Chart.js line chart
- ng2-charts customize data and whole html content of tooltip displayed when hovering on bar chart
- Charts.js tooltip overlapping text on chart
- ChartJS: Show all labels of a mixed chart in the tooltip
- Chart.js line chart tooltip shows wrong label when line doesn't start at first label
- Chart JS custom tooltip not showing
- Chartjs - Insert additional data into chart tooltip
- Add a text as tooltip for each point on a line chart
- Changing width of chart.js chart without changing height
- ChartJS (React) Line Chart - How to show single tooltip with data and labels from 3 (multiple) dataset?
- Chart.js tooltip at any point on the chart
- Tooltip flickering when hovered on chart
- Changing tooltip color in Chart.js
More Query from same tag
- I am using chartjs node canvas to render a chart however the graph options are being ignored
- Created an onclick function to remove data from a line chart in ChartsJs, but getting "Cannot read property 'data' of undefined" error
- angular-chart.js, how to change colours labels
- Chart.Mvc scale begin at 0 - Nullable bool property remains null
- How to add new x axis in chart JS?
- Chart Js - Globally set bar chart color based on value
- How to sort MMMYY on Chart.Js and display the values that match the month?
- Add dynamically function to Chart.js
- Chart.JS Get Json data for chart and return dataset where a type equals a certain type
- Chart.js t.ticks.map is not a function
- set scroll on bar chart in chartjs
- How to create line graph use chartjs?
- chartjs table each making series not proceeding to next tr
- "Responsive" Chart.js chart is taller than the containing window
- Is it possible to print a chart with vue-chartjs?
- How to hide more than one legend on Chartjs?
- How to make a 30days chart responsive on mobile using chart.js?
- Show dynamic data with dynamic colours in chartjs
- Chart.js canvas rendered by ng-repeat does not show charts
- Chart.js 2.9.4 - howto set a margin for axes min\max and time axis label separation
- Chart JS distance between bar stacked
- Missing colors in my Chart.js, my data looks fine, but no color
- chart js in an especific label
- display vertical axis label in line chart using chart.js
- Pie Chart shows up only when I zoom in-out
- How to use HTML in custom tooltip in angular charts?
- chart.js php live update
- Dynamically update chart in chartJs
- Chart.JS tooltip color doesn't works as expected in pie chart. How to solve it?
- chartjs show 24 hours in line chart