我正在尝试编写一个简单的应用程序来显示图表数据。我想在用户加载页面后立即显示一些数据,所以我在 gwt-visualization Getting StartedRunnable
中描述了数据和绘图表。
一切似乎都很好,只是图表往往会被多次加载。下面是我的onModuleLoad()
。
private final StatisticsServiceAsync statisticsService = GWT.create(StatisticsService.class);
GWTBeanFactory factory = GWT.create(GWTBeanFactory.class);
DataTable locationData;
AnnotatedTimeLine atl;
GeoMap usMap;
TextBox storeField;
Button log10Button;
DateRange durationChartRange;
String eusrJson = null;
Button b;
HTML last1000Html;
public void onModuleLoad() {
storeField = new TextBox();
storeField.setText("Enter a store");
storeField.addKeyDownHandler(new MyKeyHandler());
b = new Button("Get Stats!");
log10Button = new Button("Show Log10 Scale");
log10Button.addClickHandler(new Log10ClickHandler());
b.addClickHandler(new MyClickHandler());
last1000Html = new HTML();
getLast1000Avg();
Runnable onLoadCallback = new Runnable() {
public void run() {
storeDurationData = DataTable.create();
storeDurationDataLog10 = DataTable.create();
RootPanel.get("storeDurationDiv").add(storeField);
RootPanel.get("storeDurationDiv").add(b);
RootPanel.get("storeDurationDiv").add(log10Button);
RootPanel.get("storeDurationDiv").add(last1000Html);
log10Button.setVisible(false);
// Get initial Data
getAvgByRegion();
getLast1000Avg();
Scheduler.get().scheduleFixedDelay(new RepeatingCommand() {
@Override
public boolean execute() {
getLast1000Avg();
return true;
}
}, 5000);
}
};
// Load the visualization api, passing the onLoadCallback to be called
// when loading is done.
VisualizationUtils.loadVisualizationApi(onLoadCallback, AnnotatedTimeLine.PACKAGE);
VisualizationUtils.loadVisualizationApi(onLoadCallback, GeoMap.PACKAGE);
}
所有“简单”元素似乎都被正确填充,因为Button
,HTML
和TextBox
所有元素都被正确放置(以前在 内部run
,由于调试以前的错误,它们现在在它们所在的位置)。但是,GeoMap
被放置了两次,查看日志可以看出Runnable
'run
至少被执行了两次,这似乎是合理的,但我不知道如何防止它添加两次。
我可能搞砸了异步的东西,但我是新的和困惑的。下面是我的getAvgByRegion()
方法:
private void getAvgByRegion() {
statisticsService.getEusrForRegion(new AsyncCallback<String>() {
@Override
public void onFailure(Throwable caught) {
System.out.println(":(");
}
@Override
public void onSuccess(String result) {
createLocTable();
DataTable dt = parseEusrLocations(result);
usMap = new GeoMap(dt, createGeoMapOptions());
usMap.setSize("800px", "600px");
RootPanel.get("storeDurationDiv").add(usMap);
}
});
}
欢迎任何关于如何最好地使用 GWT 的建议。