JFreeChart与JSP动态图表

上传人:仙*** 文档编号:97267486 上传时间:2022-05-27 格式:DOC 页数:16 大小:598KB
返回 下载 相关 举报
JFreeChart与JSP动态图表_第1页
第1页 / 共16页
JFreeChart与JSP动态图表_第2页
第2页 / 共16页
JFreeChart与JSP动态图表_第3页
第3页 / 共16页
点击查看更多>>
资源描述
Pie Chart Demo 1亍口 xPie Chart 3D Demo 1Pie Chart Demo 1亍口 xPie Chart 3D Demo 1Pie Chart Demo 1亍口 xPie Chart Demo 1亍口 xVisual Basic = 10C/C+ = 17.5PHP= 32.5Pie Chart Demo 1亍口 xPie Chart Demo 1亍口 x Java = 43.2 Visual Basic = 10 C/C+ = 17.5 PHP = 32.5 Perl = nullPrice Volume Chart Demo-口 XPie Chart Demo 1亍口 xEurodollar Futures Contract (MARO3)-700,000600,000500,000400,000300,000200,000100,0000Sep-2002Nov-2002Jan-2002Mar-2002May-2002Jul-2002Date98.5098.0097.5095.5095.0094.5097.00七 96.50亠 96.00vo-umePie Chart Demo 1亍口 xPie Chart Demo 1亍口 xPrice I VolumePopulation Chart Demo x70+Population Chart DemoPopulation (millions)-20-15-1050510152050-6950-5940-4930-3920-2910-19 Male Female9odnouPopulation Chart Demo x70+Population Chart Demo x70+Difference Chart Demo 2-X20:00-16:00-12:00- 08:00 -04:00Feb-2004Apr-2004Jun-2004Aug-2004Oct-2004Dec-2004TimeDaylight Hours - London, UKData source: Population Chart Demo x70+Population Chart Demo x70+Sunrise SunsetDual Axis Demo 1Dual Axis ChartSecondaryPopulation Chart Demo x70+Population Chart Demo x70+S4 SI HS2 S3State Executions USAState Executions - USASource: http:/AAA.amnestyusa.org/abolish/listbyyear.doa-doold jo3qEnN198019851990:995200015500A 35302345YearI ExecutionsJFreeChart: XYSplineRendererDemola.java一 XXYSplineRenderer67891011X Series 1 -Series 215Dial Demo 1Dial Demo 2JFreeChart - Demo Dial 2a10.00,020.0-10.030.040,0-30.050.060.0-40,0i/ T emperature0.0-10,030.010.00-30,073.060.080.0 4 0,010,0事20,010;/5O.Oi-40.01_I _-J# Temperaturep pF-40 -30 -20 -100102030 4050 60 -40 -20 020 40 60 020 40 60 80 1001.下载http:/www.jfree.org/jfreechart/download.htmljfreechart-1.0.5jarjcommon-1.0.9.jar、gnujaxp.jarweb.xml酉己置DisplayChart org.jfree.chart.servlet.DisplayChartDisplayChart /servlet/DisplayChart 生成动态图表步骤:创建绘图数据集合创建JFreeChart实例自定义图表绘制属性,该步可选生成指定格式的图片,并返回图片名称 组织图片浏览路径通过HTML中的img元素显示图片类名功能JFreeChart图表对象,生成任何类型的图表都要通过该对象,JFreeChart插件提供了一个工厂类ChartFactory,用来创建各种类型的图表对象XXXDataset数据集对象,用来保存绘制图表的数据,不同类型的图表对应着不同类型的数据集对象XXXPIot绘制区对象,如果需要自行定义绘图区的相关绘制属性,需要通过该对象进行设置XXXAxis坐标轴对象,用来定义坐标轴的绘制属性XXXRe nderer图片渲染对象,用于渲染和显示图表XXXURLGe nerator链接对象,用于生成 Web图表中项目的鼠标单击链接XXXToolTipGe nrator图表提示对象,用于生成图表提示信息,不同类型的图表对应着不同类型的图表提示对象3.利用DefaultCategoryDataset数据集绘制柱形图編程类图廿年销量柱形图分析勿肚|1甘個:2008ir5 0 5 07 5 2 ASP aJSP PHP编程类图书同期销戲对比柱形图分析对tt2D朋年崎20年同期对比李度 ASP aJSP PHPpackage com.mwq.jfreechart;import java.awt.Color;import java.awt.Gradie ntPa int;import java.i o.I OExcepti on;import javax.servlet.http.HttpSessi on;import org.jfree.chart.ChartFactory;import org.jfree.chart.ChartRe nderi ngl nfo;import org.jfree.chart.JFreeChart;import org.jfree.chart.axis.CategoryAxis;import org.jfree.chart.axis.CategoryLabelPositi ons;import org.jfree.chart.e ntity.Sta ndardE ntityCollect ion;import org.jfree.chart.plot.CategoryPlot;import org.jfree.chart.plot.PlotOrie ntati on;import org.jfree.chart.re nderer.category.BarRe nderer;import org.jfree.chart.servlet.ServletUtilities;import org.jfree.chart.title.TextTitle;import org.jfree.data.category.DefaultCategoryDataset;public class DrawHistogram int width;/ 图象宽度int height; /图象高度Stri ng chartTitle;/图表标题String subtitle;/ 副标题String xTitle;/ X 轴标题String yTitle;/ Y 轴标题String cutline;/ 图例名称String category; / 统计种类Double data;/ 绘图数据String servletURI = /servlet/DisplayChart;/ 映射路径public DrawHistogram() this.width = 450; this.height = 325;this.chartTitle = 编程类图书年销量柱形图分析 ; this.subtitle = 统计时间: 2008 年 ;this.xTitle = 销售季度 ; this.yTitle = 销售量单位 :万册 ;this.cutline = new String ASP, JSP, PHP ; this.category = new String 第1季度, 第 2季度, 第3季度, 第4季度 ; this.data = new Doublecutline.lengthcategory.length;for (int m = 0; m cutline.length; m+) for (int n = 0; n category.length; n+) datamn = 1 + Math.random() * 100; public DrawHistogram(int width, int height, String chartTitle,String subtitle, String xTitle, String yTitle, String cutline, String category, Double data) this.width = width; this.height = height; this.chartTitle = chartTitle; this.subtitle = subtitle; this.xTitle = xTitle; this.yTitle = yTitle; this.cutline = cutline; this.category = category;this.data = data;public DrawHistogram(int width, int height, String chartTitle, String subtitle, String xTitle, String yTitle, String cutline, String category, Double data, String servletURI) this.width = width;this.height = height; this.chartTitle = chartTitle; this.subtitle = subtitle;this.xTitle = xTitle; this.yTitle = yTitle;this.cutline = cutline; this.category = category;this.data = data; this.servletURI = servletURI;public String draw(HttpSession session, String contextPath) / 创建绘图数据集DefaultCategoryDataset dataset = new DefaultCategoryDataset(); for (int m = 0; m cutline.length; m+) for (int n = 0; n 0) / 添加副标题 chart.addSubtitle(new TextTitle(subtitle);GradientPaint chartGP = new GradientPaint(0, 0,new Color(219, 227, 251), 0, height, Color.WHITE, false);/ 创建渐变色对 象chart.setBackgroundPaint(chartGP);/ 设置图片背景色/ 通过绘图区对象,可以设置更多的绘图属性 CategoryPlot plot = chart.getCategoryPlot();plot.setBackgroundPaint(new Color(241, 219, 127);/ 设置绘图区背景色plot.setRangeGridlinePaint(Color.RED);/ 设置水平方向背景线颜色 plot.setRangeGridlinesVisible(true);/ 设置是否显示水平方向背景线 , 默认值为 trueplot.setDomainGridlinePaint(Color.RED);/ 设置垂直方向背景线颜色 plot.setDomainGridlinesVisible(true);/ 设置是否显示垂直方向背景线 ,默认值为 false/ 通过柱形对象,可以设置柱形的绘图属性 BarRenderer renderer = (BarRenderer) plot.getRenderer();renderer.setDrawBarOutline(false);/ 设置是否绘制柱形的轮廓线,默认值为 true/ 设置填充柱形的渐进色Color color = new Colorcutline.length; color0 = new Color(99, 99, 0); color1 = new Color(255, 169, 66); color2 = new Color(33, 255, 66); for (int i = 0; i color.length; i+) GradientPaint gp = new GradientPaint(0, 0, colori.brighter(), 0, height, colori.darker();renderer.setSeriesPaint(i, gp);/ 设置横轴标题文字的旋转方向 CategoryAxis domainAxis = plot.getDomainAxis(); domainAxis.setCategoryLabelPositions(CategoryLabelPositions.UP_45);/ 自定义图表结束!/ 固定用法ChartRenderingInfo info = new ChartRenderingInfo(new StandardEntityCollection();/ 生成指定格式的图片,并返回图片名称String fileName = ;try fileName = ServletUtilities.saveChartAsPNG(chart, width, height, info, session); catch (IOException e) System.out.println( 在绘制图片时抛出异常,内容如下:e.printStackTrace();/ 组织图片浏览路径String graphURL = contextPath + servletURI + ?filename= + fileName;/ 返回图片浏览路径return graphURL;JSP: 利用 DefaultCategoryDataset 数据集绘制柱形图   );cellspacing=0 利用 DefaultCategoryDataset 数据集绘制柱 形图  img src= border=1 alt=  %/ 如果在绘图数据中存在负数,绘制出的图片将是另一种效果int width = 450;/ 图象宽度int height = 325; / 图象高度String chartTitle = 编程类图书同期销量对比柱形图分析 ;/ 图表标题 String subtitle = 对比 2008年与 2007年同期 ;/ 副标题String xTitle = 对比季度 ;/ X 轴标题String yTitle = 增减量单位 :万册 ;/ Y 轴标题String cutline = new String ASP, JSP, PHP ;/图例名称String category = new String 第 1季度, 第2季度, 第 3季度, 第4季度 ; / 统计种类Double data = new Doublecutline.lengthcategory.length;/ 绘图数据 for (int m = 0; m cutline.length; m+) for (int n = 0; n img src= border=1 alt=    4. 利用 XYDataset 数据集绘制与日期相关的柱形图package com.mwq.jfreechart;import java.awt.Color;import java.awt.GradientPaint;import java.io.IOException;import java.util.HashMap;import java.util.Iterator;import java.util.Map;import javax.servlet.http.HttpSession;import org.jfree.chart.ChartFactory;import org.jfree.chart.ChartRenderingInfo;import org.jfree.chart.JFreeChart;import org.jfree.chart.axis.DateAxis;import org.jfree.chart.axis.DateTickMarkPosition;import org.jfree.chart.entity.StandardEntityCollection;import org.jfree.chart.plot.PlotOrientation;import org.jfree.chart.plot.XYPlot;import org.jfree.chart.servlet.ServletUtilities;import org.jfree.chart.title.TextTitle;import org.jfree.data.time.Month;import org.jfree.data.time.TimeSeries;import org.jfree.data.time.TimeSeriesCollection; import org.jfree.data.time.Y ear;import org.jfree.data.xy .IntervalXYDataset;public class DrawHistogram int width;/ 图象宽度int height; / 图象高度String chartTitle;/ 图表标题String subtitle;/ 副标题String xTitle;/ X 轴标题String yTitle;/ Y 轴标题Map data;/ 绘图数据String servletURI = /servlet/DisplayChart;/ 映射路径public DrawHistogram() this.width = 450; this.height = 325; this.chartTitle = 倾诚科技年销售额柱形图分析 ; this.subtitle = 统计时间: 2008 年 ; this.xTitle = 统计月份 ;this.yTitle = 销售额单位 :万元 ;this.data = new HashMap();for (int i = 1; i 13; i+) data.put(i, 1 + Math.random() * 100); public DrawHistogram(int width, int height, String chartTitle, String subtitle, String title, String title2, Map data) super();this.width = width; this.height = height; this.chartTitle = chartTitle; this.subtitle = subtitle; xTitle = title; yTitle = title2;this.data = data;public String draw(HttpSession session, String contextPath) / 创建绘图数据集TimeSeries timeSeries = new TimeSeries(chartTitle, xTitle, yTitle, Month.class);Iterator it = data.keySet().iterator();while (it.hasNext() int key = (Integer) it.next();timeSeries.add(new Month(key, new Y ear(2008), (Double) data .get(key);TimeSeriesCollection tsc = new TimeSeriesCollection(timeSeries); IntervalXYDataset dataset = tsc;/ 创建图表对象JFreeChart chart = ChartFactory .createXYBarChart(chartTitle, xTitle, true, yTitle, dataset, PlotOrientation.VERTICAL, false, false, false);/ 开始自定义图表绘制的相关属性/ 添加副标题 chart.addSubtitle(new TextTitle(subtitle);/ 设置图表的背景颜色GradientPaint chartGP = new GradientPaint(0, 0, Color.GREEN, 0, height, Color.WHITE, false);chart.setBackgroundPaint(chartGP);/ 通过绘图区对象,可以设置更多的绘图属性XYPlot plot = chart.getXYPlot(); plot.setBackgroundPaint(Color.WHITE);/ 设置绘图区背景色 plot.setRangeGridlinePaint(Color.BLUE);/ 设置水平方向背景线颜色/ 设置日期轴的相关绘制属性 DateAxis axis = (DateAxis) plot.getDomainAxis(); axis.setTickMarkPosition(DateTickMarkPosition.MIDDLE);/ 设置时间标记的显示位 置,默认为左对齐axis.setLowerMargin(0.02);/ 设置最左侧柱形与绘图区边框的距离 axis.setUpperMargin(0.02);/ 设置最右侧柱形与绘图区边框的距离/ 自定义图表结束!/ 固定用法 ChartRenderingInfo info = new ChartRenderingInfo( new StandardEntityCollection();/ 生成指定格式的图片,并返回图片名称Stri ng fileName =;try fileName = ServletUtilities.saveChartAsPNG(chart, width, height, info, sessi on); catch (IOExcepti on e) System.out.println(” 在绘制图片时抛出异常,内容如下:);e.pri ntStackTrace();/组织图片浏览路径Stri ng graphURL = con textPath + servletURI + ?file name= + fileName;/返回图片浏览路径retur n graphURL;倾诚科技年销售额柱形图分析 VDOCTYPE HTML PUBLIC -/W3C/DTD HTML 4.01 Tran sitio nal/EN 柱形图    利用 XYDataset 数据集绘制与日期相关的 柱形图  img src= border=1 alt=   
展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 管理文书 > 施工组织


copyright@ 2023-2025  zhuangpeitu.com 装配图网版权所有   联系电话:18123376007

备案号:ICP2024067431-1 川公网安备51140202000466号


本站为文档C2C交易模式,即用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知装配图网,我们立即给予删除!