Monthly and Yearly Chart of Primefaces

2

I am trying to query a chart by month and year that when I click on the chart series it will display the records belonging to that path. When doing the search the graph is mounted correctly according to the selected month and year, but when I click on some series of an indexOutOfBoudsException error index: 28, size: 18 for example. I noticed that 18 is from the list of the current month (when loading the page the graph is always mounted with the current month) and 28 is the list of the date I am searching.

CodewheretheSeriesisselectedandopensamodalwiththeregisters

publicvoidcaminhoSelecionado(ItemSelectEventevent)throwsParseException{modal=event.getItemIndex();Stringcaminho=retornaTipoCaminho(modal,ano,mes);System.out.println("o que tem aqui  " + retornaTipoCaminho(modal, ano, mes));

    if (ano != null && mes != null) {
        listaModal = filtroBS.retornaCaminhoPorAno(caminho, ano, mes);
        System.out.println("o que caiu aqui? " + caminho);

    } else {
        listaModal.clear();
        listaModal = filtroBS.retornaCaminhoModal(caminho);
        System.out.println(" Tamanho " + listaModal.size());
    }
}

Code where I return a List of Paths to the Graph.

public String retornaTipoCaminho(Integer codigo, String ano, Integer mes) throws ParseException {

    List<String> listaCaminhos = new ArrayList<String>();

    for (ChamadosEntity f : listaCategoria) {
        listaCaminhos.add(f.getCaminho());

    }
    return listaCaminhos.get(codigo).toString();
}

Code where I create the chart from the search

public void pesquisaCaminhoData() throws ParseException {     
    if (ano != null && mes != null) {
        System.out.println("Tem lista aqui " + listaModal.size());

        createHorizontalBarModel(ano, mes);  
    } else {
        System.out.println("Erro está aqui ");
    }
}

And here's my xhtml.

<p:dialog modal="true" header="Pesquisar" widgetVar="dlg2" style="width: 500px; height: 800px" closable="true" responsive="true">
  <h:form id="FrmData">
    <p:panelGrid columns="1" layout="grid" styleClass="ui-panelgrid-blank form-group" style="text-align: center;">
      <h:panelGroup styleClass="md-inputfield">
        <p:selectOneMenu value="#{graficoBarMB.mes}" class="fonte">
          <f:selectItem itemLabel="Selecione" />
          <f:selectItem itemLabel="Janeiro"  itemValue="0" />
          <f:selectItem itemLabel="Fevereiro" itemValue="1" />
          <f:selectItem itemLabel="Março" itemValue="2" />
          <f:selectItem itemLabel="Abril" itemValue="3" />
          <f:selectItem itemLabel="Maio" itemValue="4" />
          <f:selectItem itemLabel="Junho" itemValue="5" />
          <f:selectItem itemLabel="Julho" itemValue="6" />
          <f:selectItem itemLabel="Agosto" itemValue="7" />
          <f:selectItem itemLabel="Setembro" itemValue="8" />
          <f:selectItem itemLabel="Outubro" itemValue="9" />
          <f:selectItem itemLabel="Novembro" itemValue="10" />
          <f:selectItem itemLabel="Dezembro" itemValue="11" />
        </p:selectOneMenu>
      </h:panelGroup>
        <h:panelGroup styleClass="md-inputfield">
          <p:selectOneMenu value="#{graficoBarMB.ano}" class="fonte">
              <f:selectItem itemLabel="Selecione" />
              <f:selectItems value="#{graficoBarMB.listaAno}" />
              <p:ajax event="itemSelect" listener="#{graficoBarMB.anoSelecionado}"/>
          </p:selectOneMenu>
        </h:panelGroup>
        <p:commandButton value="Filtrar" action="#{graficoBarMB.pesquisaCaminhoData}" ajax="false" update="frmChart :frmChart:caminho" process="@all"  styleClass="green-btn">
            <f:param name="ano" value="#{graficoBarMB.anoT}" />                    
            <f:param name="mes" value="#{graficoBarMB.mes}" />
        </p:commandButton>
      </p:panelGrid>
  </h:form>
