jfreechat整理Word格式.docx

上传人:b****5 文档编号:21331781 上传时间:2023-01-29 格式:DOCX 页数:20 大小:540.28KB
下载 相关 举报
jfreechat整理Word格式.docx_第1页
第1页 / 共20页
jfreechat整理Word格式.docx_第2页
第2页 / 共20页
jfreechat整理Word格式.docx_第3页
第3页 / 共20页
jfreechat整理Word格式.docx_第4页
第4页 / 共20页
jfreechat整理Word格式.docx_第5页
第5页 / 共20页
点击查看更多>>
下载资源
资源描述

jfreechat整理Word格式.docx

《jfreechat整理Word格式.docx》由会员分享,可在线阅读,更多相关《jfreechat整理Word格式.docx(20页珍藏版)》请在冰豆网上搜索。

jfreechat整理Word格式.docx

3.JCommon:

JFreeReport和JFreeChart的公共类库

4.JFreeDesigner:

JFreeReport的报表设计工具

我们可以从jfree官方网站上获取最新版本和相关资料(但是jfree的document需要40美金才能获取),

获取地址:

http:

//www.jfree.org/jfreechart/index.html(同时可以获得简明介绍)

我们以当前最新版本:

jfreechart-1.0.1.zip为例子进行说明。

三、JFreeChart配置安装

1、解压jfreechart-1.0.1.zip.zip到指定位置,其中source是jfreechart的源码,jfreechart-1.0.1-demo.jar是例子程序,可以先运行一下看看各种效果,就知道他的nb之处了。

2、为了配置成功,我们需要关注的文件有如下三个:

设置classpath。

加入下面三个jar包。

jfreechart-1.0.1.jar、jcommon-1.0.0.jar、gnujaxp.jar

加上第三个jar包有时web.xml会报错,把它去掉就好了。

至此jfreechart的配置就完成了,下面就可以进行jfreechart的开发了。

这里值得提出的是jfreechart的类

结构设计前后兼容性不是很好,不同版本的jfreechart中类库结构可能不一样,有时候可能需要查源码。

如果是中文显示的时候可能依据观感需要改变源码的字体。

四、JFreeChart功能介绍

JFreeChart目前是最好的java图形解决方案,基本能够解决目前的图形方面的需求,主要包括如下几个方面:

piecharts(2Dand3D):

饼图(平面和立体)

barcharts(regularandstacked,withanoptional3Deffect):

柱状图

lineandareacharts:

曲线图

scatterplotsandbubblecharts

timeseries,high/low/open/closechartsandcandlestickcharts:

时序图

combinationcharts:

复合图

Paretocharts

Ganttcharts:

甘特图

windplots,meterchartsandsymbolcharts

wafermapcharts

(态图表,饼图(二维和三维),柱状图(水平,垂直),线图,点图,时间变化图,甘特图,股票行情图,混和图,温度计图,刻度图等常用商用图表)

图形可以导出成PNG和JPEG格式,同时还可以与PDF和EXCEL关联

JFreeChart核心类库介绍:

研究jfreechart源码发现源码的主要由两个大的包组成:

org.jfree.chart,org.jfree.data。

其中前者主要与图形

本身有关,后者与图形显示的数据有关。

具体研究如果大家有兴趣的话可以自己研究。

核心类主要有:

org.jfree.chart.JFreeChart:

图表对象,任何类型的图表的最终表现形式都是在该对象进行一些属性的定制。

JFreeChart引擎本身提供了一个工厂类用于创建不同类型的图表对象

org.jfree.data.category.XXXDataSet:

数据集对象,用于提供显示图表所用的数据。

根据不同类型的图表对应着很多类型的数据集对象类

org.jfree.chart.plot.XXXPlot:

图表区域对象,基本上这个对象决定着什么样式的图表,创建该对象的时候需要Axis、Renderer以及数据集对象的支持

org.jfree.chart.axis.XXXAxis:

用于处理图表的两个轴:

纵轴和横轴

org.jfree.chart.render.XXXRender:

负责如何显示一个图表对象

org.jfree.chart.urls.XXXURLGenerator:

用于生成Web图表中每个项目的鼠标点击链接

XXXXXToolTipGenerator:

用于生成图象的帮助提示,不同类型图表对应不同类型的工具提示类

个人感觉JFreeChart可以满足大部分图片创建的需要,美中不足的是:

对字体的设置做的不够好,特别是使用中文的时候字体很不清晰。

因为这个原因建议你自己去修改他的源代码,最好使用properties文件去设置字体.还有就是文档要钱所以要多花点时间去看源代码。

或多上社区.

五.开始开发

对JfreeChart有了初步了解并做好准备工作之后,开始作例子程序试验。

