JFreeChart Pie с использованием базы данных MySql (PHPMyAdmin) - PullRequest
0 голосов
/ 12 апреля 2019

Я новичок здесь, и это мой первый вопрос, который я задаю. Я ломал голову, пытаясь составить несколько диаграмм либо с помощью Jasper (от которого я отказался), либо от JFreeChart (который я сейчас использую, пытаясь использовать ). То, что я пытаюсь сделать, это просто обработать фотографию, которая приводит к круговой диаграмме. В моей базе данных есть один столбец, описывающий технику объекта (ГДЕ 'tehnica' = ulei). И я хочу отобразить каждую из этих техник на графике по количеству объектов (количество). Я что-то пропустил в своем код

import java.io.File;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.swing.JTextField;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.data.general.DefaultPieDataset;


/*
 * To change this license header, choose License Headers in Project 
Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author Belial
 */
public class Rapoarte extends javax.swing.JFrame {

 JTextField ulei = null;
 JTextField ceara = null;
 JTextField acuarela = null;
 JTextField guasa = null;
 JTextField vopseaacrilica = null;
 JTextField pastel = null;
 JTextField fresca = null;
 JTextField cerneala = null;
 JTextField tempera = null;
 JTextField grisaille = null;
 JTextField pointilism = null;
 JTextField vitraliu = null;
 Connection con = null;
 Statement st = null;
 ResultSet rs = null;

public Rapoarte() {
    initComponents();
    con = MyConnection.getConnection();
}

/**
 * This method is called from within the constructor to initialize the form.
 * WARNING: Do NOT modify this code. The content of this method is always
 * regenerated by the Form Editor.
 */
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">                          
private void initComponents() {

    jPanel1 = new javax.swing.JPanel();
    jLabel1 = new javax.swing.JLabel();
    jButton2 = new javax.swing.JButton();
    jButton3 = new javax.swing.JButton();
    jButton4 = new javax.swing.JButton();
    jButton5 = new javax.swing.JButton();

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

    jLabel1.setFont(new java.awt.Font("Times New Roman", 0, 24)); // NOI18N
    jLabel1.setText("Rapoarte");

    jButton2.setText("jButton2");
    jButton2.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            jButton2ActionPerformed(evt);
        }
    });

    jButton3.setText("jButton3");

    jButton4.setText("jButton4");

    jButton5.setText("Cancel");

    javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
    jPanel1.setLayout(jPanel1Layout);
    jPanel1Layout.setHorizontalGroup(
        jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(jPanel1Layout.createSequentialGroup()
            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel1Layout.createSequentialGroup()
                    .addGap(99, 99, 99)
                    .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 108, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGroup(jPanel1Layout.createSequentialGroup()
                    .addGap(108, 108, 108)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(jButton2)
                        .addComponent(jButton3)
                        .addComponent(jButton4)
                        .addComponent(jButton5))))
            .addContainerGap(115, Short.MAX_VALUE))
    );
    jPanel1Layout.setVerticalGroup(
        jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(jPanel1Layout.createSequentialGroup()
            .addGap(31, 31, 31)
            .addComponent(jLabel1)
            .addGap(85, 85, 85)
            .addComponent(jButton2)
            .addGap(18, 18, 18)
            .addComponent(jButton3)
            .addGap(18, 18, 18)
            .addComponent(jButton4)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 121, Short.MAX_VALUE)
            .addComponent(jButton5)
            .addGap(25, 25, 25))
    );

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addGap(0, 0, Short.MAX_VALUE))
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addGap(0, 0, Short.MAX_VALUE))
    );

    pack();
}// </editor-fold>                        

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    try {
 st = con.createStatement();

 if (st.execute("select count(id_exponata) as ulei from exponata where tehnica = 'ulei'")) {
            rs = st.getResultSet();
            rs.next();
            ulei.setText(rs.getString("ulei"));

        }
 if (st.execute("select count(id_exponata) as ceara from exponata where tehnica = 'ceara'")) {
            rs = st.getResultSet();
            rs.next();
            ceara.setText(rs.getString("ceara"));

        }
 if (st.execute("select count(id_exponata) as acuarela from exponata where tehnica = 'acuarela'")) {
            rs = st.getResultSet();
            rs.next();
            acuarela.setText(rs.getString("acuarela"));

        }
 if (st.execute("select count(id_exponata) as guasa from exponata where tehnica = 'guasa'")) {
            rs = st.getResultSet();
            rs.next();
            guasa.setText(rs.getString("guasa"));

        }
 if (st.execute("select count(id_exponata) as vopseaacrilica from exponata where tehnica = 'vopsea acrilica'")) {
            rs = st.getResultSet();
            rs.next();
            vopseaacrilica.setText(rs.getString("vopsea acrilica"));

        }
 if (st.execute("select count(id_exponata) as pastel from exponata where tehnica = 'pastel'")) {
            rs = st.getResultSet();
            rs.next();
            pastel.setText(rs.getString("pastel"));

        }
 if (st.execute("select count(id_exponata) as fresca from exponata where tehnica = 'fresca'")) {
            rs = st.getResultSet();
            rs.next();
            fresca.setText(rs.getString("fresca"));

        }
 if (st.execute("select count(id_exponata) as cerneala from exponata where tehnica = 'cerneala'")) {
            rs = st.getResultSet();
            rs.next();
            cerneala.setText(rs.getString("cerneala"));

        }
 if (st.execute("select count(id_exponata) as tempera from exponata where tehnica = 'tempera'")) {
            rs = st.getResultSet();
            rs.next();
            tempera.setText(rs.getString("tempera"));

        }
 if (st.execute("select count(id_exponata) as grisaille from exponata where tehnica = 'grisaille'")) {
            rs = st.getResultSet();
            rs.next();
            grisaille.setText(rs.getString("grisaille"));

        }
 if (st.execute("select count(id_exponata) as pointilism from exponata where tehnica = 'pointilism'")) {
            rs = st.getResultSet();
            rs.next();
            pointilism.setText(rs.getString("pointilism"));

        }
 if (st.execute("select count(id_exponata) as vitraliu from exponata where tehnica = 'vitraliu'")) {
            rs = st.getResultSet();
            rs.next();
            vitraliu.setText(rs.getString("vitraliu"));

        }
 String uleiE = ulei.getText();
 String cearaE = ceara.getText();
 String acuarelaE = acuarela.getText();
 String guasaE = guasa.getText();
 String vopseaacrilicaE = vopseaacrilica.getText();
 String pastelE = pastel.getText();
 String frescaE = fresca.getText();
 String cernealaE = cerneala.getText();
 String temperaE = tempera.getText();
 String grisailleE = grisaille.getText();
 String pointilismE = pointilism.getText();
 String vitraliuE = vitraliu.getText();

 DefaultPieDataset pieDataset = new DefaultPieDataset();
        pieDataset.setValue("ulei", new Integer(uleiE));
        pieDataset.setValue("ceara", new Integer(cearaE));
        pieDataset.setValue("acuarela", new Integer(acuarelaE));
        pieDataset.setValue("guasa", new Integer(guasaE));
        pieDataset.setValue("vopseaacrilica", new Integer(vopseaacrilicaE));
        pieDataset.setValue("pastel", new Integer(pastelE));
        pieDataset.setValue("fresca", new Integer(frescaE));
        pieDataset.setValue("cerneala", new Integer(cernealaE));
        pieDataset.setValue("tempera", new Integer(temperaE));
        pieDataset.setValue("grisaille", new Integer(grisailleE));
        pieDataset.setValue("pointilism", new Integer(pointilismE));
        pieDataset.setValue("vitraliu", new Integer(vitraliuE));

 JFreeChart chart = ChartFactory.createPieChart("Exponate pe tehnici", pieDataset, true, true, false);
        // Title
        // Dataset
        // Show legend
        // Use tooltips
 try {
            ChartUtilities.saveChartAsJPEG(new File("chart.jpg"), chart, 500, 300);
        } catch (Exception e) {
            System.out.println("Problem occurred creating chart.");
        }

 }

 catch(Exception e)
 {

 }

}                                        

