当我第一次点击"Visa lista"按钮时,图表不会显示任何结果。即使是第一次,"teamList"也会正确更新。createChart()将被调用,并且在第一次尝试时退出函数时具有正确的信息。函数将返回CartesianChartModel。
如果我退出页面或重新加载页面,图表将是正确的。每次我点击"Visa列表"时,它也会正确更新。
编辑:按下按钮后,页面也将停止处理咆哮信息。这也将在页面刷新后恢复工作。
<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>
根据要求。添加bean和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;
}
}
如果你试图在没有真实信息的情况下呈现图表,Primefaces中似乎有一些东西最终处于非法状态。
在初始化之前删除渲染后,它就可以工作了。缺少的是停止在空上渲染。
rendered="#{not empty serviceTeam.teamLists}"
这就是我更改后的条形图的样子。
<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"/>
我相信serviceTeam
也是@ManagedBean
。不能将@ManagedBean
用作函数的参数。serviceTeam
需要的是teamList
,因此更改createChart
函数如下:
public CartesianChartModel createChart(List<Team> teamList) {
...
}
那么你的<p:barChart>
应该是这样的:
<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"/>