在这里我只介绍饼图的做法,而这张图采用不同的方式进行输出,其他类型的图片可以参考jfreechart提供的例子,做法都差不多。

1)直接生成图片

Java代码

publicclassPieChartPicture{

publicstaticvoidmain(String[]args)

{

PieDatasetdataset=getDataSet();

<

SPANclass=hilite1>

JFreeChart<

/SPAN>

chart=ChartFactory.createPieChart3D(

"

项目进度分布"

//charttitle

dataset,//data

true,//includelegend

true,

false

);

PiePlot3Dplot=(PiePlot3D)chart.getPlot();

//图片中显示百分比:

默认方式

//plot.setLabelGenerator(newStandardPieSectionLabelGenerator(StandardPieToolTipGenerator.DEFAULT_TOOLTIP_FORMAT));

自定义方式,{0}表示选项,{1}表示数值,{2}表示所占比例,小数点后两位

plot.setLabelGenerator(newStandardPieSectionLabelGenerator("

{0}={1}({2})"

NumberFormat.getNumberInstance(),newDecimalFormat("

0.00%"

)));

//图例显示百分比:

自定义方式,{0}表示选项,{1}表示数值,{2}表示所占比例

plot.setLegendLabelGenerator(newStandardPieSectionLabelGenerator("

));

//设置背景色为白色

chart.setBackgroundPaint(Color.white);

//指定图片的透明度(0.0-1.0)

plot.setForegroundAlpha(1.0f);

//指定显示的饼图上圆形(false)还椭圆形(true)

plot.setCircular(true);

//设置图标题的字体

Fontfont=newFont("

黑体"

Font.CENTER_BASELINE,20);

TextTitletitle=newTextTitle("

项目状态分布"

title.setFont(font);

chart.setTitle(title);

FileOutputStreamfos_jpg=null;

try{

fos_jpg=newFileOutputStream("

D:

\\项目状态分布.jpg"

ChartUtilities.writeChartAsJPEG(fos_jpg,100,chart,640,480,null);

fos_jpg.close();

}catch(Exceptione){

}

privatestaticPieDatasetgetDataSet(){

DefaultPieDatasetdataset=newDefaultPieDataset();

dataset.setValue("

市场前期"

newDouble(10));

立项"

newDouble(15));

计划"

需求与设计"

执行控制"

newDouble(35));

收尾"

运维"

newDouble(10));

returndataset;

publicclassPieChartPicture{

{

JFreeChartchart=ChartFactory.createPieChart3D(

//charttitle

dataset,//data

true,//includelegend

true,

false

默认方式

自定义方式,{0}表示选项,{1}表示数值,{2}表示所占比例,小数点后两位

}

2)采用servlet和struts的action方式输出

采用这种方式输出,不用生成图片。

A.servlet输出

publicclassPieByServletextendsHttpServlet{

publicvoidservice(ServletRequestreq,ServletResponseres)

throwsServletException,IOException

res.setContentType("

image/jpeg"

水果饼图"

true,//includelegend

false);

//设置图表属性

//输出图片

ChartUtilities.writeChartAsJPEG(res.getOutputStream(),100,chart,800,600,null);

JFreeChartchart=ChartFactory.createPieChart3D(

//设置图表属性

B.struts的action方式输出

只将这条语句加上trycatch即可,并返回null。

try{

ChartUtilities.writeChartAsJPEG(response.getOutputStream(),100,chart,800,600,null);

returnnull;

其实采用这两种方式与生成图片的方式改动并不大

加上语句response.setContentType("

ChartUtilities.writeChartAsJPEG(newFileOutputStream("

100,chart,640,480,null);

文件流改成response的输出流就可以了

hartUtilities.writeChartAsJPEG(response.getOutputStream(),100,chart,800,600,null);

3)jsp+servlet+javabean方式

1.CreateChartViewerservlet

publicclassChartViewerextendsHttpServlet{

publicvoidinit()throwsServletException{

//ProcesstheHTTPGetrequest

publicvoiddoGet(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{

//getthechartfromsession

HttpSessionsession=request.getSession();

BufferedImagechartImage=(BufferedImage)session.getAttribute("

chartImage"

//setthecontenttypesothebrowsercanseethisasapicture

response.setContentType("

image/png"

//sendthepicture

PngEncoderencoder=newPngEncoder(chartImage,false,0,9);

response.getOutputStream().write(encoder.pngEncode());

//ProcesstheHTTPPostrequest

publicvoiddoPost(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{

doGet(request,response);

//ProcesstheHTTPPutrequest

publicvoiddoPut(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{

//Cleanupresources

publicvoiddestroy(){}

publicvoidinit()throwsServletException{

//ProcesstheHTTPGetrequest

//getthechartfromsession

publicvoiddoPost(HttpServ

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

当前位置:首页 > 表格模板 > 合同协议

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

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