</p:dialog>
  <div class="botãofiltrar">
    <p:commandButton style="margin-top: 15px;" value="Filtrar" onclick="PF('dlg2').show()" styleClass="green-btn" />
  </div>
  <p:dialog modal="true" widgetVar="dlg1" style="width: 500px; height: 800px" closable="true" responsive="true">
    <p:tabView id="tblView" class="tabview"  cache="false" >
      <p:tab title="Chamados por Caminho">
        <p:dataTable id="tblModal" widgetVar="tblModal" var="caminho" value="#{graficoBarMB.listaModal}" scrollable="true" scrollHeight="577">
          <p:column headerText="Chamados" sortBy="#{caminho.chamado}" width="8%">
              <h:outputText value="#{caminho.chamado}"/>
          </p:column>
          <p:column headerText="Titulo" filterBy="#{caminho.titulo}"  sortBy="#{caminho.titulo}" width="35%">
              <h:outputText value="#{caminho.titulo}"/>
          </p:column>
          <p:column headerText="Clientes" filterBy="#{caminho.cliente}" sortBy="#{caminho.cliente}" width="28%">
              <h:outputText value="#{caminho.cliente}"/>
          </p:column>
          <p:column headerText="Data Abertura" sortBy="#{caminho.dataAbertura}" style="height: 1px">
              <h:outputText value="#{caminho.dataAbertura}">
                  <f:convertDateTime  pattern=" dd/MM/Y H:mm  " locale="pt_BR" timeZone="America/Sao_Paulo" />
              </h:outputText>
          </p:column>
          <p:column headerText="Data Vencimento" sortBy="#{caminho.dataVencimento}" style="height: 1px">
              <h:outputText value="#{caminho.dataVencimento}">
                  <f:convertDateTime  pattern=" dd/MM/Y H:mm  " locale="pt_BR" timeZone="America/Sao_Paulo" />
              </h:outputText>
          </p:column>
        </p:dataTable>
      </p:tab>
    </p:tabView>
  </p:dialog>
  <div class="ui-g-12 ui-lg-12 "   onclick="PF('dlg1').show()"    >
    <h1 class="centerText">Grafico Teste</h1>
    <p:chart id="caminho" type="bar" model="#{graficoBarMB.horizontalBarModel}" responsive="true" class="Bargrafico" > 
      <p:ajax event="itemSelect" listener="#{graficoBarMB.caminhoSelecionado}" update="tblView:tblModal" />
    </p:chart>
  </div>
</h:form>

Chart:

Bean:

