0

I need help witch two charts in Java witch JFreeChart.

I try to combine two graphs, one of bars and one of lines, but I don't know how to put them... I take the two datasets for a query form my data base, i want to combinate a bar chart with a line chart te bar chart represents the total of assistance in a group ant the line chart represents the percentage of attendance percentage of the group.

This is my code:

public static void BarCharCursosGeneral(IFrmCapacitacion frm) {
        Conexion conn = new Conexion();
        Connection con = conn.getConnection();
        ResultSet rs = null;

        DefaultCategoryDataset dataset = new DefaultCategoryDataset();
        DefaultCategoryDataset dataset2 = new DefaultCategoryDataset();

        String mode = "SET sql_mode=(SELECT REPLACE(@@sql_mode,'ONLY_FULL_GROUP_BY',''));";
        String sql = "SELECT \n"
                + "    nombre_tipo,COUNT(idHistorial) AS cursos,\n"
                + "    SUM(asistentes_esperados) as esperado,\n"
                + "    SUM(num_asistentes) AS asistentes,\n"
                + "    FLOOR(SUM(num_asistentes)/SUM(asistentes_esperados)*100) AS porcentaje\n"
                + "FROM sistema_capacitacion.view_historialcursos\n"
                + "WHERE estado_curso = 'Concluido'\n"
                + "AND YEAR(fecha_cierre) = YEAR(CURDATE())\n"
                + "GROUP BY id_tipocurso\n"
                + "ORDER BY id_tipocurso;";

        try {
            PreparedStatement ps = con.prepareStatement(mode);
            ps.executeUpdate();

            ps = con.prepareStatement(sql);
            rs = ps.executeQuery();

            while (rs.next()) {
                dataset.addValue(rs.getInt("cursos"), "Total de Cursos", rs.getString("nombre_tipo"));
                dataset2.addValue(rs.getInt("porcentaje"), "% Asistencia", rs.getString("nombre_tipo"));
            }
        } catch (SQLException ex) {
            Logger.getLogger(Graphics_Capacitacion.class.getName()).log(Level.SEVERE, null, ex);
        }

        JFreeChart chart1 = ChartFactory.createBarChart(
                "Total de Cursos",
                "Cursos",
                "Cantidad",
                dataset,
                PlotOrientation.VERTICAL,
                true,
                true,
                false
        );

        JFreeChart chart2 = ChartFactory.createLineChart(
                "% Asistencia",
                "Cursos",
                "%",
                dataset2,
                PlotOrientation.VERTICAL,
                true,
                true,
                false
        );

        CombinedDomainCategoryPlot combinedPlot = new CombinedDomainCategoryPlot();
        combinedPlot.add(chart1.getCategoryPlot());
        combinedPlot.add(chart2.getCategoryPlot());

        JFreeChart combinedChart = new JFreeChart("Gráficas combinadas", JFreeChart.DEFAULT_TITLE_FONT, combinedPlot, true);
        ChartPanel chartPanel = new ChartPanel(combinedChart, 350, 200, ChartPanel.DEFAULT_MINIMUM_DRAW_WIDTH, ChartPanel.DEFAULT_MINIMUM_DRAW_HEIGHT, ChartPanel.DEFAULT_MAXIMUM_DRAW_WIDTH, ChartPanel.DEFAULT_MAXIMUM_DRAW_HEIGHT, true, true, true, true, true, true);

        JPanel chartContainer = new JPanel(new BorderLayout());
        chartContainer.add(chartPanel, BorderLayout.CENTER);

        // Agregar el panel de gráfica al JPanel existente en la interfaz (frm.jPanel24)
        frm.jPanel24.removeAll(); // Limpiar el panel existente
        frm.jPanel24.setLayout(new BorderLayout());
        frm.jPanel24.add(chartContainer, BorderLayout.CENTER);

        // Actualizar el JPanel para que se muestre la gráfica
        frm.jPanel24.revalidate();
        frm.jPanel24.repaint();
    }

But this is the result:

Can someone tell me how to do it correctly? Thanks

Combine two graphs into one

  • Are you trying to create a [_Pareto chart_](https://en.wikipedia.org/wiki/Pareto_chart)? You will likely need multiple axes and renderers, suggested [here](https://stackoverflow.com/a/3809514/230513). – trashgod Jun 23 '23 at 19:19

0 Answers0