| 加入收藏| 设为首页| 联系我们

首页 站长学习 站长之家 源码下载 建站素材 书籍教程 常用工具
 您现在的位置: 动力中国 >> 网络编程 >> JSP教程 >> 文章正文  
 [组图]用JFreeChart对JSP报表进行增强
 

用JFreeChart对JSP报表进行增强

http://www.domcn.org  文章来源:本站收藏  点击数:

  关键字:用JFreeChart对JSP报表进行增强

02D); String filename = ServletUtilities.saveChartAsPNG(chart, 500, 300, null, session); String graphURL = request.getContextPath() + /DisplayChart?filename= + filename; %> <img src=<%= graphURL %> width=500 height=300 border=0 usemap=#<%= filename %>>        可以看出,饼图的绘制与柱状图的绘制类似,该例的运行效果如下:



有时候我们还想知道某块所占的具体分值,或者需要突出显示某一块。这时候需要对上例进行部分修改:dataset.setValue(其他, 0.2);后的那段改成: //通过工厂类生成JFreeChart对象 JFreeChart chart = ChartFactory.createPieChart3D(IT行业职业分布图, dataset, true, false, false); PiePlot pieplot = (PiePlot) chart.getPlot(); pieplot.setLabelFont(new Font(宋体, 0, 12)); //没有数据的时候显示的内容 pieplot.setNoDataMessage(无数据显示); pieplot.setCircular(false); setExplodePercent方法很重要,它将Label为某名称的某块挖出来突出显示,而后两句实现的效果是在“初中高级程序员”等名称后加上百分比,改成“初中高级程序员=55%”等。加上如上的代码后,同时还需要将相关的两个java包:org.jfree.chart.labels.StandardPieSectionLabelGenerator和java.text.NumberFormat引入到该jsp页面中。此时的运行结果如下:


    通过JFreeChart还可以提供漂亮的水晶饼图效果,接着让我们新建一个sample3.jsp页面来体验一下超炫美图吧。修改sample3.jsp页面如下: <%@ page contentType=text/html;charset=GBK%> <%@ page import=org.jfree.chart.*,                  org.jfree.chart.servlet.ServletUtilities,                  org.jfree.util.Rotation,                  org.jfree.data.general.DefaultPieDataset,                  org.jfree.chart.plot.PiePlot3D%> <% //设置数据集 DefaultPieDataset dataset = new DefaultPieDataset(); dataset.setValue(初中高级程序员, 0.55); dataset.setValue(项目经理, 0.1); dataset.setValue(系统分析师, 0.1); dataset.setValue(软件架构师, 0.1); dataset.setValue(其他, 0.2);   //通过工厂类生成JFreeChart对象 JFreeChart chart = ChartFactory.createPieChart3D(IT行业职业分布图, dataset, true, true, false); //获得3D的水晶饼图对象 PiePlot3D pieplot3d = (PiePlot3D) chart.getPlot(); //设置开始角度 pieplot3d.setStartAngle(150D); //设置方向为”顺时针方向“ pieplot3d.setDirection(Rotation.CLOCKWISE); //设置透明度,0.5F为半透明,1为不透明,0为全透明 pieplot3d.setForegroundAlpha(0.5F); pieplot3d.setNoDataMessage(无数据显示);        String filename = ServletUtilities.saveChartAsPNG(chart, 500, 300, null, session); String graphURL = request.getContextPath() + /DisplayChart?filename= + filename; %> <img src=<%= graphURL %>width=500 height=300 border=0 usemap=#<%= filename %>>        利用setForegroundAlpha()方法可以设置3D饼图的透明度,利用setStartAngle()可以设置其开始角度,利用setDirection()方法可以设置其方向。该例的运行效果如下图所示:

