Java tutorial
/* * 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 pidev.presentation; import java.awt.Color; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.URL; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.HashMap; import java.util.Map; import javax.swing.ImageIcon; import javax.swing.JFrame; import javax.swing.JLabel; import org.jfree.chart.ChartFactory; import org.jfree.chart.ChartFrame; import org.jfree.chart.JFreeChart; import org.jfree.chart.plot.PiePlot3D; import org.jfree.data.general.DefaultPieDataset; import org.jfree.util.Rotation; import pidev.util.MyConnection; /** * * @author unknown */ public class Statistiques extends javax.swing.JFrame { public int nombredannonce = 0; public int nombredagence = 0; Map<String, Integer> map = new HashMap<String, Integer>(); public int nombreclient = 0; /** * Creates new form Statistiques */ public Statistiques() { initComponents(); this.getContentPane().setBackground(new Color(52, 152, 219)); this.setLocationRelativeTo(null); this.pack(); this.setSize(600, 400); } /** * 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() { jToggleButton1 = new javax.swing.JToggleButton(); jButton1 = new javax.swing.JButton(); setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE); jToggleButton1.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N jToggleButton1.setText("Nombre de Clients/Nombre d'Agences"); jToggleButton1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jToggleButton1ActionPerformed(evt); } }); jButton1.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N jButton1.setText("Nombre d'annonces / ville"); 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().addGap(172, 172, 172) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) .addComponent(jToggleButton1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) .addContainerGap(129, Short.MAX_VALUE))); layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addGap(54, 54, 54).addComponent(jToggleButton1) .addGap(38, 38, 38).addComponent(jButton1).addContainerGap(193, Short.MAX_VALUE))); pack(); }// </editor-fold>//GEN-END:initComponents private void jToggleButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jToggleButton1ActionPerformed String requete = "select Count(id_Agence) from Agence "; System.out.println(requete); try { Statement statement = MyConnection.getInstance().createStatement(); ResultSet resultat = statement.executeQuery(requete); while (resultat.next()) { nombredagence = resultat.getInt(1); } } catch (SQLException ex) { //Logger.getLogger(PersonneDao.class.getName()).log(Level.SEVERE, null, ex); System.out.println("erreur lors du chargement des annonces " + ex.getMessage()); } String requete2 = "select Count(id_Client) from Client "; System.out.println(requete2); try { Statement statement = MyConnection.getInstance().createStatement(); ResultSet resultat = statement.executeQuery(requete2); while (resultat.next()) { nombreclient = resultat.getInt(1); } } catch (SQLException ex) { //Logger.getLogger(PersonneDao.class.getName()).log(Level.SEVERE, null, ex); System.out.println("erreur lors du chargement des annonces " + ex.getMessage()); } DefaultPieDataset result = new DefaultPieDataset(); result.setValue("Agences", nombredagence); result.setValue("Clients", nombreclient); JFreeChart chart = ChartFactory.createPieChart3D("nombre d'agences + nombre de clients", result, true, true, false); PiePlot3D plot = (PiePlot3D) chart.getPlot(); plot.setStartAngle(290); plot.setDirection(Rotation.CLOCKWISE); plot.setForegroundAlpha(0.5f); ChartFrame ch = new ChartFrame("Statistiques", chart); // ChartPanel ch = new ChartPanel(chart); ch.setSize(300, 300); ch.setLocationRelativeTo(null); ch.pack(); ch.setVisible(true); }//GEN-LAST:event_jToggleButton1ActionPerformed private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed // TODO add your handling code here: String requete = "select Count(id_Annonce),destination from helpers.Annonce group by (destination) "; System.out.println(requete); try { Statement statement = MyConnection.getInstance().createStatement(); ResultSet resultat = statement.executeQuery(requete); while (resultat.next()) { map.put(resultat.getString(2), resultat.getInt(1)); } } catch (SQLException ex) { //Logger.getLogger(PersonneDao.class.getName()).log(Level.SEVERE, null, ex); System.out.println("erreur lors du chargement des annonces " + ex.getMessage()); } DefaultPieDataset result = new DefaultPieDataset(); for (Map.Entry<String, Integer> entry : map.entrySet()) { String string = entry.getKey(); Integer integer = entry.getValue(); result.setValue(string, integer); } JFreeChart chart = ChartFactory.createPieChart3D("nombres d'annonces par ville", result, true, true, false); PiePlot3D plot = (PiePlot3D) chart.getPlot(); plot.setStartAngle(290); plot.setDirection(Rotation.CLOCKWISE); plot.setForegroundAlpha(0.5f); ChartFrame ch = new ChartFrame("Statistiques", chart); // ChartPanel ch = new ChartPanel(chart); ch.setSize(400, 400); ch.setLocationRelativeTo(null); ch.pack(); ch.setVisible(true); // JFrame frame = new JFrame(); //map2.addCursor("red", ".",3.876772f,43.613087f); //frame.add(map2); //frame.setSize(400, 420); //frame.setLocation(200, 200); //map2.setVisible(true); //frame.setVisible(true); // new GMaps(3.876772f,43.613087f).setVisible(true); //jButton2.setVisible(true); // this.setContentPane(ch); }//GEN-LAST:event_jButton1ActionPerformed /** * @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 | InstantiationException | IllegalAccessException | javax.swing.UnsupportedLookAndFeelException ex) { java.util.logging.Logger.getLogger(Statistiques.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } //</editor-fold> /* Create and display the form */ java.awt.EventQueue.invokeLater(new Runnable() { @Override public void run() { new Statistiques().setVisible(true); } }); } // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JButton jButton1; private javax.swing.JToggleButton jToggleButton1; // End of variables declaration//GEN-END:variables }