Graphic Primefaces with Consultation by Month and Year

Asked

Viewed 320 times

2

I am trying to query a chart per 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 chart is mounted correctly according to the selected month and year, but when I click on some series an error of Indexoutofboudsexception index:28, size:18 for example. I found 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.

inserir a descrição da imagem aqui

Code where the Serie is selected and opens a modal with the records

public void caminhoSelecionado(ItemSelectEvent event) throws ParseException {
    modal = event.getItemIndex();

    String caminho = 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 to return a Chart Path List.

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>

Graph: inserir a descrição da imagem aqui

Bean:

@Managedbean @Viewscoped public class Graficobarmb Implements Serializable {

private final FiltroBS filtroBS = new FiltroBS();
private final ConsultaBS consultaBS = new ConsultaBS();

private BarChartDAO barChartDAO;
private List<ChamadosEntity> graficoCaminho;
private List<ChamadosEntity> graficoDosCaminho;
private HorizontalBarChartModel horizontalBarModel = new HorizontalBarChartModel();
private Integer modal;
private List<ChamadosEntity> listaModal = new ArrayList<ChamadosEntity>();
private final List<ChamadosEntity> listaCategoria = consultaBS.retornaCaminho();
private List<Integer> listaAno = new ArrayList<Integer>();
private String ano;
private Integer mes;

@PostConstruct
public void init() throws ParseException {
    barChartDAO = new BarChartDAO();
    createHorizontalBarModel(ano, mes);
    carregaLista();
    listaAno = retornaAno();
}

/*
 ************************************************** 
 *** CARREGA A CONSULTA COM A LISTA DE CAMINHOS +++ 
 ************************************************** 
 */
public void carregaLista() {
    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 ");
    }

}
  • this Exception is launched in this line return listaCaminhos.get(codigo).toString();?

  • Yeah, it’s flipping on this line.

  • first doubt, why you go through , String ano, Integer mes in the method retTipoCame, if you do not use?

  • and, you are sure that list Category is the list you need to solve your problem?

  • Why, what happens is that you are trying to access a position that does not exist in the array listCaminhos, which was mounted from listCategory. Then list Category should not have, if you want, the size of codigo that was past

  • I get it, you’re always getting the size of the list for the current month, so when I try to search for previous months that might have more or less paths that you’re falling for in this Exception.

  • when I click on some path in the graph of the current month, go through the list twice bringing the eighteen paths of the month, when I perform the search and click on some path go through the list once bringing 18 and not the amount of the current month

  • did not understand, solved the problem? what would be the path of the graph? I have no knowledge of how the graph is and how it is assembled, so I can’t understand

  • Not yet, I added a picture of the chart, it is giving an update and cleaned month and year when I try to bring results by clicking on the paths.

  • What is the scope of your bean?

  • Viewscoped.....

  • of javax.faces.bean.ViewScoped ?

  • Yes, this very one.

  • It’s hard to understand the problem, I know that Exception is launched by the fact that it lists Objects not having the element at position 18(in this example) and that it lists Blocks is built from listCategory. Do the following, debug and check if listCategory is with the data you need.

  • The list Category brings the paths correctly, I did one thing here I spent the year = "2016" and the month = 5, and he brought exactly what I wanted, that is to say he is missing the month and the year when the page is being cleaned for the creation of the chart from the search, tried to use a variable even so it loses, also tried to create an Entity with year and month and it null field error.

  • puts all your bean and describes the process

  • put, man is getting too extensive here, do not want to move to the chat?

  • can be, call there in chat.

  • few points of reputation, I can’t even call it. in loadList() use to load a list of calls to use in the event click on the bars of the chart, retAno() create a series of years to use in the search, in the initHorizontalBarChartModel() start the chart from month and year. on the selected path pick up the list that loads the calls and use it for when I click on the bar it display a modal with the so-called matching that path in the searchodata() is where I create the chart when I do the search

  • I just need a way that these year and month variables are stored, I’ve tried a lot of things only they’re cleaned every time

  • reading your code here, you are "updating" the variable ano and mês in a method and passing the two variables ano and mês per parameter. Only when you pass a String as parameter, you do not pass a reference to the variable you are passing, that is, it creates another string. So, if you upgrade, nothing will matter as the scope will only be in the method.

  • 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 ...( ano, mes)... you pass ...(filtroCaminho)... and your year and month strings will be updated

  • Blz, I’ll do it here.

  • It worked @Henriquesantiago I was able to store the way you said. Thank you!

  • For me to mark as solved you have to put as right answer?

  • show. I will make an answer, post, then you accept. (Y)

Show 21 more comments

1 answer

0


His problem was with the scope of variables mesand ano that you were trying to update on some method and were unsuccessful.

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 ...( ano, mes)... you pass ...(filterCaminho)... and your year and month strings will be updated.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.