您的位置:首页 > Web前端 > JavaScript

使用Jfreechart在jsp程序里面开发图表应用程序

2008-04-10 18:38 351 查看
eric | 07 二月, 2006 09:58
JFreeChart是基于Java平台的免费的图表库.它运行在java2(JDK 1.2.2或更高版本)平台上,并且使用Java 2D API进行制图。最新的版本是jfreechart-1.0.0-rc1,可以从http://www.jfree.org/jfreechart/下载到

1.介绍

JFreeChart是基于Java平台的免费的图表库.它运行在java2(JDK 1.2.2或更高版本)平台上,并且使用Java 2D API进行制图。最新的版本是jfreechart-1.0.0-rc1,可以从http://www.jfree.org/jfreechart/下载到。

2.文档

可以从其网站上(http://prdownloads.sourceforge.net/jfreechart/jfreechart-1.0.0-rc1-install.pdf?download)下载到它的安装文档(jfreechart-1.0.0-rc1-install.pdf)。

JFreeChart的API文档http://www.jfree.org/jfreechart/javadoc/,如果想在本地保存一份的话,一种方法是利用javadoc或者gcjdoc工具直接从源代码中生成,还有一种方法是使用Ant脚本(Build.xml).

遗憾的是,JFreeChart的开发文档(the JFreeChart Developer Guide)是需要付费的(US$39.95),也许这就是关于JFreeChart的文章比较少的原因吧。

3.依赖的包

(a) JDK 1.2.2或者更高版本.

(b) JCommon - version 1.0.0-pre2 or later.

(c) GNU JAXP

(d) servlet.jar

(e) JUnit

除(a)外, 都可以在JFreeChart的jar包里面找到.

4.安装

5.通过一个简单的例子,让我们看看JFreeChart是如何方便的生成我们想要的饼图的.

package tracy.test;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;

import org.jfree.chart.ChartFactory;

import org.jfree.chart.ChartUtilities;

import org.jfree.chart.JFreeChart;

import org.jfree.data.general.DefaultPieDataset;

public class PieChart {

public void drawToFile(){

//设置数据集

DefaultPieDataset dataset = new DefaultPieDataset();

dataset.setValue("冰箱", 1800);

dataset.setValue("电视", 1600);

dataset.setValue("空调", 3300);

dataset.setValue("洗衣机", 2000);

dataset.setValue("DVD", 400);

//通过工厂类生成JFreeChart对象

JFreeChart chart = ChartFactory.createPieChart3D("家电投资比例示意图", dataset, true, false, false);

//写图表对象到文件

FileOutputStream fos_jpg = null;

try {

fos_jpg = new FileOutputStream("D:pi.jpg");

ChartUtilities.writeChartAsJPEG(fos_jpg,100,chart,400,300,null);

} catch (FileNotFoundException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

} finally {

try {

fos_jpg.close();

} catch (Exception e) {}

}

}

}

运行后如下图:



6.设置显示方式

上一篇只是对Jfreechart做了一个介绍,下面我们来深入探讨Jfreechart的设置,随心所欲得到我们想要的图表。

(本文列出的例子是对前文例子的修改,所以请大家在阅读本文之前,先阅读前面的一篇.)

一. 加个副标题

chart.addSubtitle(new TextTitle("2005年度"));



二.对图表显示方式的设置

这就要借助Plot了,对于饼图的设置,有相应的PiePlot,得到其对象的方法:

PiePlot pie = (PiePlot)chart.getPlot();

下面通过几个例子,说明PiePlot的几个常用方法,是如何控制chart发生变化,达到我们预期效果的.

1. 将指定section的轮廓线设置成白色

plot.setSectionOutlinePaint(1, Color.WHITE);



2. 设置饼图是圆的(true),还是椭圆的(false);默认为true

plot.setCircular(false);


plot.setExplodePercent(0, 0.2);



三.百分率问题

什么是百分率问题?

默认状态下,标签和图例显示的是"关键字=数值"的格式。如果我们想标出每个section所占的百分率,那怎么做呢?我想在显示数值的同时,显示单位,又该怎么做呢?

这个问题,还是要借助Plot,只是这是一个大家经常遇到的一个问题,所以单独讨论。

设置标签使用setLabelGenerator(PieSectionLabelGenerator generator)方法,设置图例使用setLegendLabelGenerator(PieSectionLabelGenerator generator)方法

//显示格式设置成DEFAULT_TOOLTIP_FORMAT:"{0}: ({1}, {2})"

plot.setLabelGenerator(new StandardPieSectionLabelGenerator("{0}: ({1}, {2})", NumberFormat.getNumberInstance(), new DecimalFormat("0.0%")));



//标签显示格式设置成{0}={2}

plot.setLabelGenerator(new StandardPieSectionLabelGenerator("{0}={2}", NumberFormat.getNumberInstance(), new DecimalFormat("0.0%")));



//图例显示格式设置成{0}: ¥{1},加上单位,更加清楚数值的含义

plot.setLegendLabelGenerator(new StandardPieSectionLabelGenerator("{0}: ¥{1}"));



这里{0}表示"关键字",{1}表示"数值",{2}表示"百分率"

声明:JFreeChart版本修订过程中,类结构出现相当大的变化,本文仅针对jfreechart-1.0.0.

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