2013-06-11 40 views
-1

是否可以通過單擊相應按鈕在圖表類型(餅圖和列)之間切換?實時切換圖表類型

例如,我想要有兩個按鈕,一旦我點擊第一個按鈕,它將顯示餅圖。

+0

這看起來非常簡單。你試過了嗎? – smk

+0

是的,我試過了:用條形圖替換餅圖,但我必須重新加載瀏覽器才能看到正確的結果= new google.visualization.PieChart(document.getElementById('chart')); – Sergey

回答

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單擊處理程序只需更換

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