Refactor one
/**
* JfreeChartOne.java
* author:longlong
* May 31, 2007
*/
package com.ghg.java;
import java.awt.Font;
import javax.swing.JPanel;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PiePlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.data.general.DefaultPieDataset;
import org.jfree.data.general.PieDataset;
import org.jfree.ui.ApplicationFrame;
public class JfreeChartOne extends ApplicationFrame {
private static final long serialVersionUID = 1L;
public JfreeChartOne(String s)
{
super(s);
setContentPane(createJPanel());
}
public static void main(String[] args) {
JfreeChartOne one=new JfreeChartOne("CityInfoPort公司组织架构图");
one.pack();
one.setVisible(true);
}
//利用静态方法设定数据源(饼状图)
public static PieDataset createPieDataset() {
DefaultPieDataset defaultpiedataset = new DefaultPieDataset();
defaultpiedataset.setValue("管理人员", 10.02D);
defaultpiedataset.setValue("市场人员", 20.23D);
defaultpiedataset.setValue("开发人员", 60.02D);
defaultpiedataset.setValue("OEM人员", 10.02D);
defaultpiedataset.setValue("其他人员", 5.11D);
return defaultpiedataset;
}
// 通过ChartFactory创建JFreeChart的实例
public static JFreeChart createJFreeChart(PieDataset p)
{
JFreeChart a =ChartFactory.createPieChart("CityInfoPort公司组织架构图",p, true,true, true);
PiePlot pie=(PiePlot )a.getPlot();
pie.setLabelFont(new Font("SansSerif",Font.BOLD,12));
pie.setNoDataMessage("No data available");
pie.setCircular(true);
pie.setLabelGap(0.01D);//间距
return a;
}
public static JPanel createJPanel (){
JFreeChart jfreechart = createJFreeChart(createPieDataset());
return new ChartPanel(jfreechart);
}
}
效果如图所示:
two:创建类别图
/**
* JfreeChartTest.java
* author:longlong
* May 31, 2007
*/
package com.ghg.java;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.data.general.DefaultPieDataset;
import org.jfree.chart.*;
import org.jfree.chart.plot.PlotOrientation;
public class JfreeChartTest {
public static void main(String[] args) {
// 创建类别图(Category)数据对象
DefaultCategoryDataset dataset = new DefaultCategoryDataset();
dataset.addValue(100, "北京", "苹果");
dataset.addValue(100, "上海", "苹果");
dataset.addValue(100, "广州", "苹果");
dataset.addValue(200, "北京", "梨子");
dataset.addValue(200, "上海", "梨子");
dataset.addValue(200, "广州", "梨子");
dataset.addValue(300, "北京", "葡萄");
dataset.addValue(300, "上海", "葡萄");
dataset.addValue(300, "广州", "葡萄");
dataset.addValue(400, "北京", "香蕉");
dataset.addValue(400, "上海", "香蕉");
dataset.addValue(400, "广州", "香蕉");
dataset.addValue(500, "北京", "荔枝");
dataset.addValue(500, "上海", "荔枝");
dataset.addValue(500, "广州", "荔枝");
JFreeChart chart=ChartFactory.createBarChart3D("水果产量图", "水果", "水果", dataset, PlotOrientation.VERTICAL, true, true, true);
ChartFrame frame=new ChartFrame ("水果产量图 ",chart,true);
frame.pack();
frame.setVisible(true);
}
}
按照one的重构办法也可以对这个程序实现重构。