GUI.Statistique.java Source code

Java tutorial

Introduction

Here is the source code for GUI.Statistique.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 GUI;

import DAO.CommandeDAO;
import DAO.ProduitDAO;
import Entity.Commande;
import Entity.Produit;
import java.awt.Color;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.List;
import javax.swing.JFrame;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartFrame;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PiePlot3D;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.data.general.DefaultPieDataset;

/**
 *
 * @author gafsi
 */
public class Statistique extends javax.swing.JFrame {

    /**
     * Creates new form Statistique
     */
    public Statistique() {
        initComponents();
        calculerBudget();
        bilanFinanciereParMois();
        bilanFinanciereParAns();
    }

    /**
     * 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() {

        container = new javax.swing.JTabbedPane();
        jpBudjet = new javax.swing.JPanel();
        jpProduit = new javax.swing.JPanel();
        jPanel3 = new javax.swing.JPanel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        javax.swing.GroupLayout jpBudjetLayout = new javax.swing.GroupLayout(jpBudjet);
        jpBudjet.setLayout(jpBudjetLayout);
        jpBudjetLayout.setHorizontalGroup(jpBudjetLayout
                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGap(0, 776, Short.MAX_VALUE));
        jpBudjetLayout.setVerticalGroup(jpBudjetLayout
                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGap(0, 491, Short.MAX_VALUE));

        container.addTab("budjet", jpBudjet);

        javax.swing.GroupLayout jpProduitLayout = new javax.swing.GroupLayout(jpProduit);
        jpProduit.setLayout(jpProduitLayout);
        jpProduitLayout.setHorizontalGroup(jpProduitLayout
                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGap(0, 776, Short.MAX_VALUE));
        jpProduitLayout.setVerticalGroup(jpProduitLayout
                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGap(0, 491, Short.MAX_VALUE));

        container.addTab("tab2", jpProduit);

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(jPanel3Layout
                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGap(0, 776, Short.MAX_VALUE));
        jPanel3Layout.setVerticalGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGap(0, 491, Short.MAX_VALUE));

        container.addTab("tab3", jPanel3);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addComponent(container));
        layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addComponent(container));

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

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

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTabbedPane container;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jpBudjet;
    private javax.swing.JPanel jpProduit;

    // End of variables declaration//GEN-END:variables
    private ChartPanel calculerBudget() {
        ProduitDAO produitDAO = new ProduitDAO();
        List<Produit> produits = new ArrayList<>();
        produits = produitDAO.findAll();
        CommandeDAO commandeDAO = new CommandeDAO();
        List<Commande> commandes = new ArrayList<>();
        commandes = commandeDAO.findAll();
        DefaultPieDataset dSet = new DefaultPieDataset();
        for (Produit produit : produits) {
            dSet.setValue(produit.getNom(), produit.getNbvente());
            System.out.println(produit.getNbvente());
        }
        JFreeChart chart = ChartFactory.createPieChart3D("liste des produits les plus vendus", dSet, true, true,
                true);
        chart.setBackgroundPaint(Color.yellow);
        chart.getTitle().setPaint(Color.RED);
        PiePlot3D p = (PiePlot3D) chart.getPlot();
        ChartPanel cp = new ChartPanel(chart, true, true, true, true, true);
        JFrame f = new JFrame();
        f.setContentPane(cp);
        f.pack();
        jpBudjet.add(cp);
        return cp;
    }

    // le bilan financiere par mois
    private void bilanFinanciereParMois() {
        float janvier = 0;
        float fevrier = 0;
        float mars = 0;
        float avril = 0;
        float mai = 0;
        float juin = 0;
        float juillet = 0;
        float aout = 0;
        float septembre = 0;
        float octobre = 0;
        float novembre = 0;
        float decembre = 0;
        CommandeDAO commandeDAO = new CommandeDAO();
        List<Commande> commandes = new ArrayList<>();
        commandes = commandeDAO.findAll();
        for (Commande commande : commandes) {

            if (commande.getDatecommande().getYear() + 1900 == 2016) {
                switch (commande.getDatecommande().getMonth()) {
                case 0:
                    janvier += commande.getTotalcommande();
                    break;
                case 1:
                    fevrier += commande.getTotalcommande();
                    break;
                case 2:
                    mars += commande.getTotalcommande();
                    break;
                case 3:
                    avril += commande.getTotalcommande();
                    break;
                case 4:
                    mai += commande.getTotalcommande();
                    break;
                case 5:
                    juin += commande.getTotalcommande();
                    break;
                case 6:
                    juillet += commande.getTotalcommande();
                    break;
                case 7:
                    aout += commande.getTotalcommande();
                    break;
                case 8:
                    septembre += commande.getTotalcommande();
                    break;
                case 9:
                    novembre += commande.getTotalcommande();
                    break;
                case 10:
                    octobre += commande.getTotalcommande();
                    break;
                case 11:
                    decembre += commande.getTotalcommande();
                    break;
                }
            }
        }
        DefaultCategoryDataset dSet = new DefaultCategoryDataset();
        dSet.addValue(janvier, "Janvier", "Janvier");
        dSet.addValue(fevrier, "Fevrier", "Fevrier");
        dSet.addValue(mars, "Mars", "Mars");
        dSet.addValue(avril, "Avril", "Avril");
        dSet.addValue(mai, "Mai", "Mai");
        dSet.addValue(juin, "Juin", "Juin");
        dSet.addValue(juillet, "Juillet", "Juillet");
        dSet.addValue(aout, "Aout", "Aout");
        dSet.addValue(septembre, "Septembre", "Janvier");
        dSet.addValue(octobre, "Octobre", "Fevrier");
        dSet.addValue(novembre, "Novemvre", "Novembre");
        dSet.addValue(decembre, "Decembre", "Decembre");
        JFreeChart chart = ChartFactory.createBarChart3D("Rapport financier", "Rvenus", "Montant (DT)", dSet,
                PlotOrientation.VERTICAL, true, true, true);
        chart.setBackgroundPaint(Color.yellow);
        chart.getTitle().setPaint(Color.RED);
        CategoryPlot p = chart.getCategoryPlot();
        p.setRangeGridlinePaint(Color.black);
        ChartPanel cp = new ChartPanel(chart, true, true, true, false, true);
        ChartFrame f = new ChartFrame("try", chart);
        //JFrame f =new JFrame();
        f.setContentPane(cp);
        f.pack();
        //f.setVisible(true);
        // f.setSize(panelRevDep.getWidth()-20,panelRevDep.getHeight()-20);
        // f.setVisible(true);
        // panelRevDep.removeAll();
        //panelRevDep.repaint();
        jpProduit.add(cp);

    }

    // le bilan financiere pour les 5 dernieres annes
    private void bilanFinanciereParAns() {
        float firstYear = 0;
        float secondYear = 0;
        float thirdYear = 0;
        float forthYear = 0;
        float fiveYear = 0;
        CommandeDAO commandeDAO = new CommandeDAO();
        List<Commande> commandes = new ArrayList<>();
        commandes = commandeDAO.findAll();
        for (Commande commande : commandes) {

            if (commande.getDatecommande().getYear() + 1900 == 2016)
                fiveYear += commande.getTotalcommande();
            else if (commande.getDatecommande().getYear() + 1900 == 2015)
                forthYear += commande.getTotalcommande();
            else if (commande.getDatecommande().getYear() + 1900 == 2014)
                thirdYear += commande.getTotalcommande();
            else if (commande.getDatecommande().getYear() + 1900 == 2013)
                secondYear += commande.getTotalcommande();
            else if (commande.getDatecommande().getYear() + 1900 == 2012)
                firstYear += commande.getTotalcommande();
        }
        DefaultCategoryDataset dSet = new DefaultCategoryDataset();
        dSet.addValue(firstYear, "2012", "2012");
        dSet.addValue(secondYear, "2013", "2013");
        dSet.addValue(thirdYear, "2014", "2014");
        dSet.addValue(forthYear, "2015", "2015");
        dSet.addValue(fiveYear, "2016", "2016");

        JFreeChart chart = ChartFactory.createBarChart3D("Rapport financier", "Rvenus", "Montant (DT)", dSet,
                PlotOrientation.VERTICAL, true, true, true);
        chart.setBackgroundPaint(Color.yellow);
        chart.getTitle().setPaint(Color.RED);
        CategoryPlot p = chart.getCategoryPlot();
        p.setRangeGridlinePaint(Color.black);
        ChartPanel cp = new ChartPanel(chart, true, true, true, false, true);
        ChartFrame f = new ChartFrame("try", chart);
        //JFrame f =new JFrame();
        f.setContentPane(cp);
        f.pack();
        //f.setVisible(true);
        // f.setSize(panelRevDep.getWidth()-20,panelRevDep.getHeight()-20);
        // f.setVisible(true);
        // panelRevDep.removeAll();
        //panelRevDep.repaint();
        jPanel3.add(cp);

    }

}