开发者

Adding and removing parts from pie with google visualization

开发者 https://www.devze.com 2023-01-22 03:48 出处:网络
Using the Pie Chart ( ht开发者_如何学JAVAtp://code.google.com/apis/visualization/documentation/gallery/piechart.html ), is there anyway I can add/remove a data after the page was loaded?

Using the Pie Chart ( ht开发者_如何学JAVAtp://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


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:

  • Make the DataTable and PieChart accessible from outside the drawChart() function (or whatever your draw method is called)
  • Use the removeRow() method on the DataTable object
  • Redraw the chart.


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).

0

精彩评论

暂无评论...
验证码 换一张
取 消