@ManagedBean@ViewScopedpublicclassGraficoBarMBimplementsSerializable{

privatefinalFiltroBSfiltroBS=newFiltroBS();privatefinalConsultaBSconsultaBS=newConsultaBS();privateBarChartDAObarChartDAO;privateList<ChamadosEntity>graficoCaminho;privateList<ChamadosEntity>graficoDosCaminho;privateHorizontalBarChartModelhorizontalBarModel=newHorizontalBarChartModel();privateIntegermodal;privateList<ChamadosEntity>listaModal=newArrayList<ChamadosEntity>();privatefinalList<ChamadosEntity>listaCategoria=consultaBS.retornaCaminho();privateList<Integer>listaAno=newArrayList<Integer>();privateStringano;privateIntegermes;@PostConstructpublicvoidinit()throwsParseException{barChartDAO=newBarChartDAO();createHorizontalBarModel(ano,mes);carregaLista();listaAno=retornaAno();}/******************************************************CARREGAACONSULTACOMALISTADECAMINHOS+++***************************************************/publicvoidcarregaLista(){try{listaModal=filtroBS.retornaCaminhoModal("PDV » TECLADO");
    } catch (ParseException i) {
        i.printStackTrace();
    }
}

/*
 ******************************************** 
 *** CRIA UMA LISTA DE ANOS PARA PESQUISA +++ 
 ******************************************** 
 */
private List<Integer> retornaAno() {

    Integer i = 0;
    Integer inicio = 2015;
    Integer calculo = 0;

    for (i = 0; i <= 5; i++) {
        calculo = inicio + i;
        listaAno.add(calculo);
    }
    return listaAno;
}

/*
 ******************************* 
 *** GET DO HORIZONTAL CHART +++ 
 ******************************* 
 */
public HorizontalBarChartModel getHorizontalBarModel() {
    return horizontalBarModel;
}

/*
 ******************************************** 
 *** INICIA GRAFICO A PARTIR DO MES E ANO +++ 
 ******************************************** 
 */
private HorizontalBarChartModel initHorizontalBarChartModel(String ano, Integer mes) throws ParseException {

    graficoCaminho = new ArrayList<ChamadosEntity>();

    HorizontalBarChartModel model = new HorizontalBarChartModel();

    if (ano != null && mes != null) {

        graficoCaminho = filtroBS.retornaCaminhoPesquisado(ano, mes);

        System.out.println("CAIU NO FILTRO DO GRAFICO!!!!!!!!!!!!");

    } else {

        graficoCaminho = barChartDAO.listaCaminho();

    }

    ChartSeries barras = new ChartSeries();

    for (ChamadosEntity cm : graficoCaminho) {
        barras.set(cm.getCaminho(), cm.getChamados());

    }

    model.addSeries(barras);
    return model;
}

/*
 ***************************** 
 *** MONTA GRAFICO NA TELA +++ 
 ***************************** 
 */
private void createHorizontalBarModel(String ano, Integer mes) throws ParseException {

    horizontalBarModel = initHorizontalBarChartModel(ano, mes);
    horizontalBarModel.setTitle("Horizontal and Stacked");
    horizontalBarModel.setLegendPosition("e");
    horizontalBarModel.setStacked(true);
    horizontalBarModel.setShowPointLabels(true);

    Axis xAxis = horizontalBarModel.getAxis(AxisType.X);
    xAxis.setLabel("Quantidade de Chamados");
    xAxis.setMin(0);
    xAxis.setMax(100);
    xAxis.setTickFormat("%1$.0f");

    Axis yAxis = horizontalBarModel.getAxis(AxisType.Y);
    yAxis.setLabel("Caminhos");
}

/*
 ******************************** 
 *** EXIBE MODAL COM CHAMADOS +++ 
 ******************************** 
 */
public void caminhoSelecionado(ItemSelectEvent event) throws ParseException {
    modal = event.getItemIndex();

    String caminho = retornaTipoCaminho(modal);
    System.out.println("o que tem aqui  " + retornaTipoCaminho(modal));

    if (ano != null && mes != null) {
        listaModal = filtroBS.retornaCaminhoPorAno(caminho, ano, mes);
        System.out.println("o que caiu aqui? " + caminho);

    } else {
        listaModal.clear();
        listaModal = filtroBS.retornaCaminhoModal(caminho);
        System.out.println(" Tamanho " + listaModal.size());
    }
}

/*
 ***************************************** 
 *** RETORNA UMA LISTA COM OS CAMINHOS +++ 
 *****************************************
 */
public String retornaTipoCaminho(Integer codigo) throws ParseException {

    List<String> listaCaminhos = new ArrayList<String>();

    for (ChamadosEntity f : listaCategoria) {
        listaCaminhos.add(f.getCaminho());
        //System.out.println("lista de caminhos " + listaCaminhos);

    }
    System.out.println(" Qual o tamanho da lista de caminhos " + listaCaminhos.size());
    return listaCaminhos.get(codigo).toString();
}


/*
 **************************************************************** 
 *** MONTA O GRAFICO PELA PESQUISA +++ 
 ****************************************************************
 */
public void pesquisaCaminhoData() throws ParseException {

    System.out.println("ANO: " + ano);
    System.out.println("MES: " + mes);

    if (ano != null && mes != null) {

        System.out.println("Tem lista aqui " + listaModal.size());

        createHorizontalBarModel(ano, mes);

        listaModal.clear();
    } else {
        System.out.println("Erro está aqui ");
    }

}
    
asked by anonymous 24.08.2017 / 16:21

1 answer

0

Your problem was with the scope of variables mes and ano that you tried to update in some method and it was not successful.

You can do the following. Create a class to represent this month and year.

ex:

public class FiltroCaminho{
    private String ano; 
    private String mes; 

    //getters e setters
}

and instead of passing as parameter ... (year, month) ... you pass ... (filterCaminho) ... and your year and month strings will be updated.

    
28.08.2017 / 16:56