Google Chart getSelection没有列属性

时间:2013-11-23 17:07:50

标签: properties google-visualization getselection

当我使用时:

chart.getChart().getSelection()[0]

在图表上(从图表包装器,因此首先是getChart()),getSelection()函数返回 只有行属性但没有列属性,即使我的“图表”是一个表,单击其中的任何位置都应返回行和列属性。

这是一个已知的谷歌图表错误?有没有人知道一个解决方法?

此外,我在google群组中找到了这个主题: https://groups.google.com/forum/#!topic/google-visualization-api/O_t7-s96A9w

他们在这里说: 目前,Table对象仅支持行选择,因此列属性始终未定义。如果这对您很重要,您可以通过在每个单元格的格式化值中添加一些特殊的html代码来自行捕获这些事件。

任何人都知道如何做到这一点?

2 个答案:

答案 0 :(得分:1)

Even though google.visualization.table supports row selection only (it explains why column property returns null), you could consider the following approach to access column property:

google.load('visualization', '1', {
    packages: ['table']
});
google.setOnLoadCallback(drawTable);

function drawTable() {
    var data = new google.visualization.DataTable();
    data.addColumn('string', 'Name');
    data.addColumn('number', 'Salary');
    data.addColumn('boolean', 'Full Time Employee');
    data.addRows([
      ['Mike', {
          v: 10000,
          f: '$10,000'
      },
        true
      ],
      ['Jim', {
          v: 8000,
          f: '$8,000'
      },
        false
      ],
      ['Alice', {
          v: 12500,
          f: '$12,500'
      },
        true
      ],
      ['Bob', {
          v: 7000,
          f: '$7,000'
      },
        true
      ]
    ]);

    var table = new google.visualization.Table(document.getElementById('table_div'));
    google.visualization.events.addListener(table, 'select', function(){
        selectHandler(table);
    });
    table.draw(data, {
        showRowNumber: false
    });
}



function selectHandler(table) {
  var selection = table.getSelection();
  if(selection.length === 0)
      return;

  var e = event || window.event;
  var cell = e.target; //get selected cell
 
  document.getElementById('output').innerHTML = "Row: " +  selection[0].row + " Column: " +  cell.cellIndex;

}
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<div id="table_div"></div>
<div id="output"></div>

答案 1 :(得分:0)

您可以使用HTML格式化您的值,并在绘制选项中传递'allowHtml:true'。然后,当用户点击单元格值时,您的HTML可以引发事件/执行您想要的js。

例如,以下项目会在点击时发出警告:

function drawTable() {
    var data = new google.visualization.DataTable();
    data.addColumn('string', 'Name');
    data.addColumn('number', 'Salary');
    data.addColumn('boolean', 'Full Time Employee');
    data.addRows([
        ['Ted',  {v: 10000, f: '<span onclick="alert(0)">$10,000</span>'}, true],
        ['Jim',   {v:8000,   f: '<span onclick="alert(1)">$8,000</span>'},  false],
        ['Alice', {v: 12500, f: '<span onclick="alert(2)">$12,500</span>'}, true],
        ['Bob',   {v: 7000,  f: '<span onclick="alert(3)">$7,000</span>'},  true]
    ]);

    var table = new google.visualization.Table(document.getElementById('table_div'));
    google.visualization.events.addListener(table, 'select', function () {
        var s = table.getSelection();
        document.getElementById('row').innerHTML = s[0].row;
        document.getElementById('col').innerHTML = s[0].column;
        console.log(s);
    });
    table.draw(data, {showRowNumber: true, allowHtml: true});
}

google.load('visualization', '1', {packages:['table'], callback: drawTable});

请参阅:

http://jsfiddle.net/fZzch/2/

更简洁的方法是使用自定义格式化程序:

How to write a custom formatter for Google DataTables (for use on visualisation api)