JFreeChart与JSP动态图表.docx

上传人:b****5 文档编号:11883826 上传时间:2023-04-08 格式:DOCX 页数:19 大小:330.71KB
下载 相关 举报
JFreeChart与JSP动态图表.docx_第1页
第1页 / 共19页
JFreeChart与JSP动态图表.docx_第2页
第2页 / 共19页
JFreeChart与JSP动态图表.docx_第3页
第3页 / 共19页
JFreeChart与JSP动态图表.docx_第4页
第4页 / 共19页
JFreeChart与JSP动态图表.docx_第5页
第5页 / 共19页
点击查看更多>>
下载资源
资源描述

JFreeChart与JSP动态图表.docx

《JFreeChart与JSP动态图表.docx》由会员分享,可在线阅读,更多相关《JFreeChart与JSP动态图表.docx(19页珍藏版)》请在冰豆网上搜索。

JFreeChart与JSP动态图表.docx

JFreeChart与JSP动态图表

1.下载http:

//www.jfree.org/jfreechart/download.html

jfreechart-1.0.5jar

jcommon-1.0.9.jar、

\gnujaxp.jar

web.xml配置

DisplayChart

org.jfree.chart.servlet.DisplayChart

DisplayChart

/servlet/DisplayChart

生成动态图表步骤:

●创建绘图数据集合

●创建JFreeChart实例

●自定义图表绘制属性,该步可选

●生成指定格式的图片,并返回图片名称

●组织图片浏览路径

●通过HTML中的元素显示图片

2.核心类

类名

功能

JFreeChart

图表对象,生成任何类型的图表都要通过该对象,JFreeChart插件提供了一个工厂类ChartFactory,用来创建各种类型的图表对象

XXXDataset

数据集对象,用来保存绘制图表的数据,不同类型的图表对应着不同类型的数据集对象

XXXPlot

绘制区对象,如果需要自行定义绘图区的相关绘制属性,需要通过该对象进行设置

XXXAxis

坐标轴对象,用来定义坐标轴的绘制属性

XXXRenderer

图片渲染对象,用于渲染和显示图表

XXXURLGenerator

链接对象,用于生成Web图表中项目的鼠标单击链接

XXXToolTipGenrator

图表提示对象,用于生成图表提示信息,不同类型的图表对应着不同类型的图表提示对象

3.利用DefaultCategoryDataset数据集绘制柱形图

packagecom.mwq.jfreechart;

importjava.awt.Color;

importjava.awt.GradientPaint;

importjava.io.IOException;

importjavax.servlet.http.HttpSession;

importorg.jfree.chart.ChartFactory;

importorg.jfree.chart.ChartRenderingInfo;

importorg.jfree.chart.JFreeChart;

importorg.jfree.chart.axis.CategoryAxis;

importorg.jfree.chart.axis.CategoryLabelPositions;

importorg.jfree.chart.entity.StandardEntityCollection;

importorg.jfree.chart.plot.CategoryPlot;

importorg.jfree.chart.plot.PlotOrientation;

importorg.jfree.chart.renderer.category.BarRenderer;

importorg.jfree.chart.servlet.ServletUtilities;

importorg.jfree.chart.title.TextTitle;

importorg.jfree.data.category.DefaultCategoryDataset;

