0

Basically, I want to implement a webpage using Primefaces line chart that can change every one second.

In my managed bean, I get, say 100 data from my database. Instead of populating the line chart all at once, I want to display them like a data wave, so that these 100 data can be shown in 100 seconds.

How can I implement this?

Cacheing
  • 3,431
  • 20
  • 46
  • 65
  • You can use poll: http://www.primefaces.org/showcase/ui/poll.jsf – Rong Nguyen Apr 10 '13 at 07:16
  • Primefaces charts used to have a `live` attribute in v3.2. `live="true"` would cause the charts to refresh itself at intervals. Can't imagine why they removed it – kolossus Apr 10 '13 at 12:48

1 Answers1

2

You should get 100 data from DB and store them in a list let say main list, and there should be one more list which will be refreshed every one second and the current element of main list will be appended to second list . Sth. like that:

@ManagedBean
@ViewScoped
public class DataBean {

    private ArrayList<Data> mainList;//fill this from DB via seperate DAO class
    private ArrayList<Data> viewList;
    public int counter;
    //getter and setters
    public void refreshList() {
        viewList.add(mainList.get(counter);
        counter++;
    } 
}

And at the view layer you should call refreshList method periodically of course this can be done with p:poll or p:remoteCommand. RemoteCommand needs to be called more than once via JS code, it's like:

jQuery(document).ready(function () {
    setInterval("updateChart", 1000);//every 1 sec
});

And this func. will call the p:remoteCommand which is named as updateChart:

<p:remoteCommand name="updateChart" actionListener="#{dataBean.refreshList}"
                             update="myChart"/>

myChart is the client id of your p:lineChart component you have to give the exact ID, you can detect it via your browser's developer settings.

And remember that this is not the exact implementation there can be errors, and p:lineChart expects model classes instead arrayLists, I've never used chart components but I'm assuming series similar to lists. Hope this can guide you.

Ömer Faruk Almalı
  • 3,792
  • 6
  • 37
  • 63
  • 1
    Good answer, he may use poller for the last part http://www.primefaces.org/showcase/ui/poll.jsf – Serkan Arıkuşu Apr 10 '13 at 08:34
  • @SerkanArıkuşu thank you. I'm having some problems and there can be bugs of `p:poll` like [here](http://stackoverflow.com/questions/15903816/jsf-primefaces-social-network-notification-system-with-ppoll-push). That's why I've mentioned `p:remoteCommand` – Ömer Faruk Almalı Apr 10 '13 at 10:02
  • @ÖmerFarukAlmalı Thank you for giving me two choices. The `poller` works for me. – Cacheing Apr 11 '13 at 21:06