Saturday Nov 10, 2012

Live Updates in PrimeFaces Line Chart

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;

    public ChartController() {
        createLinearModel();
    }

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

    private ChartSeries getStockChartData(String label) {
        ChartSeries 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.

What I need is, instead of the for loop, to have an incrementing index to be shown, i.e., each second should be displayed on the x axis, which should increment dynamically per second. Can anyone help?

About

Geertjan Wielenga (@geertjanw) is a Principal Product Manager in the Oracle Developer Tools group living & working in Amsterdam. He is a Java technology enthusiast, evangelist, trainer, speaker, and writer. He blogs here daily.

The focus of this blog is mostly on NetBeans (a development tool primarily for Java programmers), with an occasional reference to NetBeans, and sometimes diverging to topics relating to NetBeans. And then there are days when NetBeans is mentioned, just for a change.

Search

Archives
« November 2012 »
SunMonTueWedThuFriSat
    
11
13
14
15
24
25
 
       
Today