使用谷歌可视化从饼图中添加和删除部分
使用饼图 (http://code.google.com/apis/ Visualization/documentation/gallery/piechart.html ),我是否可以在页面加载后添加/删除数据?
例如,在用户单击链接后,我希望能够删除饼图的一部分,而当他单击另一个链接时,我想添加一个新部分。
谢谢。
乔尔
Using the Pie Chart ( http://code.google.com/apis/visualization/documentation/gallery/piechart.html ), is there anyway I can add/remove a data after the page was loaded?
For example, after a user click on a link I want to be able to remove one of the parts of the pie and when he clicks another link I want to add a new part.
Thanks.
Joel
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是的,这是可能的。我整理了JsFiddle 上的示例。这可能与您在生产环境中的做法不完全一样,但它应该可以给您一个想法。
基本上我所做的是:
DataTable
和PieChart
从drawChart()
函数(或任何调用的绘制方法)外部removeRow()
方法DataTable
对象Yes, it is possible. I've thrown together an example on JsFiddle. It's probably not exactly how you would do in a production environment but it should give you an idea.
Basically what I do is:
DataTable
andPieChart
accessible from outside thedrawChart()
function (or whatever your draw method is called)removeRow()
method on theDataTable
object乔尔,我使用复选框而不是链接做了你要求的事情。当取消选中该复选框时,正如 jensgram 指出的那样,我调用了 data.removeRow(data.getNumberOfRows()-1) 。当它被检查时,我调用了 addRow(theDeletedRow)。
只需注意添加/删除的行应该是 RataTable 的最后一行(删除第一行不会使数组中的所有其他元素上升),以将删除的值保留在变量中,以便您可以再次添加它稍后,并尝试在选项中指定颜色,这样饼图的每个部分都不会改变颜色(它看起来像是一个完全不同的饼图,而不是只有一个部分的相同饼图)。
Joel, I did what you asked using a ckeckbox instead of links. When the checkbox has unchecked, I called data.removeRow(data.getNumberOfRows()-1) as jensgram points out. And when it was checked I called addRow(theDeletedRow).
Just take care that the added/removed row should be the last one of the RataTable (removing the first row doesn't make all other elements in the array go up), to keep the deleted value in a variable so you can add it again later, and try to specify colors in your options, so each part of the pie doesn't change of color (it gives the appearance that it is a completelly different pie, instead of the same one without just one part).