Q about the custom tooltip
I attempting to do multiple doughnut charts on a web page. These charts is sharing the css and jscript file for. These chart have their own value and label.
I follow the exactly way shown in the example and tried to do it but encounter a problem that all of my charts are displaying the same data.
Is the custom tooltip example is modifying the css property or the property that assigned to the div id=chartjs-tooltip?
I'm doing the same thing. You'll have to create a tooltip div, css AND javascript code for each of your doughnut charts.
For example: for donut1, create a div as in the example but with id="chartjs-tooltip1", create css for this div as in the example and then, when you initialise the doughnut "donut1", add the custom tooltip extension code into the initialisation, so:
var donutChart1 = new Chart(donut1).Doughnut(data1, { tooltipFontSize: 10, tooltipTemplate: "<%if (label){%><%=label%>: <%}%><%= value %> tasks", percentageInnerCutout : 85, customTooltips: function(tooltip) { // Tooltip Element var tooltipEl = $('#chartjs-tooltip1');
// Hide if no tooltip
if (!tooltip) {
tooltipEl.css({
opacity: 0
});
return;
}
// Set caret Position
tooltipEl.removeClass('above below');
tooltipEl.addClass(tooltip.yAlign);
// Set Text
tooltipEl.html(tooltip.text);
// Find Y Location on page
var top;
if (tooltip.yAlign == 'above') {
top = tooltip.y - tooltip.caretHeight - tooltip.caretPadding;
} else {
top = tooltip.y + tooltip.caretHeight + tooltip.caretPadding;
}
// Display, position, and set styles for font
tooltipEl.css({
opacity: 1,
left: tooltip.chart.canvas.offsetLeft + tooltip.x + 'px',
top: tooltip.chart.canvas.offsetTop + top + 'px',
fontFamily: tooltip.fontFamily,
fontSize: tooltip.fontSize,
fontStyle: tooltip.fontStyle,
});
}
});
Then repeat the above for donut2, etc. making sure you create the tooltip divs and the css for each new id.
The important thing is to add the custom tooltip code to the initialisation of the doughnut, NOT as a general extension as in the example. Then it'll all work fine.
A lot of Javascript and css but I am creating my doughnut charts in a php loop and simply use the loop index appended to the div names, etc to automate the whole thing. There are probably other ways ;-)
Good luck!
There is an extremely easy way to do this. You'll have to modify it for your own needs but here's what I have:
JS
function customTooltips(tooltip) {
// Tooltip Element
var tooltipEl = $('#chartjs-customtooltip');
// Make the element if not available
if (!tooltipEl[0]) {
$('body').append('<div id="chartjs-customtooltip"></div>');
tooltipEl = $('#chartjs-customtooltip');
}
// Hide if no tooltip
if (!tooltip) {
tooltipEl.css({
opacity: 0
});
return;
}
// Set caret Position
tooltipEl.removeClass('above below no-transform');
if (tooltip.yAlign) {
tooltipEl.addClass(tooltip.yAlign);
} else {
tooltipEl.addClass('no-transform');
}
// Set Text
if (tooltip.text) {
tooltipEl.html(tooltip.text);
} else {
var innerHtml = '<div class="title">' + tooltip.title + '</div>';
for (var i = 0; i < tooltip.labels.length; i++) {
innerHtml += [
'<div class="section">',
' <span class="key" style="background-color:' + tooltip.legendColors[i].fill + '"></span>',
' <span class="value">' + tooltip.labels[i] + '</span>',
'</div>'
].join('');
}
tooltipEl.html(innerHtml);
}
// Find Y Location on page
var top = 0;
if (tooltip.yAlign) {
if (tooltip.yAlign == 'above') {
top = tooltip.y - tooltip.caretHeight - tooltip.caretPadding;
} else {
top = tooltip.y + tooltip.caretHeight + tooltip.caretPadding;
}
}
var offset = $(tooltip.chart.canvas).offset();
// Display, position, and set styles for font
tooltipEl.css({
opacity: 1,
width: tooltip.width ? (tooltip.width + 'px') : 'auto',
left: offset.left + tooltip.x + 'px',
top: offset.top + top + 'px',
fontFamily: tooltip.fontFamily,
fontSize: tooltip.fontSize,
fontStyle: tooltip.fontStyle,
});
}
CSS (Stylus)
#chartjs-customtooltip
opacity: 1
position: absolute
background: rgba(0, 0, 0, .7)
color: white
padding: 3px
border-radius: 3px
transition: all, .1s ease, opacity .1s .2s linear
pointer-events: none
&.below
transform: translate(-50%, 0)
&.below:before
border: solid
border-color: #111 transparent
border-color: rgba(0, 0, 0, .8) transparent
border-width: 0 8px 8px 8px
bottom: 1em
content: ""
display: block
left: 50%
position: absolute
z-index: 99
transform: translate(-50%, -100%)
&.above
transform: translate(-50%, -100%)
&.above:before
border: solid
border-color: #111 transparent
border-color: rgba(0, 0, 0, .8) transparent
border-width: 8px 8px 0 8px
bottom: 1em
content: ""
display: block
left: 50%
top: 100%
position: absolute
z-index: 99
transform: translate(-50%, 0)
.title
width: 100%
font-weight: 700
text-align: center
.key
display: inline-block
width: 10px
height: 10px
@tannerlinsley so thats the easy way?
It is pretty involved and they are custom tooltip after all so I look at it this way. I can write one extremely customizable function that handles all of my tooltips or I can write one for every chart.
@tannerlinsley How do I cast the tooltip obj into the function?
The tooltip object is first parameter passed to your customTooltips function.
In your chart defaults (or in an individual chart, if you want to really get custom:
{
// ...
customTooltips: function(tooltip){
// tooltip is the tooltip object
},
// ...
}
Am new to chart.js..and am attempting to do bar chart with custom tool tips..currently default tooltip is displaying and in toltip am able to display only data values and same label but i need to display different label for each bar.. can i able to display different label for each bar? it would be grateful if you help in this..
I've created a gist that takes in a custom property based on this code:
https://gist.github.com/MikeGraf/149c6ec2640288de6df7
Just thought I'd share.
simple.
Im working with this in my work, so I cant develop too much(cant waste time, deadlines :'( )
So what you do is this:
Create your chart normally but pass your custom id for the tooltip:
window.myDoughnut = new Chart(ctx).Doughnut(doughnutData, { responsive: true, customTooltips: function (tooltip) { chartcustomtooltip(tooltip, '#chartjs-example-tooltip') } });
Here instead of having one and only one tooltip, it will just get the id that you inserted above
function chartcustomtooltip(tooltip, id) {
// Tooltip Element
var tooltipEl = $(id);
// Hide if no tooltip
if (!tooltip) {
tooltipEl.css({
opacity: 0
});
return;
}
// Set caret Position
tooltipEl.removeClass('above below');
tooltipEl.addClass(tooltip.yAlign);
// Set Text
tooltipEl.html(tooltip.text);
// Find Y Location on page
var top;
if (tooltip.yAlign == 'above') {
top = tooltip.y - tooltip.caretHeight - tooltip.caretPadding;
} else {
top = tooltip.y + tooltip.caretHeight + tooltip.caretPadding;
}
// Display, position, and set styles for font
tooltipEl.css({
opacity: 1,
left: tooltip.chart.canvas.offsetLeft + tooltip.x + 'px',
top: tooltip.chart.canvas.offsetTop + top + 'px',
fontFamily: tooltip.fontFamily,
fontSize: tooltip.fontSize,
fontStyle: tooltip.fontStyle,
});
}
Now you can change the css, add the class to the div and youre done. it will work :+1:
Closing since a number of good solutions have been given in this thread.
I'm having an issue getting custom tooltips to work (using angular-chart-js)
$scope.options = {
customTooltips: function(tooltip) {
var raw = [1,2,3,4,5];
var tooltipEl = $('#chartjs-tooltip');
if (!tooltip) {
tooltipEl.css({
opacity: 0
});
return;
}
tooltipEl.removeClass('above below');
tooltipEl.addClass(tooltip.yAlign);
var innerHtml = '';
for (var i = tooltip.labels.length - 1; i >= 0; i--) {
innerHtml += [
'<div class="chartjs-tooltip-section">',
' <span class="chartjs-tooltip-key" style="background-color:' + tooltip.legendColors[i].fill + '"></span>',
' <span class="chartjs-tooltip-value">' + tooltip.labels[i] + '</span>',
' <span class="chartjs-tooltip-value" style="background-color: cyan;"> (' + raw[i] + ')</span>',
'</div>'
].join('');
}
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,
});
}
};
The chart renders correctly, but when I mouseover it, I get Uncaught TypeError: Cannot read property 'length' of undefined, which is a reference to trying to iterate over tooltip.labels.length in my function (copied from the sample). Is there a reason tooltip.labels would be undefined in the function?
I believe the api has changed a bit. Log out the entire tooltip or the 'this' variable and you should see the properties available to you you in the function. On Fri, Feb 5, 2016 at 4:33 PM David Brownman [email protected] wrote:
I'm having an issue getting custom tooltips to work (using angular-chart-js https://jtblin.github.io/angular-chart.js/)
$scope.options = { customTooltips: function(tooltip) { var raw = [1,2,3,4,5]; var tooltipEl = $('#chartjs-tooltip'); if (!tooltip) { tooltipEl.css({ opacity: 0 }); return; } tooltipEl.removeClass('above below'); tooltipEl.addClass(tooltip.yAlign); var innerHtml = ''; for (var i = tooltip.labels.length - 1; i >= 0; i--) { innerHtml += [ '
', ' ', ' ' + tooltip.labels[i] + '', ' (' + raw[i] + ')', '' ].join(''); } 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,}); } };
The chart renders correctly, but when I mouseover it, I get Uncaught TypeError: Cannot read property 'length' of undefined, which is a reference to trying to iterate over tooltip.labels.length in my function (copied from the sample). Is there a reason tooltip.labels would be undefined in the function?
— Reply to this email directly or view it on GitHub https://github.com/nnnick/Chart.js/issues/928#issuecomment-180621149.
If the api has changed, shouldn't the documentation be updated to reflect it?
~DavidB [sent from mobile]
On Feb 5, 2016, 7:30 PM -0800, Tanner [email protected], wrote:
I believe the api has changed a bit. Log out the entire tooltip or the 'this' variable and you should see the properties available to you you in the function. On Fri, Feb 5, 2016 at 4:33 PM David [email protected] wrote:
I'm having an issue getting custom tooltips to work (using angular-chart-jshttps://jtblin.github.io/angular-chart.js/)
$scope.options = { customTooltips: function(tooltip) { var raw = [1,2,3,4,5]; var tooltipEl = $('#chartjs-tooltip'); if (!tooltip) { tooltipEl.css({ opacity: 0 }); return; } tooltipEl.removeClass('above below'); tooltipEl.addClass(tooltip.yAlign); var innerHtml = ''; for (var i = tooltip.labels.length - 1; i>= 0; i--) { innerHtml += [ '
', '', '' + tooltip.labels[i] + '', '(' + raw[i] + ')', '' ].join(''); } 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, }); } };
The chart renders correctly, but when I mouseover it, I get Uncaught TypeError: Cannot read property 'length' of undefined, which is a reference to trying to iterate over tooltip.labels.length in my function (copied from the sample). Is there a reason tooltip.labels would be undefined in the function?
— Reply to this email directly or view it on GitHub https://github.com/nnnick/Chart.js/issues/928#issuecomment-180621149.
— Reply to this email directly orview it on GitHub(https://github.com/nnnick/Chart.js/issues/928#issuecomment-180665406).