import java.awt.Color;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.labels.PieSectionLabelGenerator;
import org.jfree.chart.labels.StandardPieSectionLabelGenerator;
import org.jfree.chart.plot.PiePlot3D;
import org.jfree.data.general.DefaultPieDataset;
import org.jfree.ui.RectangleEdge;
/**
* 饼图模板
*
* @author hongming du
* @date 2010-03-16
* @version 1.0
*
*/
/**
* 整个饼图包括了许多的东西
* 一张饼图的本生图片叫做PiePlot3D plot,
* 而整个饼图又是由许都的饼子组成,每一个组成的成员饼子叫做 PieSectionLabelGenerator lable
* 在经常看到饼图的旁边,比如下方或者其他,会显示许多不同颜色的但具有相同形状的小图像,这个小图像叫Legend
* 图片显示出不同的分布,这个分布是由数据形成的,某一个饼子在整个饼图中所在的比例是由DefaultPieDataset dataset决定的
* 当然,每个饼图具有不同的title
* 最后这个所有形成的饼图叫做 JFreeChart chart
*/
public class PieChartTemplate {
private static NumberFormat numberFormat;
private static DecimalFormat decimalFormat;
/**
* 整个饼图是由许多的饼图平凑而成,PieSectionLabelGenerator表示的就是其中的一个饼子
* 在他的参数中,第一个参数中:表示的是饼子的整个构造以及形成,参数第一个{0}表示的是饼子所代表的名字{2}表示的是自定义, 而{1}表示的是所代表的饼子的数值
* 第二个参数:代表的是对第一个参数的格式化,或者叫对第一个参数的指认
* 第三个参数:代表的是这个这个饼子在整个饼图中所占有比例的格式-->forExample:10.1%
*/
//一个饼块生成一个标签,标签的内容可以自定义
private static PieSectionLabelGenerator labelGenerator;
static {
numberFormat = NumberFormat.getNumberInstance();
decimalFormat = new DecimalFormat("0.0 %");
// 图片中显示百分比:自定义方式,{0} 表示选项, {1} 表示数值, {2} 表示所占比例 ,小数点后两位
// 设置样式,0表示KEY,1表示VALUE,2表示百分之几,DecimalFormat用来显示百分比的格式
labelGenerator = new StandardPieSectionLabelGenerator(
"{0}( {2} )", numberFormat, decimalFormat);
}
public static JFreeChart getPieChart3D(String title,
// 构造一个新的数据集,最初是空的
DefaultPieDataset dataset) {
// 创建一个具有三维使用指定的数据集效果饼图
PiePlot3D plot = new PiePlot3D(dataset);
// 没有数据时显示的消息
plot.setNoDataMessage("没有需要的数据");
// 没有数据时显示的消息颜色
plot.setNoDataMessagePaint(Color.RED);
// 饼图的初始角度
plot.setStartAngle(120);
// 数据区的前景透明度(0.0~1.0)
plot.setForegroundAlpha(0.8f);
// 指定显示的饼图上圆形(false)还椭圆形(true)
plot.setCircular(false);
// 3D饼图的Z轴高度(0.0~1.0)
plot.setDepthFactor(0.2f);
// 分类标签的格式,设置成null则整个标签包括连接线都不显示
plot.setLabelGenerator(labelGenerator);
JFreeChart chart = new JFreeChart(title, plot);
// 背景色
chart.setBackgroundPaint(Color.WHITE);
// 边界线条颜色
chart.setBorderPaint(Color.WHITE);
// 返回图表示图 ,设置标题的位置,RectangleEdge用来表示一个矩形的边缘
chart.getLegend().setPosition(RectangleEdge.RIGHT);
return chart;
}
}
在网上找了不少的资料,但没有个是详细的。最近公司要画图,逼着学习了JfreeChart ,在这里做了点小小的总结,希望
对学习者有所用。最后还要感谢我公司的同事“Michael Gu“.