Live Updates in PrimeFaces Line Chart
- by Geertjan
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.