django - How get sum of total values in stackedBar ChartJs -


i'm trying sum of values of stackedbar , include total in tooltip.

note: datasets aren't static, example

var barchartdata = {         labels: ["january", "february", "march", "april", "may", "june", "july"],         datasets: [{             label: 'corporation 1',             backgroundcolor: "rgba(220,220,220,0.5)",             data: [50, 40, 23, 45, 67, 78, 23]         }, {             label: 'corporation 2',             backgroundcolor: "rgba(151,187,205,0.5)",             data: [50, 40, 78, 23, 23, 45, 67]         }, {             label: 'corporation 3',             backgroundcolor: "rgba(151,187,205,0.5)",             data: [50, 67, 78, 23, 40, 23, 55]         }]      };      window.onload = function() {         var ctx = document.getelementbyid("canvas").getcontext("2d");         window.mybar = new chart(ctx, {             type: 'bar',             data: barchartdata,             options: {                 title:{                     display:true,                     text:"chart.js bar chart - stacked"                 },                 tooltips: {                     mode: 'label',                     callbacks: {                        label: function(tooltipitem, data) {                        var corporation = data.datasets[tooltipitem.datasetindex].label;                        var valor = data.datasets[tooltipitem.datasetindex].data[tooltipitem.index];                        var total = eval(data.datasets[tooltipitem.datasetindex].data.join("+"));                        return total+"--"+ corporation +": $" + valor.tofixed(2).replace(/(\d)(?=(\d{3})+\.)/g, '$1,');                   }                     }                 },                 responsive: true,                 scales: {                     xaxes: [{                         stacked: true,                     }],                     yaxes: [{                         stacked: true                     }]                 }             }         });     }; 

now total sum per dataset , need sum per stackedbar.

example

label a: value a

label b: value b

label c: value c

total: value + value b + value c

it possible total value?

thanks, idalia.

first should know if return array instead of single string in callback of tooltip, display strings in array if different datasets (see this answer more details).

so edited little bit callback following:

callbacks: {     label: function(tooltipitem, data) {         var corporation = data.datasets[tooltipitem.datasetindex].label;         var valor = data.datasets[tooltipitem.datasetindex].data[tooltipitem.index];          // loop through datasets actual total of index         var total = 0;         (var = 0; < data.datasets.length; i++)             total += data.datasets[i].data[tooltipitem.index];          // if not last dataset, display         if (tooltipitem.datasetindex != data.datasets.length - 1) {             return corporation + " : $" + valor.tofixed(2).replace(/(\d)(?=(\d{3})+\.)/g, '$1,');         } else { // .. else, display dataset , total, using array             return [corporation + " : $" + valor.tofixed(2).replace(/(\d)(?=(\d{3})+\.)/g, '$1,'), "total : $" + total];         }     } } 

you can see full code in jsfiddle, , here result :

enter image description here


Comments

Popular posts from this blog

serialization - Convert Any type in scala to Array[Byte] and back -

Slow performance first queries on SQL Azure -

Java Entity Manager - JSON reader was expecting a value but found 'db' -