0

I'm working with the JfreeChart library in java and I'm doing the graphs for the ordering methods, but I want the exchanges to be shown in execution time, what has occurred to me is that in each exchange the graph is updated but at the end of the code the graph does not show me correctly ordered values ​​within it. However, I thought for a moment that what was wrong was the bubble method but it is not, the method orders correctly. Any solution?.

example image: image

Here the code that I have made:

    private void graficar(int vector[]) {

    DefaultCategoryDataset datos = new DefaultCategoryDataset();
    JFreeChart chart = null;
    for (int i = 0; i < vector.length; i++) {

        datos.addValue(vector[i], "", "" + vector[i]);
        //Crear el gráfico...
    }
    chart = ChartFactory.createBarChart("Metodo " + cbMetodo.getSelectedItem().toString(),
            "",
            "",
            datos,
            PlotOrientation.VERTICAL,
            true,
            true,
            true);
    //crear y visualizar en el panel
    ChartPanel panel = new ChartPanel(chart);

    panelGrafica.setLayout(new java.awt.BorderLayout());
    panelGrafica.add(panel);
    panelGrafica.validate();

}

private void burbuja() {
    for (int x = 0; x < vector.length; x++) {
        for (int i = 0; i < vector.length - x - 1; i++) {
            if (vector[i] > vector[i + 1]) {
                int tmp = vector[i + 1];
                vector[i + 1] = vector[i];
                vector[i] = tmp;
                graficar(vector);

            }

        }

    }

}
  • Why are you creating a new chart for every item in your vector? That might be the issue. Should be outside the for loop. Not sure if that's the only problem, but I'd move the chart = ChartFactory line outside the loop and run again. – Evan Mar 27 '19 at 20:05
  • first of all apologize if you do not understand what I explain, I do not speak English and I try to translate everything, now yes, I had not noticed that line but it does not solve the problem what happens is that I do not finish ordering the data inside of the graph, I have already edited the question with a link of the image about what happens – Steven Camargo Mar 27 '19 at 20:26
  • Create the chart once; at each sort iteration, invoke `datos.clear()` and then loop through `vector`, calling `datos.addValue()`; the listening plot will update itself. If this is not a duplicate, please [edit] your question to include a [mcve] that shows your revised approach. – trashgod Mar 28 '19 at 02:10
  • Thanks @trashgod but it does not solve my problem what I want is to show the exchanges one by one in the graph – Steven Camargo Mar 28 '19 at 03:10
  • I think I understand; very broadly, you want to visualize the sorting process as it occurs, but a complete solution will require the you study the examples cited and adapt the approach shown in order to meet your specific requirements. – trashgod Mar 28 '19 at 08:26

0 Answers0