Wednesday, December 4, 2013

Re: PieChart stays in detached DOM tree when removed from parent widget

package demo.client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.visualization.client.AbstractDataTable;
import com.google.gwt.visualization.client.DataTable;
import com.google.gwt.visualization.client.VisualizationUtils;
import com.google.gwt.visualization.client.AbstractDataTable.ColumnType;
import com.google.gwt.visualization.client.visualizations.corechart.Options;
import com.google.gwt.visualization.client.visualizations.corechart.PieChart;
//import com.smartgwt.client.widgets.calendar.Timeline;

/**
* This is a demo of how to properly handle dynamically created and destroyed
* widgets. Entry point classes define <code>onModuleLoad()</code>.
*/
public class WidgetAddDelete implements EntryPoint {

private final Button addChartButton = new Button("Add PieChart");
private final Button removeChartButton = new Button("Remove PieChart");
// private final Button addTimelineButton = new Button("Add Timeline");
// private final Button removeTimelineButton = new Button("Remove Timeline");

private final VerticalPanel mainPanel = new VerticalPanel();

private Widget contentWidget = null;
// private Widget timelineWidget = null;

/**
* This is the entry point method.
*/
public void onModuleLoad() {
mainPanel.add(addChartButton);
mainPanel.add(removeChartButton);
// mainPanel.add(addTimelineButton);
// mainPanel.add(removeTimelineButton);

RootPanel.get().add(mainPanel);

setUpButtons();
}

/**
* Add click handlers to the buttons.
*/
private void setUpButtons() {
addChartButton.addClickHandler(new ClickHandler() {
public void onClick(final ClickEvent event) {
if (contentWidget == null) {
Runnable onLoadCallback = new Runnable() {
public void run() {
PieChart content = new PieChart(createTable(), createOptions());
mainPanel.add(content);
contentWidget = content;
}
};
VisualizationUtils.loadVisualizationApi(onLoadCallback, PieChart.PACKAGE);
}
}
});
removeChartButton.addClickHandler(new ClickHandler() {
public void onClick(final ClickEvent event) {
if (contentWidget != null) {
mainPanel.remove(contentWidget);
contentWidget = null;
}
}
});
// addTimelineButton.addClickHandler(new ClickHandler() {
// public void onClick(final ClickEvent event) {
// if (timelineWidget == null) {
// Timeline timeline = new Timeline();
// mainPanel.add(timeline);
// timelineWidget = timeline;
// }
// }
// });
// removeTimelineButton.addClickHandler(new ClickHandler() {
// public void onClick(final ClickEvent event) {
// if (timelineWidget != null) {
// timelineWidget.removeFromParent();
// mainPanel.remove(timelineWidget);
// timelineWidget = null;
// }
// }
// });
}

private AbstractDataTable createTable() {
DataTable data = DataTable.create();
data.addColumn(ColumnType.STRING, "Task");
data.addColumn(ColumnType.NUMBER, "Hours per Day");
data.addRows(2);
data.setValue(0, 0, "Work");
data.setValue(0, 1, 14);
data.setValue(1, 0, "Sleep");
data.setValue(1, 1, 10);
return data;
}

private Options createOptions() {
Options options = Options.create();
options.setWidth(400);
options.setHeight(240);
options.setTitle("My Daily Activities");
return options;
}
}
Hi Colin,

Th
anks for replying so quickly.  I've tried compiling with -Dgwt.style=PRETTY and even DETAILED, but the short variable names remain for the gwt-viz PieChartwidget.  Using Chrome's dev tools, I can look inside these objects to verify that they are created by, or at least related to, gwt-viz.



I've trimmed down my code to a single java file with an onModuleLoad method (attached).  That should be easier to look at than trying to clone the entire project.  If you want to try compiling and running, the *.gwt.xml file needs to include the following:
<inherits name='com.google.gwt.visualization.Visualization'/>

--
You received this message because you are subscribed to the Google Groups "Google Web Toolkit" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-web-toolkit+unsubscribe@googlegroups.com.
To post to this group, send email to google-web-toolkit@googlegroups.com.
Visit this group at http://groups.google.com/group/google-web-toolkit.
For more options, visit https://groups.google.com/groups/opt_out.

No comments:

Post a Comment