Hi,
I would like to call Wicket repeatedly to obtain data from a service for a
JQplot chart. I need to do this as the data is obtained from a slow web
service. The user will then see a graph that will gradually build up after
each new set of data from the server is added to it.
I've had some success with this part. The user clicks a button to start
building the graph, however, I cannot work out how to call for more data
from javaScript to Wicket, then back again from Wicket to javaScript to add
the new data to the JQplot graph.
Code below (there will be a flag to signal to the javaScript that there's no
more graph data, but it's not there yet):
public class GraphPanel extends ParentPanel {
@SpringBean
private GraphDataService graphDataService;
private Integer dataId;
public GraphPanel ( String id, Integer dataId) {
super(id);
this.dataId = dataId;
buildPage();
}
public void buildPage() {
//Link used for button to start to obtain chart data
add( new AjaxLink( "getGraphData" ){
public void onClick( AjaxRequestTarget target ){
getChartData(target);
}
} );
//behaviour used to obtain url for call back from javaScript
final AbstractDefaultAjaxBehavior
jsCallbackBehaviourForMoreChartData =
new AbstractDefaultAjaxBehavior() {
@Override
protected void respond(AjaxRequestTarget target) {
getChartData(target);
}
};
getBasePage().add(jsCallbackBehaviourForMoreChartData);
final CharSequence url =
jsCallbackBehaviourForMoreChartData.getCallbackUrl();
//behaviour to provide javaScript function which I can call
from my javaScript
//function if it needs to get more data. This function is
not added to mark up...
add(new Behavior() {
private static final long serialVersionUID = 1L;
@Override
public void renderHead(Component component,
IHeaderResponse response) {
super.renderHead(component, response);
String js = "function callWicket() { var wcall
= wicketAjaxGet ('"
+ url + ", function() { },
function() { } ) }";
response.renderJavaScript(js, "myScript");
}
});
}
private void getChartData(AjaxRequestTarget target) {
GraphData graphData = null;
try {
graphData = graphDataService.getChartData(dataId);
} catch (GraphDataFailureException e) {
// TODO Need to report error to user somehow
}
//This method just builds a String in the format required
for JQplot
String jsVar = buildChartDataAsJavaScriptVariable(graphData);
String chartTitle = graphData.getTitle();
List<String> namesOfSeriesOnGraph =
graphData.getNamesOfGraphSeries();
String legendNames =
Arrays.toString(namesOfSeriesOnGraph.toArray(new
String[namesOfSeriesOnGraph.size()]));
String javaScriptFunctionCall=
"buildChart('chartDivContainer',[" + jsVar
+ "],'" + chartTitle + ", " + legendNames + ")";
target.appendJavaScript(javaScriptFunctionCall);
}
}
I want the abstract Behaviour added to the panel above to provide me with
some javaScript so that I can call back to Wicket. This javaScript is not
added to the mark up. When I paste the url from the getCallbackUrl() into
the browser but I get XML back:
<ajax-response><evaluate
encoding="wicket1">buildChart('chartDivContainer',[[[1351617360000,
22.700006].....all my graph data.
Can anyone point me in the right direction as to what I have to do here or
as to what I've missed? Are there any examples that demonstrate how this is
done? Also how can I handle exceptions thrown back from the service? That
is, present some feedback to the user. Hopefully that is all clear enough,
feel free to ask if you need more information.
Many thanks.
--
View this message in context:
http://apache-wicket.1842946.n4.nabble.com/Call-Wicket-repeatedly-from-JQplot-Jquery-based-graphing-framework-tp4653472.html
Sent from the Users forum mailing list archive at Nabble.com.
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]