谷歌图表错误尝试转换为百分比

时间:2014-07-15 18:57:35

标签: javascript charts google-visualization

我试图在我的图表中添加百分比并集中标签,但我遇到了问题。我在所有行中得到0%,左侧标签没有全彩色。我该如何解决?

我的图表图片:http://i.imgur.com/a9XMlzO.png

我的剧本:

<script type="text/javascript">
    google.load("visualization", "1", {
        packages: ["corechart"]
    });
    var dataSet = [
        ['year', 'Muito acima', 'Acima', 'Aprox. Igual', 'Abaixo', 'Muito abaixo', ],
        ['N', 0, 13, 67, 10, 10],
        ['NE', 0, 7, 40, 20, 33],
        ['S', 60, 33, 0, 7, 0],
        ['SE', 0, 13, 53, 33, 0]
    ];
    var dataSet2 = dataSet;
    google.setOnLoadCallback(drawChart2);

    function drawChart2() {
        var sum = new Array();
        for (row = 1; row < dataSet.length; row++) {
            sum[row] = 0;
            for (col = 1; col < dataSet[row].length; col++) {
                sum[row] += dataSet[row][col];
            }
            for (col = 1; col < dataSet[row].length; col++) {
                dataSet2[row][col] = 100 * dataSet[row][col] / sum[row];
            }
        }
        var data = google.visualization.arrayToDataTable(dataSet2);
        var view = new google.visualization.DataView(data);
        var formatter = new google.visualization.NumberFormat({
            pattern: '##,##%'
        });
        formatter.format(data, 1);
        formatter.format(data, 2);
        formatter.format(data, 3);
        formatter.format(data, 4);
        formatter.format(data, 5);
        view.setColumns([0, 1, {
                calc: "stringify",
                sourceColumn: 1,
                type: "string",
                role: "annotation"
            },
            2, {
                calc: "stringify",
                sourceColumn: 2,
                type: "string",
                role: "annotation"
            },
            3, {
                calc: "stringify",
                sourceColumn: 3,
                type: "string",
                role: "annotation"
            },

            4, {
                calc: "stringify",
                sourceColumn: 4,
                type: "string",
                role: "annotation"
            },
            5, {
                calc: "stringify",
                sourceColumn: 5,
                type: "string",
                role: "annotation"
            }
        ]);
        var options = {
            title: '',
            format: '#,#%',
            fontSize: 12,
            ticks: [{
                v: 13,
                f: '13%'
            }],
            hAxis: {
                suffix: '%',
                ticks: [{
                    v: 0,
                    f: '0%'
                }, {
                    v: 25,
                    f: '25%'
                }, {
                    v: 50,
                    f: '50%'
                }, {
                    v: 75,
                    f: '75%'
                }, {
                    v: 100,
                    f: '100%'
                }]
            }, //Baixo
            vAxis: {
                title: '',
                fontSize: 16,
                format: '#%',
                maxValue: 100
            },
            isStacked: 'true',
            color: '#000',
            colors: ['#4747D1', '#C2C2F0', '#B9CAFF', '#FF9999', '#A32900'],
            legend: {
                position: 'top'
            }
        };
        var chart = new google.visualization.BarChart(document.getElementById('chart_div2'));
        chart.draw(view, options);
    }
</script>

0 个答案:

没有答案