Graphics in PrimeFaces

2

I have a method that does a query in the database and returns me a List , in this List I have three data that interest me: Funcionário and StatusTarefa .

Example: I have 2 Employees performing various tasks, a task can have two statuses, Finalizado and Pausado . I would like to display in a graph the amount of Tasks Finished and Paused per employee. But I have a doubt, I believe that for this I would need to use an operation to group and count the statuses. How do I do this?

Image that exemplifies how I want to leave the chart:

I'mdoingthisgraphwithfixeddata:

@ManagedBean@RequestScopedpublicclassGraficoBarraBean{privateBarChartModelbarModel;publicvoidinit(){this.barModel=newBarChartModel();createBarModels();}publicBarChartModelgetBarModel(){returnbarModel;}privateBarChartModelinitBarModel(){BarChartModelmodel=newBarChartModel();ChartSeriesfinalizado=newChartSeries();finalizado.setLabel("Finalizado");
        finalizado.set("Diego", 6);
        finalizado.set("Lucas", 10);
        finalizado.set("Cris", 12);


        ChartSeries pausado = new ChartSeries();
        pausado.setLabel("Pausado");
        pausado.set("Diego", 2);
        pausado.set("Lucas", 1);
        pausado.set("Cris", 7);



        model.addSeries(finalizado);
        model.addSeries(pausado);
        model.setAnimate(true);


        return model;
    }

    private void createBarModels() {
        createBarModel();
    }

    private void createBarModel() {
        barModel = initBarModel();

        barModel.setTitle("Tarefas");
        barModel.setLegendPosition("ne");


        Axis xAxis = barModel.getAxis(AxisType.X);
        xAxis.setLabel("Funcionários");


        Axis yAxis = barModel.getAxis(AxisType.Y);
        yAxis.setLabel("Tarefas");
        yAxis.setMin(0);
        yAxis.setMax(20);
    }

    //Método que faz a consulta no banco e atribui os dados a uma lista de Tarefas
    public List<Tarefa> listarTodos() {
        List<Tarefa> lista = new ArrayList<>();
        try {
            TarefaDAO tarefaDAO = new TarefaDAO();
            lista = tarefaDAO.listar();

        } catch (RuntimeException e) {
            FacesUtil.adicionarMsgErro("Erro ao listar tarefas: "
                    + e.getMessage());
        }
        return lista;
    }    
}
    
asked by anonymous 20.07.2015 / 13:55

2 answers

3

Try something like this with Map , so you'll have the amount for each employee of paused and completed tasks:

Map<Funcionario, Integer> tarefasPausadas = new HashMap<Funcionario, Integer>();
Map<Funcionario, Integer> tarefasFinalizadas = new HashMap<Funcionario, Integer>();


        for (Tarefa tarefa : listaTarefa) {
            Funcionario f = tarefa.getFuncionario();
            if (tarefa.getStatusTarefa() == Tarefa.StatusTarefa.Finalizado) {

                if (tarefasFinalizadas.containsKey(f)) {
                    tarefasFinalizadas.put(f, tarefasFinalizadas.get(f) + 1);
                } else {
                    tarefasFinalizadas.put(f, 1);
                }

            } else if (tarefa.getStatusTarefa() == Tarefa.StatusTarefa.Pausada) {
                if (tarefasPausadas.containsKey(f)) {
                    tarefasPausadas.put(f, tarefasPausadas.get(f) + 1);
                } else {
                    tarefasPausadas.put(f, 1);
                }
            }
        }

So when asking Employee X for the number of completed (or paused) tasks, just do:

Integer qt=  tarefasFinalizadas.get(f);//se null é porque o funcionário não tem tarefa finalizada.
    
20.07.2015 / 15:54
2

@Techies, I think with this you will know how many statuses of each you have:

private int contadorFinalizado = 0;
private int contadorPausado = 0;

public void contarLista() {
    for (Tarefa tarefa : listaTarefa) {
        if (tarefa.StatusTarefa == tarefa.StatusTarefa.Finalizado) {
            contadorFinalizado++;
        } else {
            contadorPausado++;
        }
    }
}
    
20.07.2015 / 14:32