score:1
Accepted answer
Customized Multiline Tooltip in ChartJS:
<!DOCTYPE html>
<!--
homt.html
-->
<html>
<head>
<title>Customised tooltip</title>
<!--ChartJs-->
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.4.0/Chart.min.js"></script>
<!--ChartJs-->
<style>
div {
height: 600px;
width: 700px;
}
</style>
</head>
<body>
<div id="myCan" >
<canvas id="bar"></canvas>
</div>
</body>
<script type="text/javascript">
var dataJason = {
"meth": [{
"tech": "CSS", "avg": 3, "Count": 80, "sum": 53
},
{
"tech": "CCS", "avg": 9, "Count": 70, "sum": 25
},
{
"tech": "CSC", "avg": 7, "Count": 50, "sum": 66
}]
};
var techDATA = [];
var countDATA = [];
var avgDATA = [];
var sumDATA = [];
function techData(){
var jdata = dataJason.meth;
var jl = jdata.length;
for(var i = 0; i < jl; i++){
techDATA.push(dataJason.meth[i].tech);
countDATA.push(dataJason.meth[i].Count);
avgDATA.push(dataJason.meth[i].avg);
sumDATA.push(dataJason.meth[i].sum);
}
}
var colorCode = ['#5DADE2','#F1C40F','#00FFFF','#A569BD','#F5B041','#566573'];
function init(){
techData();
var ctx1 = document.getElementById("bar").getContext('2d');
var myChart1 = new Chart(ctx1, {
type: 'bar',
options: {
legend: {
display: false
},
tooltips: {
callbacks: {
beforeLabel: function(tooltipItem, data){
var dataset = data.datasets[tooltipItem.datasetIndex];
return data.datasets[tooltipItem.datasetIndex].label+ ' : '+dataset.data[tooltipItem.index]+"%";
},
label: function(tooltipItem, data) {
var avg = "Avg: "+avgDATA[tooltipItem.index];
return avg;
},
afterLabel: function(tooltipItem, data){
var sum = "Sum: "+sumDATA[tooltipItem.index];
return sum;
}
}
},
scales: {
yAxes: [{
ticks: {
beginAtZero: true, steps: 10, stepValue: 5, max: 100
}
}]
}
},
data: {
labels: techDATA, datasets: [{
backgroundColor: colorCode, label: 'Method Covered', data: countDATA
}]
}
}
);
}
window.onload = function(){
init();
};
</script>
</html>
score:2
The graph with custom tool tip using chartjs.
JavaScript code.
//
// appcharts.js
//alert("ToolTips");
//var countDATAt = [80, 70, 50];
//var techDATAt = ["CSS", "CCS", "CSC"];
//var avgDATAt = [3, 9, 7];
//var sumDATAt = [53, 25, 66];
var dataJason = {
"meth": [{
"tech": "CSS", "avg": 3, "Count": 80, "sum": 53
},
{
"tech": "CCS", "avg": 9, "Count": 70, "sum": 25
},
{
"tech": "CSC", "avg": 7, "Count": 50, "sum": 66
}]
};
var techDATA = [];
var countDATA = [];
var avgDATA = [];
var sumDATA = [];
function techData(){
var jdata = dataJason.meth;
var jl = jdata.length;
for(var i = 0; i < jl; i++){
techDATA.push(dataJason.meth[i].tech);
countDATA.push(dataJason.meth[i].Count);
avgDATA.push(dataJason.meth[i].avg);
sumDATA.push(dataJason.meth[i].sum);
}
}
var colorCode = [
'rgba(255, 99, 132, 0.2)',
'rgba(54, 162, 235, 0.2)',
'rgba(255, 206, 86, 0.2)',
'rgba(75, 192, 192, 0.2)',
'rgba(153, 102, 255, 0.2)',
'rgba(255, 159, 64, 0.2)'
];
function init(){
techData();
var ctx1 = document.getElementById("bar").getContext('2d');
var myChart1 = new Chart(ctx1, {
type: 'bar', legend: {
display: true
},
options: {
tooltips: {
callbacks: {
label: function(tooltipItem, data) {
var avg = ", avg: "+avgDATA[tooltipItem.index];
var sum = ", sum: "+sumDATA[tooltipItem.index];
var dataset = data.datasets[tooltipItem.datasetIndex];
return data.datasets[tooltipItem.datasetIndex].label+ ' : '+dataset.data[tooltipItem.index]+"%"+avg+sum;
}
}
},
scales: {
yAxes: [{
ticks: {
beginAtZero: true, steps: 10, stepValue: 5, max: 100
}
}]
}
},
data: {
labels: techDATA, datasets: [{
backgroundColor: colorCode, label: 'Method Covered', data: countDATA
}]
}
}
);
}
window.onload = function(){
init();
};
//alert("Done");
//
HTML code.
<!DOCTYPE html>
<!--
index.html
-->
<html>
<head>
<title>Custom tooltip</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script src="lib/charts/Chart.bundle.js"></script>
<script src="js/appcharts.js"></script>
</head>
<body>
<br><hr><br>
<div id="myCan">
<canvas id="bar" width="100" height="100"></canvas>
</div>
<br><hr><br>
</body>
</html>
Enjoy.
Source: stackoverflow.com
Related Query
- How to create a custom tooltip for chartJS graph with data in JSON array in JavaScript?
- ChartJS (React) Line Chart - How to show single tooltip with data and labels from 3 (multiple) dataset?
- How can I create custom tooltips for each data point in a graph?
- Create an interactive custom tooltip for chartjs
- How to create chartjs chart with data from database C#
- how to set color for each item in tooltip with ChartJS
- How to display chart using Chartjs with JSON data in Laravel
- How to write better code in es6 for formatting an object with array values
- How to set min max for x axis depend on data with Chartjs and Spring Boot?
- How to set Custom tooltip event with Chartjs version 2.X
- How to create rounded bar graph with Angular 7 and Chartjs (v2)
- How to use computed property with data from JSON in data object for time axis in Vue using Chart.js
- chartjs - json data for scatter graph, issue with date
- How to bind json array data to chart.js with same canvas id?
- Chartjs random colors for each part of pie chart with data dynamically from database
- How to disable a tooltip for a specific dataset in ChartJS
- Chartjs v2 - format tooltip for multiple data sets (bar and line)
- Chartjs - data format for bar chart with multi-level x-axes
- How to use 'time' (data from database, data type: timestamp ) for plotting graph in Chart JS
- Create a rounded bar graph with Angular and chartJS
- How to create custom legend in ChartJS
- How to append more data in tooltip of graph in chart.js
- How to remove bars for those bars with zero value in Chartjs bar chart?
- How to create a simple Gauge with ChartJS v3?
- ChartJS v2 custom tooltip for rLabel
- ChartJS - Custom tooltip with icon
- How to create a stacked graph using ChartJS
- How to Create Chart.JS Bar Graph with Min, Max & Average
- How to map json array to two different array for chart
- Graph streaming real-time data with react and chartjs
More Query from same tag
- Datas put one on each other on chart.js
- Is there a way to change color of a chart's grid in y-axis - ng2-charts
- Dynamically Setting Height of Chart
- Formatting Data With Charts.JS
- Google Charts, HighCharts or ChartJS Dual Axis Gantt Chart and Line Chart Visualization
- chart is not getting updated from the values it received from Jquery
- Map event position to y axis value in chartjs line chart
- Adjust appearance of tooltip in lien chart rendered by Chart.js
- is there a way to do automatic scrolling?
- Highlight external element on ChartJS hover
- Pause chart.js horizontal scroll
- Need to put border on variablepie highchart?
- Chart.js get array of currently visible points on graph
- Charts.js line chart: Show the most recent date on x-axis when setting ticks.maxTicksLimit
- Chart.js animation triggered only on hover?
- Horizontal stacked angular bar charts
- drawing bar chart with Chart.js
- ChartJS How to set color to just one bar
- Feeding PHP MySQL Call into Charts.js
- Chart.js error: type lacks a call signature '((...items: number[]) => number) | ((...items: ChartPoint[]) => number)' in Angular
- show more dataset than labels in chart.js
- Output (column bars) from Chart.js blurry in Opera browser?
- Remove background on Chartjs v2 fixed tooltips
- Change font family of labels in piechart and linechart in chartjs
- ChartJS - Graphic with multiple values on the same date
- ChartJS works with Angular 5 while developing, but ng build fails
- Chart.js multiTooltip Labels in Pie
- how to increase space between legend and chart in chartjs (ng2charts ) using angular
- Calculate the average distance between two data sets of X and Y (in JS)
- Chart js doughnut box shadow