前段时间学习了一下JFreeChart,现在来整理一下自己所作的实例。
下面分别用两种方式来实现: 一种是以java应用程序的方式,一种是以web项目程序的方式
需要加入的jar包有: jcommon-1.0.17.jar 、 jfreechart-1.0.14.jar(前两个是JFreeChart中所带的,在下载的JFreeChart的lib目录下) 、 struts2-jfreechart-plugin-2.3.16.3.jar(这个是Struts2所带的,在下载的Struts2的lib目录下)、struts2所常用的9个核心jar包 。 jar包的版本可以有所不同
上述jar包放入到项目的WebRoot/WEB-INF/lib目录下
PieChart3DTest.java
package com.jfreechart.test;
import java.awt.Font;
import java.io.File;
import java.io.IOException;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.labels.StandardPieSectionLabelGenerator;
import org.jfree.chart.plot.PiePlot3D;
import org.jfree.chart.title.LegendTitle;
import org.jfree.chart.title.TextTitle;
import org.jfree.data.general.DefaultPieDataset;
import org.jfree.data.general.PieDataset;
import org.jfree.util.Rotation;
public class PieChart3DTest {
public static void main(String[] args) throws IOException{
//步骤1:创建CategoryDataset对象(准备数据)
PieDataset dataset = createDataset();
//步骤2:根据Dataset 生成JFreeChart对象,以及做相应的设置
JFreeChart jfreeChart = createChart(dataset);
//步骤3:将JFreeChart对象输出到文件
saveAsFile("F:\\PieChart3D.jpg", jfreeChart, 800, 600);
}
/**
* 创建一个dataset,该dataset包含图表要显示的数据
* @return DefaultPieDataset
*/
private static DefaultPieDataset createDataset() {
//DefaultPieDataset是默认的饼形图数据集合对象类,可用于创建饼形图数据集合对象
DefaultPieDataset dataset = new DefaultPieDataset();
//为dataset对象设值
dataset.setValue("文学类",20000);
dataset.setValue("科技类",42000);
dataset.setValue("财经类",21000);
dataset.setValue("娱乐类",29000);
//返回数据集合对象
return dataset;
}
/**
* 根据PieDataset创建JFreeChart对象
* @return JFreeChart
*/
public static JFreeChart createChart(PieDataset pieDataset) {
//JFreeChart类是一个制图对象类,先用它来创建一个制图对象chart
//ChartFactory类是制图工厂类,用它来为制图对象chart完成实例化
//createPieChart3D()是制图工厂的一个方法,用来创建一个3D的饼形图对象
JFreeChart chart = ChartFactory.createPieChart3D(
"图书销量统计图", //图表标题
pieDataset, //数据集
true, //是否显示图例
false, //是否采用标准生成器
false //是否支持超链接
);
//通过JFreeChart对象的 setTitle方法,修改统计图表的标题部分(包括修改图表标题内容、字体大小等)
chart.setTitle(new TextTitle("图书销量统计图", new Font("黑体", Font.ITALIC , 22)));
//调用 JFreeChart对象的 getLegend(int index)方法,取得该图表的指定索引的图例对象,通过 LegendTitle对象来修改统计图表的图例
LegendTitle legend = chart.getLegend(0);
//设置图例的字体和字体大小,即位于下方的字的字体和大小
legend.setItemFont(new Font("宋体", Font.BOLD, 14));
// 设置画布背景色
//chart.setBackgroundPaint(new Color(192, 228, 106));
//取得饼图3D的绘图(plot3D)对象
PiePlot3D plot3D = (PiePlot3D)chart.getPlot();
//设置Pie图的分类标签的字体,即位于Pie图的中间部分的每个扇形旁边对应的字的字体
plot3D.setLabelFont(new Font("隶书", Font.BOLD, 16));
//设置数据区的背景透明度,范围在0.0~1.0间
plot3D.setBackgroundAlpha(0.9F);
//设置PieChart是否显示为圆形
plot3D.setCircular(true);
//完全关闭片区外廓,若将值设为 true 即可让外廓显示出来
plot3D.setSectionOutlinesVisible(false);
//设置忽略零值,不设值时默认会将一个标签放置在饼图片区显示的位置,并且在图表的图例中添加一个分类。
//plot3D.setIgnoreZeroValues(true);
//设置忽略null值,不设值时默认情况跟零值一样
//plot3D.setIgnoreNullValues(true);
//设置旋转角度
plot3D.setStartAngle(90.0);
//设置旋转方向,Rotation.CLOCKWISE)为顺时针。
plot3D.setDirection(Rotation.CLOCKWISE);
//设置图表透明图0.0~1.0范围。0.0为完全透明,1.0为完全不透明。
plot3D.setForegroundAlpha(0.4F);
//设置每个数据区的填充颜色,不设置即默认,默认时是自动分配的
//plot3D.setSectionPaint("文学类", new Color(200, 255, 255));
// 图片中显示百分比:自定义方式,{0} 表示选项, {1} 表示数值, {2} 表示所占比例 ,小数点后两位
plot3D.setLabelGenerator(new StandardPieSectionLabelGenerator("{0}={1}({2})", NumberFormat.getNumberInstance(),new DecimalFormat("0.00%")));
// 图例显示百分比:自定义方式, {0} 表示选项, {1} 表示数值, {2} 表示所占比例
//plot3D.setLegendLabelGenerator(new StandardPieSectionLabelGenerator("{0}={1}({2})"));
return chart;
}
/**
* 保存图表为文件
*/
public static void saveAsFile(String filePath, JFreeChart jfreeChart,
int weight, int height) throws IOException {
//输出图表到文件,saveCharAsJPEG()方法的参数(File file,JFreeChart chart,int width,int height)
ChartUtilities.saveChartAsJPEG(new File(filePath), jfreeChart, weight, height);
}
}</span><strong>
</strong>
package com.jfreechart.action;
import java.awt.Font;
import java.io.File;
import java.io.IOException;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.Map;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.labels.StandardPieSectionLabelGenerator;
import org.jfree.chart.plot.PiePlot3D;
import org.jfree.chart.title.LegendTitle;
import org.jfree.chart.title.TextTitle;
import org.jfree.data.general.DefaultPieDataset;
import org.jfree.util.Rotation;
import com.jfreechart.commons.FileUtil;
import com.opensymphony.xwork2.ActionContext;
public class PieChart3DAction {
private JFreeChart chart;
// 必须提供 getChart() 方法,且由该方法返回 JFreeChart 对象
public JFreeChart getChart() throws Exception {
//JFreeChart类是一个制图对象类,先用它来创建一个制图对象chart
//ChartFactory类是制图工厂类,用它来为制图对象chart完成实例化
//createPieChart3D()是制图工厂的一个方法,用来创建一个3D的饼形图对象
chart = ChartFactory.createPieChart3D(
"图书销量统计图", //图表标题
createDataset(), //数据集
true, //是否显示图例
false, //是否采用标准生成器
false //是否支持超链接
);
//通过JFreeChart对象的 setTitle方法,修改统计图表的标题部分(包括修改图表标题内容、字体大小等)
chart.setTitle(new TextTitle("图书销量统计图", new Font("黑体", Font.ITALIC , 22)));
//调用 JFreeChart对象的 getLegend(int index)方法,取得该图表的指定索引的图例对象,通过 LegendTitle对象来修改统计图表的图例
LegendTitle legend = chart.getLegend(0);
//设置图例的字体和字体大小,即位于下方的字的字体和大小
legend.setItemFont(new Font("宋体", Font.BOLD, 14));
// 设置画布背景色
//chart.setBackgroundPaint(new Color(192, 228, 106));
//取得饼图3D的绘图(plot3D)对象
PiePlot3D plot3D = (PiePlot3D)chart.getPlot();
//设置Pie图的分类标签的字体,即位于Pie图的中间部分的每个扇形旁边对应的字的字体
plot3D.setLabelFont(new Font("隶书", Font.BOLD, 16));
//设置数据区的背景透明度,范围在0.0~1.0间
plot3D.setBackgroundAlpha(0.9F);
//设置PieChart是否显示为圆形
plot3D.setCircular(true);
//完全关闭片区外廓,若将值设为 true 即可让外廓显示出来
plot3D.setSectionOutlinesVisible(false);
//设置忽略零值,不设值时默认会将一个标签放置在饼图片区显示的位置,并且在图表的图例中添加一个分类。
//plot3D.setIgnoreZeroValues(true);
//设置忽略null值,不设值时默认情况跟零值一样
//plot3D.setIgnoreNullValues(true);
//设置旋转角度
plot3D.setStartAngle(90.0);
//设置旋转方向,Rotation.CLOCKWISE)为顺时针。
plot3D.setDirection(Rotation.CLOCKWISE);
//设置图表透明图0.0~1.0范围。0.0为完全透明,1.0为完全不透明。
plot3D.setForegroundAlpha(0.4F);
//设置每个数据区的填充颜色,不设置即默认,默认时是自动分配的
//plot3D.setSectionPaint("文学类", new Color(200, 255, 255));
// 图片中显示百分比:自定义方式,{0} 表示选项, {1} 表示数值, {2} 表示所占比例 ,小数点后两位
plot3D.setLabelGenerator(new StandardPieSectionLabelGenerator("{0}={1}({2})", NumberFormat.getNumberInstance(),new DecimalFormat("0.00%")));
// 图例显示百分比:自定义方式, {0} 表示选项, {1} 表示数值, {2} 表示所占比例
//plot3D.setLegendLabelGenerator(new StandardPieSectionLabelGenerator("{0}={1}({2})"));
//设置生成的图表的文件名
String fileName = "PieChart3DBook.jpg";
//设置图像输出的指定路径
String filePath = FileUtil.getWebRootPath()+"images\\chart\\"+fileName;
//输出图表到文件
saveAsFile(filePath, chart, 800, 600);
//取得request对象
Map request = (Map)ActionContext.getContext().get("request");
//把生成的图表文件的路径filePath放进request对象中
request.put("filePath", filePath);
return chart;
}
/**
* 保存图表为文件
*/
public static void saveAsFile(String filePath, JFreeChart jfreeChart,
int weight, int height) throws IOException {
//输出图表到文件,saveCharAsJPEG()方法的参数(File file,JFreeChart chart,int width,int height)
ChartUtilities.saveChartAsJPEG(new File(filePath), jfreeChart, weight, height);
}
/**
* 创建一个dataset,该dataset包含图表要显示的数据
* @return DefaultPieDataset
*/
private static DefaultPieDataset createDataset() {
//DefaultPieDataset是默认的饼形图数据集合对象类,可用于创建饼形图数据集合对象
DefaultPieDataset dataset = new DefaultPieDataset();
//为dataset对象设值
dataset.setValue("文学类",20000);
dataset.setValue("科技类",42000);
dataset.setValue("财经类",21000);
dataset.setValue("娱乐类",29000);
//返回数据集合对象
return dataset;
}
//在struts.xml中的对应<action>里,应该写的是 method="pieChart3D" 和 <result type="chart">
public String pieChart3D() {
return "success";
}
}
原文:http://blog.csdn.net/wangcunhuazi/article/details/40920665