Membuat Phie Chart Sederhana Java Netbeans

Studi kasus, grafik untuk mengukur Responden Lock Down terkait dengan Pandemi Covid 19, masing masing rsponden bernilai persentase. Berikut adalah langkah-langkahnya:

1.New Project

Jalankan Java Netbean lanjutkn dengan klik File-> Pilih New Project -> Pilih -> Java Application -> Klik Next -> Finish

New Project
Java Application

UAS_Ristanto_171011400916
Pada tahap ini kita sudah selesai membuat Project Baru yang merupakan judul Aplikasi.

2. Membuat Packages

Klik kanan Pada Project -> pilih New -> Java Packages -> ristanto( Packages  Name) -> Finish

Name Packages
Hasil

3. Membuat JFrame

Klik Kanan pada ristanto (Source Packages) -> New -> pilih JFrame Form -> Piechart_Sederhana(nama Jframe) – > Finish

JFrame Form
PhieChart Sederhana
Tampilan JFrame
/*Source Code
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 ristanto;
/**
*
@author RISTANTO
*/
public class Piechart_Sederhana extends javax.swing.JFrame {
/**
Creates new form Piechart_Sederhana
*/
public Piechart_Sederhana() {
/**
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")
//
private void initComponents() {
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 400, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 300, Short.MAX_VALUE)
);
pack();
}//
/**
@param args the command line arguments
/ public static void main(String args[]) { / Set the Nimbus look and feel / // / 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(Piechart_Sederhana.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(Piechart_Sederhana.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(Piechart_Sederhana.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(Piechart_Sederhana.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Piechart_Sederhana().setVisible(true);
}
});
}
// Variables declaration - do not modify
// End of variables declaration
}

4. Import Library Free Chart

Klik kanan Library ->Add Library->select file Zip dan Jar dari folder komputer anda->Selesai

5. Config Pie Chart

Memanggil Library Grafik

Paste kan source code Import Javax.Swing FreeChart berikut;

package ristanto;
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;
Memanggil Library Free Chart

Inisialisasi Frame

Paste kan source code  inisialisasi frame;
public Piechart_Sederhana() {
//initComponents();
//inisialisasiframe
this.setSize(600, 400);
this.setTitle("Diagram PIE Sederhana");
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//membuatdiagram/chart jfreechart
//Membuat/mengkakulasikandata char
PieDataset dataset = buildData();
// Membuatchart berdasarkan data yang ada pada dataset
JFreeChart chart = buildChart(dataset, "RESPONDEN LOCK DOWN CEGAH COVID-19");
// Memasukanchart padapanel agar lebihmudahditampilkanpadakomponenswing containe
ChartPanel pieChart= new ChartPanel(chart);
//memasukanchart panel kedalamjpanel
this.setContentPane(pieChart);
}
Inisalisasi Frame

Membuat Mathod Grafik

//mathod grafik
private JFreeChart buildChart(PieDataset dataset, String judul) {
JFreeChart chart = ChartFactory.createPieChart3D(judul, // JudulChart
dataset, // data chart yang akanditampilkan
true, // jikaya, makaakanlegend chart/diagram ditampilkan
true,
false);
PiePlot3D plot = (PiePlot3D) chart.getPlot();
plot.setStartAngle(290);
plot.setDirection(Rotation.CLOCKWISE);
plot.setForegroundAlpha(0.5f);
return chart;
}

Membuat Tampilan Garafik

//tampilan garfik
private PieDataset buildData() {
DefaultPieDataset dataSet= new DefaultPieDataset();
//set value dan parameter nilai presentase
dataSet.setValue("Sangat Setuju", 45);
dataSet.setValue("Setuju", 30);
dataSet.setValue("Kurang Setuju", 15);
dataSet.setValue("Menolak", 8);
dataSet.setValue("Pasif", 2);
return dataSet;
}

Tampilan Garfik

Tinggalkan Balasan