Java tutorial
/* * Copyright (C) 2014 Dendi Pradigta * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ package com.pusksesmas.form_statistik; import com.mysql.jdbc.Connection; import com.orsoncharts.Colors; import com.pusksesmas.controller.koneksi; import java.awt.Color; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.Statement; import java.text.DecimalFormat; import javax.swing.table.DefaultTableModel; import org.jfree.chart.ChartFactory; import org.jfree.chart.ChartFrame; import org.jfree.chart.JFreeChart; import org.jfree.chart.labels.ItemLabelAnchor; import org.jfree.chart.labels.ItemLabelPosition; import org.jfree.chart.labels.StandardCategoryItemLabelGenerator; import org.jfree.chart.plot.CategoryPlot; import org.jfree.chart.plot.PiePlot; import org.jfree.chart.plot.PlotOrientation; import org.jfree.chart.renderer.category.BarRenderer; import org.jfree.data.category.CategoryDataset; import org.jfree.data.category.DefaultCategoryDataset; import org.jfree.data.general.DefaultPieDataset; import org.jfree.ui.TextAnchor; /** * * @author Dendi Pradigta */ public class statistik_obat_masuk extends javax.swing.JPanel { Connection kon = null; Statement stmt; ResultSet rsdata; PreparedStatement ps; String status; DefaultTableModel tabModel; Object[] judul_kolom = { "Nama Obat", "Januari", "Februari", "Maret", "April", "Mei", "Juni", "Juli", "Agustus", "September", "Oktober", "November", "Desember" }; /** * Creates new form penyakitBerdasarkanUmur */ public statistik_obat_masuk() { initComponents(); tampilData(); } /** * 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(); jScrollPane1 = new javax.swing.JScrollPane(); tabelData = new javax.swing.JTable(); vpil = new javax.swing.JComboBox(); jLabel1 = new javax.swing.JLabel(); jLabel7 = new javax.swing.JLabel(); jPanel3 = new javax.swing.JPanel(); jLabel9 = new javax.swing.JLabel(); jPanel1.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0))); tabelData .setModel(new javax.swing.table.DefaultTableModel( new Object[][] { { null, null, null, null }, { null, null, null, null }, { null, null, null, null }, { null, null, null, null } }, new String[] { "Title 1", "Title 2", "Title 3", "Title 4" })); tabelData.addMouseListener(new java.awt.event.MouseAdapter() { public void mouseClicked(java.awt.event.MouseEvent evt) { tabelDataMouseClicked(evt); } }); jScrollPane1.setViewportView(tabelData); vpil.setModel(new javax.swing.DefaultComboBoxModel( new String[] { "Pilih", "Grafik Batang", "Grafik Lingkaran " })); vpil.addItemListener(new java.awt.event.ItemListener() { public void itemStateChanged(java.awt.event.ItemEvent evt) { vpilItemStateChanged(evt); } }); jLabel1.setText("Jenis Tampilan Grafik"); javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1); jPanel1.setLayout(jPanel1Layout); jPanel1Layout.setHorizontalGroup(jPanel1Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel1Layout.createSequentialGroup().addContainerGap().addGroup(jPanel1Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addComponent(jScrollPane1) .addGroup(jPanel1Layout.createSequentialGroup().addComponent(jLabel1).addGap(18, 18, 18) .addComponent(vpil, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(0, 0, Short.MAX_VALUE))) .addContainerGap())); jPanel1Layout.setVerticalGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel1Layout.createSequentialGroup().addContainerGap() .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel1).addComponent(vpil, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGap(19, 19, 19) .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 250, Short.MAX_VALUE) .addContainerGap())); jLabel7.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N jLabel7.setHorizontalAlignment(javax.swing.SwingConstants.CENTER); jLabel7.setText("STATISTIK PENERIMAAN OBAT"); jLabel7.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0))); jPanel3.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.LOWERED)); jPanel3.setOpaque(false); jLabel9.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/header (Mobile).png"))); // NOI18N jPanel3.add(jLabel9); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this); this.setLayout(layout); layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addContainerGap() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(jLabel7, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(jPanel3, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 952, Short.MAX_VALUE)) .addContainerGap())); layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup( javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup().addContainerGap() .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED).addComponent(jLabel7) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addContainerGap())); }// </editor-fold>//GEN-END:initComponents private void tabelDataMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tabelDataMouseClicked if (vpil.getSelectedIndex() == 1) { int row = tabelData.getSelectedRow(); String namaObat = (tabelData.getModel().getValueAt(row, 0)).toString(); String jan = (tabelData.getModel().getValueAt(row, 1)).toString(); String feb = (tabelData.getModel().getValueAt(row, 2)).toString(); String mar = (tabelData.getModel().getValueAt(row, 3)).toString(); String apr = (tabelData.getModel().getValueAt(row, 4)).toString(); String mei = (tabelData.getModel().getValueAt(row, 5)).toString(); String jun = (tabelData.getModel().getValueAt(row, 6)).toString(); String jul = (tabelData.getModel().getValueAt(row, 7)).toString(); String agus = (tabelData.getModel().getValueAt(row, 8)).toString(); String sept = (tabelData.getModel().getValueAt(row, 9)).toString(); String okt = (tabelData.getModel().getValueAt(row, 10)).toString(); String nov = (tabelData.getModel().getValueAt(row, 11)).toString(); String des = (tabelData.getModel().getValueAt(row, 12)).toString(); DefaultCategoryDataset pieDataset = new DefaultCategoryDataset(); // pieDataset.setValue("Diagnosa", new Integer(jan)); pieDataset.setValue(new Integer(jan), "", "Januari"); pieDataset.setValue(new Integer(feb), "", "Februari"); pieDataset.setValue(new Integer(mar), "", "Maret"); pieDataset.setValue(new Integer(apr), "", "April"); pieDataset.setValue(new Integer(mei), "", "Mei"); pieDataset.setValue(new Integer(jun), "", "Juni"); pieDataset.setValue(new Integer(jul), "", "Juli"); pieDataset.setValue(new Integer(agus), "", "Agustus"); pieDataset.setValue(new Integer(sept), "", "September"); pieDataset.setValue(new Integer(okt), "", "Oktober"); pieDataset.setValue(new Integer(nov), "", "November"); pieDataset.setValue(new Integer(des), "", "Desember"); JFreeChart chart = ChartFactory.createBarChart3D("STATISTIK PENERIMAAN OBAT\nNAMA OBAT: " + namaObat, "BULAN", "JUMLAH", (CategoryDataset) pieDataset, PlotOrientation.VERTICAL, false, true, false); chart.setBackgroundPaint(Color.yellow); chart.getTitle().setPaint(Color.red); final CategoryPlot p = chart.getCategoryPlot(); BarRenderer renderer = (BarRenderer) p.getRenderer(); DecimalFormat sdf = new DecimalFormat("#,##0"); renderer.setItemLabelGenerator(new StandardCategoryItemLabelGenerator("{2}", sdf)); p.setRenderer(renderer); renderer.setBasePositiveItemLabelPosition( new ItemLabelPosition(ItemLabelAnchor.OUTSIDE12, TextAnchor.TOP_CENTER)); renderer.setItemLabelsVisible(true); chart.getCategoryPlot().setRenderer(renderer); p.setRangeGridlinePaint(Color.blue); ChartFrame frame = new ChartFrame("barchart", chart); frame.setVisible(true); frame.setLocation(250, 100); frame.setSize(800, 600); } else if (vpil.getSelectedIndex() == 2) { int row = tabelData.getSelectedRow(); String jan = (tabelData.getModel().getValueAt(row, 1)).toString(); String feb = (tabelData.getModel().getValueAt(row, 2)).toString(); String mar = (tabelData.getModel().getValueAt(row, 3)).toString(); String apr = (tabelData.getModel().getValueAt(row, 4)).toString(); String mei = (tabelData.getModel().getValueAt(row, 5)).toString(); String jun = (tabelData.getModel().getValueAt(row, 6)).toString(); String jul = (tabelData.getModel().getValueAt(row, 7)).toString(); String agus = (tabelData.getModel().getValueAt(row, 8)).toString(); String sept = (tabelData.getModel().getValueAt(row, 9)).toString(); String okt = (tabelData.getModel().getValueAt(row, 10)).toString(); String nov = (tabelData.getModel().getValueAt(row, 11)).toString(); String des = (tabelData.getModel().getValueAt(row, 12)).toString(); DefaultPieDataset pieDataset = new DefaultPieDataset(); pieDataset.setValue("Januari", new Integer(jan)); pieDataset.setValue("Februari", new Integer(feb)); pieDataset.setValue("Maret", new Integer(mar)); pieDataset.setValue("April", new Integer(apr)); pieDataset.setValue("Mei", new Integer(mei)); pieDataset.setValue("Juni", new Integer(jun)); pieDataset.setValue("Juli", new Integer(jul)); pieDataset.setValue("Agustus", new Integer(agus)); pieDataset.setValue("September", new Integer(sept)); pieDataset.setValue("Oktober", new Integer(okt)); pieDataset.setValue("November", new Integer(nov)); pieDataset.setValue("Desember", new Integer(des)); JFreeChart chart = ChartFactory.createPieChart("STATISTIK PENERIMAAN OBAT", pieDataset, true, true, true); // PiePlot3D P = (PiePlot3D)chart.getPlot(); PiePlot P = (PiePlot) chart.getPlot(); //P.setForegroundAlpha(TOP_ALIGNMENT); ChartFrame frame = new ChartFrame("STATISTIK PENERIMAAN OBAT", chart); frame.setVisible(true); frame.setLocation(250, 100); frame.setSize(800, 600); } else { } }//GEN-LAST:event_tabelDataMouseClicked private void vpilItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_vpilItemStateChanged }//GEN-LAST:event_vpilItemStateChanged // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JLabel jLabel1; private javax.swing.JLabel jLabel7; private javax.swing.JLabel jLabel9; private javax.swing.JPanel jPanel1; private javax.swing.JPanel jPanel3; private javax.swing.JScrollPane jScrollPane1; private javax.swing.JTable tabelData; private javax.swing.JComboBox vpil; // End of variables declaration//GEN-END:variables void tampilData() { tabModel = new DefaultTableModel(null, judul_kolom); tabelData.setModel(tabModel); tabelData.setAutoResizeMode(5); try { kon = new koneksi().getCon(); String sql = "select * from view_rekap_statistik_obat_masuk "; PreparedStatement pst = kon.prepareStatement(sql); rsdata = pst.executeQuery(); while (rsdata.next()) { Object[] data = { rsdata.getString("namaObat"), rsdata.getString("Januari"), rsdata.getString("Februari"), rsdata.getString("Maret"), rsdata.getString("April"), rsdata.getString("Mei"), rsdata.getString("Juni"), rsdata.getString("Juli"), rsdata.getString("Agustus"), rsdata.getString("September"), rsdata.getString("Oktober"), rsdata.getString("November"), rsdata.getString("Desember") }; tabModel.addRow(data); } kon.close(); } catch (Exception e) { e.printStackTrace(); } } }