在飞行中切换图表类型

时间:2013-06-11 18:54:02

标签: google-visualization

是否可以通过单击相应按钮在图表类型(饼图和列)之间切换?

例如,我想要两个按钮,一旦我点击第一个按钮,它将显示饼图。

1 个答案:

答案 0 :(得分:2)

这很直接

function drawChart() {

    // Create the data table.
    var data = new google.visualization.DataTable();
    data.addColumn('string', 'Topping');
    data.addColumn('number', 'Slices');
    data.addRows([
      ['Mushrooms', 3],
      ['Onions', 1],
      ['Olives', 1],
      ['Zucchini', 1],
      ['Pepperoni', 2]
    ]);

    // Set chart options
    var options = {'title':'How Much Pizza I Ate Last Night',
                   'width':400,
                   'height':300};

    // Instantiate and draw our chart, passing in some options.
    var piechart = new google.visualization.PieChart(document.getElementById('chart_div'));
    chart.draw(data, options);

onyour click handler只需替换

var barchart = new google.visualization.BarChart(document.getElementById('chart_div'));
        chart.draw(data, options);