Graphics in Primefaces

Asked

Viewed 215 times

2

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

Example: I have 2 Employees performing various tasks, one task can have two status, Finalizado and Pausado. I would like to display on a chart the number of Completed and Paused tasks per employee. But I have a question, I believe that for this I would need to use an operation to group and count the status. How I do this?

Image that exemplifies how I want to leave the chart: inserir a descrição da imagem aqui

I’m making this graph with fixed data:

@ManagedBean
@RequestScoped
public class GraficoBarraBean {
    private BarChartModel barModel;



    public void init() {
        this.barModel = new BarChartModel();
        createBarModels();
    }

    public BarChartModel getBarModel() {
        return barModel;
    }



    private BarChartModel initBarModel() {
        BarChartModel model = new BarChartModel();

        ChartSeries finalizado = new ChartSeries();
        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;
    }    
}

2 answers

3


Try something like that with Map, this way will have the amount for each employee of tasks paused and finalized:

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 you ask Employee X the number of tasks completed (or paused) just do:

Integer qt=  tarefasFinalizadas.get(f);//se null é porque o funcionário não tem tarefa finalizada.
  • Oops. I did some tests and the map ta returning the right data, now I will try to put in the graph

  • For example in my graphic model I have to pass two parameters, the second I put tarefasFinalizadas.get(f) which is the quantity and in the first which is the name of the Official placed tarefa.getNome()?

  • This way so far: finalizado.setLabel("Finalizado");&#xA; finalizado.set("Diego", tarefasFinalizadas.get(f));

  • 1

    You can use the f.getNome() since f is your employee, if you do tarefa.getNome() will come the name of the task.

  • Thanks a lot for the help, I managed to solve the problem. Now I will study a little more about Hashmap

  • 1

    Oops. Here’s a good article: Java Hashmap: Working with Key-value Lists

Show 1 more comment

2

@Techies, I think with this you will know how many status 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++;
        }
    }
}
  • But how do I connect this counter to my employee?

  • your employee has a to-do list?

  • Actually the to-do list has an employee.

  • each task has an official or a list of them?

  • each task has one employee and one employee can have many tasks

  • What kind is List, task or employee? Because you said 'this List I have three data that interest me: Employee and Statustarefa.' you said three but showed only 2.

  • I typed wrong, they are only 2 same. I edited the question with an image of how I want to leave and the code I am doing so far

  • The amount of each status and status can be considered the same item

  • I know I have to do a go and every repeat pick up the employee and the amount of tasks, but how to assign the amount to the right employee?

  • If each task has an employee and you have the task list, and still want to group per employee use a hashmap then

  • Would you have an example of Hashmap?

  • @Techies, posted the answer an answer there take a look.

Show 7 more comments

Browser other questions tagged

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