Java 报表之JFreeChart(第一讲)

Posted 啊远

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java 报表之JFreeChart(第一讲)相关的知识,希望对你有一定的参考价值。

 

1、利用 JFreeChart 创建垂直柱状报表

 1 package com.wcy.chart.bar;
 2 
 3 import javax.servlet.http.HttpSession;
 4 
 5 import org.jfree.chart.ChartFactory;
 6 import org.jfree.chart.JFreeChart;
 7 import org.jfree.chart.plot.PlotOrientation;
 8 import org.jfree.chart.servlet.ServletUtilities;
 9 import org.jfree.data.category.DefaultCategoryDataset;
10 
11 public class BarChart1 {
12 
13     public static String genBarChart(HttpSession session) throws Exception {
14         DefaultCategoryDataset dataset = new DefaultCategoryDataset();
15         dataset.addValue(510, "深圳", "苹果");
16         dataset.addValue(320, "深圳", "香蕉");
17         dataset.addValue(580, "深圳", "橘子");
18         dataset.addValue(390, "深圳", "梨子");
19         JFreeChart chart=ChartFactory.createBarChart3D("水果销售统计图", "水果", "销售", dataset,
20                 PlotOrientation.VERTICAL, true, true, true);
21         String fileName=ServletUtilities.saveChartAsPNG(chart, 700, 500, null,session);
22         return fileName;
23     }
24 }
 1 <%@page import="com.wcy.chart.bar.BarChart1"%>
 2 <%@ page language="java" contentType="text/html; charset=UTF-8"
 3     pageEncoding="UTF-8"%>
 4 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 5 <html>
 6 <head>
 7 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 8 <title>Insert title here</title>
 9 </head>
10 <body>
11 <%
12     String fileName = BarChart1.genBarChart(session);
13 %>
14 <img src="DisplayChart?filename=<%=fileName %>" width="700" height="500" border="0">
15 </body>
16 </html>

2、利用 JFreeChart 创建横向柱状报表

 1 package com.wcy.chart.bar;
 2 
 3 import javax.servlet.http.HttpSession;
 4 
 5 import org.jfree.chart.ChartFactory;
 6 import org.jfree.chart.JFreeChart;
 7 import org.jfree.chart.plot.PlotOrientation;
 8 import org.jfree.chart.servlet.ServletUtilities;
 9 import org.jfree.data.category.DefaultCategoryDataset;
10 
11 public class BarChart2 {
12 
13     public static String genBarChart(HttpSession session)throws Exception{
14         DefaultCategoryDataset dataset = new DefaultCategoryDataset();
15         dataset.addValue(510, "深圳", "苹果");
16         dataset.addValue(320, "深圳", "香蕉");
17         dataset.addValue(580, "深圳", "橘子");
18         dataset.addValue(390, "深圳", "梨子");
19         JFreeChart chart = ChartFactory.createBarChart3D("水果销量统计图", "水果", "销售", dataset, PlotOrientation.HORIZONTAL, true,true, true);
20         String fileName = ServletUtilities.saveChartAsPNG(chart, 700, 500, null, session);
21         return fileName;
22     }
23 }
 1 <%@page import="com.wcy.chart.bar.BarChart2"%>
 2 <%@ page language="java" contentType="text/html; charset=UTF-8"
 3     pageEncoding="UTF-8"%>
 4 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 5 <html>
 6 <head>
 7 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 8 <title>Insert title here</title>
 9 </head>
10 <body>
11 <%
12     String fileName = BarChart2.genBarChart(session);
13 %>
14 <img  src="DisplayChart?filename=<%=fileName %>" width="700" height="500" border="0">
15 </body>
16 </html>

 声明:此程序代码本人只是用于学习总结,非原创,如有侵权,联系本人。

以上是关于Java 报表之JFreeChart(第一讲)的主要内容,如果未能解决你的问题,请参考以下文章

JFreechart在linux下不显示及中文乱码问题

JFreeChart - Java 内存泄漏

JFreeChart 之柱状图

如何利用jfreechart生成柱状,饼状,折线形图形。

freemarker+Jfreechart生成Word文档(含图片)

数据库期末复习之第一讲:绪论