Live Updates in PrimeFaces Line Chart

Posted by Geertjan on Oracle Blogs See other posts from Oracle Blogs or by Geertjan
Published on Sat, 10 Nov 2012 16:49:25 +0000 Indexed on 2012/11/11 17:09 UTC
Read the original article Hit count: 250

Filed under:

In the Facelets file:

<p:layoutUnit position="center">
    <h:form>
        <p:poll 
            interval="3" 
            update=":chartPanel" 
            autoStart="true" /> 
    </h:form>
    <p:panelGrid 
        columns="1" 
        id="chartPanel">
        <p:lineChart 
            xaxisLabel="Time"
            yaxisLabel="Position"
            value="#{chartController.linearModel}"
            legendPosition="nw" 
            animate="true"
            style="height:400px;width: 1000px;"/>
    </p:panelGrid>
</p:layoutUnit>

The controler:

import java.io.Serializable;
import javax.inject.Named;
import org.primefaces.model.chart.CartesianChartModel;
import org.primefaces.model.chart.ChartSeries;

@Named
public class ChartController implements Serializable {

    private CartesianChartModel model;
    private ChartSeries data;

    public ChartController() {
        createLinearModel();
    }

    private void createLinearModel() {
        model = new CartesianChartModel();
        model.addSeries(getStockChartData("Stock Chart"));
    }

    private ChartSeries getStockChartData(String label) {
        data = new ChartSeries();
        data.setLabel(label);
        for (int i = 1; i <= 20; i++) {
            data.getData().put(i, (int) (Math.random() * 1000));
        }
        return data;
    }

    public CartesianChartModel getLinearModel() {
        return model;
    }

}

Based on this sample.

© Oracle Blogs or respective owner

Related posts about /NetBeans IDE