I'm having problems using the best method of transferring data from a Java backing / managed bean to a jQuery / Javascript component like Highcharts, so my web application creates / displays data in real-time dynamic mode, I'm pretty solid on the Java side, but I have fairly limited knowledge of JavaScript / jQuery, which is obviously where I fall. As far as I read, the best way is to Ajaxify a hidden field in my web application and pass in an object or JSON string? into it, and then pass that value to my JS component.
Firstly, it seems a little time consuming since I would have an Ajax call to update the JSON data, and then a setInterval call to read the data back into the JS component? I was hoping I could pass the data directly to the JS component ...
In any case, someone can just strengthen my knowledge and tell me a good tried and tested method if it differs from the above ... Guides / Demos will also be weighted !!
I also use Primeface 2.2.1, if it affects the proposed methodology?
Greetings
Ally
UPDATE:
The code is below, but I just want to describe what I am trying to achieve quickly: effectively I am trying to implement a dynamic Highcharts chart using the simple count ++ function from my bean support. Obviously, down the line I will use the channel in real time to provide this data, but at the moment I'm just trying to get Highcharts to work based on changing the information from my JSF bean support.
Here is my simple function of counting and converting to JSON (I'm not sure if the JSON method is really necessary, since only 1 value (int) is passed, but I would like to keep this method, because I'm sure I will use other parts of the web more widely applications):
public class TestBean { private int output; public TestBean() { output = 1; } public int getOutput() { return output; } public void setOutput(int output) { this.output = output; } public void update() { setOutput(getOutput() + 1); } public void prepareChartDate() {
Highcharts External JS File, again itβs worth noting that I saved the series function at the bottom of the chart in order to build / fill the chart before I start adding the values ββobtained from JSF Beans:
Highcharts.setOptions({ global: { useUTC: false } }); var chart; $(document).ready(function() { chart = new Highcharts.Chart({ chart: { backgroundColor: "#F8F0DB", renderTo: 'containerHigh', defaultSeriesType: 'area', margin: 10, marginLeft:30, marginBottom:17, zoomType: 'y', events: { load: updateChartData } }, title: { text: 'Feed Flow Rate ' }, xAxis: { type: 'datetime', tickPixelInterval: 150 }, yAxis: { title: { text: '' }, plotLines: [{ value: 0, width: 1, color: '#808080' }] }, tooltip: { formatter: function() { return '<b>'+ this.series.name +'</b><br/>'+ Highcharts.dateFormat('%Y-%m-%d %H:%M:%S', this.x) +'<br/>'+ Highcharts.numberFormat(this.y, 2); } }, legend: { enabled: false }, exporting: { enabled: false }, plotOptions: { area: { fillColor: { linearGradient: [0, 0, 0, 100], stops: [ [0, Highcharts.getOptions().colors[0]], [1, 'rgba(2,0,0,0)'] ] }, lineWidth: 1, marker: { enabled: false, states: { hover: { enabled: true, radius: 5 } } }, shadow: false, states: { hover: { lineWidth: 1 } } } }, series: [{ name: 'Random data', data: (function() {
And the inline JS function stored on my XHTML page:
It works:
function updateChartData(xhr, status, args) { var series = this.series[0]; setInterval(function() { var x = (new Date()).getTime() y = 50 * Math.random(); series.addPoint([x, y], true, true); }, 1000)
but when I try to pass the bean value:
function updateChartData(xhr, status, args) { var jsonString = args.chartData var series = this.series[0]; setInterval(function() { var x = (new Date()).getTime() y = jsonString series.addPoint([x, y], true, true); }, 1000)
This does not work...
I also try both remoteCommand and polling to get the diagram to work, none of which I failed:
<h:form> <p:commandButton value="update" action="#{testBean.update}" update="beanvalue"/> <h:outputText value="#{testBean.output}" id="beanvalue"/> <br /> <h:outputText value="#{testBean.output}" id="chartValue"/> <br /> <p:commandButton value="Load" type="button" onclick="fetchChartData();"/> <p:remoteCommand name="fetchChartData" action="#{testBean.prepareChartDate()}" oncomplete="updateChartTest(xhr, status, args);"/> </h:form>
As I said before Bhesh, your help was widely appreciated, and it would be great!
Greetings
Ally