edu.esprit.charts.JfreeChart.java Source code

Java tutorial

Introduction

Here is the source code for edu.esprit.charts.JfreeChart.java

Source

/*
 * 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.
 */
package edu.esprit.charts;

import edu.esprit.DAO.classes.ClientDAO;
import java.awt.BorderLayout;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.category.DefaultCategoryDataset;

import edu.esprit.DAO.classes.ReservationDAO;
import edu.esprit.entities.Client;
import edu.esprit.entities.Reservation;
import java.util.List;
import org.jfree.data.general.PieDataset;

/**
 *
 * @author samar
 */
public class JfreeChart extends javax.swing.JFrame {

    /**
     * Creates new form JfreeChart
     */
    public JfreeChart() {
        initComponents();
    }

    /**
     * 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">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        Client = new javax.swing.JLabel();
        t2 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowOpened(java.awt.event.WindowEvent evt) {
                formWindowOpened(evt);
            }
        });

        jPanel1.setBackground(new java.awt.Color(204, 204, 255));
        jPanel1.setLayout(new java.awt.BorderLayout());

        Client.setText("Responsable");

        t2.setText("Client");

        jButton1.setText("CHarger");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup().addGroup(layout
                        .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup().addGap(63, 63, 63).addComponent(Client)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addGroup(layout.createSequentialGroup().addGap(141, 141, 141)
                                                .addComponent(jButton1))
                                        .addGroup(layout.createSequentialGroup().addGap(58, 58, 58)
                                                .addComponent(t2))))
                        .addGroup(layout.createSequentialGroup().addContainerGap().addComponent(jPanel1,
                                javax.swing.GroupLayout.PREFERRED_SIZE, 340,
                                javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addContainerGap(283, Short.MAX_VALUE)));
        layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup().addContainerGap()
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 191,
                                javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(Client).addComponent(t2, javax.swing.GroupLayout.PREFERRED_SIZE, 14,
                                        javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED).addComponent(jButton1)
                        .addContainerGap(270, Short.MAX_VALUE)));

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:

        DefaultCategoryDataset dataset = new DefaultCategoryDataset();
        int i = 0;
        int j = 0;
        List<Reservation> listReservation = ReservationDAO.getInstance().DisplayAllReservation();
        for (Reservation listReservation1 : listReservation) {
            i++;
        }
        List<Client> listClient = ClientDAO.getInstance().DisplayAllClients();
        for (Client listClient1 : listClient) {
            j++;
        }
        try {
            dataset.setValue(i, "", "t1");
            dataset.setValue(j, "", "t2");

            JFreeChart chart = ChartFactory.createBarChart("Statistiques", "", "", dataset,
                    PlotOrientation.HORIZONTAL, false, false, false);
            CategoryPlot catPlot = chart.getCategoryPlot();
            ChartPanel ChartPanel = new ChartPanel(chart);
            jPanel1.removeAll();
            jPanel1.add(ChartPanel, BorderLayout.CENTER);
            jPanel1.validate();
        } catch (Exception e) {
            System.out.println(i);
        }

    }//GEN-LAST:event_jButton1ActionPerformed

    private void formWindowOpened(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowOpened
        DefaultCategoryDataset dataset = new DefaultCategoryDataset();
        int i = 0;
        int j = 0;
        List<Reservation> listReservation = ReservationDAO.getInstance().DisplayAllReservation();
        for (Reservation listReservation1 : listReservation) {
            i++;
        }
        List<Client> listClient = ClientDAO.getInstance().DisplayAllClients();
        for (Client listClient1 : listClient) {
            j++;
        }
        try {
            dataset.setValue(i, "", "t1");
            dataset.setValue(j, "", "t2");

            JFreeChart chart = ChartFactory.createBarChart("Statistiques", "", "", dataset,
                    PlotOrientation.VERTICAL, false, false, false);
            // JFreeChart chart = ChartFactory.createPieChart3D("Liste des fazet", (PieDataset) dataset);
            CategoryPlot catPlot = chart.getCategoryPlot();
            ChartPanel ChartPanel = new ChartPanel(chart);
            jPanel1.removeAll();
            jPanel1.add(ChartPanel, BorderLayout.CENTER);
            jPanel1.validate();
        } catch (Exception e) {
            System.out.println(i);
        }
    }//GEN-LAST:event_formWindowOpened

    /**
     * @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(JfreeChart.class.getName()).log(java.util.logging.Level.SEVERE, null,
                    ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(JfreeChart.class.getName()).log(java.util.logging.Level.SEVERE, null,
                    ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(JfreeChart.class.getName()).log(java.util.logging.Level.SEVERE, null,
                    ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(JfreeChart.class.getName()).log(java.util.logging.Level.SEVERE, null,
                    ex);
        }
        //</editor-fold>
        //</editor-fold>

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

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel Client;
    private javax.swing.JButton jButton1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JLabel t2;
    // End of variables declaration//GEN-END:variables
}