2012-03-02 92 views
3

以下是Google Charts API的表格。我正在尝试对“Numbers”列进行降序排序。有人知道怎么做吗?对表格进行排序 - Google Chart Tools

<script type="text/javascript" src="http://www.google.com/jsapi"></script> 
    <script type="text/javascript"> 
     google.load('visualization', '1', {packages: ['table']}); 
    </script> 
    <script type="text/javascript"> 
    function drawVisualization() { 
     // Create and populate the data table. 
     var data = new google.visualization.DataTable(); 
     data.addColumn('string', 'Names'); 
     data.addColumn('number', 'Numbers'); 
     data.addRows(3); 
     data.setCell(0, 0, 'Name 1'); 
     data.setCell(1, 0, 'Name 2'); 
     data.setCell(2, 0, 'Name 3'); 
     data.setCell(0, 1, 1); 
     data.setCell(1, 1, 2); 
     data.setCell(2, 1, 3); 



     visualization = new google.visualization.Table(document.getElementById('table')); 
     visualization.draw(data, null); 
    } 


    google.setOnLoadCallback(drawVisualization); 
    </script> 

    <div id="table"></div> 
+1

https://developers.google.com/chart/interactive/docs/gallery/table搜索事件'sort'在这里寻找游乐场https://code.google.com/apis/ajax/playground/?type=visualization#sort_event – 2012-05-22 11:12:25

回答

5

是的。只需在数据def下面添加以下行,它会按数字降序排序,然后按名称上升。

data.sort([{column: 1, desc:true}, {column: 0}]); 

哦,你也可以使用这样的:

data.addRow(['Name 1',1]); 
data.addRow(['Name 2',2]); 
data.addRow(['Name 3',3]); 

问候,豪迈