package com.hoocy.struts;
import java.awt.Color;
import java.awt.Font;
import java.awt.RenderingHints;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartFrame;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.labels.StandardPieSectionLabelGenerator;
import org.jfree.chart.plot.PiePlot;
import org.jfree.chart.title.TextTitle;
import org.jfree.data.general.DefaultPieDataset;
public class TestPieChart {
/**
* 创建饼图对象的JFreeChart 对象
*
* @param condVo
* @param list
* @return
* @throws Exception
*/
public static void createPieChart() throws Exception {
JFreeChart[] jfreechart = new JFreeChart[2];
// 饼图的标题
String ChartTitle = "";
// 用工厂类创建饼图
JFreeChart pieChart = ChartFactory.createPieChart(ChartTitle,
createPieDataset(), true, true, false);
// RenderingHints做文字渲染参数的修改
// VALUE_TEXT_ANTIALIAS_OFF表示将文字的抗锯齿关闭.
pieChart.getRenderingHints().put(RenderingHints.KEY_TEXT_ANTIALIASING,
RenderingHints.VALUE_TEXT_ANTIALIAS_OFF);
// 设置Legend的位置 图例的位置
// ((JFreeChart) pieChart).getLegend().setPosition(RectangleEdge.RIGHT);
// 设置图片背景色
pieChart.setBackgroundPaint(new Color(191, 191, 255));
// 设置图标题的字体重新设置title
Font font = new Font("隶书", Font.BOLD, 25);
TextTitle title = new TextTitle(ChartTitle);
title.setFont(font);
// pieChart.setTitle(title); //用来控制Chart标题的
// 得到饼图的Plot对象
PiePlot piePlot = (PiePlot) pieChart.getPlot();
// 指定饼图轮廓线的颜色
piePlot.setBaseSectionOutlinePaint(Color.BLACK);
piePlot.setBaseSectionPaint(Color.BLACK);
// 设置背景区域背景色
piePlot.setBackgroundPaint(new Color(191, 191, 255));
setSection(piePlot);
setLabel(piePlot);
// 设置没数据显示信息
setNoDataMessage(piePlot);
// 设置对于null数据和0数据的处理
setNullAndZeroValue(piePlot);
jfreechart[0] = pieChart;
ChartFrame frame = new ChartFrame("TestPieChart", pieChart);
frame.pack();
frame.setVisible(true);
}
/**
* 创建饼图对象的数据源 DefaultPieDataset
*
* @param condVo
* @param list
* @param parse
* @return
* @throws UnsupportedEncodingException
* @throws Exception
*/
public static DefaultPieDataset createPieDataset()
throws UnsupportedEncodingException, Exception {
DefaultPieDataset pieDataset = new DefaultPieDataset();
String[] TypeName = { "java", "c++", "vb", "sheel", "ruby" };
for (int i = 0; i < TypeName.length - 2; i++) {
pieDataset.setValue(TypeName[i].toString(), 0);
}
pieDataset.setValue("sheel", 0);
pieDataset.setValue("ruby", null);
// pieDataset.
return pieDataset;
}
/**
* plot对象设置的方法,提供配置饼图扇区颜色的方法
*
* @param pieplot
*/
@SuppressWarnings("deprecation")
public static void setSection(PiePlot pieplot) {
// 扇区颜色设置
pieplot.setSectionPaint(0, new Color(153, 153, 255));
pieplot.setSectionPaint(1, new Color(153, 53, 102));
pieplot.setSectionPaint(2, new Color(255, 255, 204));
pieplot.setSectionPaint(3, new Color(204, 255, 255));
pieplot.setSectionPaint(4, new Color(102, 0, 102));// 紫色
pieplot.setSectionPaint(5, new Color(255, 128, 128));// 桃红
pieplot.setSectionPaint(6, new Color(0, 102, 204));// 蓝
pieplot.setSectionPaint(7, new Color(204, 204, 255));
// 设置扇区分离显示
// pieplot.setExplodePercent("测试数据1", 0.2D);
// 设置扇区边框不可见
// pieplot.setSectionOutlinesVisible(false);
}
/**
* 设置扇区标题格式的方法类
*
* @param pieplot
*/
public static void setLabel(PiePlot pieplot) {
// 设置扇区标签显示格式:关键字:值(百分比)
// pieplot.setLabelGenerator(new StandardPieSectionLabelGenerator(
// "{0}:{1}({2} percent)"));
pieplot.setLabelGenerator(new StandardPieSectionLabelGenerator(
"{0} {2}", NumberFormat.getNumberInstance(), new DecimalFormat(
"0.00%")));
// 设置扇区标签颜色
pieplot.setLabelBackgroundPaint(new Color(220, 220, 220));
pieplot.setLabelFont((new Font("宋体", Font.PLAIN, 15)));// 设置扇区标签字体大小
}
/**
* 设置饼图对象没数据时所显示内容和字体
*
* @param pieplot
*/
public static void setNoDataMessage(PiePlot pieplot) {
// 设置没有数据时显示的信息
pieplot.setNoDataMessage("无数据");
// 设置没有数据时显示的信息的字体
pieplot.setNoDataMessageFont(new Font("宋体", Font.ITALIC, 20));
// 设置没有数据时显示的信息的颜色
pieplot.setNoDataMessagePaint(Color.orange);
}
/**
* 设置饼图对象数据是否忽略0和null值
*
* @param piePlot
*/
public static void setNullAndZeroValue(PiePlot piePlot) {
// 设置是否忽略0和null值
// piePlot.setIgnoreNullValues(false);
// piePlot.setIgnoreZeroValues(false);
}
public static void main(String[] args) throws IOException, Exception {
createPieChart();
}
}