Aplikasi Database dengan report Grafik - NetBeans
Sesuai judul ini , dalam melengkapi tugas UAS saya, kali ini saya akan membuat aplikasi grafik dengan berbasik report berbnetuk Grafik, aplikasi ini saya buat dengan menggunakan Software NerBeans,
langkah pertama
langkah pertama
Mkholid_HOME
package mkholid_2015140810;
import com.sun.glass.events.WindowEvent;
import java.awt.event.WindowAdapter; import
javax.swing.JFrame;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartFrame; import
org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.category.DefaultCategoryDataset;
public class Mkholid_Home extends javax.swing.JFrame {
public
Mkholid_Home() {
initComponents();
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed"
desc="Generated Code">
private void initComponents() {
jLabel2 =
new javax.swing.JLabel();
jMenuBar1 = new javax.swing.JMenuBar();
jMenu1 = new javax.swing.JMenu();
jMenuItem1 = new javax.swing.JMenuItem();
jMenuItem2 = new javax.swing.JMenuItem();
jMenu2 = new javax.swing.JMenu();
jMenuItem3 = new javax.swing.JMenuItem();
jMenu3 = new javax.swing.JMenu();
jMenuItem4 = new javax.swing.JMenuItem();
jMenu4 = new javax.swing.JMenu();
jMenuItem5 = new javax.swing.JMenuItem();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); setTitle("Created by Muhamad
kholid");
jLabel2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/mkholid_2015140810/10.jpg")));
// NOI18N
jMenu1.setText("Minggu 1");
jMenuItem1.setText("Bar Chart");
jMenuItem1.addActionListener(new
java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent
evt) {
jMenuItem1ActionPerformed(evt);
}
});
jMenu1.add(jMenuItem1);
jMenuItem2.setText("Pie Chart");
jMenuItem2.addActionListener(new
java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent
evt) {
jMenuItem2ActionPerformed(evt);
}
});
jMenu1.add(jMenuItem2);
jMenuBar1.add(jMenu1);
jMenu2.setText("Minggu 2");
jMenuItem3.setText("Line Chart");
jMenuItem3.addActionListener(new
java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent
evt) {
jMenuItem3ActionPerformed(evt);
}
});
jMenu2.add(jMenuItem3);
jMenuBar1.add(jMenu2);
jMenu3.setText("Minggu 3");
jMenuItem4.setText("Input Nilai Grafik");
jMenuItem4.addActionListener(new
java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent
evt) {
jMenuItem4ActionPerformed(evt);
}
});
jMenu3.add(jMenuItem4);
jMenuBar1.add(jMenu3);
jMenu4.setText("UTS");
jMenuItem5.setText("Grafik Penyebaran
COVID-19");
jMenuItem5.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent
evt) {
jMenuItem5ActionPerformed(evt); } }); jMenu4.add(jMenuItem5);
jMenuBar1.add(jMenu4);
setJMenuBar(jMenuBar1);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jLabel2)
);
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jLabel2)
);
pack();
}// </editor-fold>
private void
jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {
DefaultCategoryDataset mkholid = new DefaultCategoryDataset(); mkholid.setValue(30.100,
"Pamulang","Google Chroome");//Value mkholid.setValue(50.100,
"Bintaro","Google Chroome"); mkholid.setValue(40.100,
"Serpong","Google Chroome"); mkholid.setValue(70.100,
"Pamulang","Opera");//Value
mkholid.setValue(60.100,
"Bintaro","Opera");
mkholid.setValue(80.100,
"Serpong","Opera");
mkholid.setValue(50.100,
"Pamulang","Mozila Firefox");//Value mkholid.setValue(90.100,
"Bintaro","Mozila Firefox"); mkholid.setValue(15.100, "Serpong","Mozila
Firefox");
FreeChart freeChart =
ChartFactory.createBarChart("Data Pengguna browser di berbagai
daerah","Aplikasi","Data pengguna Sistem Operasi (%)",
mkholid, PlotOrientation.VERTICAL,true, true,true); //String arg0,String arg1,String
arg2,Category Datasheet,Plot Orientation,boolean arg4,boolean arg5,boolean
arg6 ChartFrame cf = new
ChartFrame("Muhamad kholid",freeChart); cf.setSize(700,500);
cf.setVisible(true);
cf.setLocationRelativeTo(null); }
private void
jMenuItem2ActionPerformed(java.awt.event.ActionEvent evt) {
JavaPieChart mkholid1 = new JavaPieChart();
mkholid1.setVisible(true);
dispose();
}
private void
jMenuItem3ActionPerformed(java.awt.event.ActionEvent evt) {
JFrame frame = new JFrame("Line Charts");
frame.admkhndowFocusListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
//tambahkan
panel LineCharts canvas = new
LineCharts();
frame.getContentPane().add(canvas);
frame.pack();
frame.setVisible(true);
}
private void
jMenuItem4ActionPerformed(java.awt.event.ActionEvent evt) {
GrafikInput mkholid2 = new GrafikInput();
mkholid2.setVisible(true); }
private void
jMenuItem5ActionPerformed(java.awt.event.ActionEvent evt) {
Input mkholid3 = new Input();
mkholid3.setVisible(true);
}
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(Mkholid_Home.class.getName()).log(java.util.logging.Level.SE
VERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(Mkholid_Home.class.getName()).log(java.util.logging.Level.SE
VERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(Mkholid_Home.class.getName()).log(java.util.logging.Level.SE
VERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex)
{ java.util.logging.Logger.getLogger(Mkholid_Home.class.getName()).log(java.util.logging.Level.SE
VERE, null, ex);
}
//</editor-fold>
/* Create
and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Mkholid_Home().setVisible(true);
}
});
}
// Variables
declaration - do not modify
private javax.swing.JLabel jLabel2;
private javax.swing.JMenu jMenu1;
private javax.swing.JMenu jMenu1;
private javax.swing.JMenu jMenu2;
private javax.swing.JMenu jMenu3;
private javax.swing.JMenu jMenu4;
private javax.swing.JMenuBar jMenuBar1;
private javax.swing.JMenuItem jMenuItem1;
private javax.swing.JMenuItem jMenuItem2;
private javax.swing.JMenuItem jMenuItem3;
private javax.swing.JMenuItem jMenuItem4;
private javax.swing.JMenuItem jMenuItem5;
// End of variables declaration
}
Hailnya :
GrafikPie
package mkholid_2015140810;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PiePlot3D;
import org.jfree.data.general.DefaultPieDataset;
import org.jfree.data.general.PieDataset;
import org.jfree.util.Rotation;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.text.DecimalFormat;
import javax.swing.JPanel;
import org.jfree.chart.plot.PiePlot; import
org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;
public class GrafikPie extends JFrame {
double n1_mkholid=0,n2_mkholid=0, n3_mkholid=0, n4_mkholid=0;
public void setNilai(String nilai1_mkholid, String
nilai2_mkholid, String nilai3_mkholid, String nilai4_mkholid){
try {
n1_mkholid = Double.parseDouble(nilai1_mkholid);
} catch (NumberFormatException ex){}
try {
n2_mkholid = Double.parseDouble(nilai2_mkholid);
} catch (NumberFormatException ex){}
try {
n3_mkholid = Double.parseDouble(nilai3_mkholid);
} catch (NumberFormatException ex){}
try {
n4_mkholid = Double.parseDouble(nilai4_mkholid);
catch
(NumberFormatException ex){}
double total = n1_mkholid+n2_mkholid+n3_mkholid+n4_mkholid,
totalppo = n1_mkholid+n2_mkholid+n3_mkholid,
positif = n1_mkholid*100/totalppo,
pdp = n2_mkholid*100/totalppo,
odp = n3_mkholid*100/totalppo,
kematian = n4_mkholid*100/total,
ppo = (n1_mkholid+n2_mkholid+n3_mkholid)*100/total; this.setSize(800,600);
this.setTitle("Data Covid-19 created by Muhamad
kholid");
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel Panel_mkholid= new JPanel(new GridLayout(2, 2));
DecimalFormat GrafikPie_mkholid = new
DecimalFormat("##,##%");
DefaultPieDataset GrafikPie_mkholid2 = new
DefaultPieDataset(); GrafikPie_mkholid2.setValue("Positif
: "+GrafikPie_mkholid.format(positif),positif); GrafikPie_mkholid2.setValue("PDP :
"+GrafikPie_mkholid.format(pdp),pdp);
GrafikPie_mkholid2.setValue("ODP : "+GrafikPie_mkholid.format(odp),odp); DefaultPieDataset GrafikPie_mkholid3 =
new DefaultPieDataset(); GrafikPie_mkholid3.setValue("Data
Termasuk Positif, PDP, ODP :"+GrafikPie_mkholid.format(ppo),ppo);
GrafikPie_mkholid3.setValue("Kematian : "+GrafikPie_mkholid.format(kematian),kematian);
JFreeChart Chart_mkholid =
ChartFactory.createPieChart3D("Data Penduduk Yang TerkenaCOVID-19",
GrafikPie_mkholid2, false, false, false);
PiePlot3D Plot_mkholid = (PiePlot3D) Chart_mkholid.getPlot(); Plot_mkholid.setForegroundAlpha(0.6f);
lot_mkholid.setCircular(true);
JFreeChart Chart_mkholid2 =
ChartFactory.createPieChart3D("Perbandingan Data Keselamatan",
GrafikPie_mkholid3, false, false, false);
PiePlot3D Plot_mkholid2 = (PiePlot3D) Chart_mkholid2.getPlot(); Plot_mkholid2.setForegroundAlpha(0.6f);
Plot_mkholid2.setCircular(true);
Panel_mkholid.add(new ChartPanel(Chart_mkholid));
Panel_mkholid.add(new ChartPanel(Chart_mkholid2));
Panel_mkholid.setPreferredSize(new Dimension(800, 600));
setContentPane(Panel_mkholid); }
public static void main(String[] args) {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception e) {
}
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new GrafikPie().setVisible(true);
}
});
}
void
setSelected(boolean b) {
throw new UnsupportedOperationException("Not
supported yet.");
//To change body of generated methods, choose Tools |
Templates.
}
}
Hasilnya
LineCharts
package mkholid_2015140810;
import java.awt.*;
import java.awt.geom.*;
import javax.swing.*;
public class LineCharts extends JPanel{
public LineCharts() {
this.setPreferredSize(new Dimension(400, 340)); this.setBackground(Color.WHITE);
}
protected void
paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
GeneralPath gp = new
GeneralPath(GeneralPath.WIND_NON_ZERO);
gp.moveTo(30,290);
gp.lineTo(370, 290);
gp.moveTo(30,90);
gp.lineTo(370, 90);
gp.moveTo(30, 130);
gp.lineTo(370, 130);
gp.moveTo(30, 170);
gp.lineTo(370, 170);
gp.moveTo(30, 210);
gp.lineTo(370, 210);
gp.moveTo(30, 250);
gp.lineTo(370,
250);
//LineCharts
gp.moveTo(115,250);
gp.lineTo(150,170);
gp.moveTo(150, 170);
gp.lineTo(185,90);
gp.moveTo(185, 90);
gp.lineTo(220,130);
gp.moveTo(220, 130);
gp.lineTo(255,170);
gp.closePath();
g2.draw(gp);
g2.drawString("100",7,90); //g2.drawString("90",13,110); g2.drawString("80",13,130);
//g2.drawString("70",13,150); g2.drawString("60",13,170);
//g2.drawString("50",13,190); g2.drawString("40",13,210); //g2.drawString("30",13,230); g2.drawString("20",13,250); //g2.drawString("10", 13,
270);
g2.drawString("0",20,290);
g2.drawString("2016",110,310); g2.drawString("2017",145,310);
g2.drawString("2018",180,310);
g2.drawString("2019",215,310); g2.drawString("2020",250,310);
Shape mkh1 =
new Rectangle2D.Double(115,250,5,5);
Shape mkh2 = new Rectangle2D.Double(150,170,5,5);
Shape mkh3 = new Rectangle2D.Double(185,90,5,5);
Shape mkh4 = new Rectangle2D.Double(220,130,5,5);
Shape mkh5 = new Rectangle2D.Double(255,170,5,5);
g2.setColor(Color.red);
g2.fill(mkh1);
g2.fill(mkh2);
g2.fill(mkh3);
g2.fill(mkh4);
g2.fill(mkh5);
}
}
GrafikInput
package mkholid_2015140810;
public class GrafikInput extends javax.swing.JFrame
{
public GrafikInput() {
initComponents();
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed"
desc="Generated Code"> private void
initComponents() {
jLabel1 =
new javax.swing.JLabel();
T1_mkh = new javax.swing.JTextField();
T2_mkh = new javax.swing.JTextField();
jLabel2 = new javax.swing.JLabel();
T3_mkh = new javax.swing.JTextField();
jLabel3 = new javax.swing.JLabel();
jButton1 = new javax.swing.JButton();
jButton2 = new javax.swing.JButton();
jButton3 = new javax.swing.JButton();
jLabel4 = new javax.swing.JLabel();
jLabel5 = new javax.swing.JLabel();
jLabel6 = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jLabel1.setText("
");
jLabel2.setText(" ");
jLabel3.setText("
");
jButton1.setText("Simpan");
jButton2.setText("Grafik
Pie");
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton2ActionPerformed(evt);
}
});
jButton3.setText("Grafik
Bar");
jButton3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton3ActionPerformed(evt);
}
});
jLabel4.setText("Nilai
1");
jLabel5.setText("Nilai
2");
jLabel6.setText("Nilai
3");
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(41, 41, 41)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jLabel6)
.addComponent(jLabel5)
.addComponent(jLabel4))
.addGap(32, 32, 32)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(T3_mkh,
javax.swing.GroupLayout.PREFERRED_SIZE, 108,
javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(T2_mkh,
javax.swing.GroupLayout.PREFERRED_SIZE, 108,
javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(T1_mkh,
javax.swing.GroupLayout.PREFERRED_SIZE, 108,
javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(81, 81, 81)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel3)
.addComponent(jLabel2)
.addComponent(jLabel1)))
.addGroup(layout.createSequentialGroup()
.addGap(147, 147, 147)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING,
false)
.addComponent(jButton3,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE,
Short.MAX_VALUE)
.addComponent(jButton2, 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(107,
Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup()
.addGap(42, 42, 42)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel1)
.addComponent(T1_mkh, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel2)
.addComponent(T2_mkh,
javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel3)
.addComponent(T3_mkh,
javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jLabel6)))
.addGroup(layout.createSequentialGroup()
.addGap(44, 44, 44)
.addComponent(jLabel4)
.addGap(18, 18, 18)
.addComponent(jLabel5)))
.addGap(42, 42, 42)
.addComponent(jButton1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(jButton2) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jButton3)
.addContainerGap(48, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
private void jButton1ActionPerformed(java.awt.event.ActionEvent
evt) {
int a,b,c;
a=Integer.parseInt(T1_mkh.getText());
b=Integer.parseInt(T2_mkh.getText());
c=Integer.parseInt(T3_mkh.getText());
jLabel1.setText(""+a);
jLabel2.setText(""+b);
jLabel3.setText(""+c);
}
private void
jButton2ActionPerformed(java.awt.event.ActionEvent evt) { PieGrafikInput mkh =
new PieGrafikInput();
mkh.setNilai(jLabel1.getText(),jLabel2.getText(),jLabel3.getText());
mkh.setVisible(true);
dispose();
}
private void
jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
BarGrafikInput mkhario = new BarGrafikInput();
mkhario.setNilai(jLabel1.getText(),jLabel2.getText(),jLabel3.getText()); mkhario.setVisible(true);
dispose();
}
/** * @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 {
or (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(GrafikInput.class.getName()).log(java.util.logging.Level.SEVERE,
null, ex);
} catch (InstantiationException ex) { java.util.logging.Logger.getLogger(GrafikInput.class.getName()).log(java.util.logging.Level.SEVERE,
null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(GrafikInput.class.getName()).log(java.util.logging.Level.SEVERE,
null, ex); } catch
(javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(GrafikInput.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 GrafikInput().setVisible(true);
}
});
}
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;
// End of variables declaration }
PieGrafikInput package mkholid_2015140810;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import javax.swing.UIManager; import
org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel; import
org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PiePlot3D;
import org.jfree.data.general.DefaultPieDataset;
import org.jfree.data.general.PieDataset;
import org.jfree.util.Rotation;
public class PieGrafikInput extends JFrame
{
double n1_mkholido=0,n2_mkholido=0, n3_mkholido=0;
public
void setNilai(String nilai1_mkholido,
String nilai2_mkholido,
String nilai3_mkholido){
// Variables declaration - do not
modify
private javax.swing.JTextField T1_mkh;
private javax.swing.JTextField T2_mkh;
private javax.swing.JTextField T3_mkh;
private javax.swing.JButton
jButton1;
private javax.swing.JButton
jButton2;
try {
n1_mkholido = Double.parseDouble(nilai1_mkholido);
} catch (NumberFormatException ex){}
try {
n2_mkholido = Double.parseDouble(nilai2_mkholido);
} catch (NumberFormatException ex){}
try {
n3_mkholido = Double.parseDouble(nilai3_mkholido);
} catch (NumberFormatException ex){}
this.setSize(600, 400);
this.setTitle("Diagram PIE Mkh Ario Rinaldo");
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//membuat diagram/chart jfreechart
//Membuat/mengkakulasikan data char
PieDataset dataset = buildData();
// Membuat chart berdasarkan data yang ada pada dataset
JFreeChart chart = buildChart(dataset, "Browser yang Yang
Digunakan");
// Memasukan chart pada panel agar lebih mudah ditampilkan pada komponen
swing containe
ChartPanel pieChart = new ChartPanel(chart);
//memasukan chart panel kedalam jpanel
this.setContentPane(pieChart);
private JFreeChart buildChart(PieDataset dataset, String judul) {
JFreeChart chart =
ChartFactory.createPieChart3D(judul, // Judul Chart dataset, // data chart yang akan di
tampilkan
true, // jika ya, maka akan legend chart/diagram ditampilkan
true,
false);
PiePlot3D plot = (PiePlot3D)
chart.getPlot();
plot.setStartAngle(290);
plot.setDirection(Rotation.CLOCKWISE);
plot.setForegroundAlpha(0.5f);
return chart; }
private PieDataset buildData() {
DefaultPieDataset dataSet = new DefaultPieDataset();
dataSet.setValue("Nilai 1", n1_mkholido);
dataSet.setValue("Nilai 2", n2_mkholido);
dataSet.setValue("Nilai 3", n3_mkholido);
return dataSet; }
public static void main(String[] args) {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); } catch (Exception e) {
}
SwingUtilities.invokeLater(new
Runnable() {
@Override
public void run() {
new PieGrafikInput().setVisible(true);
} }); }
void setSelected(boolean b)
{
throw new UnsupportedOperationException("Not supported yet.");
//To change body of generated methods, choose Tools | Templates.
} }
BarGrafikInput
package mkholid_2015140810;
import javax.swing.JFrame;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartFrame;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.category.DefaultCategoryDataset;
public class BarGrafikInput extends JFrame {
double n1_mkholido=0,n2_mkholido=0, n3_mkholido=0;
public void setNilai(String nilai1_mkholido, String nilai2_mkholido, String
nilai3_mkholido){
try {
n1_mkholido = Double.parseDouble(nilai1_mkholido);
} catch (NumberFormatException ex){}
try { n2_mkholido =
Double.parseDouble(nilai2_mkholido);
} catch (NumberFormatException ex){}
try {
n3_mkholido = Double.parseDouble(nilai3_mkholido);
} catch (NumberFormatException ex){}
DefaultCategoryDataset mkhario = new DefaultCategoryDataset(); mkhario.addValue(n1_mkholido,
"Nilai 1", "");
mkhario.addValue(n2_mkholido, "Nilai 2", "");
mkhario.addValue(n3_mkholido, "Nilai 3", "");
mkhario.addValue(n1_mkholido, "Nilai 1", "");
mkhario.addValue(n2_mkholido, "Nilai 2", "");
mkhario.addValue(n3_mkholido, "Nilai 3", "");
JFreeChart chart = ChartFactory.createBarChart3D("Data nilai
siswa", "",
"NIlai (%)",mkhario, PlotOrientation.VERTICAL, true, true,
false);
ChartFrame Cframemkhario = new ChartFrame("Diagram Bar Mkh Ario
Rinaldo", chart); Cframemkhario.setSize(600,
400);
Cframemkhario.setVisible(true);
Cframemkhario.setLocationRelativeTo(null); } }
Hasilnya :
Input
package mkholid_2015140810;
public class Input extends javax.swing.JFrame {
public Input() {
initComponents(); }
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated
Code">
private void initComponents() {
jPanel1 = new javax.swing.JPanel();
mkh_label1 = new javax.swing.JLabel();
mkh_text1 = new javax.swing.JTextField();
mkh_text2 = new javax.swing.JTextField();
mkh_label2 = new javax.swing.JLabel();
mkh_text3 = new javax.swing.JTextField();
mkh_label3 = new javax.swing.JLabel();
mkh_text4 = new javax.swing.JTextField();
mkh_label4 = new javax.swing.JLabel();
jToggleButton1 = new javax.swing.JToggleButton();
jToggleButton2 = new javax.swing.JToggleButton();
mkh_labelisi1 = new javax.swing.JLabel();
mkh_labelisi2 = new javax.swing.JLabel();
mkh_labelisi3 = new javax.swing.JLabel();
mkh_labelisi4 = new javax.swing.JLabel();
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();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); setTitle("Created by Mkh Ario
Rinaldo");
jPanel1.setBackground(new
java.awt.Color(0, 0, 0));
mkh_label1.setForeground(new
java.awt.Color(255, 255, 255)); mkh_label1.setText("Positif");
mkh_label2.setForeground(new
java.awt.Color(255, 255, 255)); mkh_label2.setText("PDP");
mkh_label3.setForeground(new
java.awt.Color(255, 255, 255)); mkh_label3.setText("ODP");
mkh_label4.setForeground(new
java.awt.Color(255, 255, 255)); mkh_label4.setText("Kematian");
javax.swing.GroupLayout
jPanel1Layout = new javax.swing.GroupLayout(jPanel1); jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup( jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(27, 27, 27)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING,
false)
.addGroup(jPanel1Layout.createSequentialGroup() .addComponent(mkh_label4)
.addGap(26, 26, 26)
.addComponent(mkh_text4, javax.swing.GroupLayout.PREFERRED_SIZE, 111,
javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING,
jPanel1Layout.createSequentialGroup() .addComponent(mkh_label3)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED,
javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(mkh_text3,
javax.swing.GroupLayout.PREFERRED_SIZE, 111,
javax.swing.GroupLayout.PREFERRED_SIZE)) .addGroup(jPanel1Layout.createSequentialGroup() .addComponent(mkh_label2)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED,
javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(mkh_text2,
javax.swing.GroupLayout.PREFERRED_SIZE, 111,
javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(jPanel1Layout.createSequentialGroup() .addComponent(mkh_label1) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED,
javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(mkh_text1,
javax.swing.GroupLayout.PREFERRED_SIZE, 111, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addContainerGap(55, Short.MAX_VALUE))
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel1Layout.createSequentialGroup()
.addGap(19, 19, 19)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(mkh_label1)
.addComponent(mkh_text1, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.PREFERRED_SIZE)) .addGap(18, 18, 18)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(mkh_label2)
.addComponent(mkh_text2, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.PREFERRED_SIZE)) .addGap(18, 18, 18)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(mkh_label3)
.addComponent(mkh_text3, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.PREFERRED_SIZE)) .addGap(18, 18, 18)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(mkh_label4)
.addComponent(mkh_text4, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.PREFERRED_SIZE)) .addContainerGap(24,
Short.MAX_VALUE))
);
jToggleButton1.setText("Simpan");
jToggleButton1.addActionListener(new java.awt.event.ActionListener() { public void
actionPerformed(java.awt.event.ActionEvent evt) { jToggleButton1ActionPerformed(evt);
}
});
jToggleButton2.setText("Grafik");
jToggleButton2.addActionListener(new java.awt.event.ActionListener() { public void
actionPerformed(java.awt.event.ActionEvent evt) { jToggleButton2ActionPerformed(evt);
}
});
mkh_labelisi1.setText("
");
mkh_labelisi2.setText("
");
mkh_labelisi3.setText("
");
mkh_labelisi4.setText("
");
jLabel1.setText("Orang");
jLabel2.setText("Orang");
jLabel3.setText("Orang");
jLabel4.setText("Orang");
jLabel5.setFont(new
java.awt.Font("Tahoma", 1, 12)); // NOI18N jLabel5.setText("PRESENTASI DATA
COVID-19 DAERAH JAWA BARAT");
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(33, 33, 33)
.addComponent(jToggleButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 253,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(0, 0, Short.MAX_VALUE))
.addGroup(layout.createSequentialGroup()
.addGap(23, 23, 23)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jLabel5)
.addGroup(layout.createSequentialGroup()
.addComponent(jPanel1,
javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup() .addComponent(mkh_labelisi1)
.addGap(18, 18, 18)
.addComponent(jLabel1))
.addGroup(layout.createSequentialGroup() .addComponent(mkh_labelisi2)
.addGap(18, 18, 18)
.addComponent(jLabel2))
.addGroup(layout.createSequentialGroup() .addComponent(mkh_labelisi3)
.addGap(18, 18, 18) .
addComponent(jLabel3))
.addGroup(layout.createSequentialGroup() .addComponent(mkh_labelisi4)
.addGap(18, 18, 18)
.addComponent(jLabel4))))
.addGroup(layout.createSequentialGroup()
.addGap(10, 10, 10)
.addComponent(jToggleButton1,
javax.swing.GroupLayout.PREFERRED_SIZE, 253, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE,
Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup()
.addGap(22, 22, 22)
.addComponent(jLabel5)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jPanel1,
javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addGroup(layout.createSequentialGroup()
.addGap(20, 20, 20)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(mkh_labelisi1)
.addComponent(jLabel1))
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup() .addGap(18,
18, 18)
.addComponent(mkh_labelisi2))
.addGroup(layout.createSequentialGroup()
.addGap(26, 26, 26)
.addComponent(jLabel2)))
.addGap(22, 22, 22)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(mkh_labelisi3)
.addComponent(jLabel3)) .
addGap(26, 26, 26) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(mkh_labelisi4)
.addComponent(jLabel4))))
.addGap(13, 13, 13)
.addComponent(jToggleButton1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(jToggleButton2))
);
pack();
}// </editor-fold>
private void
jToggleButton1ActionPerformed(java.awt.event.ActionEvent evt) {
int a,b,c,d;
a=Integer.parseInt(mkh_text1.getText()); b=Integer.parseInt(mkh_text2.getText()); c=Integer.parseInt(mkh_text3.getText()); d=Integer.parseInt(mkh_text4.getText()); mkh_labelisi1.setText(""+a); mkh_labelisi2.setText(""+b);
kh_labelisi3.setText(""+c); mkh_labelisi4.setText(""+d); }
private void
jToggleButton2ActionPerformed(java.awt.event.ActionEvent evt) {
GrafikPie mkhario = new
GrafikPie(); mkhario.setNilai(mkh_labelisi1.getText(),mkh_labelisi2.getText(),mkh_labelisi3.getText(),mkh_labelisi4.getT
ext());
mkhario.setVisible(true);
dispose(); }
/** * @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(Input.class.getName()).log(java.util.logging.Level.SEVERE,
null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(Input.class.getName()).log(java.util.logging.Level.SEVERE,
null, ex);
} catch (IllegalAccessException ex)
{
java.util.logging.Logger.getLogger(Input.class.getName()).log(java.util.logging.Level.SEVERE,
null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) { java.util.logging.Logger.getLogger(Input.class.getName()).log(java.util.logging.Level.SEVERE,
null, ex);
}
//</editor-fold>
//</editor-fold>
/* Create and display the
form */ j
ava.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Input().setVisible(true);
} }); }
// Variables declaration - do not
modify
private javax.swing.JLabel mkh_label1;
private javax.swing.JLabel mkh_label2;
private javax.swing.JLabel mkh_label3;
private javax.swing.JLabel mkh_label4;
private javax.swing.JLabel mkh_labelisi1;
private javax.swing.JLabel mkh_labelisi2;
private javax.swing.JLabel mkh_labelisi3;
private javax.swing.JLabel mkh_labelisi4;
private javax.swing.JTextField mkh_text1;
private javax.swing.JTextField mkh_text2;
private javax.swing.JTextField mkh_text3;
private javax.swing.JTextField mkh_text4;
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.JPanel jPanel1;
private javax.swing.JToggleButton
jToggleButton1;
private javax.swing.JToggleButton jToggleButton2;
// End of variables declaration }
GrafikPie
package mkholid_2015140810;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PiePlot3D;
import org.jfree.data.general.DefaultPieDataset;
import org.jfree.data.general.PieDataset;
import org.jfree.util.Rotation; import java.awt.Dimension; import
java.awt.GridLayout;
import java.text.DecimalFormat;
import javax.swing.JPanel;
import org.jfree.chart.plot.PiePlot;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;
public class GrafikPie extends JFrame {
double n1_mkholido=0,n2_mkholido=0, n3_mkholido=0, n4_mkholido=0;
public void setNilai(String nilai1_mkholido, String nilai2_mkholido, String
nilai3_mkholido, String nilai4_mkholido){
try {
n1_mkholido = Double.parseDouble(nilai1_mkholido);
} catch (NumberFormatException ex){}
try {
n2_mkholido = Double.parseDouble(nilai2_mkholido);
} catch (NumberFormatException ex){}
try {
n3_mkholido = Double.parseDouble(nilai3_mkholido);
} catch (NumberFormatException
ex){}
try {
n4_mkholido = Double.parseDouble(nilai4_mkholido);
} catch (NumberFormatException ex){}
double total = n1_mkholido+n2_mkholido+n3_mkholido+n4_mkholido, totalppo = n1_mkholido+n2_mkholido+n3_mkholido,
positif = n1_mkholido*100/totalppo,
pdp = n2_mkholido*100/totalppo,
odp = n3_mkholido*100/totalppo,
kematian = n4_mkholido*100/total,
ppo = (n1_mkholido+n2_mkholido+n3_mkholido)*100/total; this.setSize(800,600);
this.setTitle("Data Covid-19 created by Mkh Ario Rinaldo"); this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel Panel_mkhario= new JPanel(new GridLayout(2, 2));
DecimalFormat GrafikPie_mkhario = new DecimalFormat("##,##%");
DefaultPieDataset GrafikPie_mkhario2 = new DefaultPieDataset(); GrafikPie_mkhario2.setValue("Positif
: "+GrafikPie_mkhario.format(positif),positif); GrafikPie_mkhario2.setValue("PDP :
"+GrafikPie_mkhario.format(pdp),pdp);
GrafikPie_mkhario2.setValue("ODP : "+GrafikPie_mkhario.format(odp),odp); DefaultPieDataset GrafikPie_mkhario3 =
new DefaultPieDataset(); GrafikPie_mkhario3.setValue("Data
Termasuk Positif, PDP, ODP :"+GrafikPie_mkhario.format(ppo),ppo);
GrafikPie_mkhario3.setValue("Kematian : "+GrafikPie_mkhario.format(kematian),kematian);
JFreeChart Chart_mkhario = ChartFactory.createPieChart3D("Data
Penduduk Yang TerkenaCOVID-19", GrafikPie_mkhario2, false, false,
false);
PiePlot3D Plot_mkhario = (PiePlot3D) Chart_mkhario.getPlot(); Plot_mkhario.setForegroundAlpha(0.6f);
Plot_mkhario.setCircular(true);
JFreeChart Chart_mkhario2 =
ChartFactory.createPieChart3D("Perbandingan Data Keselamatan",
GrafikPie_mkhario3, false, false, false);
PiePlot3D Plot_mkhario2 = (PiePlot3D) Chart_mkhario2.getPlot(); Plot_mkhario2.setForegroundAlpha(0.6f);
Plot_mkhario2.setCircular(true);
Panel_mkhario.add(new ChartPanel(Chart_mkhario));
Panel_mkhario.add(new ChartPanel(Chart_mkhario2));
Panel_mkhario.setPreferredSize(new Dimension(800, 600));
setContentPane(Panel_mkhario); }
public static void main(String[] args) {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception e) {
}
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new GrafikPie().setVisible(true);
} }); }
void setSelected(boolean b)
{
throw new UnsupportedOperationException("Not supported yet.");
//To change body of generated methods, choose Tools | Templates.
} }
HASIL :
Silahkan dwonload filenya di bawah ini :
Komentar
Posting Komentar