------------------------------------------------------------------------------------------
jfreechart文档居然是收费的,在网上找了好多资料都是针对1。0之前的版本的,好入容易找到一个1.0下面可以用的
package com.meetexpo.cms.backend.util;
import java.awt.Color;
import java.awt.Font;
import java.awt.GradientPaint;
import java.awt.RenderingHints;
import java.io.PrintWriter;
import javax.servlet.http.HttpSession;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartRenderingInfo;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.CategoryLabelPositions;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.entity.StandardEntityCollection;
import org.jfree.chart.labels.CategoryItemLabelGenerator;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.BarRenderer;
import org.jfree.chart.renderer.category.CategoryItemRenderer;
import org.jfree.chart.servlet.ServletUtilities;
import org.jfree.chart.title.TextTitle;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;
public class CategoryItemChart {
public static String generateBarChart(
HttpSession session,
PrintWriter pw,
int w, int h) {
String filename = null;
try {
CategoryDataset dataset=createDataset();
JFreeChart chart=ChartFactory.createBarChart(
"世界傻瓜大奖赛",//图表标题
"比赛场次",//X轴标题
"傻瓜程度",//Y轴标题
dataset,//数据集合
PlotOrientation.VERTICAL,//图表显示方向(水平、垂直)
true,//是否使用图例
false,//是否使用工具提示
false//是否为图表增加URL
);
/*------------配置图表属性--------------*/
// 1,设置整个图表背景颜色
chart.setBackgroundPaint(Color.yellow);
/*------------设定Plot参数-------------*/
CategoryPlot plot=chart.getCategoryPlot();
// 2,设置详细图表的显示细节部分的背景颜色
plot.setBackgroundPaint(Color.PINK);
// 3,设置垂直网格线颜色
plot.setDomainGridlinePaint(Color.black);
//4,设置是否显示垂直网格线
plot.setDomainGridlinesVisible(true);
//5,设置水平网格线颜色
plot.setRangeGridlinePaint(Color.yellow);
//6,设置是否显示水平网格线
plot.setRangeGridlinesVisible(true);
/*---------将所有数据转换为整数形式---------*/
final NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
/*---------设置是否在柱图的状态条上显示边框----*/
CategoryItemRenderer renderer = (CategoryItemRenderer) plot.getRenderer();
BarRenderer render=(BarRenderer) plot.getRenderer();
render.setItemMargin(0.1);
/*---------设置状态条颜色的深浅渐变-----------*/
GradientPaint gp0 = new GradientPaint(
0.0f, 0.0f, new Color(255,200,80),
0.0f, 0.0f, new Color(255,255,40)
);
GradientPaint gp1 = new GradientPaint(
0.0f, 0.0f, new Color(50,255,50),
0.0f, 0.0f, new Color(100,255,100)
);
GradientPaint gp2 = new GradientPaint(
0.0f, 0.0f, Color.red,
0.0f, 0.0f, new Color(255,100,100)
);
GradientPaint gp3 = new GradientPaint(
0.0f, 0.0f, new Color(108,108,255),
0.0f, 0.0f, new Color(150, 150, 200)
);
renderer.setSeriesPaint(0, gp0);
renderer.setSeriesPaint(1, gp1);
renderer.setSeriesPaint(2, gp2);
renderer.setSeriesPaint(3, gp3);
/*
*
* 解决柱状体与图片边框的间距问题
*
*
* */
/*------设置X轴标题的倾斜程度----*/
CategoryAxis domainAxis = plot.getDomainAxis();
domainAxis.setCategoryLabelPositions(
CategoryLabelPositions.createUpRotationLabelPositions(Math.PI / 6.0)
);
/*------设置柱状体与图片边框的左右间距--*/
domainAxis.setLowerMargin(0.01);
domainAxis.setUpperMargin(0.01);
/*------设置柱状体与图片边框的上下间距---*/
ValueAxis rAxis = plot.getRangeAxis();
rAxis.setUpperMargin(0.15);
rAxis.setLowerMargin(0.15);
/*---------设置每一组柱状体之间的间隔---------*/
render.setItemMargin(0.0);
/*
*
* 解决柱状体与图片边框的间距问题
*
*
* */
/*
*
*
* 解决JFREECHART的中文显示问题
*
*
* */
/*----------设置消除字体的锯齿渲染(解决中文问题)--------------*/
chart.getRenderingHints().put(
RenderingHints.KEY_TEXT_ANTIALIASING,RenderingHints.VALUE_TEXT_ANTIALIAS_OFF
);
/*----------设置标题字体--------------------------*/
TextTitle textTitle = chart.getTitle();
textTitle.setFont(new Font("黑体", Font.PLAIN, 20));
/*------设置X轴坐标上的文字-----------*/
domainAxis.setTickLabelFont(new Font("sans-serif",Font.PLAIN,11));
/*------设置X轴的标题文字------------*/
domainAxis.setLabelFont(new Font("宋体",Font.PLAIN,12));
/*------设置Y轴坐标上的文字-----------*/
rAxis.setTickLabelFont(new Font("sans-serif",Font.PLAIN,12));
/*------设置Y轴的标题文字------------*/
rAxis.setLabelFont(new Font("黑体",Font.PLAIN,12));
/*---------设置柱状体上的显示的字体---------*/
renderer.setItemLabelGenerator(new LabelGenerator(0.0));
renderer.setItemLabelFont(new Font("宋体",Font.PLAIN,12));
renderer.setItemLabelsVisible(true);
/*
*
*
* 解决JFREECHART的中文显示问题
*
*
* */
/*------得到chart的保存路径----*/
ChartRenderingInfo info = new ChartRenderingInfo(new
StandardEntityCollection());
filename = ServletUtilities.saveChartAsPNG(chart, w, h, info, session);
/*------使用printWriter将文件写出----*/
ChartUtilities.writeImageMap(pw, filename, info,true);
pw.flush();
}
catch (Exception e) {
System.out.println("Exception - " + e.toString());
e.printStackTrace(System.out);
filename = "public_error_500x300.png";
}
return filename;
}
/*-------------设置柱状体顶端的数据显示--------------*/
static class LabelGenerator implements CategoryItemLabelGenerator {
private double threshold;
public LabelGenerator(double threshold) {
this.threshold = threshold;
}
public String generateLabel(CategoryDataset dataset,
int row,int column) {
String result = null;
final Number value = dataset.getValue(row, column);
if (value != null) {
final double v = value.doubleValue();
if (v > this.threshold) {
result = value.toString();
}
}
return result;
}
public String generateRowLabel(CategoryDataset dataset,
int row){
return null;
}
public String generateColumnLabel(CategoryDataset dataset,
int column){
return null;
}
}
/*-----------数据集合封装-------------*/
private static CategoryDataset createDataset(){
String s1="笨笨";
String s2="蛋蛋";
String s3="傻傻";
String s4="瓜瓜";
String c1="第一场";
String c2="第二场";
String c3="第三场";
String c4="第四场";
String c5="第五场";
/*-------------封装图表使用的数据集-------------*/
DefaultCategoryDataset dataset=new DefaultCategoryDataset();
dataset.setValue(1.0,s1,c1);
dataset.setValue(2.0,s1,c2);
dataset.setValue(3.0,s1,c3);
dataset.setValue(4.0,s1,c4);
dataset.setValue(5.0,s1,c5);
dataset.setValue(5.0,s2,c1);
dataset.setValue(4.0,s2,c2);
dataset.setValue(3.0,s2,c3);
dataset.setValue(2.0,s2,c4);
dataset.setValue(1.0,s2,c5);
dataset.setValue(1.0,s3,c1);
dataset.setValue(2.0,s3,c2);
dataset.setValue(3.0,s3,c3);
dataset.setValue(2.0,s3,c4);
dataset.setValue(1.0,s3,c5);
dataset.setValue(1.0,s4,c1);
dataset.setValue(2.0,s4,c2);
dataset.setValue(3.0,s4,c3);
dataset.setValue(4.0,s4,c4);
dataset.setValue(5.0,s4,c5);
return dataset;
}
}
JSP代码如下:
<%@ page contentType="text/html;charset=GBK"%>
<%@ page import="java.io.PrintWriter"%>
<%@ page import="com.meetexpo.cms.backend.util.CategoryItemChart;"%>
<html>
<head>
<title>
</title>
<%
//以下部分为图象输出
String fileName=CategoryItemChart.generateBarChart(session,new PrintWriter(out),580,250);
String graphURL = request.getContextPath() + "/servlet/DisplayChart?filename=" + fileName;
%>
</head>
<body bgcolor="#ffffff">
<table width="580" border="0" cellspacing="0" cellpadding="0">
<tr>
<td>
<img src="<%= graphURL %>" width=580 height=250 border=0
>
</td>
</tr>
</table>
</body>
</html>
类包是jcommon-1.0.0.jar和jfreechart-1.0.0.jar
-------------------------------------------------------------------------------------------
用JFreeChart画统计分析柱状图 |
源作者:未知 人气:28605 |
|
我们介绍使用 JFreeChart 生成柱状图,首先从一个最简单的例子开始。 一 最简单的例子 为了降低门槛,让大家心理有个底,先介绍一个简单的不能再简单的例子,图片中的各类属性都采用默认值。 <%@ page contentType="text/html;charset=GBK"%><BR> <%@ page import="org.jfree.chart.ChartFactory,<BR> org.jfree.chart.JFreeChart,<BR> org.jfree.chart.plot.PlotOrientation,<BR> org.jfree.chart.servlet.ServletUtilities,<BR> org.jfree.data.DefaultCategoryDataset"%><BR> <% <BR> DefaultCategoryDataset dataset = new DefaultCategoryDataset();<BR> dataset.addValue(300, "广州", "苹果");<BR> dataset.addValue(200, "广州", "梨子");<BR> dataset.addValue(500, "广州", "葡萄");<BR> dataset.addValue(340, "广州", "芒果");<BR> dataset.addValue(280, "广州", "荔枝");<BR> JFreeChart chart = ChartFactory.createBarChart3D("水果销量统计图", "水果", "销量",dataset, PlotOrientation.VERTICAL, false, false, false);<BR> String filename = ServletUtilities.saveChartAsPNG(chart, 500, 300, null, session);<BR> String graphURL = request.getContextPath() + "/servlet/DisplayChart?filename=" + filename;<BR> %> <IMG height=300 src="<%= graphURL %>" width=500 useMap="#<%= filename %>" border=0> 这个 JSP 程序运行的结果如下图
图 一
二 柱状图高级特性 上面的程序简单,但生成的柱状图也很简单。更多的时候,我们可能需要不同的效果。org.jfree.chart.ChartFactory 这个工厂类有createBarChart,createStackedBarChart,createBarChart3D,createStackedBarChart3D这几个工厂方法创建不同类型的柱状图。关于这四个方法的 JFreeChart 的 Java Doc API 文档有详细说明,比较重要的是 PlotOrientation.VERTICAL 让平行柱垂直显示,而 PlotOrientation.HORIZONTAL 则让平行柱水平显示。 几个对柱状图影响较大的几个类,它们分别是: org.jfree.chart.axis.CategoryAxis org.jfree.chart.axis.ValueAxis org.jfree.chart.renderer.BarRenderer org.jfree.chart.renderer.BarRenderer3D 我们还是以实例来说明这几个类,先来假设一个需要统计的数据表:
|
北京 |
上海 |
广州 |
成都 |
深圳 |
苹果 |
672 |
766 |
223 |
540 |
126 |
梨子 |
325 |
521 |
210 |
340 |
106 |
葡萄 |
332 |
256 |
523 |
240 |
526 |
根据上表数据,首先构造 CategoryDataset, 这里不再使用上面简单例子里面的 DefaultCategoryDataset 类,而是 DatasetUtilities 更有效的构造 CategoryDataset,如下列代码:
double[][] data = new double[][] {{672, 766, 223, 540, 126}, {325, 521, 210, 340, 106}, {332, 256, 523, 240, 526} }; String[] rowKeys = {"苹果","梨子","葡萄"}; String[] columnKeys = {"北京","上海","广州","成都","深圳"}; CategoryDataset dataset = DatasetUtilities.createCategoryDataset(rowKeys, columnKeys, data);
用上面的 dataset 生成的 3D 柱状图
图 二
org.jfree.chart.axis.CategoryAxis CategoryAxis domainAxis = plot.getDomainAxis(); //设置 columnKey 是否垂直显示 domainAxis.setVerticalCategoryLabels(true); //设置距离图片左端距离 domainAxis.setLowerMargin(0.1); //设置距离图片右端距离 domainAxis.setUpperMargin(0.1); //设置 columnKey 是否间隔显示 domainAxis.setSkipCategoryLabelsToFit(true); plot.setDomainAxis(domainAxis); 上面代码产生的效果如下图,注意与图二的区别。
图 三
org.jfree.chart.axis.ValueAxis
ValueAxis rangeAxis = plot.getRangeAxis(); //设置最高的一个柱与图片顶端的距离 rangeAxis.setUpperMargin(0.15); //设置最低的一个柱与图片底端的距离 //rangeAxis.setLowerMargin(0.15); plot.setRangeAxis(rangeAxis);
上面代码产生的效果如下图,注意与图二的区别。
图 四
org.jfree.chart.renderer.BarRenderer3D
BarRenderer3D renderer = new BarRenderer3D(); renderer.setBaseOutlinePaint(Color.BLACK); //设置 Wall 的颜色 renderer.setWallPaint(Color.gray); //设置每种水果代表的柱的颜色 renderer.setSeriesPaint(0, new Color(0, 0, 255)); renderer.setSeriesPaint(1, new Color(0, 100, 255)); renderer.setSeriesPaint(2, Color.GREEN); //设置每种水果代表的柱的 Outline 颜色 renderer.setSeriesOutlinePaint(0, Color.BLACK); renderer.setSeriesOutlinePaint(1, Color.BLACK); renderer.setSeriesOutlinePaint(2, Color.BLACK); //设置每个地区所包含的平行柱的之间距离 renderer.setItemMargin(0.1); //显示每个柱的数值,并修改该数值的字体属性 renderer.setItemLabelGenerator(new StandardCategoryItemLabelGenerator()); renderer.setItemLabelFont(new Font("黑体",Font.PLAIN,12)); renderer.setItemLabelsVisible(true); 上面代码产生的效果如下图,注意与图二的区别。
图 五
补充两个有用的方法 补充 org.jfree.chart.plot.CategoryPlot 的两个方法,这两个方法对所有类型的图表都有作用,因为在前面没有介绍,这里补充一下。
//设置地区、销量的显示位置 plot.setDomainAxisLocation(AxisLocation.TOP_OR_RIGHT); plot.setRangeAxisLocation(AxisLocation.BOTTOM_OR_RIGHT);
上面代码产生的效果如下图,注意与图二的区别。
图 六
三 完整范例 前面都是一些代码片段,现在把这些片段组合成一个完整范例。 <DIV class=code> <%@ page contentType="text/html;charset=GBK"%> <%@ page import="java.awt.Color, java.awt.Font, org.jfree.chart.ChartFactory, org.jfree.chart.JFreeChart, org.jfree.chart.plot.PlotOrientation, org.jfree.chart.servlet.ServletUtilities, org.jfree.data.CategoryDataset, org.jfree.data.DatasetUtilities, org.jfree.chart.plot.CategoryPlot, org.jfree.chart.axis.CategoryAxis, org.jfree.chart.axis.ValueAxis, org.jfree.chart.renderer.BarRenderer3D, org.jfree.chart.labels.StandardCategoryItemLabelGenerator, org.jfree.chart.axis.AxisLocation"%> <% double[][] data = new double[][] { {672, 766, 223, 540, 126}, {325, 521, 210, 340, 106}, {332, 256, 523, 240, 526} }; String[] rowKeys = {"苹果","梨子","葡萄"}; String[] columnKeys = {"北京","上海","广州","成都","深圳"}; CategoryDataset dataset = DatasetUtilities.createCategoryDataset(rowKeys, columnKeys, data); JFreeChart chart = ChartFactory.createBarChart3D("水果销量图统计",null,null,dataset, PlotOrientation.VERTICAL,true,false,false); chart.setBackgroundPaint(Color.WHITE); CategoryPlot plot = chart.getCategoryPlot(); CategoryAxis domainAxis = plot.getDomainAxis(); domainAxis.setVerticalCategoryLabels(false); plot.setDomainAxis(domainAxis); ValueAxis rangeAxis = plot.getRangeAxis(); //设置最高的一个 Item 与图片顶端的距离 rangeAxis.setUpperMargin(0.15); //设置最低的一个 Item 与图片底端的距离 rangeAxis.setLowerMargin(0.15); <BR>plot.setRangeAxis(rangeAxis); BarRenderer3D renderer = new BarRenderer3D(); renderer.setBaseOutlinePaint(Color.BLACK); //设置 Wall 的颜色<BR> renderer.setWallPaint(Color.gray); //设置每种水果代表的柱的颜色 renderer.setSeriesPaint(0, new Color(0, 0, 255)); renderer.setSeriesPaint(1, new Color(0, 100, 255)); renderer.setSeriesPaint(2, Color.GREEN); //设置每个地区所包含的平行柱的之间距离 renderer.setItemMargin(0.1); //显示每个柱的数值,并修改该数值的字体属性<BR> renderer.setItemLabelGenerator(new StandardCategoryItemLabelGenerator()); renderer.setItemLabelsVisible(true); plot.setRenderer(renderer); //设置柱的透明度<BR> plot.setForegroundAlpha(0.6f); //设置地区、销量的显示位置<BR> plot.setDomainAxisLocation(AxisLocation.TOP_OR_RIGHT); plot.setRangeAxisLocation(AxisLocation.BOTTOM_OR_RIGHT); String filename = ServletUtilities.saveChartAsPNG(chart, 500, 300, null, session); String graphURL = request.getContextPath() + "/servlet/DisplayChart?filename=" + filename; %s> <img src="<%= graphURL %>" width=500 height=300 border=0 usemap="#<%= filename %>"> </DIV> 看看程序运行的结果吧:
图 七
三 总结
我只介绍了少量的方法,更多的请参考 JFreeChart 的 Java Doc API 文档和 Sample Code。
|
|
--------------------------------------------------------------------------------------------
使用JFreeChart增强Java绘图功能
|
|
在给用户演示软件产品时,我们往往发现客户最初更感兴趣的是我们程序的用户界面,而不是程序的功能本身。也正因如此,图形用户界面的应用程序一经出现即广受欢迎,界面友好的Windows操作系统虽然性能很不稳定、安全漏洞百出,仍有无数用户有怨无悔地追随就是一个典型的例子。计算机编程技术发展到今天,遇到数据展示时人们已不满足于枯燥的列表方式,而更喜欢色彩丰富、一目了然的的图形效果。于是,在许多软件产品中会涉及到制图功能。
本文将介绍是一组功能强大、灵活易用的Java制图API——JFreeChart,使用JFreeChart我们可以极大地增强Java应用程序的高级制图功能,生成多种通用性的图表,包括饼图、柱状图、折线图、甘特图等。对于要在Swing或Web应用程序中加入自制图表的Java开发者而言,JFreeChart无疑是一种理想的选择。
下面以生成最常用的饼图(Pie Chart)为例,介绍一下使用JFreeChart制图的基本过程:
一.获取JFreeChart最新版本 JFreeChart为开放源码自由软件,可以到下述网址免费下载:http://www.jfree.org/jfreechart/,目前最新版本为v1.0.0-pre2版,下载并解压缩后,将其中包含的下述两个.jar文件加入到计算机编译和运行环境的环境变量CLASSPATH中:jcommon-1.0.0-pre2.jar,jfreechart-1.0.0-pre2.jar。
二.开发Java Application,使用JFreeChart API实现创建饼图的功能,源代码如下: 源文件:PieChart.java import java.io.*; import java.awt.*; import org.jfree.chart.*; import org.jfree.chart.title.TextTitle; import org.jfree.data.general.*;
public class PieChart{ public static void main(String[] args){ String title = "梦泽科技员工学历情况统计"; DefaultPieDataset piedata = new DefaultPieDataset(); piedata.setValue("大专", 8.1); piedata.setValue("大学", 27.6); piedata.setValue("硕士", 53.2); piedata.setValue("博士及以上", 19.2); piedata.setValue("大专以下", 1.9); JFreeChart chart = ChartFactory.createPieChart(title, piedata, true, true, true); chart.setTitle(new TextTitle(title, new Font("宋体", Font.BOLD, 25))); chart.addSubtitle(new TextTitle("最后更新日期:2005年5月19日", new Font("楷书", Font.ITALIC, 18))); chart.setBackgroundPaint(Color.white); try { ChartUtilities.saveChartAsJPEG(new File("PieChart.jpg"), chart, 360, 300); } catch (IOException exz) { System.out.print("....Cant′t Create image File"); } } }
三.编译运行上述源程序,生成的饼图文件"PieChart.jpg"效果如下:
关于上述应用程序使用的JFreeChart API,可查阅JFreeChart提供的在线API文档,以获取更详细的信息,其URL为http://www.jfree.org/jfreechart/javadoc/。读者可尝试参照上述例程、并借助于API 文档创建其他类型的图表。 需要说明的是,JFreeChart虽是一个开源(open source)项目——API可以免费下载、API文档可以免费在线查阅,其官方开发教程却是要花钱(US$39.95)购买的,相关URL为 http://www.object-refinery.com/jfreechart/guide.html。
|
|
--------------------------------------------------------------------------------------------
类名
|
类的作用以及简单描述
|
JFreeChart
|
图表对象,任何类型的图表的最终表现形式都是在该对象进行一些属性的定制。JFreeChart引擎本身提供了一个工厂类用于创建不同类型的图表对象
|
XXXXXDataset
|
数据集对象,用于提供显示图表所用的数据。根据不同类型的图表对应着很多类型的数据集对象类。常用的数据集对象有:柱状图数据集对象DefaultCategoryDataset、饼图数据集对象DefauldPieDataset和曲线图数据集对象DefaultTableXYDataset
|
XXXXXPlot
|
图表区域对象,基本上这个对象决定着什么样式的图表,创建该对象的时候需要Axis、Renderer以及数据集对象的支持。常用的Plot对象有:柱状图CategoryPlot、饼图PiePlot和曲线图XYPlot。
|
XXXXXAxis
|
用于处理图表的两个轴:纵轴和横轴。常用的有NumberAxis数据轴,DateAxis日期轴。
|
XXXXXRenderer
|
负责如何显示一个图表对象。常用的Renderer有DefaultCategoryItemRenderer 柱状图显示器StandardXYItemRenderer曲线图显示器。饼图的Renderer
|
XXXXXURLGenerator
|
用于生成Web图表中每个项目的鼠标点击链接。常用的URLCenerator有StandardCategoryURLGenerator StandardPieURLGenerator StandardXYURLGenerator
|
XXXXXToolTipGenerator
|
用于生成图象的帮助提示,不同类型图表对应不同类型的工具提示类。常用的有:StandardXYToolTipGenerator StandardCategoryToolTipGenerator StandardPieItemLabelGenerator
|
一般的创建图表的过程为:
1. 从数据库里读取数据
2. 将数据保存到图表对应的数据集对象中
3. 创建坐标轴对象
4. 创建Renderer对象
5. 创建Plot对象
6. 创建JfreeChart对象
7. 生成图片文件(或者二进制流)
以JfreeChart提供的Web应用的例子来说明一下以上几个步骤:
一.柱图。
public static String generateBarChart(Date hitDate, HttpSession session, PrintWriter pw) {
String filename = null;
try {
// Retrieve list of WebHits
WebHitDataSet whDataSet = new WebHitDataSet();
ArrayList list = whDataSet.getDataBySection(hitDate);//获得数据集
// Throw a custom NoDataException if there is no data
if (list.size() == 0) {
System.out.println("No data has been found");
throw new NoDataException();
}
// Create and populate a CategoryDataset
Iterator iter = list.listIterator();
DefaultCategoryDataset dataset = new DefaultCategoryDataset();
while (iter.hasNext()) {
WebHit wh = (WebHit)iter.next();
dataset.addValue(new Long(wh.getHitCount()), "Hits", wh.getSection());
}//将数据集保存到
Dataset对象
// Create the chart object
CategoryAxis categoryAxis = new CategoryAxis("");
ValueAxis valueAxis = new NumberAxis("");//创建坐标轴
BarRenderer renderer = new BarRenderer();//创建Renderer
renderer.setItemURLGenerator(new StandardCategoryURLGenerator("xy_chart.jsp","series","section"));//创建URLGenerator.再不需要连接的情况下将第一个参数设置成"###"就可以了
renderer.setToolTipGenerator(new StandardCategoryToolTipGenerator());//创建提示标签
Plot plot = new CategoryPlot(dataset, categoryAxis, valueAxis, renderer);//创建图表区域对象
JFreeChart chart = new JFreeChart("", JFreeChart.DEFAULT_TITLE_FONT, plot, false);
chart.setBackgroundPaint(java.awt.Color.white);//创建图表
// Write the chart image to the temporary directory
ChartRenderingInfo info = new ChartRenderingInfo(new StandardEntityCollection());//该工具类上面没有介绍,在鼠标移动到图片时显示提示信息是用Map实现的,这些Map是用该类生成的。
filename = ServletUtilities.saveChartAsPNG(chart, 500, 300, info, session);//保存图表为文件
// Write the image map to the PrintWriter
ChartUtilities.writeImageMap(pw, filename, info);//生成
Map信息。这些信息写在
pw的输出流中,这里的输出流就是
Response.out。也就是直接输出到页面了
pw.flush();
} catch (NoDataException e) {
System.out.println(e.toString());
filename = "public_nodata_500x300.png";
} catch (Exception e) {
System.out.println("Exception - " + e.toString());
e.printStackTrace(System.out);
filename = "public_error_500x300.png";
}
return filename;
}
二.图表显示:
<%
......
String filename = WebHitChart.generateBarChart(dDate, session, new PrintWriter(out));//获得图片的文件名。注意该语句写在jsp文件(bar_chart.jsp)的开头,而用out的封装类作为参数,访问该页面后可以看到生成Map热点的内容输出再该页面的开头部分。也就是说最好是把这几句话写在页面有输出之前。
String graphURL = request.getContextPath() + "/servlet/DisplayChart?filename=" + filename;//显示图片的Servlet全路径名,另外文件名作为该Servlet的参数
......
%>
......
<img src="<%= graphURL %>" width=500 height=300 border=0 usemap="#<%= filename %>">
src指定了servlet的全路径名,usermap指定了使用的热点。
该Servlet必须在WEB-INF/web.xml文件中配置:
<servlet>
<servlet-name>DisplayChart</servlet-name>
<servlet-class>org.jfree.chart.servlet.DisplayChart</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>DisplayChart</servlet-name>
<url-pattern>/servlet/DisplayChart</url-pattern>
</servlet-mapping>
该Servlet的作用就是将图片文件的二进制流数据写道response的输出流中,在客户端也就显示成了图片。
---------------------------------------------------------------------------------------------
封装FreeChart的Bean
FreeChart.java
package com.function;
/*基于JFreeChart 1.0.0 Pre2.jar*/
import java.io.*;
import java.awt.Font;
import java.awt.Color;
import java.text.SimpleDateFormat;
import java.text.DecimalFormat;
import java.util.Calendar;
import javax.servlet.http.HttpSession;
import org.jfree.data.*;
import org.jfree.chart.*;
//数据集
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.IntervalCategoryDataset;
import org.jfree.data.general.DefaultPieDataset;
import org.jfree.data.general.PieDataset;
import org.jfree.data.xy.TableXYDataset;
import org.jfree.data.xy.XYZDataset;
import org.jfree.data.xy.IntervalXYDataset;
import org.jfree.data.xy.WindDataset;
import org.jfree.data.general.WaferMapDataset;
import org.jfree.data.xy.XYDataset;
import org.jfree.data.xy.OHLCDataset;
import org.jfree.data.xy.SignalsDataset;
import org.jfree.data.statistics.BoxAndWhiskerXYDataset;
//数据集辅助
import org.jfree.data.time.Day;
import org.jfree.data.time.Hour;
import org.jfree.data.time.TimeSeries;
import org.jfree.data.time.TimeSeriesCollection;
//图集
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PiePlot;
import org.jfree.chart.plot.MultiplePiePlot;
import org.jfree.chart.plot.PiePlot3D;
import org.jfree.chart.plot.XYPlot;
//表现形式集
import org.jfree.chart.renderer.category.BarRenderer;
import org.jfree.chart.renderer.category.BarRenderer3D;
import org.jfree.chart.renderer.category.StackedBarRenderer;
import org.jfree.chart.renderer.category.StackedBarRenderer3D;
import org.jfree.chart.renderer.category.StackedAreaRenderer;
import org.jfree.chart.renderer.category.LineAndShapeRenderer;
import org.jfree.chart.renderer.category.LineRenderer3D;
import org.jfree.chart.renderer.category.WaterfallBarRenderer;
import org.jfree.chart.renderer.xy.XYItemRenderer;
//标签
import org.jfree.chart.labels.StandardPieItemLabelGenerator;
//刻度
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.axis.DateAxis;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.axis.SegmentedTimeline;
import org.jfree.chart.axis.TickUnit;
import org.jfree.chart.axis.TickUnits;
import org.jfree.chart.axis.TickUnitSource;
//通用
import org.jfree.data.general.DatasetUtilities;
//常量
import org.jfree.chart.axis.AxisLocation;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.util.TableOrder;
public class FreeChart
{
/*定义各类图表所需的Dataset*/
private DefaultCategoryDataset vDefaultCategoryDataset=null;
private CategoryDataset vCategoryDataset=null;
private DefaultPieDataset vDefaultPieDataset=null;
private PieDataset vPieDataset=null;
private XYDataset vXYDataset=null;
private TableXYDataset vTableXYDataset=null;
private XYZDataset vXYZDataset=null;
private IntervalXYDataset vIntervalXYDataset=null;
private WindDataset vWindDataset=null;
private WaferMapDataset vWaferMapDataset=null;
private IntervalCategoryDataset vIntervalCategoryDataset=null;
private OHLCDataset vOHLCDataset=null;
private SignalsDataset vSignalsDataset=null;
private BoxAndWhiskerXYDataset vBoxAndWhiskerXYDataset=null;
/*定义各类图表所需的Dataset辅助*/
TimeSeries vTimeSeries=null;
TimeSeriesCollection vTimeSeriesCollection=null;
/*定义图表*/
private JFreeChart vFreeChart=null;
private JFreeChart vFreeChartExtend=null;
/*映射图片的路径名称*/
private String strFileName=null;
/*定义效果*/
private CategoryPlot vCategoryPlot=null;
private PiePlot vPiePlot=null;
private MultiplePiePlot vMultiplePiePlot=null;
private PiePlot3D vPiePlot3D=null;
private XYPlot vXYPlot=null;
private StandardPieItemLabelGenerator vStandardPieItemLabelGenerator=null;
/*定义刻度*/
private NumberAxis vNumberAxis=null;
private DateAxis vDateAxis=null;
private CategoryAxis vCategoryAxis=null;
private ValueAxis vValueAxis=null;
private SegmentedTimeline vSegmentedTimeline=null;
private BarRenderer vBarRenderer=null;
private BarRenderer3D vBarRenderer3D=null;
private StackedBarRenderer vStackedBarRenderer=null;
private StackedBarRenderer3D vStackedBarRenderer3D=null;
private StackedAreaRenderer vStackedAreaRenderer=null;
private LineAndShapeRenderer vLineAndShapeRenderer=null;
private LineRenderer3D vLineRenderer3D=null;
private WaterfallBarRenderer vWaterfallBarRenderer=null;
private XYItemRenderer vXYItemRenderer=null;
//是否启用效果模式
private boolean bRender=false;
/*定义静态数据*/
private String[] strItemArray=null;
private String[] strMultipleItemArray=null;
private String[] strCategoryArray=null;
private String[] strDataArray=null;
private String[] strMultipleDataArray=null;
private double[][] iMultipleDataArray=null;
/**/
public static String[] vChartTypeArray={"饼图","分离型饼图","三维饼图","复合饼图","三维分离型饼图","簇状柱形图","三维簇状柱形图","堆积柱形图","三维堆积柱形图","面积图","三维面积图","折线图","三维折线图","三维曲面图","三维柱形图","雷达图","散点图","百分比堆积面积图","三维百分比堆积面积图","折线散点图","面积散点图","时间序列图","三维柱形圆锥图","盘高-盘低-收盘图","开盘-盘高-盘低-收盘图","曲面图(俯视框架图)","气泡图","簇状条形图","数据点折线图","无数据点折线散点图","无数据点平滑线散点图"};
public String strTimeAxis="";
public void FreeChart()
{
}
public void createDataset(String strMultipleItemList,String strCategoryList,String strMultipleDataList)
{
strMultipleItemArray=strMultipleItemList.split(",");
strCategoryArray=strCategoryList.split(",");
strMultipleDataArray=strMultipleDataList.split("#");
iMultipleDataArray=new double[strCategoryArray.length][strMultipleItemArray.length];
java.text.SimpleDateFormat vSimpleDateFormat=new SimpleDateFormat("yyyy-MM-dd");
java.text.SimpleDateFormat vSimpleDateFormatToday=new SimpleDateFormat("yyyy-MM-");
java.util.Date vItemDate=null;
java.util.Date vTodayDate=null;
Day vDay=null;
vTodayDate=new java.util.Date();
vTimeSeriesCollection=new TimeSeriesCollection();
vTimeSeriesCollection.setDomainIsPointsInTime(false);
for(int mIndex=0;mIndex<strMultipleDataArray.length;mIndex++)
{
String[] strElementArray=strMultipleDataArray[mIndex].split(",");
if(strTimeAxis.equals("Hour"))
{
vTimeSeries=new TimeSeries(strCategoryArray[mIndex],Hour.class);
}
else
{
vTimeSeries=new TimeSeries(strCategoryArray[mIndex],Day.class);
}
//vTimeSeries.clear();
for(int nIndex=0;nIndex<strElementArray.length;nIndex++)
{
iMultipleDataArray[mIndex][nIndex]=Double.parseDouble(strElementArray[nIndex]);
try
{
//vItemDate=vSimpleDateFormat.parse(vSimpleDateFormatToday.format(vTodayDate)+String.valueOf(nIndex+1));
vItemDate=vSimpleDateFormat.parse(strMultipleItemArray[nIndex]);
if(strTimeAxis.equals("Hour"))
{
vDay=new Day(vItemDate.getDate(),1+vItemDate.getMonth(),1900+vItemDate.getYear());
vTimeSeries.add(new Hour(vItemDate.getHours(),vDay),Double.parseDouble(strElementArray[nIndex]));
}
else
{
vTimeSeries.add(new Day(vItemDate.getDate(),1+vItemDate.getMonth(),1900+vItemDate.getYear()),Double.parseDouble(strElementArray[nIndex]));
}
}
catch(Exception e)
{System.out.println(e.getMessage());}
}
vTimeSeriesCollection.addSeries(vTimeSeries);
}
try
{
vCategoryDataset=DatasetUtilities.createCategoryDataset(strCategoryArray,strMultipleItemArray,iMultipleDataArray);
vPieDataset=DatasetUtilities.createPieDatasetForColumn(vCategoryDataset,0);
vPieDataset=DatasetUtilities.createPieDatasetForRow(vCategoryDataset,0);
//vWaferMapDataset=(WaferMapDataset)vCategoryDataset;
vTableXYDataset=(TableXYDataset)vTimeSeriesCollection;
vIntervalXYDataset=(IntervalXYDataset)vTimeSeriesCollection;
vXYDataset=(XYDataset)vTimeSeriesCollection;
/*
vXYZDataset=(XYZDataset)vTimeSeriesCollection;
//vWaferMapDataset=(WaferMapDataset)vTimeSeriesCollection;
vWindDataset=(WindDataset)vTimeSeriesCollection;
vOHLCDataset=(OHLCDataset)vTimeSeriesCollection;
vSignalsDataset=(SignalsDataset)vTimeSeriesCollection;
vBoxAndWhiskerXYDataset=(BoxAndWhiskerXYDataset)vTimeSeriesCollection;
*/
}
catch(Exception e)
{}
}
public void createDataset(String strItemList,String strDataList)
{
vDefaultCategoryDataset=new DefaultCategoryDataset();
vDefaultPieDataset=new DefaultPieDataset();
strItemArray=strItemList.split(",");
strDataArray=strDataList.split(",");
for(int kIndex=0;kIndex<strItemArray.length;kIndex++)
{
vDefaultCategoryDataset.addValue(Double.parseDouble(strDataArray[kIndex])," ",strItemArray[kIndex]);
vDefaultPieDataset.setValue(strItemArray[kIndex],Double.parseDouble(strDataArray[kIndex]));
}
}
public DefaultCategoryDataset getDefaultCategoryDataset()
{
return this.vDefaultCategoryDataset;
}
public CategoryDataset getCategoryDataset()
{
return this.vCategoryDataset;
}
public DefaultPieDataset getDefaultPieDataset()
{
return this.vDefaultPieDataset;
}
public PieDataset getPieDataset()
{
return this.vPieDataset;
}
public XYDataset getXYDataset()
{
return this.vXYDataset;
}
public TableXYDataset getTableXYDataset()
{
return this.vTableXYDataset;
}
public XYZDataset getXYZDataset()
{
return this.vXYZDataset;
}
public IntervalXYDataset getIntervalXYDataset()
{
return this.vIntervalXYDataset;
}
public WindDataset getWindDataset()
{
return this.vWindDataset;
}
public WaferMapDataset getWaferMapDataset()
{
return this.vWaferMapDataset;
}
public IntervalCategoryDataset getIntervalCategoryDataset()
{
return this.vIntervalCategoryDataset;
}
public OHLCDataset getOHLCDataset()
{
return this.vOHLCDataset;
}
public SignalsDataset getSignalsDataset()
{
return this.vSignalsDataset;
}
public BoxAndWhiskerXYDataset getBoxAndWhiskerXYDataset()
{
return this.vBoxAndWhiskerXYDataset;
}
/*
iChartType:图表类型
*/
public void createChart(int iChartType,String strFreeChartInfo,String strFreeChartXInfo,String strFreeChartYInfo)
{
switch(iChartType)
{
case 1:
vFreeChart=ChartFactory.createPieChart(strFreeChartInfo,this.getPieDataset(),true,false,false);
try
{
vPiePlot=(PiePlot)vFreeChart.getPlot();
if(vPiePlot!=null)
{
if(bRender)
{
if(strItemArray.length>0)
{
for(int iIndex=0;iIndex<strItemArray.length;iIndex++)
{
//指定Section色彩
vPiePlot.setSectionPaint(iIndex,new Color(0,0+iIndex*(255/strItemArray.length),255));
//指定Section轮廓线颜色
vPiePlot.setSectionOutlinePaint(0,Color.BLACK);
}
}
}
//指定Section标签格式
vStandardPieItemLabelGenerator=new StandardPieItemLabelGenerator("{1}");
vPiePlot.setLabelGenerator(vStandardPieItemLabelGenerator);
//
vPiePlot.setForegroundAlpha(0.5f);
}
}
catch(Exception e)
{}
break;
case 2:
vFreeChart=ChartFactory.createPieChart(strFreeChartInfo,this.getPieDataset(),this.getPieDataset(),0,false,true,false,false,false,false);
try
{
vPiePlot=(PiePlot)vFreeChart.getPlot();
if(vPiePlot!=null)
{
if(bRender)
{
if(strItemArray.length>0)
{
for(int iIndex=0;iIndex<strItemArray.length;iIndex++)
{
//指定Section色彩
vPiePlot.setSectionPaint(iIndex,new Color(0,0+iIndex*(255/strItemArray.length),255));
//指定Section轮廓线颜色
vPiePlot.setSectionOutlinePaint(0,Color.BLACK);
}
//抽取指定块
vPiePlot.setExplodePercent(0,1.00);
}
}
//指定Section标签格式
vStandardPieItemLabelGenerator=new StandardPieItemLabelGenerator("{1}");
vPiePlot.setLabelGenerator(vStandardPieItemLabelGenerator);
vPiePlot.setForegroundAlpha(0.5f);
}
}
catch(Exception e)
{}
break;
case 3:
vFreeChart=ChartFactory.createMultiplePieChart(strFreeChartInfo,this.getCategoryDataset(),TableOrder.BY_ROW,true,false,false);
try
{
vMultiplePiePlot=(MultiplePiePlot)vFreeChart.getPlot();
if(vMultiplePiePlot!=null)
{
vFreeChartExtend=vMultiplePiePlot.getPieChart();
vPiePlot=(PiePlot)vFreeChartExtend.getPlot();
if(bRender)
{
if(strItemArray.length>0)
{
for(int iIndex=0;iIndex<strItemArray.length;iIndex++)
{
//指定Section色彩
vPiePlot.setSectionPaint(iIndex,new Color(0,0+iIndex*(255/strItemArray.length),255));
//指定Section轮廓线颜色
vPiePlot.setSectionOutlinePaint(0,Color.BLACK);
}
}
}
//指定Section标签格式
vStandardPieItemLabelGenerator=new StandardPieItemLabelGenerator("{1}");
vPiePlot.setLabelGenerator(vStandardPieItemLabelGenerator);
vPiePlot.setForegroundAlpha(0.5f);
}
}
catch(Exception e)
{}
break;
case 4:
vFreeChart=ChartFactory.createPieChart3D(strFreeChartInfo,this.getPieDataset(),true,false,false);
try
{
vPiePlot3D=(PiePlot3D)vFreeChart.getPlot();
if(vPiePlot!=null)
{
if(bRender)
{
if(strItemArray.length>0)
{
for(int iIndex=0;iIndex<strItemArray.length;iIndex++)
{
//指定Section色彩
vPiePlot3D.setSectionPaint(iIndex,new Color(0,0+iIndex*(255/strItemArray.length),255));
//指定Section轮廓线颜色
vPiePlot3D.setSectionOutlinePaint(0,Color.BLACK);
}
}
}
//指定Section标签格式
vStandardPieItemLabelGenerator=new StandardPieItemLabelGenerator("{1}");
vPiePlot3D.setLabelGenerator(vStandardPieItemLabelGenerator);
//
vPiePlot3D.setForegroundAlpha(0.8f);
}
}
catch(Exception e)
{}
break;
case 5:
vFreeChart=ChartFactory.createMultiplePieChart3D(strFreeChartInfo,this.getCategoryDataset(),TableOrder.BY_ROW,true,false,false);
try
{
vMultiplePiePlot=(MultiplePiePlot)vFreeChart.getPlot();
if(vMultiplePiePlot!=null)
{
vFreeChartExtend=vMultiplePiePlot.getPieChart();
vPiePlot3D=(PiePlot3D)vFreeChartExtend.getPlot();
if(bRender)
{
if(strItemArray.length>0)
{
for(int iIndex=0;iIndex<strItemArray.length;iIndex++)
{
//指定Section色彩
vPiePlot3D.setSectionPaint(iIndex,new Color(0,0+iIndex*(255/strCategoryArray.length),255));
//指定Section轮廓线颜色
vPiePlot3D.setSectionOutlinePaint(0,Color.BLACK);
}
}
}
//指定Section标签格式
vStandardPieItemLabelGenerator=new StandardPieItemLabelGenerator("{1}");
vPiePlot3D.setLabelGenerator(vStandardPieItemLabelGenerator);
//
vPiePlot3D.setForegroundAlpha(0.8f);
}
}
catch(Exception e)
{}
break;
case 6:
vFreeChart=ChartFactory.createBarChart(strFreeChartInfo,strFreeChartXInfo,strFreeChartYInfo,this.getCategoryDataset(),PlotOrientation.VERTICAL,true,false,false);
try
{
vCategoryPlot=vFreeChart.getCategoryPlot();
if(vCategoryPlot!=null)
{
if(bRender)
{
vBarRenderer=new BarRenderer();
vBarRenderer.setBaseOutlinePaint(Color.GRAY);
//设置X轴代表的柱的颜色
if(strCategoryArray.length>0)
{
for(int iIndex=0;iIndex<strCategoryArray.length;iIndex++)
{
vBarRenderer.setSeriesPaint(iIndex,new Color(0,0+iIndex*(255/strCategoryArray.length),255));
vBarRenderer.setSeriesOutlinePaint(0,Color.BLACK);
}
}
vBarRenderer.setItemMargin(0.1);
vCategoryPlot.setRenderer(vBarRenderer);
//设置X轴、Y轴的显示位置
vCategoryPlot.setDomainAxisLocation(AxisLocation.TOP_OR_RIGHT);
vCategoryPlot.setRangeAxisLocation(AxisLocation.BOTTOM_OR_RIGHT);
}
//设置柱的透明度
vCategoryPlot.setForegroundAlpha(0.5f);
}
}
catch(Exception e)
{}
break;
case 7:
vFreeChart=ChartFactory.createStackedBarChart(strFreeChartInfo,strFreeChartXInfo,strFreeChartYInfo,this.getCategoryDataset(),PlotOrientation.VERTICAL,true,false,false);
try
{
vCategoryPlot=vFreeChart.getCategoryPlot();
if(vCategoryPlot!=null)
{
if(bRender)
{
vStackedBarRenderer=new StackedBarRenderer();
vStackedBarRenderer.setBaseOutlinePaint(Color.GRAY);
//设置X轴代表的柱的颜色
if(strCategoryArray.length>0)
{
for(int iIndex=0;iIndex<strCategoryArray.length;iIndex++)
{
vStackedBarRenderer.setSeriesPaint(iIndex,new Color(0,0+iIndex*(255/strCategoryArray.length),255));
vStackedBarRenderer.setSeriesOutlinePaint(0,Color.BLACK);
}
}
vCategoryPlot.setRenderer(vStackedBarRenderer);
//设置X轴、Y轴的显示位置
vCategoryPlot.setDomainAxisLocation(AxisLocation.TOP_OR_RIGHT);
vCategoryPlot.setRangeAxisLocation(AxisLocation.BOTTOM_OR_RIGHT);
}
//设置柱的透明度
vCategoryPlot.setForegroundAlpha(0.5f);
}
}
catch(Exception e)
{}
break;
case 8:
vFreeChart=ChartFactory.createBarChart3D(strFreeChartInfo,strFreeChartXInfo,strFreeChartYInfo,this.getCategoryDataset(),PlotOrientation.VERTICAL,true,false,false);
try
{
vCategoryPlot=vFreeChart.getCategoryPlot();
if(vCategoryPlot!=null)
{
if(bRender)
{
vBarRenderer3D=new BarRenderer3D();
vBarRenderer3D.setBaseOutlinePaint(Color.GRAY);
//设置X轴代表的柱的颜色
if(strCategoryArray.length>0)
{
for(int iIndex=0;iIndex<strCategoryArray.length;iIndex++)
{
vBarRenderer3D.setSeriesPaint(iIndex,new Color(0,0+iIndex*(255/strCategoryArray.length),255));
vBarRenderer3D.setSeriesOutlinePaint(0,Color.BLACK);
}
}
vBarRenderer3D.setItemMargin(0.1);
vCategoryPlot.setRenderer(vBarRenderer3D);
//设置X轴、Y轴的显示位置
vCategoryPlot.setDomainAxisLocation(AxisLocation.TOP_OR_RIGHT);
vCategoryPlot.setRangeAxisLocation(AxisLocation.BOTTOM_OR_RIGHT);
}
//设置柱的透明度
vCategoryPlot.setForegroundAlpha(0.8f);
}
}
catch(Exception e)
{}
break;
case 9:
vFreeChart=ChartFactory.createStackedBarChart3D(strFreeChartInfo,strFreeChartXInfo,strFreeChartYInfo,this.getCategoryDataset(),PlotOrientation.VERTICAL,true,false,false);
try
{
vCategoryPlot=vFreeChart.getCategoryPlot();
if(vCategoryPlot!=null)
{
if(bRender)
{
vStackedBarRenderer3D=new StackedBarRenderer3D();
vStackedBarRenderer3D.setBaseOutlinePaint(Color.GRAY);
//设置X轴代表的柱的颜色
if(strCategoryArray.length>0)
{
for(int iIndex=0;iIndex<strCategoryArray.length;iIndex++)
{
vStackedBarRenderer3D.setSeriesPaint(iIndex,new Color(0,0+iIndex*(255/strCategoryArray.length),255));
vStackedBarRenderer3D.setSeriesOutlinePaint(0,Color.BLACK);
}
}
vCategoryPlot.setRenderer(vStackedBarRenderer3D);
//设置X轴、Y轴的显示位置
vCategoryPlot.setDomainAxisLocation(AxisLocation.TOP_OR_RIGHT);
vCategoryPlot.setRangeAxisLocation(AxisLocation.BOTTOM_OR_RIGHT);
}
//设置柱的透明度
vCategoryPlot.setForegroundAlpha(0.8f);
}
}
catch(Exception e)
{}
break;
case 10:
vFreeChart=ChartFactory.createAreaChart(strFreeChartInfo,strFreeChartXInfo,strFreeChartYInfo,this.getDefaultCategoryDataset(),PlotOrientation.VERTICAL,true,false,false);
try
{
vCategoryPlot=vFreeChart.getCategoryPlot();
if(vCategoryPlot!=null)
{
if(bRender)
{
vStackedAreaRenderer=new StackedAreaRenderer();
vStackedAreaRenderer.setBaseOutlinePaint(Color.GRAY);
//设置区域颜色
if(strItemArray.length>0)
{
for(int iIndex=0;iIndex<strItemArray.length;iIndex++)
{
vStackedAreaRenderer.setSeriesPaint(iIndex,new Color(0,0+iIndex*(255/strItemArray.length),255));
vStackedAreaRenderer.setSeriesOutlinePaint(0,Color.BLACK);
}
}
vCategoryPlot.setRenderer(vStackedAreaRenderer);
//设置X轴、Y轴的显示位置
vCategoryPlot.setDomainAxisLocation(AxisLocation.TOP_OR_RIGHT);
vCategoryPlot.setRangeAxisLocation(AxisLocation.BOTTOM_OR_RIGHT);
}
//设置区域透明度
vCategoryPlot.setForegroundAlpha(0.5f);
//背景表格线
vCategoryPlot.setDomainGridlinesVisible(true);
}
}
catch(Exception e)
{}
break;
case 11:
vFreeChart=ChartFactory.createStackedAreaChart(strFreeChartInfo,strFreeChartXInfo,strFreeChartYInfo,this.getCategoryDataset(),PlotOrientation.VERTICAL,true,false,false);
try
{
vCategoryPlot=vFreeChart.getCategoryPlot();
if(vCategoryPlot!=null)
{
if(bRender)
{
vStackedAreaRenderer=new StackedAreaRenderer();
vStackedAreaRenderer.setBaseOutlinePaint(Color.GRAY);
//设置区域颜色
if(strCategoryArray.length>0)
{
for(int iIndex=0;iIndex<strCategoryArray.length;iIndex++)
{
vStackedAreaRenderer.setSeriesPaint(iIndex,new Color(0,0+iIndex*(255/strCategoryArray.length),255));
vStackedAreaRenderer.setSeriesOutlinePaint(0,Color.BLACK);
}
}
vCategoryPlot.setRenderer(vStackedAreaRenderer);
//设置X轴、Y轴的显示位置
vCategoryPlot.setDomainAxisLocation(AxisLocation.TOP_OR_RIGHT);
vCategoryPlot.setRangeAxisLocation(AxisLocation.BOTTOM_OR_RIGHT);
}
//设置区域透明度
vCategoryPlot.setForegroundAlpha(0.5f);
}
}
catch(Exception e)
{}
break;
case 12:
vFreeChart=ChartFactory.createLineChart(strFreeChartInfo,strFreeChartXInfo,strFreeChartYInfo,this.getCategoryDataset(),PlotOrientation.VERTICAL,true,false,false);
try
{
vCategoryPlot=vFreeChart.getCategoryPlot();
if(vCategoryPlot!=null)
{
vLineAndShapeRenderer=new LineAndShapeRenderer();
vLineAndShapeRenderer.setBaseOutlinePaint(Color.GRAY);
if(bRender)
{
//设置线条颜色
if(strCategoryArray.length>0)
{
for(int iIndex=0;iIndex<strCategoryArray.length;iIndex++)
{
vLineAndShapeRenderer.setSeriesPaint(iIndex,new Color(0,0+iIndex*(255/strCategoryArray.length),255));
vLineAndShapeRenderer.setSeriesOutlinePaint(0,Color.BLACK);
}
}
//设置X轴、Y轴的显示位置
vCategoryPlot.setDomainAxisLocation(AxisLocation.TOP_OR_RIGHT);
vCategoryPlot.setRangeAxisLocation(AxisLocation.BOTTOM_OR_RIGHT);
}
vCategoryPlot.setRenderer(vLineAndShapeRenderer);
//背景表格线
vCategoryPlot.setDomainGridlinesVisible(true);
}
}
catch(Exception e)
{}
break;
case 13:
vFreeChart=ChartFactory.createLineChart3D(strFreeChartInfo,strFreeChartXInfo,strFreeChartYInfo,this.getCategoryDataset(),PlotOrientation.VERTICAL,true,false,false);
try
{
vCategoryPlot=vFreeChart.getCategoryPlot();
if(vCategoryPlot!=null)
{
vLineRenderer3D=new LineRenderer3D();
vLineRenderer3D.setBaseOutlinePaint(Color.GRAY);
if(bRender)
{
//设置线面颜色
if(strCategoryArray.length>0)
{
for(int iIndex=0;iIndex<strCategoryArray.length;iIndex++)
{
vLineRenderer3D.setSeriesPaint(iIndex,new Color(0,0+iIndex*(255/strCategoryArray.length),255));
vLineRenderer3D.setSeriesFillPaint(iIndex,new Color(0,0+iIndex*(255/strCategoryArray.length),255));
vLineRenderer3D.setSeriesOutlinePaint(0,Color.BLACK);
}
}
//设置X轴、Y轴的显示位置
vCategoryPlot.setDomainAxisLocation(AxisLocation.TOP_OR_RIGHT);
vCategoryPlot.setRangeAxisLocation(AxisLocation.BOTTOM_OR_RIGHT);
}
vCategoryPlot.setRenderer(vLineRenderer3D);
//背景表格线
vCategoryPlot.setDomainGridlinesVisible(true);
}
}
catch(Exception e)
{}
break;
case 14:
vFreeChart=ChartFactory.createGanttChart(strFreeChartInfo,strFreeChartXInfo,strFreeChartYInfo,this.getIntervalCategoryDataset(),true,false,false);
break;
case 15:
vFreeChart=ChartFactory.createWaterfallChart(strFreeChartInfo,strFreeChartXInfo,strFreeChartYInfo,this.getCategoryDataset(),PlotOrientation.VERTICAL,true,false,false);
try
{
vCategoryPlot=vFreeChart.getCategoryPlot();
if(vCategoryPlot!=null)
{
vWaterfallBarRenderer=new WaterfallBarRenderer();
vWaterfallBarRenderer.setBaseOutlinePaint(Color.GRAY);
if(bRender)
{
//设置柱颜色
if(strCategoryArray.length>0)
{
for(int iIndex=0;iIndex<strCategoryArray.length;iIndex++)
{
vWaterfallBarRenderer.setSeriesPaint(iIndex,new Color(0,0+iIndex*(255/strCategoryArray.length),255));
vWaterfallBarRenderer.setSeriesFillPaint(iIndex,new Color(0,0+iIndex*(255/strCategoryArray.length),255));
vWaterfallBarRenderer.setSeriesOutlinePaint(0,Color.BLACK);
}
}
//设置X轴、Y轴的显示位置
vCategoryPlot.setDomainAxisLocation(AxisLocation.TOP_OR_RIGHT);
vCategoryPlot.setRangeAxisLocation(AxisLocation.BOTTOM_OR_RIGHT);
}
vCategoryPlot.setRenderer(vWaterfallBarRenderer);
//背景表格线
vCategoryPlot.setDomainGridlinesVisible(true);
//设置区域透明度
vCategoryPlot.setForegroundAlpha(0.5f);
}
}
catch(Exception e)
{}
break;
case 16:
vFreeChart=ChartFactory.createPolarChart(strFreeChartInfo,this.getXYDataset(),true,false,false);
break;
case 17:
vFreeChart=ChartFactory.createScatterPlot(strFreeChartInfo,strFreeChartXInfo,strFreeChartYInfo,this.getXYDataset(),PlotOrientation.VERTICAL,true,false,false);
break;
case 18:
vFreeChart=ChartFactory.createXYBarChart(strFreeChartInfo,strFreeChartXInfo,false,strFreeChartYInfo,this.getIntervalXYDataset(),PlotOrientation.VERTICAL,true,false,false);
break;
case 19:
vFreeChart=ChartFactory.createXYAreaChart(strFreeChartInfo,strFreeChartXInfo,strFreeChartYInfo,this.getXYDataset(),PlotOrientation.VERTICAL,true,false,false);
break;
case 20:
vFreeChart=ChartFactory.createStackedXYAreaChart(strFreeChartInfo,strFreeChartXInfo,strFreeChartYInfo,this.getTableXYDataset(),PlotOrientation.VERTICAL,true,false,false);
break;
case 21:
vFreeChart=ChartFactory.createXYLineChart(strFreeChartInfo,strFreeChartXInfo,strFreeChartYInfo,this.getXYDataset(),PlotOrientation.VERTICAL,true,false,false);
try
{
vXYPlot=(XYPlot)vFreeChart.getXYPlot();
if(vXYPlot!=null)
{
vXYItemRenderer=vXYPlot.getRenderer();
vXYItemRenderer.setBaseOutlinePaint(Color.GRAY);
if(bRender)
{
//设置线面颜色
if(strItemArray.length>0)
{
for(int iIndex=0;iIndex<strItemArray.length;iIndex++)
{
vXYItemRenderer.setSeriesPaint(iIndex,new Color(0,0+iIndex*(255/strItemArray.length),255));
vXYItemRenderer.setSeriesOutlinePaint(0,Color.BLACK);
}
}
//设置X轴、Y轴的显示位置
vXYPlot.setDomainAxisLocation(AxisLocation.TOP_OR_RIGHT);
vXYPlot.setRangeAxisLocation(AxisLocation.BOTTOM_OR_RIGHT);
}
//设置线条颜色
vXYItemRenderer.setPaint(new Color(0,100,255));
vXYPlot.setRenderer(vXYItemRenderer);
//背景表格线
vXYPlot.setDomainGridlinesVisible(true);
}
}
catch(Exception e)
{}
break;
case 22:
vFreeChart=ChartFactory.createXYStepChart(strFreeChartInfo,strFreeChartXInfo,strFreeChartYInfo,this.getXYDataset(),PlotOrientation.VERTICAL,true,false,false);
break;
case 23:
vFreeChart=ChartFactory.createXYStepAreaChart(strFreeChartInfo,strFreeChartXInfo,strFreeChartYInfo,this.getXYDataset(),PlotOrientation.VERTICAL,true,false,false);
break;
case 24:
vFreeChart=ChartFactory.createTimeSeriesChart(strFreeChartInfo,strFreeChartXInfo,strFreeChartYInfo,this.getXYDataset(),true,false,false);
try
{
vXYPlot=(XYPlot)vFreeChart.getXYPlot();
if(vXYPlot!=null)
{
vXYItemRenderer=vXYPlot.getRenderer();
vXYItemRenderer.setBaseOutlinePaint(Color.GRAY);
if(bRender)
{
//设置线面颜色
if(strItemArray.length>0)
{
for(int iIndex=0;iIndex<strItemArray.length;iIndex++)
{
vXYItemRenderer.setSeriesPaint(iIndex,new Color(0,0+iIndex*(255/strItemArray.length),255));
vXYItemRenderer.setSeriesOutlinePaint(0,Color.BLACK);
}
}
//设置X轴、Y轴的显示位置
vXYPlot.setDomainAxisLocation(AxisLocation.TOP_OR_RIGHT);
vXYPlot.setRangeAxisLocation(AxisLocation.BOTTOM_OR_RIGHT);
//设置线条颜色
vXYItemRenderer.setPaint(new Color(0,100,255));
vXYPlot.setRenderer(vXYItemRenderer);
}
//背景表格线
vXYPlot.setDomainGridlinesVisible(true);
SimpleDateFormat vSimpleDateFormat=null;
if(strTimeAxis.equals("Hour"))
{
vSimpleDateFormat=new SimpleDateFormat("dd日HH点");
}
else
{
vSimpleDateFormat=new SimpleDateFormat("MM月dd日");
}
vDateAxis=(DateAxis)vXYPlot.getDomainAxis();
vDateAxis.setDateFormatOverride(vSimpleDateFormat);
}
}
catch(Exception e)
{}
break;
case 25:
vFreeChart=ChartFactory.createCandlestickChart(strFreeChartInfo,strFreeChartXInfo,strFreeChartYInfo,this.getOHLCDataset(),true);
break;
case 26:
vFreeChart=ChartFactory.createHighLowChart(strFreeChartInfo,strFreeChartXInfo,strFreeChartYInfo,this.getOHLCDataset(),true);
break;
case 27:
vFreeChart=ChartFactory.createHighLowChart(strFreeChartInfo,strFreeChartXInfo,strFreeChartYInfo,this.getOHLCDataset(),true);
break;
case 28:
vFreeChart=ChartFactory.createSignalChart(strFreeChartInfo,strFreeChartXInfo,strFreeChartYInfo,this.getSignalsDataset(),true);
break;
case 29:
vFreeChart=ChartFactory.createBubbleChart(strFreeChartInfo,strFreeChartXInfo,strFreeChartYInfo,this.getXYZDataset(),PlotOrientation.VERTICAL,true,false,false);
break;
case 30:
vFreeChart=ChartFactory.createHistogram(strFreeChartInfo,strFreeChartXInfo,strFreeChartYInfo,this.getIntervalXYDataset(),PlotOrientation.VERTICAL,true,false,false);
break;
case 31:
vFreeChart=ChartFactory.createBoxAndWhiskerChart(strFreeChartInfo,strFreeChartXInfo,strFreeChartYInfo,this.getBoxAndWhiskerXYDataset(),true);
break;
case 32:
vFreeChart=ChartFactory.createWindPlot(strFreeChartInfo,strFreeChartXInfo,strFreeChartYInfo,this.getWindDataset(),true,false,false);
break;
case 33:
vFreeChart=ChartFactory.createWaferMapChart(strFreeChartInfo,this.getWaferMapDataset(),PlotOrientation.VERTICAL,true,false,false);
break;
default:
vFreeChart=ChartFactory.createPieChart(strFreeChartInfo,this.getPieDataset(),true,false,false);
break;
}
//图表背景颜色
vFreeChart.setBackgroundPaint(new Color(212,234,243));
}
public JFreeChart getChart()
{
return this.vFreeChart;
}
public void createImageFile(int iWidth,int iHeight,HttpSession session)
{
try
{
java.util.Date vDate=new java.util.Date(System.currentTimeMillis());
java.text.SimpleDateFormat vSimpleDateFormat=new SimpleDateFormat("yyyyMMddHHmmss");
java.util.Date vFileDate=null;
File[] vFileList=(new File(session.getServletContext().getRealPath("/"))).listFiles();
for(int kIndex=0;kIndex<vFileList.length;kIndex++)
{
if(vFileList[kIndex].isFile())
{
if(vFileList[kIndex].getName().indexOf(".jpg")!=-1)
{
vFileDate=vSimpleDateFormat.parse(vFileList[kIndex].getName().substring(0,vFileList[kIndex].getName().length()-4));
//十分钟前的临时文件删除
if(vDate.getTime()-vFileDate.getTime()>60*10*1000)
{
vFileList[kIndex].delete();
}
}
}
}
String strTime=vSimpleDateFormat.format(vDate);
strFileName=session.getServletContext().getRealPath("/")+"http://www.java-asp.net/java/200601/\\"+strTime+".jpg";
ChartUtilities.saveChartAsJPEG(new File(strFileName),100,this.getChart(),iWidth,iHeight);
strFileName="/"+getApplicationName(session.getServletContext().getRealPath("/"))+"/"+strTime+".jpg";
}
catch(Exception e)
{}
}
public String getImageFile()
{
return this.strFileName;
}
public String getApplicationName(String strRealPath)
{
String[] strAppArray=strRealPath.split("\\\\");
return strAppArray[strAppArray.length-1];
}
public boolean getRender()
{
return bRender;
}
public void setRender(boolean bRender)
{
this.bRender=bRender;
}
public String getTimeAxis()
{
return this.strTimeAxis;
}
public void setTimeAxis(String strTimeAxis)
{
this.strTimeAxis=strTimeAxis;
}
}
JSP应用示例
<%@ page contentType="text/html; charset=gb2312"%>
<%@ page import="java.io.*"%>
<jsp:useBean id="FreeChart" scope="session" class="com.function.FreeChart"/>
<head>
<meta http-equiv="Pragma" content="no-cache">
<meta http-equiv="Cache-Control" content="no-cache">
<meta http-equiv="Expires" content="0">
</head>
<link rel="stylesheet" type="text/css" href="/defineOA/css/default.css">
<%
//初始化
//考虑图形中是否直接链接到统计查询表格
String strTimeList="";
String strMultipleTimeList="";
String strCategoryList="";
String strData="";
String strMultipleData="";
strTimeList="2005-05-01 00:00:00,2005-05-02 05:00:00,2005-05-03 10:00:00,2005-05-04 15:00:00,2005-05-05 20:00:00";
strMultipleTimeList="2005-05-01 00:00:00,2005-05-02 05:00:00,2005-05-03 10:00:00,2005-05-04 15:00:00,2005-05-05 20:00:00";
strCategoryList="JSP工作量,Servlet工作量,Bean工作量";
strData="1,2,3,4,5";
strMultipleData="1,2,3,4,5#2,3,4,5,6#3,4,5,6,7";
FreeChart.createDataset(strMultipleTimeList,strCategoryList,strMultipleData);
FreeChart.createDataset(strTimeList,strData);
FreeChart.setRender(true);
FreeChart.setTimeAxis("Hour");
if(request.getParameter("ChartType")==null)
{
FreeChart.createChart(1,"默认统计图","时间轴","统计值轴");
}
else
{
FreeChart.createChart(Integer.parseInt(request.getParameter("ChartType")),"统计图","时间轴","统计值轴");
}
FreeChart.createImageFile(800,400,session);
response.setHeader("Pragma","No-cache");
response.setDateHeader("Expires",0);
response.setHeader("Cache-Control","no-cache");
%>
<%
for(int iIndex=0;iIndex<FreeChart.vChartTypeArray.length;iIndex++)
{
%>
<input type=button class="GeneralButton" value="<%=FreeChart.vChartTypeArray[iIndex]%>" onclick="window.location=´/defineOA/test/chart.jsp?ChartType=<%=iIndex+1%>´;">
<%
}
%>
<br>
<fieldset>
<legend>图表</legend>
<!--class="GeneralImage"-->
<br>
<center>
<table>
<tr>
<td style="border:1 solid #3399CC;"><img src="<%=FreeChart.getImageFile()%>"></td>
</tr>
</table>
</center>
<br>
</fieldset>
也可以将应用继续用Bean封装