Sending data to a javascript routine via JSF Bean is not a good idea, but my solution works with java or JAX-RS web service. The Java web service contains 2 classes, a JaxRsActivator and a resource class. Here is the source code of the JaxRsActivator:
package service; import javax.ws.rs.ApplicationPath; import javax.ws.rs.core.Application; @ApplicationPath("/rest") public class JaxRsActivator extends Application { }
Here is the source code of the resource class.
package service; import static javax.ws.rs.core.MediaType.TEXT_PLAIN; import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.Produces; @Path("/resource") @Produces(TEXT_PLAIN) public class resource { @GET @Path("cities") public String dizi() { String s = "{\"Tokyo\",\"Jakarta\",\"New York\",\"Seoul\",\r\n" + "\"Manila\",\"Mumbai\",\"Sao Paulo\",\"Mexico City\",\r\n" + "\"Dehli\",\"Osaka\",\"Cairo\",\"Kolkata\",\r\n" + "\"Los Angeles\",\"Shanghai\",\"Moscow\",\"Beijing\",\r\n" + "\"Buenos Aires\",\"Guangzhou\",\"Shenzhen\",\"Istanbul\"};\r\n"; return s; }
}
Now a modification in our JavaScript. Make your anonymous function for creating a chart a named function, for example: generateChart (CityData) and change the line with the data: becomes data: CityData, your JavaScript starts with:
$(function () { var xhr = new XMLHttpRequest();
// End JavaScript
Also put this JavaScript at the end of your JSF page. After loading the page, start JavaScript with data loading, and after loading the data, start generating the chart.
Sucches.