1、JFreeChart与JSP动态图表1 下载 http:/www.jfree.org/jfreechart/download.htmljfreechart-1.0.5jarjcommon-1.0.9.jar、gnujaxp.jarweb.xml配置 DisplayChart org.jfree.chart.servlet.DisplayChart DisplayChart /servlet/DisplayChart 生成动态图表步骤: 创建绘图数据集合 创建JFreeChart实例 自定义图表绘制属性,该步可选 生成指定格式的图片,并返回图片名称 组织图片浏览路径 通过HTML中的元素显示图片
2、2 核心类 类名功能JFreeChart图表对象,生成任何类型的图表都要通过该对象,JFreeChart插件提供了一个工厂类ChartFactory,用来创建各种类型的图表对象XXXDataset数据集对象,用来保存绘制图表的数据,不同类型的图表对应着不同类型的数据集对象XXXPlot绘制区对象,如果需要自行定义绘图区的相关绘制属性,需要通过该对象进行设置XXXAxis坐标轴对象,用来定义坐标轴的绘制属性XXXRenderer图片渲染对象,用于渲染和显示图表XXXURLGenerator链接对象,用于生成Web图表中项目的鼠标单击链接XXXToolTipGenrator图表提示对象,用于生成图
3、表提示信息,不同类型的图表对应着不同类型的图表提示对象3 利用DefaultCategoryDataset数据集绘制柱形图package com.mwq.jfreechart;import java.awt.Color;import java.awt.GradientPaint;import java.io.IOException;import javax.servlet.http.HttpSession;import org.jfree.chart.ChartFactory;import org.jfree.chart.ChartRenderingInfo;import org.jfree.
4、chart.JFreeChart;import org.jfree.chart.axis.CategoryAxis;import org.jfree.chart.axis.CategoryLabelPositions;import org.jfree.chart.entity.StandardEntityCollection;import org.jfree.chart.plot.CategoryPlot;import org.jfree.chart.plot.PlotOrientation;import org.jfree.chart.renderer.category.BarRendere
5、r;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; / 图象高度 String chartTitle;/ 图表标题 String subtitle;/ 副标题 String xTitle;/ X轴标题 String yTitle;/ Y轴标题 Strin
6、g 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 Strin
7、g 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, Str
8、ing 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;
9、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.
10、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
11、; 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.setBackgrou
12、ndPaint(new Color(241, 219, 127);/ 设置绘图区背景色 plot.setRangeGridlinePaint(Color.RED);/ 设置水平方向背景线颜色 plot.setRangeGridlinesVisible(true);/ 设置是否显示水平方向背景线,默认值为true plot.setDomainGridlinePaint(Color.RED);/ 设置垂直方向背景线颜色 plot.setDomainGridlinesVisible(true);/ 设置是否显示垂直方向背景线,默认值为false / 通过柱形对象,可以设置柱形的绘图属性 BarRen
13、derer 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+) Gradi
14、entPaint 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 Cha
15、rtRenderingInfo( new StandardEntityCollection(); / 生成指定格式的图片,并返回图片名称 String fileName = ; try fileName = ServletUtilities.saveChartAsPNG(chart, width, height, info, session); catch (IOException e) System.out.println(- 在绘制图片时抛出异常,内容如下:); e.printStackTrace(); / 组织图片浏览路径 String graphURL = contextPath +
16、servletURI + ?filename= + fileName; / 返回图片浏览路径 return graphURL; JSP: 利用DefaultCategoryDataset数据集绘制柱形图 利用DefaultCategoryDataset数据集绘制柱形图 img src= border=1 alt= % / 如果在绘图数据中存在负数,绘制出的图片将是另一种效果 int width = 450;/ 图象宽度 int height = 325; / 图象高度 String chartTitle = 编程类图书同期销量对比柱形图分
17、析;/ 图表标题 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
18、 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.servl
19、et.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
20、.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.Year;import org
21、.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.heig
22、ht = 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
23、, 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 Mon
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1