package com.wkx.jfreechart;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.util.Random;
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.title.LegendTitle;
import org.jfree.chart.title.TextTitle;
import org.jfree.chart.urls.StandardPieURLGenerator;
import org.jfree.data.general.DefaultPieDataset;
import org.jfree.data.general.PieDataset;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;
public class MyPieChartTest extends ApplicationFrame {
//private String[] engStr={"One","Two","Three","Four","Five","Six","Seven","eight","Nine","Ten"};
private String[] cheStr={"第一","第二","第三","第四","第五","第六","第七","第八","第九","第十"};
public MyPieChartTest(String title) {
super(title);
JFreeChart pieChart = createChart(createDataSet());
JPanel localPanel = new ChartPanel(pieChart);
localPanel.setPreferredSize(new Dimension(500,270));
setContentPane(localPanel);
}
public static void main(String[] args) {
MyPieChartTest pieChartTest = new MyPieChartTest("饼形图测试");
pieChartTest.pack();
RefineryUtilities.centerFrameOnScreen(pieChartTest);
pieChartTest.setVisible(true);
}
public PieDataset createDataSet(){
DefaultPieDataset dataset = new DefaultPieDataset();
Random r = new Random();
for(int i=0;i<cheStr.length;i++){
dataset.setValue(cheStr[i], r.nextDouble()*10);
}
return dataset;
}
public JFreeChart createChart(PieDataset dataset){
//创建绘图区
JFreeChart pieChart = ChartFactory.createPieChart("测试饼形图", dataset, true, true, true);
//设置绘图区背景色
pieChart.setBackgroundPaint(Color.PINK);
//获取图片标题
TextTitle title = pieChart.getTitle();
//设置标题样式
title.setFont(new Font("黑体", Font.PLAIN, 20));
//设置鼠标悬浮字
title.setToolTipText("这是我的测试饼形图。");
//获取绘图区
PiePlot plot = (PiePlot)pieChart.getPlot();
//设置没有数据时显示的消息
plot.setNoDataMessage("没有数据。");
//设置没有数据时显示消息的字体
plot.setNoDataMessageFont(new Font("黑体",Font.PLAIN,20));
//设置图片超链接
plot.setURLGenerator(new StandardPieURLGenerator("http://wenkaixuan.iteye.com"));
//设置内容字体
plot.setLabelFont(new Font("宋体",Font.PLAIN,14));
//获取绘图区底部标签
LegendTitle legTitle = pieChart.getLegend();
//设置底部标签字体
legTitle.setItemFont(new Font("黑体",Font.PLAIN,14));
return pieChart;
}
}