JFreeChart与JSP动态图表.docx
《JFreeChart与JSP动态图表.docx》由会员分享,可在线阅读,更多相关《JFreeChart与JSP动态图表.docx(19页珍藏版)》请在冰豆网上搜索。
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;mfor(intn=0;ndata[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;mfor(intn=0;ndataset.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;iGradientPaintgp=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;mfor(intn=0;ndata[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=""> |
|
|