jfreechart详解一饼形图

<div class="iteye-blog-content-contain" style="font-size: 14px"></div>
 package com.xlk.jfreechart;
 
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridLayout;
import java.io.File;
import java.io.IOException;
 
import javax.swing.JPanel;
 
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.labels.StandardPieSectionLabelGenerator;
import org.jfree.chart.plot.MultiplePiePlot;
import org.jfree.chart.plot.PiePlot;
import org.jfree.chart.plot.PiePlot3D;
import org.jfree.chart.title.TextTitle;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.general.DatasetUtilities;
import org.jfree.data.general.DefaultPieDataset;
import org.jfree.data.general.PieDataset;
import org.jfree.util.Rotation;
import org.jfree.util.TableOrder;
import org.junit.Test;
 
 
/**
 * 关于pie图的一系列操作
 * 
 * @author star
 * 
 */
public class PieChartDemo {
/**
*
*/
private static final long serialVersionUID = 2598557557724085474L;
//中文乱码
public static final Font font = new Font("宋体", Font.BOLD, 20);
 
private static PieDataset createDataset() {
DefaultPieDataset defaultpiedataset = new DefaultPieDataset();
defaultpiedataset.setValue("One", new Double(43.2));
defaultpiedataset.setValue("Two", new Double(10.0));
defaultpiedataset.setValue("Three", new Double(27.5));
defaultpiedataset.setValue("Four", new Double(17.5));
defaultpiedataset.setValue("Five", new Double(11.0));
defaultpiedataset.setValue("Six", new Double(19.4));
return defaultpiedataset;
}
/**
* 
*@time: 2013-5-7
*@author: star
* @Title: createChart 
* @Description: TODO(创建普通饼图) 
* @return JFreeChart    返回类型 
* @throws
*/
private static JFreeChart createPie2DChart(PieDataset piedataset) {
JFreeChart jfreechart = ChartFactory.createPieChart("普通饼图",piedataset, true, true, false);
 
//得到标题
TextTitle texttitle = jfreechart.getTitle();
//标题
texttitle.setFont(font);
texttitle.setToolTipText("A title tooltip!");
//得到绘图区
PiePlot pieplot = (PiePlot) jfreechart.getPlot();
//设置标签字体 
pieplot.setLabelFont(font);
pieplot.setNoDataMessage("No data available");
pieplot.setCircular(false);
pieplot.setLabelGap(0.02);
//提示条字体
jfreechart.getLegend().setItemFont(font);
 
return jfreechart;
}
@Test
public void createPie1(){
PieDataset dataset = createDataset();
JFreeChart chart = createPie2DChart(dataset);
try {
ChartUtilities.saveChartAsJPEG(new File("E:/temp/pie01.jpg"), chart, 800, 400);
} catch (IOException e) {
System.out.println("生成图片出错");
e.printStackTrace();
}
}
jfreechart详解一饼形图_第1张图片
 
/**
* 
*@time: 2013-5-7
*@author: star
* @Title: createPie2DChart2 
* @Description: TODO(取出片显示图) 
* @return JFreeChart    返回类型 
* @throws
*/
/**
* setSectionPaint("One", new Color(160, 160, 255)):设置某个片区的填充颜色。
第一个参数为片区的标识,第二个参数为色值。
 
setNoDataMessage("Nodata available"): 设置 dataset 为 null 时显示的提示信息。
 
setLabelGenerator(new StandardPieSectionLabelGenerator("{0}({2}
percent)")):设置标签显示的格式。
 
setLabelBackgroundPaint(new Color(220, 220, 220)):设置标签的背景颜色。
 
setLegendLabelToolTipGenerator(new
StandardPieSectionLabelGenerator("Tooltip for legend item {0}")):设置鼠标
滑过图表是显示鼠标当前片区的提示信息。
 
pieplot.setExplodePercent("Two", 0.5):将第2个片区取出显示。后面一个参数是
取出的距离,是一个比例数
*/
public static JFreeChart createPie2DChart2(PieDataset dataset){
JFreeChart chart = ChartFactory.createPieChart("取出显示片", dataset, true, true, false);
//设置标题字体
chart.getTitle().setFont(font);
PiePlot pieplot = (PiePlot) chart.getPlot();
pieplot.setSectionPaint("One", new Color(160, 160, 255));
pieplot.setSectionPaint("Two", new Color(128, 128, 223));
pieplot.setSectionPaint("Three", new Color(96, 96, 191));
pieplot.setSectionPaint("Four", new Color(64, 64, 159));
pieplot.setSectionPaint("Five", new Color(32, 32, 127));
pieplot.setSectionPaint("Six", new Color(0, 0, 111));
pieplot.setNoDataMessage("没有数据");
//取出的片
pieplot.setExplodePercent("Two", 0.5);
pieplot.setLabelGenerator(new  StandardPieSectionLabelGenerator("{0} ({2} percent)"));
pieplot.setLabelBackgroundPaint(new Color(20, 20, 220));
pieplot.setLegendLabelToolTipGenerator(new StandardPieSectionLabelGenerator("Tooltip for legend item {0}"));
return chart;
}
@Test
public void createPie2DChart2Test(){
PieDataset dataset = createDataset();
JFreeChart  chart = createPie2DChart2(dataset);
try {
ChartUtilities.saveChartAsJPEG(new File("E:/temp/pie2.jpg"), chart, 800, 400);
} catch (IOException e) {
System.out.println("生成图片错误    ");
e.printStackTrace();
}
}
jfreechart详解一饼形图_第2张图片
 
/**
* 
*@time: 2013-5-7
*@author: star
* @Title: createPie2DChart3 
* @Description: TODO(显示了dataset为空时,设置的提示信息) 
* @return JFreeChart    返回类型 
* @throws
*/
/**
* 程序代码说明:
 
setNoDataMessage("没有有效的数据显示!"):设置提示信息内容。
 
setNoDataMessageFont(new Font("Serif", 2, 10)):设置提示信息的字体和大小。
 
setNoDataMessagePaint(Color.red):设置提示信息字体的颜色。。
*/
public static JFreeChart createPie2DChart3(PieDataset dataset){
JFreeChart jfreechart = ChartFactory.createPieChart("没有数据",
null, true, true, false);
jfreechart.getTitle().setFont(font);
PiePlot pieplot = (PiePlot) jfreechart.getPlot();
pieplot.setNoDataMessage("没有有效的数据显示!");
pieplot.setNoDataMessageFont(new Font("黑体", 2, 20));
pieplot.setNoDataMessagePaint(Color.red);
return jfreechart;
}
@Test
public void createPie2DChart3Test(){
PieDataset dataset = createDataset();
JFreeChart  chart = createPie2DChart3(dataset);
try {
ChartUtilities.saveChartAsJPEG(new File("E:/temp/pie3.jpg"), chart, 800, 400);
} catch (IOException e) {
System.out.println("生成图片错误    ");
e.printStackTrace();
}
}
jfreechart详解一饼形图_第3张图片
 
/**
* 
*@time: 2013-5-7
*@author: star
* @Title: createPie2DChart4 
* @Description: TODO(多饼图) 
* @return JPanel    返回类型 
* @throws
*/
public static JPanel createPie2DChart4(PieDataset dataset){
JFreeChart jfreechart = ChartFactory.createPieChart("Chart 1", dataset, true, true, false);
jfreechart.addSubtitle(new TextTitle("setCircular(true);", new Font(
"Dialog", 0, 12)));
PiePlot pieplot = (PiePlot) jfreechart.getPlot();
pieplot.setCircular(true);
//
JFreeChart jfreechart_0_ = ChartFactory.createPieChart("Chart 2",
dataset, false, false, false);
jfreechart_0_.addSubtitle(new TextTitle("setCircular(false);",
new Font("Dialog", 0, 12)));
PiePlot pieplot_1_ = (PiePlot) jfreechart_0_.getPlot();
pieplot_1_.setCircular(false);
//
JFreeChart jfreechart_2_ = ChartFactory.createPieChart3D("Chart 3",
dataset, false, false, false);
jfreechart_2_.addSubtitle(new TextTitle("setCircular(true);", new Font(
"Dialog", 0, 12)));
PiePlot3D pieplot3d = (PiePlot3D) jfreechart_2_.getPlot();
pieplot3d.setForegroundAlpha(0.6F);
pieplot3d.setCircular(true);
//
JFreeChart jfreechart_3_ = ChartFactory.createPieChart3D("Chart 4",
dataset, false, false, false);
jfreechart_3_.addSubtitle(new TextTitle("setCircular(false);",
new Font("Dialog", 0, 12)));
PiePlot3D pieplot3d_4_ = (PiePlot3D) jfreechart_3_.getPlot();
pieplot3d_4_.setForegroundAlpha(0.6F);
pieplot3d_4_.setCircular(false);
 
 
JPanel jpanel = new JPanel(new GridLayout(2, 2));
jpanel.add(new ChartPanel(jfreechart));
jpanel.add(new ChartPanel(jfreechart_0_));
jpanel.add(new ChartPanel(jfreechart_2_));
jpanel.add(new ChartPanel(jfreechart_3_));
jpanel.setPreferredSize(new Dimension(800, 600));
return jpanel;
}
@Test
public void createPie2DChart4(){
 
}
/**
* sdsdsds
*@time: 2013-5-7
*@author: star
* @Title: createPie3DChart 
* @Description: TODO(生成3D饼图) 
* @return JFreeChart    返回类型 
* @throws
*/
/**
* 程序代码说明:
 
使用ChartFactory的方法createPieChart3D创建3D效果的饼图。
 
setStartAngle(180.0):设置旋转角度。
 
setDirection(Rotation.CLOCKWISE):设置旋转方向,Rotation.CLOCKWISE)为顺时针。
 
setForegroundAlpha(0.5F):设置图表透明图0.0~1.0范围。0.0为完全透明,1.0为
完全不透明
*/
public static JFreeChart createPie3DChart(PieDataset dataset){
JFreeChart chart = ChartFactory.createPieChart3D("3D饼图", dataset, true, true, false);
chart.getTitle().setFont(font);
PiePlot3D piePlot3D = (PiePlot3D) chart.getPlot();
piePlot3D.setStartAngle(180);
piePlot3D.setDirection(Rotation.CLOCKWISE);
piePlot3D.setForegroundAlpha(0.5F);
piePlot3D.setNoDataMessage("No data to display");
return chart;
}
@Test
public void createPie3DChartTest(){
PieDataset dataset = createDataset();
JFreeChart  chart = createPie3DChart(dataset);
try {
ChartUtilities.saveChartAsJPEG(new File("E:/temp/pie3D01.jpg"), chart, 800, 400);
} catch (IOException e) {
System.out.println("生成图片错误    ");
e.printStackTrace();
}
}
jfreechart详解一饼形图_第4张图片
 
/**
* 
*@time: 2013-5-7
*@author: star
* @Title: createPie3DChart2 
* @Description: TODO(创建3D多饼图) 
* @return JFreeChart    返回类型 
* @throws
*/
/**
* 程序代码说明:
 
使用ChartFactory的方法createMultiplePieChart()创建多个饼图的图表。
 
multiplepieplot.getPieChart():获得单个饼图的图表。
*/
public static JFreeChart createPie3DChart2(CategoryDataset categorydataset){
JFreeChart jfreechart = ChartFactory.createMultiplePieChart("3D多饼图", categorydataset, TableOrder.BY_ROW, true,true, false);
jfreechart.getTitle().setFont(font);
MultiplePiePlot multiplepieplot = (MultiplePiePlot) jfreechart.getPlot();
JFreeChart jfreechart_0_ = multiplepieplot.getPieChart();
PiePlot pieplot = (PiePlot) jfreechart_0_.getPlot();
pieplot.setLabelGenerator(new StandardPieSectionLabelGenerator("{0}"));
pieplot.setLabelFont(font);
pieplot.setInteriorGap(0.3);
return jfreechart;
}
private static CategoryDataset createCategoryDataset() {
double[][] ds = { { 3.0, 4.0, 3.0, 5.0 }, { 5.0, 7.0, 6.0, 8.0 },
{ 5.0, 7.0, Double.NaN, 3.0 }, { 1.0, 2.0, 3.0, 4.0 },
{ 2.0, 3.0, 2.0, 3.0 } };
CategoryDataset categorydataset = DatasetUtilities.createCategoryDataset("pie ", "Ss/Q", ds);
return categorydataset;
}
@Test
public void createPie3DChar2tTest(){
CategoryDataset dataset = createCategoryDataset();
JFreeChart  chart = createPie3DChart2(dataset);
try {
ChartUtilities.saveChartAsJPEG(new File("E:/temp/pie3D02.jpg"), chart, 800, 400);
} catch (IOException e) {
System.out.println("生成图片错误    ");
e.printStackTrace();
}
}
jfreechart详解一饼形图_第5张图片
 
}
 

 

你可能感兴趣的:(java,jfreechart)