Adding and removing parts from pie with google visualization
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
andPieChart
accessible from outside thedrawChart()
function (or whatever your draw method is called) - Use the
removeRow()
method on theDataTable
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).
精彩评论