Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google Chart: Labelling Stacked Column Charts

Based on googleAPI documentation: https://developers.google.com/chart/interactive/docs/gallery/columnchart?hl=en

As you can see in "labeling columns" section each column is labeled with a static value. I want to know if it is possible to label a column with a specific value resulting of the sum of all.

// Set chart options
     var options = {

         width: 400,
         height: 300,
         calc:????
     };

Should i set this "calc" field with a specific function?

JSFIDDLE: Total Labeling Column

I can't figure out how can i customize a label with the sum values of each stacked column.

like image 695
ePascoal Avatar asked Jan 17 '26 23:01

ePascoal


1 Answers

You can use getNumberOfRows(), getNumberOfColumns() and getValue() functions to calculate total and set that value instead of string total:

function drawChart() {
    // Create the data table.
    var data = new google.visualization.arrayToDataTable(array);

    for (var i = 0; i < data.getNumberOfRows(); i++) {
        var total = 0;

        for (var j = 1; j < data.getNumberOfColumns() - 2; j++) {
            // console.log(data.getValue(i, j));
            total += data.getValue(i, j);
        }
        data.setValue(i, data.getNumberOfColumns() - 1, '' + total);
    }
...

You will have to change or size of chart or size of fonts to get values properly displayed on columns. Labels are displayed correctly.

like image 174
Anto Jurković Avatar answered Jan 21 '26 04:01

Anto Jurković



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!