在Google可视化数据表中更新数据 [英] Updating Data in Google Visualization DataTable

查看:62
本文介绍了在Google可视化数据表中更新数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你如何更新谷歌可视化数据表中的数据?示例:

  var data = new google.visualization.DataTable(); 

data.addColumn('string','Name');
data.addColumn('string','Occupation');

data.addRow(['Bob','鞋穿着者']);
data.addRow(['Henry','Transformer']);
data.addRow(['Betty','Seltzer Connoisseur']);

//时光流逝,鲍勃改变工作:
data.addRow(['Bob','Beach Comber']);

当然,这增加了一个新行,现在我有两个Bob。 我如何更新Bob的职业? 位于索引为0的行和索引为1的列中:

  data.setValue(0,1,'Beach Comber'); 

如果您不知道要更新职业的人的行索引,我建议迭代或过滤来查找所有Bob行(或者在您的案例中 Bob行)。 (var y = 0,maxrows = data.getNumberOfRows())这是一种'蛮力'方式,并且像这样

  ; y< maxrows; y ++){
if(data.getValue(y,0)=='Bob'){
data.setValue(y,1,'Beach Comber');


过滤更优雅:

  var foundRows = data.getFilteredRows([{column:0,value:'Bob'}]); (var y = 0,maxrows = foundRows.length; y  data.setValue(foundRows [y],1,'Beach Comber')的
;
}

参考API-Doc可以在这里找到: https://developers.google.com/chart/interactive/docs/reference#DataTable 和持有一堆很好的例子。

How do you update data in a google visualization datatable? Example:

var data = new google.visualization.DataTable();

data.addColumn('string', 'Name');
data.addColumn('string', 'Occupation');

data.addRow(['Bob', 'Shoe Wearer']);
data.addRow(['Henry', 'Transformer']);
data.addRow(['Betty', 'Seltzer Connoisseur']);

// Time passes and Bob changes jobs:
data.addRow(['Bob', 'Beach Comber']);

Of course, that adds a new row and now I have two Bobs. How can I update Bob's occupation?

解决方案

As Bob is the first row you inserted, his occupation resides in row with index 0 and column with index 1:

data.setValue(0, 1, 'Beach Comber');

In case you don't know the row index of a person who's occupation is to be updated, I suggest iterating or filtering to find all Bob rows (or the one Bob row in your case). Iteration is the 'brute force' way and goes like this

for (var y = 0, maxrows = data.getNumberOfRows(); y < maxrows; y++) {
    if (data.getValue(y, 0) == 'Bob') {
        data.setValue(y, 1, 'Beach Comber');
    }
}

Filtering is more elegant:

var foundRows = data.getFilteredRows([{column: 0, value: 'Bob'}]);
for (var y = 0, maxrows = foundRows.length; y < maxrows; y++) {
     data.setValue(foundRows[y], 1, 'Beach Comber');
}

The reference API-Doc can be found here: https://developers.google.com/chart/interactive/docs/reference#DataTable and holds a bunch a good examples.

这篇关于在Google可视化数据表中更新数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