一、JFreeChart简介
JFreeChart是JAVA平台上的一个开放的图表绘制类库。它完全使用JAVA语言编写,是为applications,servlets以及JSP等使用所设计。
JFreeChart可生成饼图(pie charts)、柱状图(bar charts)、散点图(scatter plots)、时序图(time series)、甘特图(Gantt charts)等等多种图表,并且可以产生PNG和JPEG格式的输出,还可以与PDF和EXCEL关联。
二、JFreeChart的使用
1、加入jar包
jcommon-1.0.13.jar
jfreechart-1.0.10.jar
2、创建DefaultPieDataSet对象,用来保存图表中要显示的数据
DefaultPieDataSet dataSet = new DefaultPieDataSet();
dataSet.setValue("男生",20);
dataSet.setValue("女生",50);
3、创建图表数据对应的JFreeChart对象
JFreeChart chart = ChartFactory.createPieChart3D("男女比例图",dataSet);
4、将图表数据写入文件
File file = new File("pie.jpg");
ChartUtilities.saveChartAsJPEG(file,chart,500,300);
5、修饰
①.设置“标题”部分字体、风格、字号
chart.getTitle().setFont(new Font("隶书",Font.BOLD,50));
②.设置“图例”部分信息"字体、风格、字号
chart.getLegend().setItemFont(new Font("宋体",Font.PLAIN,20));
③.通过PiePlot对象设置绘图区信息
//获取代表当前图表绘图区的PiePlot对象
PiePlot plot = (PiePlot)chart.getPlot();
//设置标签字体、风格、字号
plot.setLabelFont(new Font("微软雅黑",Font.ITALIC,15));
//设置前景色半透明
plot.setForegroundAlpha(0.6f);
//设置标签信息格式
plot.setLabelGenerator(new StandardPieSectionLabelGenerator("{0},{1}/{3},{2}"));
View Code
0:标签本身
1:当前标签对应的数量
2:百分比
3:总数量
案例1:
//1.
DefaultPieDataset dataSet = new DefaultPieDataset();
dataSet.setValue("华为", 20);
dataSet.setValue("小米", 30);
dataSet.setValue("中兴", 10);
dataSet.setValue("锤子", 10);
//2.
JFreeChart chart = ChartFactory.createPieChart("手机市场占有份额", dataSet, true, true, true);
//设置标题部分
chart.getTitle().setFont(new Font("微软雅黑",Font.ITALIC,50));
//设置图例部分
chart.getLegend().setItemFont(new Font("华文新魏",Font.BOLD,40));
//设置绘图区
//1.获取绘图区对象
PiePlot3D plot = (PiePlot3D)chart.getPlot();
//2.设置绘图区字体
plot.setLabelFont(new Font("华文新魏",Font.BOLD,40));
//3.设置绘图区前景色透明度
plot.setForegroundAlpha(0.6f);
//4.设置标签信息格式
plot.setLabelGenerator(new StandardPieSectionLabelGenerator("{0},{1}/{3},{2}"));
//3.
ChartUtilities.saveChartAsPNG(new File("1.png"), chart, 700, 500);
View Code
案例2:代码演示:【JFreeChart创建垂直/横向柱状报表】
1)Java代码
public class BarChart01 {
public static String getBarChart(HttpSession session){
//1.构建数据集
DefaultCategoryDataset dataSet = new DefaultCategoryDataset();
dataSet.addValue(510,"河南", "面食");
dataSet.addValue(580,"黑龙江", "大米");
dataSet.addValue(390,"陕西", "泡馍");
dataSet.addValue(420,"广东", "杂食");
//2.用Chart工厂生成JFreeChar图表
JFreeChart barChart3D = ChartFactory.createBarChart3D("面食消费统计图","面食", "比例", dataSet, PlotOrientation.VERTICAL, true, true, true);
//生成png图片
String chartAsPNG =null;
try {
chartAsPNG= ServletUtilities.saveChartAsPNG(barChart3D, 700, 500, null, session);
} catch (IOException e) {
e.printStackTrace();
}
return chartAsPNG;
}
}
View Code
2)在web.xml文件中配置如下:
<servlet>
<servlet-name>DisplayChart</servlet-name>
<servlet-class>org.jfree.chart.servlet.DisplayChart</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>DisplayChart</servlet-name>
<url-pattern>/DisplayChart</url-pattern>
</servlet-mapping>
View Code
3)index.jsp页面【需要注意的是:参数名必须是filename】
<body>
<%
String fileName = BarChart01.getBarChart(session);
%>
<img src="${pageContext.request.contextPath}/DisplayChart?filename=<%=fileName%>">
</body>
View Code
三、CategoryDataset 分类数据集引入
1、利用JFreeChart 创建按颜色分类的水果销售报表
1)编写Java代码,如下所示:
public class BarChart03 {
public static String getBarChart(HttpSession session){
//1.构建数据集
double [][] dataSet = new double[][]{{1330},{740},{660},{880}};
String[] rowKeys = new String[]{"苹果","香蕉","橘子","梨子"};
String[] columnKeys = new String[]{"深圳"};
CategoryDataset dataset = DatasetUtilities.createCategoryDataset(rowKeys, columnKeys, dataSet);
//2.用Chart工厂生成JFreeChar图表
JFreeChart barChart3D = ChartFactory.createBarChart3D("面食消费统计图","水果", "数量", dataset, PlotOrientation.VERTICAL, true, true, true);
//生成png图片
String chartAsPNG =null;
try {
chartAsPNG= ServletUtilities.saveChartAsPNG(barChart3D, 700, 500, null, session);
} catch (IOException e) {
e.printStackTrace();
}
return chartAsPNG;
}
}
View Code
2)在web.xml文件中配置如下:
<servlet>
<servlet-name>DisplayChart</servlet-name>
<servlet-class>org.jfree.chart.servlet.DisplayChart</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>DisplayChart</servlet-name>
<url-pattern>/DisplayChart</url-pattern>
</servlet-mapping>
View Code
3)在index.jsp页面中
<body>
<%
String fileName = BarChart03.getBarChart(session);
%>
<img src="${pageContext.request.contextPath}/DisplayChart?filename=<%=fileName%>">
</body>
View Code
2、利用JFreeChart 创建按颜色分类并且按地区分类水果销售报表
- 编写Java代码
public class BarChart03 {
public static String getBarChart(HttpSession session){
//1.构建数据集
double [][] dataSet = new double[][]{{1330,770,880,990},{740,760,890,890},{660,760,780,980},{880,790,880,790}};
String[] rowKeys = new String[]{"苹果","香蕉","橘子","梨子"};
String[] columnKeys = new String[]{"深圳","上海","海南","广东"};
CategoryDataset dataset = DatasetUtilities.createCategoryDataset(rowKeys, columnKeys, dataSet);
//2.用Chart工厂生成JFreeChar图表
JFreeChart barChart3D = ChartFactory.createBarChart3D("水果销量统计图","水果", "销量", dataset, PlotOrientation.VERTICAL, true, true, true);
//生成png图片
String chartAsPNG =null;
try {
chartAsPNG= ServletUtilities.saveChartAsPNG(barChart3D, 700, 500, null, session);
} catch (IOException e) {
e.printStackTrace();
}
return chartAsPNG;
}
}
View Code
2)在web.xml文件中配置如下:
<servlet>
<servlet-name>DisplayChart</servlet-name>
<servlet-class>org.jfree.chart.servlet.DisplayChart</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>DisplayChart</servlet-name>
<url-pattern>/DisplayChart</url-pattern>
</servlet-mapping>
View Code
3)在index.jsp页面中
<body>
<%
String fileName = BarChart03.getBarChart(session);
%>
<img src="${pageContext.request.contextPath}/DisplayChart?filename=<%=fileName%>">
</body>
View Code
四、CategoryPlot ,BarRender3D 引入
1、利用JFreeChart 创建自定义3D 柱状报表
1).Java代码编写
public class BarChart03 {
public static String getBarChart(HttpSession session){
//1.构建数据集
double [][] dataSet = new double[][]{{1330,770,880,990},{740,760,890,890},{660,760,780,980},{880,790,880,790}};
String[] rowKeys = new String[]{"苹果","香蕉","橘子","梨子"};
String[] columnKeys = new String[]{"深圳","上海","海南","广东"};
CategoryDataset dataset = DatasetUtilities.createCategoryDataset(rowKeys, columnKeys, dataSet);
//2.用Chart工厂生成JFreeChar图表
JFreeChart barChart3D = ChartFactory.createBarChart3D("水果销量统计图","水果", "销量", dataset, PlotOrientation.VERTICAL, true, true, true);
//这里换成了getCategoryPlot方法!
CategoryPlot plot = barChart3D.getCategoryPlot();
plot.setBackgroundPaint(Color.white);
// 设置网格竖线颜色
plot.setOutlinePaint(Color.pink);
// 设置网格横线颜色
plot.setOutlinePaint(Color.pink);
// 显示每个柱的数值,并修改该数值的字体属性
BarRenderer3D renderer=new BarRenderer3D();
renderer.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator());
renderer.setBaseItemLabelsVisible(true);
renderer.setBasePositiveItemLabelPosition(new ItemLabelPosition(ItemLabelAnchor.OUTSIDE12, TextAnchor.BASELINE_LEFT));
renderer.setItemLabelAnchorOffset(10D);
// 设置平行柱的之间距离
renderer.setItemMargin(0.4);
plot.setRenderer(renderer);
//生成png图片
String chartAsPNG =null;
try {
chartAsPNG= ServletUtilities.saveChartAsPNG(barChart3D, 700, 500, null, session);
} catch (Exception e) {
e.printStackTrace();
}
return chartAsPNG;
}
}
View Code
2).在web.xml文件中配置如下:
<servlet>
<servlet-name>DisplayChart</servlet-name>
<servlet-class>org.jfree.chart.servlet.DisplayChart</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>DisplayChart</servlet-name>
<url-pattern>/DisplayChart</url-pattern>
</servlet-mapping>
View Code
3).在index.jsp页面中
<body>
<%
String fileName = BarChart03.getBarChart(session);
%>
<img src="${pageContext.request.contextPath}/DisplayChart?filename=<%=fileName%>">
</body>
View Code
五、饼状图
1、DefaultPieDataset,PiePlot,PiePlot3D 引入
2、利用JFreeChart 创建普通饼状图报表
1)Java代码
public class PieChart1 {
public static String getPieChart(HttpSession session) throws Exception{
//1.拿到数据集
DefaultPieDataset dataSet = new DefaultPieDataset();
dataSet.setValue("苹果",1000);
dataSet.setValue("梨子",800);
dataSet.setValue("香蕉",1200);
dataSet.setValue("菠萝",400);
//2.利用Chart工厂生产对象
JFreeChart chart = ChartFactory.createPieChart("人均每年水果消耗量", dataSet, true, true, true);
chart.addSubtitle(new TextTitle("2013年度"));
PiePlot pieplot=(PiePlot)chart.getPlot();
pieplot.setLabelFont(new Font("宋体",0,11));
// 设置饼图是圆的(true),还是椭圆的(false);默认为true
pieplot.setCircular(true);
// 没有数据的时候显示的内容
pieplot.setNoDataMessage("无数据显示");
StandardPieSectionLabelGenerator standarPieIG = new StandardPieSectionLabelGenerator("{0}:({1}.{2})", NumberFormat.getNumberInstance(), NumberFormat.getPercentInstance());
pieplot.setLabelGenerator(standarPieIG);
//3.
String png = ServletUtilities.saveChartAsPNG(chart, 700, 500, null, session);
return png;
}
}
View Code
2)在web.xml文件中配置如下:
<servlet>
<servlet-name>DisplayChart</servlet-name>
<servlet-class>org.jfree.chart.servlet.DisplayChart</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>DisplayChart</servlet-name>
<url-pattern>/DisplayChart</url-pattern>
</servlet-mapping>
View Code
3)在index.jsp页面中
<body>
<%
String fileName = BarChart03.getBarChart(session);
%>
<img src="${pageContext.request.contextPath}/DisplayChart?filename=<%=fileName%>">
</body>
View Code
3、利用JFreeChart 创建普通饼状图报表,并且突出一个
只需要在原来的java代码中加入:
pieplot.setExplodePercent("菠萝",0.23);
即可将这一栏突出显示;
4、利用JFreeChart 创建3D 饼状图报表
只需要将:
JFreeChart chart = ChartFactory.createPieChart("人均每年水果消耗量", dataSet, true, true, true);
改为:
JFreeChart chart = ChartFactory.createPieChart3D("人均每年水果消耗量", dataSet,true, true, true);
即可创建3D饼状图报表!
5、针对3D饼状图添加 透明度等设置
【只需要在上面的代码中加入如下的设置】:
//设置开始角度
pieplot.setStartAngle(120D);
//设置方向为”顺时针方向“
pieplot.setDirection(Rotation.CLOCKWISE);
//设置透明度,0.5F为半透明,1为不透明,0为全透明
pieplot.setForegroundAlpha(0.7F);
View Code
六、SpringMVC整合JFreeChart
1、在springMVC的controller代码中:
@RequestMapping("/testView")
public ModelAndView testView(HttpServletRequest request) throws Exception{
ModelAndView mv = new ModelAndView();
//1.构建数据集
DefaultCategoryDataset dataSet = new DefaultCategoryDataset();
dataSet.addValue(510,"河南", "面食");
dataSet.addValue(580,"黑龙江", "大米");
dataSet.addValue(390,"陕西", "泡馍");
dataSet.addValue(420,"广东", "杂食");
//2.用Chart工厂生成JFreeChar图表
JFreeChart barChart3D = ChartFactory.createBarChart3D("面食消费统计图","面食", "比例", dataSet, PlotOrientation.VERTICAL, true, true, true);
// 6. 将图形转换为图片,传到前台
String fileName = ServletUtilities.saveChartAsJPEG(barChart3D, 700, 400, null, request.getSession());
String chartURL = request.getContextPath() + "/chart?filename=" + fileName;
mv.addObject("fileName", fileName);
mv.setViewName("success");
return mv;
}
View Code
2、在web.xml文件中配置如下:
<servlet>
<servlet-name>DisplayChart</servlet-name>
<servlet-class>org.jfree.chart.servlet.DisplayChart</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>DisplayChart</servlet-name>
<url-pattern>/chart</url-pattern>
</servlet-mapping>
View Code
3、在index.jsp页面:
<a href="${pageContext.request.contextPath }/testView">查看详情</a>
View Code
4、在success.jsp页面
<body>
<img src="${pageContext.request.contextPath }/chart?filename=${fileName}">
</body>
View Code