Google图表:标记堆叠的列图表 [英] Google Chart: Labelling Stacked Column Charts

查看:149
本文介绍了Google图表:标记堆叠的列图表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据googleAPI文档:
https:/ /developers.google.com/chart/interactive/docs/gallery/columnchart?hl=zh-CN

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:????
     };

我应该用特定的函数设置这个calc

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

JSFIDDLE:总标签列

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

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

推荐答案

可以使用 getNumberOfRows() getNumberOfColumns() getValue()函数计算总数并设置该值,而不是字符串 total

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.

这篇关于Google图表:标记堆叠的列图表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