/**
 * @param args the command line arguments
 */
public static void main(String args[]) {




    /* Set the Nimbus look and feel */
    //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
    /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
     * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
     */
    try {
        for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
            if ("Nimbus".equals(info.getName())) {
                javax.swing.UIManager.setLookAndFeel(info.getClassName());
                break;
            }
        }
    } catch (ClassNotFoundException ex) {
        java.util.logging.Logger.getLogger(Rapoarte.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (InstantiationException ex) {
        java.util.logging.Logger.getLogger(Rapoarte.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (IllegalAccessException ex) {
        java.util.logging.Logger.getLogger(Rapoarte.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (javax.swing.UnsupportedLookAndFeelException ex) {
        java.util.logging.Logger.getLogger(Rapoarte.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    }
    //</editor-fold>

    /* Create and display the form */
    java.awt.EventQueue.invokeLater(new Runnable() {
        public void run() {
            new Rapoarte().setVisible(true);
        }
    });
}

// Variables declaration - do not modify                     
private javax.swing.JButton jButton2;
private javax.swing.JButton jButton3;
private javax.swing.JButton jButton4;
private javax.swing.JButton jButton5;
private javax.swing.JLabel jLabel1;
private javax.swing.JPanel jPanel1;
// End of variables declaration                   
}
...