用JFreeChart 生成报表

JFreeChart是JAVA平台上的一个开放的图表绘制类库。它完全使用JAVA语言编写,是为applications, applets, servlets 以及JSP等使用所设计。JFreeChart可生成饼图(pie charts)、柱状图(bar charts)、散点图(scatter plots)、时序图(time series)、甘特图(Gantt charts)等等多种图表,并且可以产生PNG和JPEG格式的输出,还可以与PDF和EXCEL关联。

它所有的方法都是静态的,用起来很简便.

生成饼图:

package cn.zhangao.jfreechart;
 
import java.awt.Font;
import java.awt.Image;
import java.io.File;
 
import javax.imageio.ImageIO;
 
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.PiePlot;
import org.jfree.data.general.DefaultPieDataset;
 
public class App {
    public static void main(String[] args) {
        try {
            //如果不使用Font,中文将显示不出来
            Font font = new Font("宋体", Font.BOLD, 15);
 
            DefaultPieDataset pds = new DefaultPieDataset();
            pds.setValue("sun", 100);
            pds.setValue("ibm", 300);
            pds.setValue("bea", 500);
            pds.setValue("oracle", 700);
            /**
             * 生成一个饼图的图表
             * 
             * 分别是:显示图表的标题、需要提供对应图表的DateSet对象、是否显示图例、是否生成贴士以及是否生成URL链接
             */
            JFreeChart chart = ChartFactory.createPieChart("标题", pds, true, false, true);
            //设置图片标题的字体
            chart.getTitle().setFont(font);
 
            //得到图块,准备设置标签的字体
            PiePlot plot = (PiePlot) chart.getPlot();
 
            //设置分裂效果,需要指定分裂出去的key
            plot.setExplodePercent("oracle", 0.3);
 
            //设置标签字体
            plot.setLabelFont(font);
 
            //设置图例项目字体
            chart.getLegend().setItemFont(font);
 
            /**
             * 设置开始角度(弧度计算)
             * 
             * 度    0°    30°        45°        60°        90°        120°    135°    150°    180°    270°    360°
             * 弧度    0    π/6        π/4        π/3        π/2        2π/3    3π/4    5π/6    π        3π/2    2π
             */
            plot.setStartAngle(new Float(3.14f / 2f));
 
            //设置背景图片,设置最大的背景
            Image img = ImageIO.read(new File("d:/sunset.jpg"));
            chart.setBackgroundImage(img);
 
            //设置plot的背景图片
            img = ImageIO.read(new File("d:/Water.jpg"));
            plot.setBackgroundImage(img);
 
            //设置plot的前景色透明度
            plot.setForegroundAlpha(0.7f);
 
            //设置plot的背景色透明度
            plot.setBackgroundAlpha(0.0f);
 
            //设置标签生成器(默认{0})
            //{0}:key {1}:value {2}:百分比 {3}:sum
            plot.setLabelGenerator(new StandardPieSectionLabelGenerator("{0}({1})/{2}"));
 
            //将内存中的图片写到本地硬盘
            ChartUtilities.saveChartAsJPEG(new File("d:/pie.jpg"), chart, 600, 300);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

效果:

还可以生成3D效果的图片,只需要轻松地修改几个调用的方法即可:

JFreeChart chart = ChartFactory.createPieChart3D("标题", pds, true, false, true);
PiePlot3D plot = (PiePlot3D) chart.getPlot();

其他地方都不用改.但是需要注意的是:3D不能使用分裂效果!

生成柱状图:

package cn.zhangao.jfreechart;
 
import java.awt.Font;
import java.io.File;
 
import javax.imageio.ImageIO;
 
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.category.DefaultCategoryDataset;
 
public class AppBar {
    public static void main(String[] args) {
        try {
            //种类数据集
            DefaultCategoryDataset ds = new DefaultCategoryDataset();
 
            ds.setValue(100, "ibm", "第一季度");
            ds.setValue(200, "ibm", "第二季度");
            ds.setValue(600, "ibm", "第三季度");
 
            ds.setValue(500, "google", "第一季度");
            ds.setValue(333, "google", "第二季度");
            ds.setValue(780, "google", "第三季度");
 
            ds.setValue(600, "用友", "第一季度");
            ds.setValue(1500, "用友", "第二季度");
            ds.setValue(300, "用友", "第三季度");
 
            Font font = new Font("宋体", Font.BOLD, 20);
            //创建柱状图,柱状图分水平显示和垂直显示两种
            JFreeChart chart = ChartFactory.createBarChart("前三季度各大公司JEEAS市场销售情况", "季度", "销量(万台)", ds, PlotOrientation.VERTICAL, true, true, true);
 
            //设置整个图片的标题字体
            chart.getTitle().setFont(font);
 
            //设置提示条字体
            font = new Font("宋体", Font.BOLD, 15);
            chart.getLegend().setItemFont(font);
 
            //得到绘图区
            CategoryPlot plot = (CategoryPlot) chart.getPlot();
            //得到绘图区的域轴(横轴),设置标签的字体
            plot.getDomainAxis().setLabelFont(font);
 
            //设置横轴标签项字体
            plot.getDomainAxis().setTickLabelFont(font);
 
            //设置范围轴(纵轴)字体
            plot.getRangeAxis().setLabelFont(font);
            //存储成图片
 
            //设置chart的背景图片
            chart.setBackgroundImage(ImageIO.read(new File("d:/sunset.bmp")));
 
            plot.setBackgroundImage(ImageIO.read(new File("d:/Water.jpg")));
 
            plot.setForegroundAlpha(1.0f);
 
            ChartUtilities.saveChartAsJPEG(new File("d:/bar.jpg"), chart, 600, 400);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

平面2D效果:

柱状图默认的ForegroundAlpha属性值是0.5,半透明的.创建3D柱状图只需要使用createBarChart3D()这个方法即可:

生成拆线图:

package cn.zhangao.jfreechart;
 
import java.awt.Font;
import java.io.File;
 
import javax.imageio.ImageIO;
 
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.category.DefaultCategoryDataset;
 
public class AppLine {
    public static void main(String[] args) {
        try {
            //种类数据集
            DefaultCategoryDataset ds = new DefaultCategoryDataset();
            ds.setValue(100, "ibm", "第一季度");
            ds.setValue(200, "ibm", "第二季度");
            ds.setValue(600, "ibm", "第三季度");
 
            ds.setValue(500, "google", "第一季度");
            ds.setValue(333, "google", "第二季度");
            ds.setValue(780, "google", "第三季度");
 
            ds.setValue(600, "用友", "第一季度");
            ds.setValue(1500, "用友", "第二季度");
            ds.setValue(300, "用友", "第三季度");
 
            Font font = new Font("宋体", Font.BOLD, 20);
            //创建柱状图
            JFreeChart chart = ChartFactory.createLineChart("前三季度各大公司JEEAS市场销售情况", "季度", "销量(万台)", ds, PlotOrientation.VERTICAL, true, true, true);
 
            //设置整个图片的标题字体
            chart.getTitle().setFont(font);
 
            //设置提示条字体
            font = new Font("宋体", Font.BOLD, 15);
            chart.getLegend().setItemFont(font);
 
            //得到绘图区
            CategoryPlot plot = (CategoryPlot) chart.getPlot();
            //得到绘图区的域轴(横轴),设置标签的字体
            plot.getDomainAxis().setLabelFont(font);
 
            //设置横轴标签项字体
            plot.getDomainAxis().setTickLabelFont(font);
 
            //设置范围轴(纵轴)字体
            plot.getRangeAxis().setLabelFont(font);
            //存储成图片
            //设置chart的背景图片
            chart.setBackgroundImage(ImageIO.read(new File("d:/sunset.bmp")));
 
            plot.setBackgroundImage(ImageIO.read(new File("d:/Water.jpg")));
 
            plot.setForegroundAlpha(1.0f);
            ChartUtilities.saveChartAsJPEG(new File("d:/line.jpg"), chart, 600, 400);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

平面2D效果:

同样只需要使用createLineChart3D()方法创建3D拆线图,效果:

时间: 2024-08-30 11:37:52

用JFreeChart 生成报表的相关文章

用jfreechart生成图形报表在SSH中怎么写,

问题描述 用jfreechart生成图形报表在SSH中怎么写, http://lapulande.iteye.com/blog/847961 从网上找了一个普通的项目好使,然后放SSH就不知道怎么写了, 解决方案 public class BarChart { public static String getBarChart(HttpSession session) throws Exception{ 构造图片方法 action请求 public String bar(){ HttpSessio

使用poi和jfreechart生成excel图表图片

最近项目在频繁的操作excel,里边涉及到很多和图表有关的东西.有时候需要使用java操作excel自带的图标,比较复杂的我们都是使用excel模板的形式实现. 除此之外,也有一些功能只需要生成对应的图标样式的图片就好,我们实现的时候主要用了两种方式,一种就是由前台生成图片base64码,然后后台解码生成图片插入到excel,但是这种方式有 一定的局限性,也就是当某些功能需要后台定时生成excel的时候,就无法获取这个图片. 于是我们采用了另一种方法,也就是是用jfreechart生成对应的图片

VC中调用EXECL模板生成报表

用EXCEL作为报表的输出方式还是比较常用的方式,这样可以充分利用EXCEL 的强大的编辑功能,以及打印功能.而采用模板方式是比较好的方法,首先在 EXCEL中制作模板,然后在生成报表时调用模板.这样能方便的生成我们需要的 报表.这里主要描述如何加载模板和添加每页报表.代码如下: void CExceltestDlg::OnButton1() { // TODO: Add your control notification handler code here _Application Excel

Delphi控制Excel生成报表

摘 要:Excel是当前最流行的数据报表制作工具.本文介绍如何使用Delphi来控制Excel完成数据库与报表之间的数据交换,讨论了报表制作工程中的一些细节性问题. 1 引言 数据报表作为企事业单位上报和下达的重要信息载体,随着信息化建设的不断推进,在实际的工作中得到了前所未有的应用.因此,数据报表已经成为管理信息系统中重要的一项功能,并且,由于数据的多样性和统计信息的增加,数据报表的系统实现变得越来越复杂. Delphi是一个高效的可视化数据库管理信息系统开发工具,.但是Delphi开发环境中

用Delphi的QReport部件生成报表

用户在使用数据库应用程序时经常要生成报表,利用Delphi 4的QReport部件,可以帮助我们快速方便地生成报表.这里以一个设备管理报表为例说明如何用QReport部件与Query部件设计从多个数据表中生成报表. 一.所用数据库 这里用到三个Foxpro数据表,DLBMK(设备大类编码).SBXHK(设备型号及配置).BMSBK(设备所在部门),存放在D:\SB目录下.其库结构如下: (一) DLBMK 字段名称 字段类型 解释 1 DLBH N3 设备大类的编号 2 DLMC C20 设备大

用MS SQL Reporting Services生成报表

灵活的报表功能是大多数业务应用程序的一个要求,这些报表功能在集成到 Web 应用程序中之后用途更加广泛.利用 SQL Server 2000 Reporting Services 的最新版本,您可以轻松地具有来自各种数据源的报表生成功能.在本文中,我将介绍使用 Visual Studio 和 Reporting Services 来编写报表,并演示如何将报表集成到 Web 应用程序中. Reporting Services 是基于服务器的报表生成平台,该平台构建在 .NET Framework

javascript操作excel生成报表全攻略

 这篇文章主要介绍了如何使用javascript操作excel生成报表,需要的朋友可以参考下 最近做一个项目,用到了javascript操纵excel以生成报表,下面是标有详细注解的实例  代码如下: <html> <head> <script language="javascript" type="text/javascript"> </script><script language="javascr

java报表-基于excel模板的数据填充,即允许客户自定义编辑模板并生成报表

问题描述 基于excel模板的数据填充,即允许客户自定义编辑模板并生成报表 现有这样的开发需求,针对java语言的.目前有没有这样的技术,能允许客户对web页面上展示的多行多列数据进行随意筛选,并指定这些选中的数据在excel模板上的某列显示数据,是不是可行的?我在网上搜查了一下,有一个工具比较符合要求,但是资料太少,无法开发,这工具是ExcelUtils,它是基于模板生产报表,但是效果还是不理想,不满足需求,请问还有其他工具或技术可以实现吗?谢谢! 解决方案 Java体系下读写Excel的技术

javascript操作excel生成报表全攻略_javascript技巧

最近做一个项目,用到了javascript操纵excel以生成报表,下面是标有详细注解的实例 复制代码 代码如下: <html> <head> <script language="javascript" type="text/javascript"> </script><script language="javascript" type="text/javascript"&