package cn.com.wz.gui;
import java.awt.Dimension;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import javax.swing.JFrame;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.data.general.DefaultPieDataset;
public class MyJFreechart extends JFrame {
/**
* 要建立一個JFreeChart的圖形主要有三個步驟
建立一個擁有資料的DataSet
用DataSet創造JFreeChart
對JFreeChart作一些自訂的設計
顯示JFreeChart
*/
/**
* 初学jfreechart
* 这里包括柱状图,和饼状图的实现,简单易懂
*/
public MyJFreechart(){
this.setSize(700,600);
//绘制柱状图到窗口
CategoryDataset dataSet = createDataSet();
JFreeChart chart = creatChart(dataSet);
chart = creatCustomChart(chart);
ChartPanel chartPanel = new ChartPanel(chart);
chartPanel.setPreferredSize(new Dimension(600,500));
this.add(chartPanel);
drawChar(chart);//生成柱状图图片
createPieChart();//生成饼状图图片
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
private void drawChar(JFreeChart chart) {
try {
//FileOutputStream out = new FileOutputStream("image/chart.jpg");
ChartUtilities.saveChartAsJPEG(new File("image/chart.jpg"), chart, 600, 500);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private JFreeChart creatCustomChart(final JFreeChart chart) {
return chart;
}
private JFreeChart creatChart( final CategoryDataset dataSet) {
JFreeChart chart = ChartFactory.createBarChart("fisrt Chart", "产量", "时间", dataSet,
PlotOrientation.VERTICAL, true, true, false);
return chart;
}
private CategoryDataset createDataSet() {
DefaultCategoryDataset dataset = new DefaultCategoryDataset();
// row keys...
String series1 = "First";
String series2 = "Second";
String series3 = "Third";
// column keys...
String category1 = "Category 1";
String category2 = "Category 2";
String category3 = "Category 3";
String category4 = "Category 4";
String category5 = "Category 5";
dataset.addValue(1.5, series1, category1);
dataset.addValue(4.2, series1, category2);
dataset.addValue(3.0, series1, category3);
dataset.addValue(5.0, series1, category4);
dataset.addValue(5.0, series1, category5);
dataset.addValue(5.5, series2, category1);
dataset.addValue(7.8, series2, category2);
dataset.addValue(6.0, series2, category3);
dataset.addValue(8.0, series2, category4);
dataset.addValue(4.0, series2, category5);
dataset.addValue(4.0, series3, category1);
dataset.addValue(3.0, series3, category2);
dataset.addValue(2.0, series3, category3);
dataset.addValue(3.0, series3, category4);
dataset.addValue(6.0, series3, category5);
return dataset;
}
private void createPieChart( ) {
DefaultPieDataset dataset = getDataSet();
JFreeChart chart = ChartFactory.createPieChart3D("title", dataset,true, false, false);
//JFreeChart chart = ChartFactory.createPieChart3D(title, dataset, legend, tooltips, urls)
try {
ChartUtilities.saveChartAsPNG(new File("image/peiChart.png"), chart, 600, 500);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private DefaultPieDataset getDataSet(){
DefaultPieDataset dataSet = new DefaultPieDataset();
dataSet.setValue("苹果", 100);
dataSet.setValue("橘子", 200);
dataSet.setValue("西瓜", 300);
dataSet.setValue("电脑", 1000);
return dataSet;
}
public static void main(String[] args) {
MyJFreechart freeChart = new MyJFreechart();
freeChart.setVisible(true);
}
}