publicclassDrawHistogram{

intwidth;//图象宽度

intheight;//图象高度

StringchartTitle;//图表标题

Stringsubtitle;//副标题

StringxTitle;//X轴标题

StringyTitle;//Y轴标题

String[]cutline;//图例名称

Stringcategory[];//统计种类

Double[][]data;//绘图数据

StringservletURI="/servlet/DisplayChart";//映射路径

publicDrawHistogram(){

this.width=450;

this.height=325;

this.chartTitle="编程类图书年销量柱形图分析";

this.subtitle="------统计时间:

2008年";

this.xTitle="销售季度";

this.yTitle="销售量单位:

万册";

this.cutline=newString[]{"ASP","JSP","PHP"};

this.category=newString[]{"第1季度","第2季度","第3季度","第4季度"};

this.data=newDouble[cutline.length][category.length];

for(intm=0;m

for(intn=0;n

data[m][n]=1+Math.random()*100;

}

}

}

publicDrawHistogram(intwidth,intheight,StringchartTitle,

Stringsubtitle,StringxTitle,StringyTitle,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;

}

publicDrawHistogram(intwidth,intheight,StringchartTitle,

Stringsubtitle,StringxTitle,StringyTitle,String[]cutline,

String[]category,Double[][]data,StringservletURI){

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;

}

publicStringdraw(HttpSessionsession,StringcontextPath){

//创建绘图数据集

DefaultCategoryDatasetdataset=newDefaultCategoryDataset();

for(intm=0;m

for(intn=0;n

dataset.addValue(data[m][n],cutline[m],category[n]);

}

}

//创建图表对象

JFreeChartchart=ChartFactory.createBarChart3D(chartTitle,//图表标题

xTitle,//X轴标题

yTitle,//Y轴标题

dataset,//绘图数据集

PlotOrientation.VERTICAL,//柱形图绘制方向

true,//显示图例

true,//采用标准生成器

false//生成链接

);

//自定义图表开始:

if(subtitle.length()>0){//添加副标题

chart.addSubtitle(newTextTitle(subtitle));

}

GradientPaintchartGP=newGradientPaint(0,0,

newColor(219,227,251),0,height,Color.WHITE,false);//创建渐变色对象

chart.setBackgroundPaint(chartGP);//设置图片背景色

//通过绘图区对象,可以设置更多的绘图属性

CategoryPlotplot=chart.getCategoryPlot();

plot.setBackgroundPaint(newColor(241,219,127));//设置绘图区背景色

plot.setRangeGridlinePaint(Color.RED);//设置水平方向背景线颜色

plot.setRangeGridlinesVisible(true);//设置是否显示水平方向背景线,默认值为true

plot.setDomainGridlinePaint(Color.RED);//设置垂直方向背景线颜色

plot.setDomainGridlinesVisible(true);//设置是否显示垂直方向背景线,默认值为false

//通过柱形对象,可以设置柱形的绘图属性

BarRendererrenderer=(BarRenderer)plot.getRenderer();

renderer.setDrawBarOutline(false);//设置是否绘制柱形的轮廓线,默认值为true

//设置填充柱形的渐进色

Colorcolor[]=newColor[cutline.length];

color[0]=newColor(99,99,0);

color[1]=newColor(255,169,66);

color[2]=newColor(33,255,66);

for(inti=0;i

GradientPaintgp=newGradientPaint(0,0,color[i].brighter(),0,

height,color[i].darker());

renderer.setSeriesPaint(i,gp);

}

//设置横轴标题文字的旋转方向

CategoryAxisdomainAxis=plot.getDomainAxis();

domainAxis.setCategoryLabelPositions(CategoryLabelPositions.UP_45);

//自定义图表结束!

//固定用法

ChartRenderingInfoinfo=newChartRenderingInfo(

newStandardEntityCollection());

//生成指定格式的图片,并返回图片名称

StringfileName="";

try{

fileName=ServletUtilities.saveChartAsPNG(chart,width,height,

info,session);

}catch(IOExceptione){

System.out.println("------在绘制图片时抛出异常,内容如下:

");

e.printStackTrace();

}

//组织图片浏览路径

StringgraphURL=contextPath+servletURI+"?

filename="+fileName;

//返回图片浏览路径

returngraphURL;

}

}

JSP:

<%@pagelanguage="java"import="java.util.*"pageEncoding="GB2312"%>

directive.pageimport="com.mwq.jfreechart.DrawHistogram"/>

DOCTYPEHTMLPUBLIC"-//W3C//DTDHTML4.01Transitional//EN">

利用DefaultCategoryDataset数据集绘制柱形图

 

 

利用DefaultCategoryDataset数据集绘制柱形图

 

<%

//使用默认构造方法

DrawHistogramhistogramPositive=newDrawHistogram();

%>

"border="1"alt="">

 

<%

//如果在绘图数据中存在负数,绘制出的图片将是另一种效果

intwidth=450;//图象宽度

intheight=325;//图象高度

StringchartTitle="编程类图书同期销量对比柱形图分析";//图表标题

Stringsubtitle="------对比2008年与2007年同期";//副标题

StringxTitle="对比季度";//X轴标题

StringyTitle="增减量单位:

万册";//Y轴标题

String[]cutline=newString[]{"ASP","JSP","PHP"};//图例名称

Stringcategory[]=newString[]{"第1季度","第2季度","第3季度","第4季度"};//统计种类

Double[][]data=newDouble[cutline.length][category.length];//绘图数据

for(intm=0;m

for(intn=0;n

data[m][n]=1+Math.random()*100;

if((int)(Math.random()*100)%2!

=0){

data[m][n]=-(1+Math.random()*100);

}

}

}

DrawHistogramhistogramMinus=newDrawHistogram(width,height,chartTitle,subtitle,xTitle,yTitle,cutline,category,data);

%>

"border="1"alt="">

 

 

4.利用XYDataset数据集绘制与日期相关的柱形图

packagecom.mwq.jfreechart;

importjava.awt.Color;

importjava.awt.GradientPaint;

importjava.io.IOException;

importjava.util.HashMap;

importjava.util.Iterator;

importjava.util.Map;

importjavax.servlet.http.HttpSession;

importorg.jfree.chart.ChartFactory;

importorg.jfree.chart.ChartRenderingInfo;

importorg.jfree.chart.JFreeChart;

importorg.jfree.chart.axis.DateAxis;

importorg.jfree.chart.axis.DateTickMarkPosition;

importorg.jfree.chart.entity.StandardEntityCollection;

importorg.jfree.chart.plot.PlotOrientation;

importorg.jfree.chart.plot.XYPlot;

importorg.jfree.chart.servlet.ServletUtilities;

importorg.jfree.chart.title.TextTitle;

importorg.jfree.data.time.Month;

importorg.jfree.data.time.TimeSeries;

importorg.jfree.data.time.TimeSeriesCollection;

importorg.jfree.data.time.Year;

importorg.jfree.data.xy.IntervalXYDataset;

publicclassDrawHistogram{

intwidth;//图象宽度

intheight;//图象高度

StringchartTitle;//图表标题

Stringsubtitle;//副标题

StringxTitle;//X轴标题

StringyTitle;//Y轴标题

Mapdata;//绘图数据

StringservletURI="/servlet/DisplayChart";//映射路径

publicDrawHistogram(){

this.width=450;

this.height=325;

this.chartTitle="倾诚科技年销售额柱形图分析";

this.subtitle="------统计时间:

2008年";

this.xTitle="统计月份";

this.yTitle="销售额单位:

万元";

this.data=newHashMap();

for(inti=1;i<13;i++){

data.put(i,1+Math.random()*100);

}

}

publicDrawHistogram(intwidth,intheight,StringchartTitle,

Stringsubtitle,Stringtitle,Stringtitle2,Mapdata){

super();

this.width=width;

this.height=height;

this.chartTitle=chartTitle;

this.subtitle=subtitle;

xTitle=title;

yTitle=title2;

this.data=data;

}

publicStringdraw(HttpSessionsession,StringcontextPath){

//创建绘图数据集

TimeSeriestimeSeries=newTimeSeries(chartTitle,xTitle,yTitle,

Month.class);

Iteratorit=data.keySet().iterator();

while(it.hasNext()){

intkey=(Integer)it.next();

timeSeries.add(newMon

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 高中教育 > 理化生

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1