Reputation: 2741
The chart will not show any results the first time I click the "Visa lista" button. The "teamList" will correctly update even on the first time. The createChart() will be called and has the correct information when exiting the function also on first try. The function will return CartesianChartModel.
If I exit the page or reload the page the chart will be correct. It will also update correctly every time I click "Visa lista".
Edit: After one press on the button the page will also stop processing the growl message. This will also resume working after page refresh.
<p:growl id="growl" life="5000" autoUpdate="true" sticky="false" showDetail="true" />
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:f="http://xmlns.jcp.org/jsf/core"
xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
xmlns:c="http://java.sun.com/jsp/jstl/core"
xmlns:p="http://primefaces.org/ui">
<f:metadata>
<!-- f:event type="preRenderView" listener="#{serviceHCP.getClubs(null)}"/ -->
</f:metadata>
<ui:composition template="/WEB-INF/templates/template.xhtml">
<ui:define name="content">
<p:growl id="growl" life="5000" autoUpdate="true" sticky="false" showDetail="true" />
<h:form id="form1">
<div id="left">
<p:menu>
<p:submenu label="Huvudmeny">
<p:menuitem value="Snittlista" outcome="showAverages" />
<p:menuitem value="Månadstävling" outcome="showMonthly" />
<p:menuitem value="Lagresultat" outcome="showTeam" />
<p:menuitem value="Logga in" outcome="login" />
<p:menuitem value="Logga ut" actionListener="#{login.doLogout}" />
</p:submenu>
</p:menu>
</div>
<div id="right">
<div id="pageHeader">Snitt information</div>
<h:panelGrid columns="2">
<h:outputText value="Klubb"></h:outputText>
<p:selectOneMenu id="menu" value="#{serviceHCP.myCurrentClub}" converter="clubConverter">
<f:selectItems value="#{club.clubs}"
var="clb"
itemValue="#{clb}"
itemLabel="#{clb.name}"
itemLabelEscaped="true"/>
<p:ajax event="change" process="@this" />
</p:selectOneMenu>
<h:outputText value="Serietyp"></h:outputText>
<p:selectManyMenu value="#{serviceSerieType.mySelectedSerieTypes}"
converter="serieTypeConverter" id="serieTypes" style="height:200px">
<f:selectItems value="#{serviceSerieType.serieTypes}"
var="st"
itemValue="#{st}"
itemLabel="#{st.serie_type}"
itemLabelEscaped="true"/>
</p:selectManyMenu>
<h:outputText value="Startdatum"></h:outputText>
<p:calendar value="#{calendarBean.date1}" id="popupButtonCal" showOn="button" pattern="yyyy-MM-dd HH:mm:ss" >
<!-- p:ajax event="dateSelect" process="@this" / -->
</p:calendar>
<h:outputText value="Slutdatum"></h:outputText>
<p:calendar value="#{calendarBean.date2}" id="popupButtonCal2" showOn="button" pattern="yyyy-MM-dd HH:mm:ss" >
<!-- p:ajax event="dateSelect" process="@this" / -->
</p:calendar>
<h:outputText value=""></h:outputText>
<p:commandButton id="btnShow" value="Visa lista" update=":avgChart" render=":avgChart:chart" action="#{serviceTeam.getTeamScores(serviceHCP.myCurrentClub, serviceSerieType.mySelectedSerieTypes, calendarBean )}">
<!-- p:ajax update=":avgChart:chartAverages" process="@this" listener=""/ -->
</p:commandButton>
</h:panelGrid>
</div>
</h:form>
<h:form id="avgChart">
<div id="right">
<br />
<p:dataTable id="teamMatches" var="teamList" value="#{serviceTeam.teamList }"
border="1">
<!-- Fortsätt här !!! -->
<p:column id="team" sortBy="#{teamList.serieType}">
<f:facet name="header">Lag</f:facet>
<p:commandLink value="#{teamList.serieType}" action="showTeamPlayer"
actionListener="#{teamPlayer.getTeamPlayerScores(club, teamList.serieType, teamList.playDate)}">
</p:commandLink>
</p:column>
<p:column id="playDate" sortBy="#{teamList.playDate}">
<f:facet name="header">Datum</f:facet>
<h:outputText value="#{teamList.playDate}" >
<f:convertDateTime pattern="yyyy-MM-dd" />
</h:outputText>
</p:column>
<p:column id="noSeries" sortBy="#{teamList.series}">
<f:facet name="header">Antal Serier</f:facet>
<h:outputText value="#{teamList.series}" />
</p:column>
<p:column id="total" sortBy="#{teamList.result}">
<f:facet name="header">Resultat</f:facet>
<h:outputText value="#{teamList.result}" />
</p:column>
</p:dataTable>
<p:blockUI block="teamMatches" trigger=":form1:btnShow">
<p>Laddar datan. Det kan ta en stund</p>
<p:graphicImage value="/hcp/pictures/ajax-loader.gif"/>
</p:blockUI>
<p:barChart id="chart" value="#{teamChart.CreateChart(serviceTeam)}" legendPosition="ne"
title="Lagresultat" animate="true" zoom="true" min="0" max="6000" xaxisLabel="År och vecka" style="height:300px"/>
<br />
</div>
</h:form>
</ui:define>
</ui:composition>
</html>
As requested. Adding bean and chart.
@ManagedBean
public class TeamChart implements Serializable {
private static final long serialVersionUID = 1L;
private CartesianChartModel categoryModel;
public TeamChart() {
}
public CartesianChartModel getCategoryModel() {
if (categoryModel == null)
categoryModel = new CartesianChartModel();
return categoryModel;
}
public CartesianChartModel CreateChart(ServiceTeam serviceTeam) {
categoryModel = new CartesianChartModel();
List<ChartSeries> chartSeries = new ArrayList<ChartSeries>();
try {
for (List<Team> teamList : serviceTeam.getTeamLists() ) {
ChartSeries teamChart = new ChartSeries();
teamChart.setLabel(teamList.get(0).getSerieType());
for (Team team : teamList) {
Calendar cal = Calendar.getInstance();
cal.setTime(team.getPlayDate());
int week = cal.get(Calendar.WEEK_OF_YEAR);
int year = cal.get(Calendar.YEAR);
teamChart.set(year + "-" + week, team.getResult());
}
chartSeries.add(teamChart);
}
}
catch (Exception e) {
//e.printStackTrace();
}
for (ChartSeries chartSerie : chartSeries)
categoryModel.addSeries(chartSerie);
return categoryModel;
}
}
public class ServiceTeam implements Serializable {
private static final String PERSISTENCE_UNIT_NAME = "BowlingFacelets";
public static EntityManagerFactory factory;
/**
*
*/
private static final long serialVersionUID = 1720513077556033501L;
private List<Team> teamList;
private List<List<Team>> teamLists;
private int teamCount;
public ServiceTeam() {
super();
}
public void getTeamScores(Club club, List<SerieType> serieTypes, CalendarBean calendarBean) {
if (factory == null) {
factory = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT_NAME);
}
EntityManager em = factory.createEntityManager();
Query q = em.createQuery("select new com.jk.hcp.backingBeans.Team (sum(s.result), count(s.result), " +
" st.serie_type, cast(s.serieDate as date)) " +
" from Serie s, SerieType st " +
" where st.id = s.serieType.id " +
" and st.serie_type IN :serieTypeId " +
" and s.serieDate >= :startDate" +
" and s.serieDate <= :endDate" +
" group by cast(s.serieDate as date), st.serie_type" +
" order by cast(s.serieDate as date), st.serie_type" , ServiceTeam.class);
q.setParameter("serieTypeId", serieTypes);
q.setParameter("startDate", calendarBean.getDate1());
q.setParameter("endDate", calendarBean.getDate2());
teamList = q.getResultList();
teamLists = new ArrayList<List<Team>>();
for (SerieType serieType : serieTypes) {
q = em.createQuery("select new com.jk.hcp.backingBeans.Team (sum(s.result), count(s.result), " +
" st.serie_type, cast(s.serieDate as date)) " +
" from Serie s, SerieType st " +
" where st.id = s.serieType.id " +
" and st.id = :serieTypeId " +
" and s.serieDate >= :startDate" +
" and s.serieDate <= :endDate" +
" group by cast(s.serieDate as date), st.serie_type" +
" order by cast(s.serieDate as date), st.serie_type" , ServiceTeam.class);
q.setParameter("serieTypeId", serieType.getId());
q.setParameter("startDate", calendarBean.getDate1());
q.setParameter("endDate", calendarBean.getDate2());
List<Team> team = q.getResultList();
if (team.size() > 0) {
teamLists.add(team);
}
}
}
public List<Team> getTeamList() {
return teamList;
}
public void setTeamList(List<Team> teamList) {
this.teamList = teamList;
}
public int getTeamCount() {
return teamCount;
}
public void setTeamCount(int teamCount) {
this.teamCount = teamCount;
}
public List<List<Team>> getTeamLists() {
return teamLists;
}
public void setTeamLists(List<List<Team>> teamLists) {
this.teamLists = teamLists;
}
}
Upvotes: 0
Views: 2205
Reputation: 2741
It seems that there might be something ending up in an illegal state in Primefaces if you try to render the chart without real information.
After removing rendering before it's intialized it would work. The missing thing was to stop rendering on empty.
rendered="#{not empty serviceTeam.teamLists}"
This is how my changed barChart would look.
<p:barChart id="chart" value="#{teamChart.CreateChart(serviceTeam.teamLists)}"
rendered="#{not empty serviceTeam.teamLists}" legendPosition="ne"
title="Lagresultat" animate="true" zoom="true" min="0" max="6000" xaxisLabel="År och vecka" style="height:300px"/>
Upvotes: 1
Reputation: 9266
I believe serviceTeam
is a @ManagedBean
as well. You cannot use a @ManagedBean
as a parameter for a function. What you need from the serviceTeam
is the teamList
, so change the createChart
function as following:
public CartesianChartModel createChart(List<Team> teamList) {
...
}
Then your <p:barChart>
should look like this:
<p:barChart id="chart" value="#{teamChart.createChart(serviceTeam.teamList)}" legendPosition="ne"
title="Lagresultat" animate="true" zoom="true" min="0" max="6000" xaxisLabel="År och vecka" style="height:300px"/>
Upvotes: 0