在柱形图Google Chart中显示数据值

时间:2014-01-08 10:32:48

标签: google-visualization

我使用google-visualization创建了一个图表但无法获取第二列的数据值

var data = google.visualization.arrayToDataTable([
      ['Year', 'Sales', 'Expenses'],
      ['2004', 1000, 400],
      ['2005', 1170, 460],
      ['2006', 660, 1120],
      ['2007', 1030, 540]
    ]);

    var view = new google.visualization.DataView(data);
    view.setColumns([0, 1,{
                            calc: "stringify",
                            sourceColumn: 1,
                            type: "string",
                            role: "annotation"
                        },
                        2]);

    var options = {
        title: 'Company Performance',
        hAxis: { title: 'Users', titleTextStyle: { color: 'red', fontSize: 16} },
        legend: { position: 'bottom', textStyle: { color: 'blue', fontSize: 16} }
    };
    var chart = new google.visualization.ColumnChart(document.getElementById('chart_div'));
    chart.draw(view, options);

数据值来自销售,但不是费用列

2 个答案:

答案 0 :(得分:2)

您需要在视图中添加另一个注释列:

var view = new google.visualization.DataView(data);
view.setColumns([0, 1, {
    calc: "stringify",
    sourceColumn: 1,
    type: "string",
    role: "annotation"
}, 2, {
    calc: "stringify",
    sourceColumn: 2,
    type: "string",
    role: "annotation"
}]);

答案 1 :(得分:0)

dataView.setColumns([{calc:function(data,row){return data.getFormattedValue(row,0);},type:' string'},1,2]);