Google Chart:标记堆积柱形图

ePa*_*oal 3 charts google-visualization

基于 googleAPI 文档:https : //developers.google.com/chart/interactive/docs/gallery/columnchart?hl= en

正如您在“标记列”部分中看到的,每列都标有一个静态值。我想知道是否可以用所有总和的特定值标记一列。

// Set chart options
     var options = {

         width: 400,
         height: 300,
         calc:????
     };
Run Code Online (Sandbox Code Playgroud)

我应该使用特定功能设置这个“calc”字段吗?

JSFIDDLE:总标签列

我不知道如何使用每个堆叠列的总和值自定义标签。

Ant*_*vić 5

您可以使用getNumberOfRows(),getNumberOfColumns()getValue()函数来计算总计并设置该值而不是 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);
    }
...
Run Code Online (Sandbox Code Playgroud)

您必须更改图表的大小或字体的大小才能在列上正确显示值。标签显示正确。


ePa*_*oal 5

这是我的答案..我希望它可以帮助有同样问题的人。

 function getValueAt(column, dataTable, row) {
     return dataTable.getFormattedValue(row, column);
 }

 function setLabelTotal(dataTable) {
     //dataTable must have role: annotation
     var SumOfRows = 0;   

     for (var row = 0; row < dataTable.getNumberOfRows(); row++) {
         SumOfRows = 0;
         for (var col = 0; col < dataTable.getNumberOfColumns(); col++) {

             if (dataTable.getColumnType(col) == 'number') {
                 SumOfRows += dataTable.getValue(row, col);
             }
             if(dataTable.getColumnRole(col) == 'annotation')
             {dataTable.setValue(row, col, SumOfRows.toString());}
         }
     }
 } 
Run Code Online (Sandbox Code Playgroud)

请注意,您必须在主函数(例如“drawChart”)上调用这些方法。