简体   繁体   English

你如何在每个堆积柱形图(谷歌图表)上编码一个总计?

[英]How do you code a grand total on each stacked column chart (Google Charts)?

I have a Stacked Column chart to which I want to add a "Grand Total" label at the top as the sum of each year.我有一个堆积柱形图,我想在顶部添加一个“总计”label 作为每年的总和。 The temporary solution I found was to add it in as another { role: 'annotation' } and delete the background color, but it leaves a gap above the actual data.我找到的临时解决方案是将其添加为另一个 { role: 'annotation' } 并删除背景颜色,但它会在实际数据上方留下一个空白。

Is there to code for a function to get the sum of each stacked column to display?是否有代码为 function 获取要显示的每个堆叠列的总和?

Here is what the code looks like so far:到目前为止,代码如下所示:

function drawChart() {
            // Define the chart to be drawn.
  
            var data = google.visualization.arrayToDataTable([
               ['Year', 
                'Gangs', { role: 'annotation' },
                'No Gangs', { role: 'annotation' },
                'Total', { role: 'annotation' },
               ],
               ['2012',  110,110, 488,488, 590,590],
               ['2013',  110, 110,488,488,598,598],
             ['2014',  114,114, 522,522,590,590],
               ['2015',  85,85, 521,521,590,590],
               ['2016',  82,82, 590,590,590,590],
               ['2017',  79, 79,  548, 548,590,590], 
               ['2018',  49, 49,  432,432,590,590], 
               ['2018',  41, 41, 524, 524,590,590],
            ]);

          
            var options = {title: 'Violent Crimes by Gang Rates',
                           vAxis: {title: 'Violent Deaths'},
                           vAxis:{maxValue: 800},
                            seriesType: 'bars',
                          //series: {2: {type: 'line'}},
                           colors: ['#1586DB', '#E37D24', '#fff'],
                           legend: {position: 'bottom', maxLines: 3},
                           isStacked:true};  
   
   
   //test
   
  var totalSeries;

            // Instantiate and draw the chart.
            var chart = new google.visualization.ColumnChart(document.getElementById('container'));
            chart.draw(data, options);
         }
         google.charts.setOnLoadCallback(drawChart);

I don't know if this is quite what you're looking for (and it is a bit of a hack), but you can take advantage of calculated columns to automatically sum the elements in each row.我不知道这是否正是您要寻找的(这有点像 hack),但您可以利用计算列来自动对每行中的元素求和。 You can see the full working example in this JSFiddle .您可以在此 JSFiddle中查看完整的工作示例。

In the example, I've replaced your duplicate entries with calculated columns using view.setColumns to generate annotations (to avoid redundant data).在示例中,我使用view.setColumns将重复条目替换为计算列以生成注释(以避免冗余数据)。 I've used this same technique to generate a calculated column that provides a "grand total" annotation.我已经使用相同的技术来生成提供“总计”注释的计算列。 In order for this column to display correctly, I've added an empty series (ie, 0 ) at the end of each row that simply provides a "buffer" between the annotation for series 1 and the "grand total" annotation (if this isn't inserted, the "grand total" annotation tries to associate with column 2, which ruins the formatting).为了使该列正确显示,我在每一行的末尾添加了一个空系列(即0 ),它只是在系列 1 的注释和“总计”注释之间提供了一个“缓冲区”(如果这未插入,“总计”注释尝试与第 2 列关联,这会破坏格式)。 I've generated the grand total using a calculated column that sums values from the previous two.我使用计算列生成了总计,该计算列对前两个值求和。

With the "dummy" column on the end, your data now looks like this:使用末尾的“虚拟”列,您的数据现在如下所示:

var data = google.visualization.arrayToDataTable([
    ['Year',
     'Gangs',
     'No Gangs',
     '' // our "dummy" column
    ],
    ['2012', 110, 488, 0],
    ['2013', 110, 488, 0],
    ['2014', 114, 522, 0],
    ['2015', 85, 521, 0],
    ['2016', 82, 590, 0],
    ['2017', 79, 548, 0],
    ['2018', 49, 432, 0],
    ['2018', 41, 524, 0],
]);

To "hide" the "dummy" column as best I can, I've added the following to your options object:为了尽可能“隐藏”“虚拟”列,我在您的options object 中添加了以下内容:

series: {
    2: {
       enableInteractivity: false,
       visibleInLegend: false,
       annotations: {
           stem: {
               length: 0
           }
       }
    }
}

Also, you'll want to change the "dummy" column's color (since it's the color in which the annotation will display) to something other than white (I picked black in my example).此外,您需要将“虚拟”列的颜色(因为它是显示注释的颜色)更改为白色以外的颜色(我在示例中选择了黑色)。

Lastly, you'll need to use a DataView to take advantage of calculated columns.最后,您需要使用DataView来利用计算列。 The DataView then becomes the object passed to draw , as follows:然后DataView变成了 object 传递给draw ,如下所示:

var view = new google.visualization.DataView(data);
view.setColumns([
    0, 1, // Displays columns 0 and 1 normally
    { // "Calculates" an annotation for column 1 and displays immediately after that column
        calc: "stringify",
        sourceColumn: 1,
        type: "string",
        role: "annotation"
    },
    2, // Displays column 2 normally
    {
        calc: "stringify",
        sourceColumn: 2,
        type: "string",
        role: "annotation"
    }, // "Calculates" column 2 annotation
    3, // Displays our dummy column for the purposes of preventing the following annotation from associating with column 2
    { // Computes a "grand total" by summing the values from columns 1 and 2
        calc: (table, row) => (table.getValue(row, 1) + table.getValue(row, 2)).toString(),
        type: "string",
        role: "annotation"
    },
]);
      
// Instantiate and draw the chart.
var chart = new google.visualization.ColumnChart(document.getElementById('container'));
chart.draw(view, options);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM