score:25
First you should know that if you return an array instead of a single string in the callback
of the tooltip, it will display all the strings in your array as if it were different datasets (see this answer for more details).
So I edited a little bit your callback to the following:
callbacks: {
label: function(tooltipItem, data) {
var corporation = data.datasets[tooltipItem.datasetIndex].label;
var valor = data.datasets[tooltipItem.datasetIndex].data[tooltipItem.index];
// Loop through all datasets to get the actual total of the index
var total = 0;
for (var i = 0; i < data.datasets.length; i++)
total += data.datasets[i].data[tooltipItem.index];
// If it is not the last dataset, you display it as you usually do
if (tooltipItem.datasetIndex != data.datasets.length - 1) {
return corporation + " : $" + valor.toFixed(2).replace(/(\d)(?=(\d{3})+\.)/g, '$1,');
} else { // .. else, you display the dataset and the total, using an array
return [corporation + " : $" + valor.toFixed(2).replace(/(\d)(?=(\d{3})+\.)/g, '$1,'), "Total : $" + total];
}
}
}
You can see the full code in this jsFiddle, and here is its result :
score:0
If you are on older ChartJs version you have to handle this in a different way. I'm using Chart.js Version 2.7.2 and this is how I handled it:
tooltips: {
mode: 'label',
callbacks: {
footer: function (data) {
var total = 0;
for (var i = 0; i < data.length; i++) {
total += data[i].yLabel;
}
return 'Total: ' + total
}
}
}
score:2
Using Chart.js 2.5.0
var valor = data.datasets[tooltipItem.datasetIndex].data[tooltipItem.index];
returns a string value. To calculate the correct sum value you have to add a parseFloat statement:
tooltips: {
mode: 'label',
callbacks: {
afterTitle: function() {
window.total = 0;
},
label: function(tooltipItem, data) {
var corporation = data.datasets[tooltipItem.datasetIndex].label;
var valor = data.datasets[tooltipItem.datasetIndex].data[tooltipItem.index];
//THIS ONE:
valor=parseFloat(valor);
window.total += valor;
return corporation + ": " + valor.toString().replace(/\B(?=(\d{3})+(?!\d))/g, " ");
},
footer: function() {
return "TOTAL: " + window.total.toString().replace(/\B(?=(\d{3})+(?!\d))/g, " ");
}
}
}
score:3
@Haider this is what you were looking for, I had the same problem. I have reused your code and built upon it @tektiv
I have made one small change where instead of building into the label I have made use of the afterbody. This removes the key color
afterBody code:
afterBody: function (tooltipItem, data) {
var corporation = data.datasets[tooltipItem[0].datasetIndex].label;
var valor = data.datasets[tooltipItem[0].datasetIndex].data[tooltipItem[0].index];
var total = 0;
for (var i = 0; i < data.datasets.length; i++)
total += data.datasets[i].data[tooltipItem[0].index];
return "Total : $" + total;
}
Full code here at JSFiddle
score:11
In the other answers you replace the last dataset, with this you don't need to
tooltips: {
callbacks: {
title: function(tooltipItems, data) {
return _this.chart.data.labels[tooltipItems[0].index];
},
footer: function(tooltipItems, data) {
let total = 0;
for (let i = 0; i < tooltipItems.length; i++) {
total += parseInt(tooltipItems[i].yLabel, 10);
}
return 'Total: ' + total;
}
}
}
Ps: It's typescript lang.
score:11
Shorter version of Gaspar's answer:
tooltips: {
callbacks: {
footer: (tooltipItems, data) => {
let total = tooltipItems.reduce((a, e) => a + parseInt(e.yLabel), 0);
return 'Total: ' + total;
}
}
}
Example: https://jsfiddle.net/g3ba60zc/2/
score:25
i modified tektiv answer to show Total only for active sets and move it to tooltips footer.
tooltips: {
mode: 'label',
callbacks: {
afterTitle: function() {
window.total = 0;
},
label: function(tooltipItem, data) {
var corporation = data.datasets[tooltipItem.datasetIndex].label;
var valor = data.datasets[tooltipItem.datasetIndex].data[tooltipItem.index];
window.total += valor;
return corporation + ": " + valor.toString().replace(/\B(?=(\d{3})+(?!\d))/g, " ");
},
footer: function() {
return "TOTAL: " + window.total.toString().replace(/\B(?=(\d{3})+(?!\d))/g, " ");
}
}
}
Source: stackoverflow.com
Related Query
- How get sum of total values in stackedBar ChartJs
- ChartJs how to get from mulitiple dateset which dataset bar is clicked
- How to show data values or index labels in ChartJs (Latest Version)
- Chart.js (Stacked Bar chart) How to calculate total sum of labels in tooltips?
- how to get React chartjs to resize back down with window
- How to get onClick Event for a Label in ChartJS and React?
- How to add ChartJS code in Html2Pdf to view image
- Chartjs - How to get last 7 days on x-axis labels?
- How do I add time sourced from an external source as an X axis to a ChartJS graph?
- How to get the database data into ChartJS using codeigniter
- How to get labels on ChartJS Pie chart segments
- How to reuse a Chartjs Chart component in with different Data and get past the **Canvas is already in use** error?
- Chart.js : How to get a line created by sum of others line?
- How to get Data from API to display chart using chartjs in Vuejs
- How to write better code in es6 for formatting an object with array values
- How to get the data attribute of the canvas chart created using chartjs
- How to show the chartjs bar chart data values labels as text?
- Chart.js How to get min-max values of X axis?
- Chart.js How to sum the values in a pie chart and display them in the header
- chartjs how to update dynamically data from database(Chartjs cant get the data)
- how to manipulate with the y-axis values in chartjs
- How to sum the array value in javascript like chartjs data array and input value sum
- How can I get my Chart.JS bar chart to stack two data values together on each bar, and print a calculated value on each bar?
- How do I hide values past the x-axis in chartjs 2.0?
- How to Sum values from different objects in Django?
- Many Flots using ChartJS and Ajax get data values
- How do I get the current step size of a chartjs chart whose stepSize I have not defined?
- How do you get the width of a datalabel from the Chartjs plugin after the chart animates?
- How get values on legend label withn Chart js on Angular
- How to get ChartJs object from dynamically created chart
More Query from same tag
- ChartJS 2.9.4 can't overlay line data on Horizontal Bar chart
- Error when implementing charts.js in angular
- Chart.js 2.0 - vertical lines
- How to get rid of axis lines in ChartJS?
- Have the current zoom information in chart.js with zoom plugin
- How to trigger tooltip on legend hover?
- How to load Json data onto a Charts.js chart
- How do I implement Laravel Analytics with Chart.js
- Is it possible to show each legends aligned with each bar in chart.js
- Chart js doughnut box shadow
- How to set minimal value for bar chart in Chart.js? (ver. 2.0.2)
- Second dataset with single point makes y axis too big on Chart.js
- Bar chart from Chart.js not showing the second dataset
- JS import updated JSON file for use with ChartJS
- Dropdown component not having effect in Chromium browser
- Chart.js 2 - label overlapping
- Hide all labels and tooltips in Chart.js and make it very small size
- Issue related to the usage of the library Chart.js
- How to change ChartJS font size with javascript?
- Separating list of data in Ajax
- Hiding Chart.js line, but showing it's data in the tooltip
- Chart.JS Error: this.scale is undefined
- chart js - Apply different color for each x-axes label
- Complex Javascript variables containing the values of other assigned variables?
- Chart.js 3.7 version. How to modify legend label?
- How to add border in chartjs?
- How to noshown the scale while all datasets is hidden (chartjs)?
- how to group the chart
- ChartJS remove the legends at the top of the chart
- How to set global/specific properies when updating line-chart using Chart.js?