四.曲线图 接着我们来介绍曲线图的使用。在笔者日常的开发工作中,曲线图用得最多,它可以用来绘制趋势图、统计分析等。首先我们在WebRoot下建立一个line目录,用来存放曲线图实例的jsp页面。我们在该目录下建立sample1.jsp页面来做一个简单的曲线图的例子。在开始编码前,让我们来看看与画曲线图密切相关的几个类: 1) TimeSeriesCollection 曲线数据的集合。 2) TimeSeries 曲线信息序列。 3) ChartFactory 可以利用该类的createTimeSeriesChart方法来创建曲线的JFreeChart对象。 在下例中,我们显示阿蜜果的blog在2007年度各月份的访问量情况,修改后的sample1.jsp的内容如下: <%@ page contentType=text/html;charset=GBK%> <%@ page import = org.jfree.chart.ChartFactory,               org.jfree.chart.JFreeChart,               org.jfree.chart.servlet.ServletUtilities,               org.jfree.chart.title.TextTitle,               org.jfree.data.time.TimeSeries,               org.jfree.data.time.Month,               org.jfree.data.time.TimeSeriesCollection,               java.awt.Font%> <% //访问量统计时间线 TimeSeries timeSeries = new TimeSeries(阿蜜果blog访问量统计, Month.class); //时间曲线数据集合 TimeSeriesCollection lineDataset = new TimeSeriesCollection();   //构造数据集合 timeSeries.add(new Month(1, 2007), 11200); timeSeries.add(new Month(2, 2007), 9000); timeSeries.add(new Month(3, 2007), 6200); timeSeries.add(new Month(4, 2007), 8200); timeSeries.add(new Month(5, 2007), 8200); timeSeries.add(new Month(6, 2007), 12200); timeSeries.add(new Month(7, 2007), 13200); timeSeries.add(new Month(8, 2007), 8300); timeSeries.add(new Month(9, 2007), 12400); timeSeries.add(new Month(10, 2007), 12500); timeSeries.add(new Month(11, 2007), 13600); timeSeries.add(new Month(12, 2007), 12500);   lineDataset.addSeries(timeSeries); JFreeChart chart = ChartFactory.createTimeSeriesChart(访问量统计时间线, 月份, 访问量, lineDataset, true, true, true);   //设置子标题 TextTitle subtitle = new TextTitle(2007年度, new Font(黑体, Font.BOLD, 12)); chart.addSubtitle(subtitle); //设置主标题 chart.setTitle(new TextTitle(阿蜜果blog访问量统计, new Font(隶书, Font.ITALIC, 15))); chart.setAntiAlias(true);   String filename = ServletUtilities.saveChartAsPNG(chart, 500, 300, null, session); String graphURL = request.getContextPath() + /DisplayChart?filename= + filename; %> <img src=<%= graphURL %>width=500 height=300 border=0 usemap=#<%= filename %>> 运行后的曲线效果如下图所示:



有时我们需要显示各数据点及其数值,这是我们需要对上例进行一点改动,在JFreeChart chart = ChartFactory.createTimeSeriesChart……后,TextTitle subtitle = new TextTitle……前加上如下的代码: XYPlot plot = (XYPlot) chart.getPlot(); XYLineAndShapeRenderer xylineandshaperenderer = (XYLineAndShapeRenderer)plot.getRenderer(); //设置网格背景颜色 plot.setBackgroundPaint(Color.white); //设置网格竖线颜色 plot.setDomainGridlinePaint(Color.pink); //设置网格横线颜色 plot.setRangeGridlinePaint(Color.pink); //设置曲线图与xy轴的距离 plot.setAxisOffset(new RectangleInsets(0D, 0D, 0D, 10D)); //设置曲线是否显示数据点 xylineandshaperenderer.setBaseShapesVisible(true); //设置曲线显示各数据点的值 XYItemRenderer xyitem = plot.getRenderer();   xyitem.setBaseItemLabelsVisible(true);   xyitem.setBasePositiveItemLabelPosition(new ItemLabelPosition(ItemLabelAnchor.OUTSIDE12, TextAnchor.BASELINE_LEFT)); xyitem.setBaseItemLabelGenerator(new StandardXYItemLabelGenerator()); xyitem.setBaseItemLabelFont(new Font(Dialog, 1, 14)); plot.setRenderer(xyitem);     并引入了一些另外的包,引入包的语句变成: <%@ page import = org.jfree.chart.ChartFactory,               org.jfree.chart.title.TextTitle,               org.jfree.data.time.TimeSeries,               org.jfree.data.time.Month,               org.jfree.data.time.TimeSeriesCollection,               org.jfree.chart.plot.XYPlot,               org.jfree.chart.renderer.xy.XYLineAndShapeRenderer,               java.awt.Color,               org.jfree.ui.RectangleInsets,               java.awt.Font,               org.jfree.chart.renderer.xy.XYItemRenderer,               org.jfree.chart.JFreeChart,               org.jfree.chart.servlet.ServletUtilities,               org.jfree.chart.labels.*,               org.jfree.ui.*%>        运行该实例,效果如下所示:

