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 Forms; import com.mysql.jdbc.Connection; import com.mysql.jdbc.Statement; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.util.HashMap; import java.util.Locale; import java.util.Map; import java.util.TreeMap; import java.util.logging.Level; import java.util.logging.Logger; import org.jfree.chart.ChartFactory; import org.jfree.chart.ChartFrame; 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; import org.jfree.util.Rotation; /** * * @author Nafis */ public class SalesChart extends javax.swing.JFrame { /** * Creates new form SalesChart */ public SalesChart() { 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() { jButton1 = new javax.swing.JButton(); jButton2 = new javax.swing.JButton(); jSeparator1 = new javax.swing.JSeparator(); jLabel1 = new javax.swing.JLabel(); jLabel2 = new javax.swing.JLabel(); jLabel3 = new javax.swing.JLabel(); jLabel4 = new javax.swing.JLabel(); jLabel5 = new javax.swing.JLabel(); jButton3 = new javax.swing.JButton(); jLabel6 = new javax.swing.JLabel(); setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE); setTitle("Chats"); jButton1.setText("Items Sold Pie Chart"); jButton1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton1ActionPerformed(evt); } }); jButton2.setText("Daily Profit Bar Chart"); jButton2.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton2ActionPerformed(evt); } }); jLabel1.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N jLabel1.setText("All Sold Item In Pie Charts"); jLabel2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Forms/pie.png"))); // NOI18N jLabel3.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N jLabel3.setText("Daily Profit Bar Chart"); jLabel4.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Forms/profit.png"))); // NOI18N jLabel5.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N jLabel5.setText("Daily Items Sold Bar Chart"); jButton3.setText("Daily Items Sold Bar Chart"); jButton3.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton3ActionPerformed(evt); } }); jLabel6.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Forms/Bar-Chart-icon.png"))); // NOI18N javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jSeparator1, javax.swing.GroupLayout.Alignment.TRAILING) .addGroup(layout.createSequentialGroup() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup().addGap(101, 101, 101) .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 129, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(jLabel2)) .addGroup(layout.createSequentialGroup().addGap(49, 49, 49).addComponent(jLabel4) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(jButton2))) .addGap(65, 65, 65)) .addGroup(layout.createSequentialGroup() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addGap(72, 72, 72).addComponent(jButton3)) .addGroup( layout.createSequentialGroup().addGap(122, 122, 122).addComponent(jLabel5))) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 25, Short.MAX_VALUE) .addComponent(jLabel6, javax.swing.GroupLayout.PREFERRED_SIZE, 246, javax.swing.GroupLayout.PREFERRED_SIZE) .addContainerGap()) .addGroup(layout.createSequentialGroup() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addGap(31, 31, 31).addComponent(jLabel1)) .addGroup( layout.createSequentialGroup().addGap(223, 223, 223).addComponent(jLabel3))) .addGap(0, 0, Short.MAX_VALUE))); layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addGap(70, 70, 70) .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED).addComponent(jLabel1) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addGap(36, 36, 36).addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGroup(layout.createSequentialGroup().addGap(3, 3, 3).addComponent(jLabel2))) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addGap(18, 18, 18).addComponent(jLabel3) .addGap(32, 32, 32).addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 122, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGroup(layout.createSequentialGroup() .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(52, 52, 52))) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addGap(30, 30, 30).addComponent(jLabel5) .addGap(87, 87, 87) .addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 39, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(94, 94, 94)) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() .addComponent(jLabel6, javax.swing.GroupLayout.PREFERRED_SIZE, 192, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(32, 32, 32))))); pack(); setLocationRelativeTo(null); }// </editor-fold>//GEN-END:initComponents private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed DefaultPieDataset pieDataset = new DefaultPieDataset(); String conString = "jdbc:mysql://localhost:3306/nafis"; String username = "root"; String passward = ""; String sql = "SELECT * FROM sold"; try { Connection con = (Connection) DriverManager.getConnection(conString, username, passward); Statement s = (Statement) con.prepareStatement(sql); ResultSet rs = s.executeQuery(sql); HashMap<String, Integer> map = new HashMap<String, Integer>(); while (rs.next()) { String name = rs.getString(2); String stock = rs.getString(3); String type = rs.getString(8); Integer oldVal = map.get(type); //System.out.println(oldVal); if (oldVal == null) { map.put(type, Integer.parseInt(stock)); } else { map.put(type, oldVal + Integer.parseInt(stock)); } } for (HashMap.Entry m : map.entrySet()) { //System.out.println(m.getKey()+" "+m.getValue()); pieDataset.setValue(m.getKey() + "", Integer.parseInt(m.getValue() + "")); } } catch (Exception e) { e.printStackTrace(); } JFreeChart chart = ChartFactory.createPieChart3D("pie chart", pieDataset, true, true, false); PiePlot3D p = (PiePlot3D) chart.getPlot(); p.setStartAngle(0); p.setDirection(Rotation.CLOCKWISE); p.setForegroundAlpha(0.5f); p.getBackgroundPaint(); ChartFrame frame = new ChartFrame("Pie Chart", chart); frame.setLocationByPlatform(true); frame.setVisible(true); frame.setSize(750, 600); }//GEN-LAST:event_jButton1ActionPerformed private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed String sql = "SELECT * FROM soldtime WHERE cast(datetime as date)"; DefaultCategoryDataset dataset = new DefaultCategoryDataset(); try { Connection con = (Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/nafis", "root", ""); Statement s = (Statement) con.prepareStatement(sql); ResultSet rs = s.executeQuery(sql); Map<String, Float> m = new TreeMap<String, Float>(); //int quantity=0; //float sp=0; //float bp=0; String qua = "", sprice = "", bprice = "", date = ""; while (rs.next()) { //qua = rs.getString(3); sprice = rs.getString(4); bprice = rs.getString(5); date = rs.getString(6); date = date.substring(0, 10); //quantity=quantity+Integer.parseInt(qua); /*sp = sp + Float.parseFloat(sprice); bp = bp + Float.parseFloat(bprice);*/ Float oldVal = m.get(date); if (oldVal == null) { m.put(date, Float.parseFloat(sprice) - Float.parseFloat(bprice)); } else { m.put(date, oldVal + Float.parseFloat(sprice) - Float.parseFloat(bprice)); } } for (HashMap.Entry ma : m.entrySet()) { //System.out.println(m.getKey()+" "+m.getValue()); dataset.setValue(Float.parseFloat(ma.getValue() + ""), "Profit", ma.getKey() + ""); } } catch (Exception e) { e.printStackTrace(); } /*dataset.setValue(50, "Date","Amount1"); dataset.setValue(25, "Date","Amount2"); dataset.setValue(30, "Date","Amount3"); dataset.setValue(45, "Date","Amount4");*/ JFreeChart chart = ChartFactory.createBarChart3D("PROFIT BARCHART", "DATE", "PROFIT", dataset, PlotOrientation.VERTICAL, false, true, false); CategoryPlot p = chart.getCategoryPlot(); p.setForegroundAlpha(0.5f); ChartFrame frame = new ChartFrame("Bar Chart", chart); frame.pack(); frame.setLocationRelativeTo(null); frame.setVisible(true); frame.setSize(750, 600); }//GEN-LAST:event_jButton2ActionPerformed private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed String sql = "SELECT * FROM soldtime WHERE cast(datetime as date)"; DefaultCategoryDataset dataset = new DefaultCategoryDataset(); try { Connection con = (Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/nafis", "root", ""); Statement s = (Statement) con.prepareStatement(sql); ResultSet rs = s.executeQuery(sql); Map<String, Integer> m = new TreeMap<String, Integer>(); int quantity = 0; //float sp=0; //float bp=0; String qua = "", sprice = "", bprice = "", date = ""; while (rs.next()) { qua = rs.getString(3); /*sprice = rs.getString(4); bprice = rs.getString(5);*/ date = rs.getString(6); date = date.substring(0, 10); //quantity=quantity+Integer.parseInt(qua); /*sp = sp + Float.parseFloat(sprice); bp = bp + Float.parseFloat(bprice);*/ Integer oldVal = m.get(date); if (oldVal == null) { m.put(date, Integer.parseInt(qua)); } else { m.put(date, oldVal + Integer.parseInt(qua)); } } for (HashMap.Entry ma : m.entrySet()) { //System.out.println(m.getKey()+" "+m.getValue()); dataset.setValue(Float.parseFloat(ma.getValue() + ""), "Items Sold", ma.getKey() + ""); } } catch (Exception e) { e.printStackTrace(); } /*dataset.setValue(50, "Date","Amount1"); dataset.setValue(25, "Date","Amount2"); dataset.setValue(30, "Date","Amount3"); dataset.setValue(45, "Date","Amount4");*/ JFreeChart chart = ChartFactory.createBarChart3D("SOLD BARCHART", "DATE", "PROFIT", dataset, PlotOrientation.VERTICAL, false, true, false); CategoryPlot p = chart.getCategoryPlot(); p.setForegroundAlpha(0.5f); ChartFrame frame = new ChartFrame("Bar Chart", chart); frame.pack(); frame.setLocationRelativeTo(null); frame.setVisible(true); frame.setSize(750, 600); }//GEN-LAST:event_jButton3ActionPerformed /** * @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(SalesChart.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (InstantiationException ex) { java.util.logging.Logger.getLogger(SalesChart.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (IllegalAccessException ex) { java.util.logging.Logger.getLogger(SalesChart.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (javax.swing.UnsupportedLookAndFeelException ex) { java.util.logging.Logger.getLogger(SalesChart.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 SalesChart().setVisible(true); } }); } // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JButton jButton1; private javax.swing.JButton jButton2; private javax.swing.JButton jButton3; private javax.swing.JLabel jLabel1; private javax.swing.JLabel jLabel2; private javax.swing.JLabel jLabel3; private javax.swing.JLabel jLabel4; private javax.swing.JLabel jLabel5; private javax.swing.JLabel jLabel6; private javax.swing.JSeparator jSeparator1; // End of variables declaration//GEN-END:variables }