基于jfreechart生成曲线、柱状等图片并展示到JSP
程序员文章站
2022-06-22 09:34:12
虽然现在js做报表和图形展示已经非常普遍和漂亮了,但是不能忽略有jfreechart 这样一种东西!这些翻阅资料,在看以前写的示例时发现了关于jfreechart 的简单示例,不管怎样发上来分享一下!...
虽然现在js做报表和图形展示已经非常普遍和漂亮了,但是不能忽略有jfreechart 这样一种东西!
这些翻阅资料,在看以前写的示例时发现了关于jfreechart 的简单示例,不管怎样发上来分享一下!
这个示例使用jsp和servlet做后台和前台展示,下面有源代码可以直接运行!
生产线型趋势图:
package com.xidian.servlet; import java.awt.color; import java.awt.font; import java.io.*; import java.text.simpledateformat; import javax.servlet.servletexception; import javax.servlet.http.httpservlet; import javax.servlet.http.httpservletrequest; import javax.servlet.http.httpservletresponse; import org.jfree.chart.*; import org.jfree.chart.axis.dateaxis; import org.jfree.chart.axis.valueaxis; import org.jfree.chart.plot.xyplot; import org.jfree.chart.renderer.xy.xyitemrenderer; import org.jfree.chart.renderer.xy.xylineandshaperenderer; import org.jfree.chart.title.texttitle; import org.jfree.data.time.day; import org.jfree.data.time.timeseries; import org.jfree.data.time.timeseriescollection; import org.jfree.data.xy.xydataset; /** * 生产线型趋势图 * @说明 * @author cuisuqiang * @version 1.0 * @since */ @suppresswarnings("serial") public class lineservlet extends httpservlet { @suppresswarnings("deprecation") @override protected void service(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { response.setcontenttype("text/html"); // 在mysql中使用 select // year(accessdate),month(accessdate),day(accessdate),count(*) // 其中accessdate 是一个date类型的时间 // 时间序列对象集合 timeseriescollection charttime = new timeseriescollection(); // 时间序列对象,第1个参数表示时间序列的名字,第2个参数是时间类型,这里为天 // 该对象用于保存前count天每天的访问次数 timeseries timeseries = new timeseries("日访问", day.class); // 为了演示,直接拼装数据 // day的组装格式是day-month-year 访问次数 timeseries.add(new day(1, 1, 2010), 50); timeseries.add(new day(2, 1, 2010), 47); timeseries.add(new day(3, 1, 2010), 82); timeseries.add(new day(4, 1, 2010), 95); timeseries.add(new day(5, 1, 2010), 104); timeseries.add(new day(6, 1, 2010), 425); charttime.addseries(timeseries); xydataset date = charttime; try { // 使用chartfactory来创建时间序列的图表对象 jfreechart chart = chartfactory.createtimeserieschart( "网站每天访问统计", // 图形标题 "日期", // x轴说明 "访问量", // y轴说明 date, // 数据 true, // 是否创建图例 true, // 是否生成tooltips false // 是否生产url链接 ); // 设置整个图片的背景色 chart.setbackgroundpaint(color.pink); // 设置图片有边框 chart.setbordervisible(true); // 获得图表区域对象 xyplot xyplot = (xyplot) chart.getplot(); // 设置报表区域的背景色 xyplot.setbackgroundpaint(color.lightgray); // 设置横 纵坐标网格颜色 xyplot.setdomaingridlinepaint(color.green); xyplot.setrangegridlinepaint(color.green); // 设置横、纵坐标交叉线是否显示 xyplot.setdomaincrosshairvisible(true); xyplot.setrangecrosshairvisible(true); // 获得数据点(x,y)的render,负责描绘数据点 xyitemrenderer xyitemrenderer = xyplot.getrenderer(); if (xyitemrenderer instanceof xylineandshaperenderer) { xylineandshaperenderer xylineandshaperenderer = (xylineandshaperenderer) xyitemrenderer; xylineandshaperenderer.setshapesvisible(true); // 数据点可见 xylineandshaperenderer.setshapesfilled(true); // 数据点是实心点 xylineandshaperenderer.setseriesfillpaint(0, color.red); // 数据点填充为蓝色 xylineandshaperenderer.setusefillpaint(true);// 将设置好的属性应用到render上 } // 配置以下内容方可解决乱码问题 // 配置字体 font xfont = new font("宋体", font.plain, 12); // x轴 font yfont = new font("宋体", font.plain, 12); // y轴 font kfont = new font("宋体", font.plain, 12); // 底部 font titlefont = new font("宋体", font.bold, 25); // 图片标题 // 图片标题 chart.settitle(new texttitle(chart.gettitle().gettext(), titlefont)); // 底部 chart.getlegend().setitemfont(kfont); // x 轴 valueaxis domainaxis = xyplot.getdomainaxis(); domainaxis.setlabelfont(xfont);// 轴标题 domainaxis.setticklabelfont(xfont);// 轴数值 domainaxis.setticklabelpaint(color.blue); // 字体颜色 // y 轴 valueaxis rangeaxis = xyplot.getrangeaxis(); rangeaxis.setlabelfont(yfont); rangeaxis.setlabelpaint(color.blue); // 字体颜色 rangeaxis.setticklabelfont(yfont); // 定义坐标轴上日期显示的格式 dateaxis dateaxis = (dateaxis) xyplot.getdomainaxis(); // 设置日期格式 dateaxis.setdateformatoverride(new simpledateformat("yyyy-mm-dd")); // 向客户端输出生成的图片 chartutilities.writechartasjpeg(response.getoutputstream(), 1.0f, chart, 500, 300, null); } catch (exception e) { e.printstacktrace(); } } }
生产柱状图:
package com.xidian.servlet; import java.awt.color; import java.awt.font; import java.io.ioexception; import javax.servlet.servletexception; import javax.servlet.http.httpservlet; import javax.servlet.http.httpservletrequest; import javax.servlet.http.httpservletresponse; import org.jfree.chart.chartfactory; import org.jfree.chart.chartutilities; import org.jfree.chart.jfreechart; import org.jfree.chart.axis.categoryaxis; import org.jfree.chart.axis.numberaxis; import org.jfree.chart.plot.categoryplot; import org.jfree.chart.plot.plotorientation; import org.jfree.chart.renderer.category.barrenderer; import org.jfree.chart.title.texttitle; import org.jfree.data.category.defaultcategorydataset; /** * 生产柱状图 * @说明 * @author cuisuqiang * @version 1.0 * @since */ @suppresswarnings("serial") public class pillarservlet extends httpservlet { @override protected void service(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { response.setcontenttype("text/html"); // 使用普通数据集 defaultcategorydataset chartdate = new defaultcategorydataset(); // 增加测试数据,第一个参数是访问量,最后一个是时间,中间是显示用不考虑 chartdate.addvalue(55, "访问量", "2010-01"); chartdate.addvalue(65, "访问量", "2010-02"); chartdate.addvalue(59, "访问量", "2010-03"); chartdate.addvalue(156, "访问量", "2010-04"); chartdate.addvalue(452, "访问量", "2010-05"); chartdate.addvalue(359, "访问量", "2010-06"); try { // 从数据库中获得数据集 defaultcategorydataset data = chartdate; // 使用chartfactory创建3d柱状图,不想使用3d,直接使用createbarchart jfreechart chart = chartfactory.createbarchart3d( "网站月访问量统计", // 图表标题 "时间", // 目录轴的显示标签 "访问量", // 数值轴的显示标签 data, // 数据集 plotorientation.vertical, // 图表方向,此处为垂直方向 // plotorientation.horizontal, //图表方向,此处为水平方向 true, // 是否显示图例 true, // 是否生成工具 false // 是否生成url链接 ); // 设置整个图片的背景色 chart.setbackgroundpaint(color.pink); // 设置图片有边框 chart.setbordervisible(true); font kfont = new font("宋体", font.plain, 12); // 底部 font titlefont = new font("宋体", font.bold, 25); // 图片标题 // 图片标题 chart.settitle(new texttitle(chart.gettitle().gettext(), titlefont)); // 底部 chart.getlegend().setitemfont(kfont); // 得到坐标设置字体解决乱码 categoryplot categoryplot = (categoryplot) chart.getplot(); categoryplot.setdomaingridlinesvisible(true); categoryplot.setrangecrosshairvisible(true); categoryplot.setrangecrosshairpaint(color.blue); numberaxis numberaxis = (numberaxis) categoryplot.getrangeaxis(); numberaxis.setstandardtickunits(numberaxis.createintegertickunits()); barrenderer barrenderer = (barrenderer) categoryplot.getrenderer(); barrenderer.setbaseitemlabelfont(new font("宋体", font.plain, 12)); barrenderer.setseriesitemlabelfont(1, new font("宋体", font.plain, 12)); categoryaxis domainaxis = categoryplot.getdomainaxis(); /*------设置x轴坐标上的文字-----------*/ domainaxis.setticklabelfont(new font("sans-serif", font.plain, 11)); /*------设置x轴的标题文字------------*/ domainaxis.setlabelfont(new font("宋体", font.plain, 12)); /*------设置y轴坐标上的文字-----------*/ numberaxis.setticklabelfont(new font("sans-serif", font.plain, 12)); /*------设置y轴的标题文字------------*/ numberaxis.setlabelfont(new font("宋体", font.plain, 12)); /*------这句代码解决了底部汉字乱码的问题-----------*/ chart.getlegend().setitemfont(new font("宋体", font.plain, 12)); // 生成图片并输出 chartutilities.writechartasjpeg(response.getoutputstream(), 1.0f, chart, 500, 300, null); } catch (exception e) { e.printstacktrace(); } } }
生成饼状统计图:
package com.xidian.servlet; import java.awt.color; import java.awt.font; import java.io.ioexception; import javax.servlet.servletexception; import javax.servlet.http.httpservlet; import javax.servlet.http.httpservletrequest; import javax.servlet.http.httpservletresponse; import org.jfree.chart.chartutilities; import org.jfree.chart.jfreechart; import org.jfree.chart.plot.pieplot3d; import org.jfree.chart.title.texttitle; import org.jfree.data.general.defaultpiedataset; /** * 生成饼状统计图 * @说明 * @author cuisuqiang * @version 1.0 * @since */ @suppresswarnings("serial") public class cakeservlet extends httpservlet { protected void service(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { response.setcontenttype("text/html"); // 默认数据类型 defaultpiedataset datatype = new defaultpiedataset(); // 数据参数 内容,数量 datatype.setvalue("ie6", 156); datatype.setvalue("ie7", 230); datatype.setvalue("ie8", 45); datatype.setvalue("火狐", 640); datatype.setvalue("谷歌", 245); try { defaultpiedataset data = datatype; // 生成普通饼状图除掉 3d 即可 // 生产3d饼状图 pieplot3d plot = new pieplot3d(data); jfreechart chart = new jfreechart( "用户使用的浏览器类型", // 图形标题 jfreechart.default_title_font, // 标题字体 plot, // 图标标题对象 true // 是否显示图例 ); // 设置整个图片的背景色 chart.setbackgroundpaint(color.pink); // 设置图片有边框 chart.setbordervisible(true); // 配置字体 font kfont = new font("宋体", font.plain, 12); // 底部 font titlefont = new font("宋体", font.bold, 25); // 图片标题 // 图片标题 chart.settitle(new texttitle(chart.gettitle().gettext(), titlefont)); // 底部 chart.getlegend().setitemfont(kfont); chartutilities.writechartasjpeg(response.getoutputstream(), 1.0f, chart, 500, 300, null); } catch (exception e) { e.printstacktrace(); } } }
柱状分布统计图:
package com.xidian.servlet; import java.awt.color; import java.awt.font; import java.io.ioexception; import javax.servlet.servletexception; import javax.servlet.http.httpservlet; import javax.servlet.http.httpservletrequest; import javax.servlet.http.httpservletresponse; import org.jfree.chart.chartfactory; import org.jfree.chart.chartutilities; import org.jfree.chart.jfreechart; import org.jfree.chart.axis.categoryaxis; import org.jfree.chart.axis.numberaxis; import org.jfree.chart.plot.categoryplot; import org.jfree.chart.plot.plotorientation; import org.jfree.chart.renderer.category.barrenderer; import org.jfree.chart.title.texttitle; import org.jfree.data.category.defaultcategorydataset; /** * 柱状分布统计图 * @说明 * @author cuisuqiang * @version 1.0 * @since */ @suppresswarnings("serial") public class paragraphsservlet extends httpservlet { protected void service(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { response.setcontenttype("text/html"); defaultcategorydataset datatime = new defaultcategorydataset(); // 这是一组数据 datatime.addvalue(52, "0-6", "2010-1-1"); datatime.addvalue(86, "6-12", "2010-1-1"); datatime.addvalue(126, "12-18", "2010-1-1"); datatime.addvalue(42, "18-24", "2010-1-1"); // 这是一组数据 datatime.addvalue(452, "0-6", "2010-1-2"); datatime.addvalue(96, "6-12", "2010-1-2"); datatime.addvalue(254, "12-18", "2010-1-2"); datatime.addvalue(126, "18-24", "2010-1-2"); // 这是一组数据 datatime.addvalue(256, "0-6", "2010-1-3"); datatime.addvalue(86, "6-12", "2010-1-3"); datatime.addvalue(365, "12-18", "2010-1-3"); datatime.addvalue(24, "18-24", "2010-1-3"); try { defaultcategorydataset data = datatime; // 使用chartfactory创建3d柱状图,不想使用3d,直接使用createbarchart jfreechart chart = chartfactory.createbarchart3d( "网站时间段访问量统计", "时间", "访问量", data, plotorientation.vertical, true, false, false ); // 设置整个图片的背景色 chart.setbackgroundpaint(color.pink); // 设置图片有边框 chart.setbordervisible(true); font kfont = new font("宋体", font.plain, 12); // 底部 font titlefont = new font("宋体", font.bold, 25); // 图片标题 // 图片标题 chart.settitle(new texttitle(chart.gettitle().gettext(), titlefont)); // 底部 chart.getlegend().setitemfont(kfont); // 得到坐标设置字体解决乱码 categoryplot categoryplot = (categoryplot) chart.getplot(); categoryplot.setdomaingridlinesvisible(true); categoryplot.setrangecrosshairvisible(true); categoryplot.setrangecrosshairpaint(color.blue); numberaxis numberaxis = (numberaxis) categoryplot.getrangeaxis(); numberaxis.setstandardtickunits(numberaxis.createintegertickunits()); barrenderer barrenderer = (barrenderer) categoryplot.getrenderer(); barrenderer.setbaseitemlabelfont(new font("宋体", font.plain, 12)); barrenderer.setseriesitemlabelfont(1, new font("宋体", font.plain, 12)); categoryaxis domainaxis = categoryplot.getdomainaxis(); /*------设置x轴坐标上的文字-----------*/ domainaxis.setticklabelfont(new font("sans-serif", font.plain, 11)); /*------设置x轴的标题文字------------*/ domainaxis.setlabelfont(new font("宋体", font.plain, 12)); /*------设置y轴坐标上的文字-----------*/ numberaxis.setticklabelfont(new font("sans-serif", font.plain, 12)); /*------设置y轴的标题文字------------*/ numberaxis.setlabelfont(new font("宋体", font.plain, 12)); /*------这句代码解决了底部汉字乱码的问题-----------*/ chart.getlegend().setitemfont(new font("宋体", font.plain, 12)); chartutilities.writechartasjpeg(response.getoutputstream(), 1.0f, chart, 500, 300, null); } catch (exception es) { es.printstacktrace(); } } }
jsp上使用img图片来请求servlet显示图片:
<%@ page language="java" pageencoding="utf-8"%> <% string path = request.getcontextpath(); string basepath = request.getscheme()+"://"+request.getservername()+":"+request.getserverport()+path+"/"; %> <!doctype html public "-//w3c//dtd html 4.01 transitional//en"> <html> <head> <base href="<%=basepath%>" rel="external nofollow" > <title></title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="this is my page"> </head> <body> <center> <img src="lineservlet"> <img src="pillarservlet"> <img src="paragraphsservlet"> <img src="cakeservlet"> </center> </body> </html>
web.xml配置:
<?xml version="1.0" encoding="utf-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xsi:schemalocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <filter> <filter-name>encodingfilter</filter-name> <filter-class>org.filter.encodingfilter</filter-class> </filter> <filter-mapping> <filter-name>encodingfilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <servlet> <servlet-name>lineservlet</servlet-name> <servlet-class>com.xidian.servlet.lineservlet</servlet-class> </servlet> <servlet> <servlet-name>pillarservlet</servlet-name> <servlet-class>com.xidian.servlet.pillarservlet</servlet-class> </servlet> <servlet> <servlet-name>paragraphsservlet</servlet-name> <servlet-class> com.xidian.servlet.paragraphsservlet </servlet-class> </servlet> <servlet> <servlet-name>cakeservlet</servlet-name> <servlet-class>com.xidian.servlet.cakeservlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>lineservlet</servlet-name> <url-pattern>/lineservlet</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>pillarservlet</servlet-name> <url-pattern>/pillarservlet</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>paragraphsservlet</servlet-name> <url-pattern>/paragraphsservlet</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>cakeservlet</servlet-name> <url-pattern>/cakeservlet</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
为了解决乱码问题,我配置了一个过滤器:
package org.filter; import java.io.ioexception; import javax.servlet.filter; import javax.servlet.filterchain; import javax.servlet.filterconfig; import javax.servlet.servletexception; import javax.servlet.servletrequest; import javax.servlet.servletresponse; /** * 处理乱码 * @说明 * @author cuisuqiang * @version 1.0 * @since */ public class encodingfilter implements filter { public void destroy() { } public void dofilter(servletrequest request, servletresponse response, filterchain chain) throws ioexception, servletexception { request.setcharacterencoding("gbk"); response.setcharacterencoding("gbk"); chain.dofilter(request, response); } public void init(filterconfig arg0) throws servletexception { } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。