上面两例的曲线图都是但曲线的,有时候我们对两个曲线进行比较,例如对阿蜜果在2006年度和2007年度的blog访问量进行比较,在此种情况下,我们需要在一个曲线图中显示两个曲线。在此种情况下,我们只需要在sample1.jsp的基础上新建一个TimeSeries对象,给它添加数据后,将其添加到TimeSeriesCollection型数据集合对象中,修改后的程序主体部分如下所示: //访问量统计时间线 TimeSeries timeSeries2006 = new TimeSeries(2006年度, Month.class); TimeSeries timeSeries2007 = new TimeSeries(2007年度, Month.class); //时间曲线数据集合 TimeSeriesCollection lineDataset = new TimeSeriesCollection();   //构造数据集合 timeSeries2006.add(new Month(1, 2007), 7200); timeSeries2006.add(new Month(2, 2007), 7000); timeSeries2006.add(new Month(3, 2007), 4200); timeSeries2006.add(new Month(4, 2007), 8200); timeSeries2006.add(new Month(5, 2007), 7300); timeSeries2006.add(new Month(6, 2007), 8200); timeSeries2006.add(new Month(7, 2007), 9200); timeSeries2006.add(new Month(8, 2007), 7300); timeSeries2006.add(new Month(9, 2007), 9400); timeSeries2006.add(new Month(10, 2007), 7500); timeSeries2006.add(new Month(11, 2007), 6600); timeSeries2006.add(new Month(12, 2007), 3500);   timeSeries2007.add(new Month(1, 2007), 10200); timeSeries2007.add(new Month(2, 2007), 9000); timeSeries2007.add(new Month(3, 2007), 6200); timeSeries2007.add(new Month(4, 2007), 8200); timeSeries2007.add(new Month(5, 2007), 8200); timeSeries2007.add(new Month(6, 2007), 11200); timeSeries2007.add(new Month(7, 2007), 13200); timeSeries2007.add(new Month(8, 2007), 8300); timeSeries2007.add(new Month(9, 2007), 10400); timeSeries2007.add(new Month(10, 2007), 12500); timeSeries2007.add(new Month(11, 2007), 10600); timeSeries2

上一页  [1] [2] [3] 下一页


用JFreeChart对JSP报表进行增强
  • 上一篇文章:

  • 下一篇文章:
  •  热门文章
    普通文章 电子邮件改头换面 四公司畅谈未
    普通文章 PC病毒史上最声名狼藉的八大病
    普通文章 Rails系统中的AJAX开发技术简析
    普通文章 基于ASP.NET AJAX框架实现表单
    普通文章 开发ASP.NET AJAX客户端定制行
    普通文章 用JFreeChart对JSP报表进行增强
    普通文章 SQL Server 2005上的CLR和ADO.
    普通文章 SQL Server 2005的XML支持机制
    普通文章 Firefox中标签式浏览技巧大全
    普通文章 Tomcat中的Session和Cookie大揭
     
     推荐文章
    推荐文章 把Google地图嵌入网页 就是这么
    推荐文章 迅雷搜索候选资源出错的解决
    推荐文章 轻松去除迅雷里的各种广告和资
    推荐文章 突破限制 免费领养到QQ空间五级
    推荐文章 Rational统一过程RUP贴近中小软
    推荐文章 构建自己的轻量级XML DOM分析程
    推荐文章 WPS Office 2007技巧:妙用配置
    推荐文章 Excel 2007:求余数函数实用进阶
    推荐文章 浅谈ASP.NET的Postback
    推荐文章 软件开发中项目需求管理简述
     
     相关文章
    没有相关文章
    设为首页 | 加入收藏 | 广告合作 | 联系站长 | 版权申明 |
    动力中国为网友提供免费学习资料,可用资源,如果您认为我们的相关内容侵害到了您的权利请联系管理员
    Copyright © 2006-2008 domcn.org All Rights Reserved.