chart.js tooltip error
by Tarek Said
HTML
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.5.0/Chart.min.js"></script>
<canvas id="canvas"></canvas>
JavaScript
byValue = true;
var ctx = $("#canvas");
var data = {
labels: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
datasets: [{
label: 'Percentage',
type: 'line',
data: [50, 90, 60, 20, 40, 70, 80, 10, 30, 50],
yAxisID: 'y2',
fill: false,
borderWidth: 3,
lineTension: 0,
backgroundColor: 'rgba(0,174,239,1)',
borderColor: 'rgba(0,174,239,1)',
pointBorderColor: 'rgba(0,174,239,0)',
pointHoverBorderColor: 'rgba(255,255,255,0)',
pointStyle: 'line'
}, {
label: 'Total',
type: 'bar',
data: [400, 1000, 500, 600, 800, 700, 200, 300, 500, 900],
yAxisID: 'y1',
backgroundColor: 'rgba(181,181,181,1)',
hoverBorderWidth: 1,
hoverBorderColor: 'rgba(255,255,255,0)',
pointStyle: 'circle'
}, {
label: 'Authorized',
type: 'bar',
data: [200, 900, 300, 120, 320, 490, 160, 30, 150, 450],
yAxisID: 'y1',
backgroundColor: 'rgba(104, 103, 105, 1)',
hoverBorderWidth: 1,
hoverBorderColor: 'rgba(255,255,255,0)',
pointStyle: 'circle'
}]
};
var options = {
title: {
display: true,
text: 'Authorized',
fontColor: '#000',
fontSize: 16,
fullWidth: true
},
legend: {
display: true,
position: 'bottom',
reverse: false,
labels: {
usePointStyle: true
}
},
tooltips: {
backgroundColor: 'rgba(43, 188, 242, 0.9)',
titleSpacing: 5,
bodySpacing: 5,
mode: 'index',
callbacks: {
label: function(tooltipItems, data) {
var desc = tooltipItems.yLabel;
if (tooltipItems.datasetIndex == 0) {
desc += '%';
} else if (byValue) {
desc = '$ ' + desc;
}
return data.datasets[tooltipItems.datasetIndex].label + ': ' + desc;
}
}
},
scales: {
xAxes: [{
barPercentage: 1,
categoryPercentage: 0.6,
gridLines: {
display: false
},
ticks: {
callback: function(value, index, values) {
return value + 'h';
}
}
...