当前位置: 代码迷 >> Web前端 >> jfreechart (三)
  详细解决方案

jfreechart (三)

热度:942   发布时间:2012-10-09 10:21:45.0
jfreechart (3)
一、JFreeChart的介绍与获取

JFreeChart开发包是一个开源的Java图形开发包,它从柱形图,饼形图,到雷达图,蜡烛图等等无所不包,可以在c/s,b/s,甚至是实时环境都能一显身手。

当前的JFreeChart的最新版本为jfreechart-1.0.0-rc1.zip。可以到http://www.jfree.org/jfreechart/ind...ath环境变量中。

二、JFreeChart的基本使用

不论创建的是什么图,JFreeChart都遵循以下的这个使用步骤:

1、建立Dataset。所有的数据都存放在Dataset中的。(创建一个数据源(dataset)来包含将要在图形中显示的数据)

2、建立JFreeChart。将dataset中的数据导入到JFreeChart中。(创建一个 JFreeChart 对象来代表要显示的图形 )

3、设置JFreeChart的显示属性。这一步可以省略,使用默认的JFreeChart显示属性。

3、渲染图表。即生成图片。

4、页面图片显示。

重要的类和接口:

org.jfree.data.general.Dataset 所有数据源类都要实现的接口

org.jfree.chart.ChartFactory 由它来产生 JFreeChart 对象

org.jfree.chart.JFreeChart 所有对图形的调整都是通过它噢!!

org.jfree.chart.plot.Plot 通过JFreeChart 对象获得它,然后再通过它对图形外部部分(例:坐标轴)调整

注意:它有很多子类,一般都下涉及到它的子类!

org.jfree.chart.renderer.AbstractRenderer 通过JFreeChart 对象获得它,然后再通过它对图形内部部分(例:折线的类型)调整。同样,针对不同类型的报表图,它有着不同的子类实现!在下面我们简称它为 Renderer

三、JFreeChart使用的具体例子

在web应用中的例子

/*

* 创建日期 2006-8-1

* @author:hong

* FileName:LineXYChart.java

*/

package com.hong.bean;

import java.awt.Color;

import java.awt.Font;

import java.io.IOException;

import java.io.PrintWriter;

import java.text.NumberFormat;

import java.text.SimpleDateFormat;

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.DateAxis;

import org.jfree.chart.axis.NumberAxis;

import org.jfree.chart.entity.StandardEntityCollection;

import org.jfree.chart.labels.StandardXYToolTipGenerator;

import org.jfree.chart.plot.XYPlot;

import org.jfree.chart.renderer.xy.XYItemRenderer;

import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;

import org.jfree.chart.servlet.ServletUtilities;

import org.jfree.chart.title.TextTitle;

import org.jfree.data.time.Day;

import org.jfree.data.time.TimeSeries;

import org.jfree.data.time.TimeSeriesCollection;

import org.jfree.data.xy.XYDataset;

import org.jfree.ui.RectangleInsets;

/**

* @author hong 曲线图的绘制

*/

public class LineXYChart

{

/**

* 返回生成图片的文件名

* @param session

* @param pw

* @return 生成图片的文件名

*/

public String getLineXYChart(HttpSession session, PrintWriter pw)

{

XYDataset dataset = this.createDateSet();//建立数据集

String fileName = null;

//建立JFreeChart

JFreeChart chart = ChartFactory.createTimeSeriesChart(

"JFreeChart时间曲线序列图", // title

"Date", // x-axis label

"Price", // y-axis label

dataset, // data

true, // create legend?

true, // generate tooltips?

false // generate URLs?

);

//设置JFreeChart的显示属性,对图形外部部分进行调整

chart.setBackgroundPaint(Color.red);//设置曲线图背景色

//设置字体大小,形状

Font font = new Font("宋体", Font.BOLD, 16);

TextTitle title = new TextTitle("JFreeChart时间曲线序列图", font);

chart.setTitle(title);

XYPlot plot = (XYPlot) chart.getPlot();//获取图形的画布

plot.setBackgroundPaint(Color.lightGray);//设置网格背景色

plot.setDomainGridlinePaint(Color.green);//设置网格竖线(Domain轴)颜色

plot.setRangeGridlinePaint(Color.white);//设置网格横线颜色

plot.setAxisOffset(new RectangleInsets(5.0, 5.0, 5.0, 5.0));//设置曲线图与xy轴的距离

plot.setDomainCrosshairVisible(true);

plot.setRangeCrosshairVisible(true);

XYItemRenderer r = plot.getRenderer();

if (r instanceof XYLineAndShapeRenderer)

{

XYLineAndShapeRenderer renderer = (XYLineAndShapeRenderer) r;

//renderer.setDefaultShapesVisible(true);

//renderer.setDefaultShapesFilled(true);

renderer.setShapesFilled(true);

renderer.setShapesVisible(true);//设置曲线是否显示数据点

}

//设置Y轴

NumberAxis numAxis = (NumberAxis) plot.getRangeAxis();

NumberFormat numFormater = NumberFormat.getNumberInstance();

numFormater.setMinimumFractionDigits(2);

numAxis.setNumberFormatOverride(numFormater);

//设置提示信息

StandardXYToolTipGenerator tipGenerator = new StandardXYToolTipGenerator(

"{1},{2})", new SimpleDateFormat("MM-dd"),

numFormater);

r.setToolTipGenerator(tipGenerator);

//设置X轴(日期轴)

DateAxis axis = (DateAxis) plot.getDomainAxis();

axis.setDateFormatOverride(new SimpleDateFormat("MM-dd"));

ChartRenderingInfo info = new ChartRenderingInfo(

new StandardEntityCollection());

try

{

fileName = ServletUtilities.saveChartAsPNG(chart, 500, 300, info,

session);//生成图片

// Write the image map to the PrintWriter

ChartUtilities.writeImageMap(pw, fileName, info, false);

}

catch (IOException e)

{

e.printStackTrace();

}

pw.flush();

return fileName;//返回生成图片的文件名

}

/**

* 建立生成图形所需的数据集

* @return 返回数据集

*/

private XYDataset createDateSet()

{

TimeSeriesCollection dataset = new TimeSeriesCollection();//时间曲线数据集合

TimeSeries s1 = new TimeSeries("历史曲线", Day.class);//创建时间数据源,每一个//TimeSeries在图上是一条曲线

//s1.add(new Day(day,month,year),value),添加数据点信息

s1.add(new Day(1, 2, 2006), 123.51);

s1.add(new Day(2, 2, 2006), 122.1);

s1.add(new Day(3, 2, 2006), 120.86);

s1.add(new Day(4, 2, 2006), 122.50);

s1.add(new Day(5, 2, 2006), 123.12);

s1.add(new Day(6, 2, 2006), 123.9);

s1.add(new Day(7, 2, 2006), 124.47);

s1.add(new Day(8, 2, 2006), 124.08);

s1.add(new Day(9, 2, 2006), 123.55);

s1.add(new Day(10, 2, 2006), 122.53);

dataset.addSeries(s1);

dataset.setDomainIsPointsInTime(true);

return dataset;

}

}

在jsp文件中显示图片

首先在Web应用程序部署文件web.xml中添加以下代码:

<!-- 图片显示,使用专用的servlet来进行显示,它会完成路径的搜索及映射 -->

<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>

然后在jsp中显示图片

完整的jsp文件:

<!--文件名称:timeLine.jsp-->

<%@ page contentType="text/html;charset=gb2312" pageEncoding="GB2312"%>

<%@ page import="com.hong.bean.LineXYChart"%>

<%@ page import = "java.io.PrintWriter" %>

<%

LineXYChart xyChart=new LineXYChart();

String fileName=xyChart.getLineXYChart(session,new PrintWriter(out));

String graphURL = request.getContextPath() + "/servlet/DisplayChart?filename=" + fileName;

%>

<html>

</head>

<title> JFreeChart使用例子</title>

<meta http-equiv="Content-Type" content="text/html; charset=gb2312">

</head>

<body>

<img src="<%= graphURL %>" width=500 height=300 border=0 usemap="#<%= fileName %>">

</body>

</html>



常见生成方式:

对JfreeChart有了初步了解并做好准备工作之后,开始作例子程序试验。在这里我只介绍饼图的做法,而这张图采用不同的方式进行输出,其他类型的图片可以参考jfreechart提供的例子,做法都差不多。

1) 直接生成图片

代码


public class PieChartPicture {  
public static void main(String[] args)   
{  
PieDataset dataset = getDataSet();  
JFreeChart chart = ChartFactory.createPieChart3D(  
    " 项目进度分布", // chart title  
    dataset,// data  
    true,// include legend  
    true,  
    false  
   );  
  PiePlot3D  plot=(PiePlot3D)chart.getPlot();  
    // 图片中显示百分比:默认方式  
    //plot.setLabelGenerator(new           StandardPieSectionLabelGenerator(StandardPieToolTipGenerator.DEFAULT_TOOLTIP_FORMAT));  
// 图片中显示百分比:自定义方式,{0} 表示选项, {1} 表示数值, {2} 表示所占比例 ,小数点后两位  
plot.setLabelGenerator(new StandardPieSectionLabelGenerator("{0}={1}({2})", NumberFormat.getNumberInstance(), new DecimalFormat("0.00%")));   
// 图例显示百分比:自定义方式, {0} 表示选项, {1} 表示数值, {2} 表示所占比例                  
plot.setLegendLabelGenerator(new StandardPieSectionLabelGenerator("{0}={1}({2})"));   
// 设置背景色为白色   
chart.setBackgroundPaint(Color.white);   
// 指定图片的透明度(0.0-1.0)   
plot.setForegroundAlpha(1.0f);   
// 指定显示的饼图上圆形(false)还椭圆形(true)   
plot.setCircular(true);   
// 设置图标题的字体   
Font font = new Font(" 黑体",Font.CENTER_BASELINE,20);   
TextTitle title = new TextTitle(" 项目状态分布");   
title.setFont(font);    
chart.setTitle(title);   
FileOutputStream fos_jpg = null;   
try {   
     fos_jpg=new FileOutputStream("D:\\ 项目状态分布.jpg");   
     ChartUtilities.writeChartAsJPEG(fos_jpg,100,chart,640,480,null);   
     fos_jpg.close();   
} catch (Exception e) {   
}   
}   
private static PieDataset getDataSet() {   
DefaultPieDataset dataset = new DefaultPieDataset();   
dataset.setValue(" 市场前期", new Double(10));   
dataset.setValue(" 立项", new Double(15));   
dataset.setValue(" 计划", new Double(10));   
dataset.setValue(" 需求与设计", new Double(10));   
dataset.setValue(" 执行控制", new Double(35));   
dataset.setValue(" 收尾", new Double(10));   
dataset.setValue(" 运维",new Double(10));   
return dataset;          
}  
}   

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

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

A .servlet输出

代码


public class PieByServlet extends HttpServlet{   
public void service(ServletRequest req, ServletResponse res)   
throws ServletException, IOException   
{   
   res.setContentType("image/jpeg");   
   PieDataset dataset = getDataSet();   
   JFreeChart chart = ChartFactory.createPieChart3D(   
   " 水果饼图", // chart title   
   dataset,// data   
   true, // include legend   
   true,   
   false );   
   //设置图表属性  
// 输出图片   
ChartUtilities.writeChartAsJPEG(res.getOutputStream(),100,chart,800,600,null);   
}   

B .struts的action方式输出

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

代码


try{         
ChartUtilities.writeChartAsJPEG(response.getOutputStream(),100,chart,800,600,null);   
} catch (Exception e) {   
}  
return null;   

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

加上语句response.setContentType("image/jpeg");

ChartUtilities.writeChartAsJPEG(new FileOutputStream("D:\\ 项目状态分布.jpg");,100,chart,640,480,null);

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

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

3)jsp+servlet+javabean方式

1. Create ChartViewer servlet

代码


public class ChartViewer extends HttpServlet {   
public void init() throws ServletException {  
}   
//Process the HTTP Get request  
public void doGet(HttpServletRequest request, HttpServletResponse response)  throws ServletException, IOException {   
// get the chart from session  
HttpSession session = request.getSession();   
BufferedImage chartImage = (BufferedImage) session.getAttribute("chartImage");   
// set the content type so the browser can see this as a picture   
response.setContentType("image/png");   
// send the picture   
PngEncoder encoder = new PngEncoder(chartImage, false, 0, 9);   
response.getOutputStream().write(encoder.pngEncode());  
}   
//Process the HTTP Post request   
public void doPost(HttpServletRequest request, HttpServletResponse response)  throws ServletException, IOException {   
doGet(request, response);   
}   
//Process the HTTP Put request   
public void doPut(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {   
}   
//Clean up resources   
public void destroy() { }   
}   

2. Create a servlet map in web.xml

代码


<?xml version="1.0" encoding="UTF-8"?>   
<web-app>   
<servlet>   
<servlet-name>ChartViewer</servlet-name>   
<servlet-class>myapp.webwork.servlets.ChartViewer</servlet-class>   
</servlet>   
<servlet-mapping>   
<servlet-name>ChartViewer</servlet-name>   
<url-pattern>/servlet/ChartViewer</url-pattern>   
</servlet-mapping>   
</web-app>   

3. Create a chart in a java bean (Pie3DDemo.java)

代码


public class Pie3DDemo {   
private DefaultPieDataset getDataset() {   
// categories...   
String[] section = new String[] { "Jan","Feb","Mar","Apr","May","Jun", "Jul","Aug","Sep","Oct","Nov","Dec" };   
// data...   
double[] data = new double[section.length];   
for (int i = 0; i < data.length; i++) {   
     data[i] = 10 + (Math.random() * 10);   
}   
// create the dataset...   
DefaultPieDataset dataset = new DefaultPieDataset();   
for (int i = 0; i < data.length; i++) {   
       dataset.setValue(section[i], data[i]);   
}   
return dataset;   
}   
public String getChartViewer(HttpServletRequest request, HttpServletResponse response) {   
DefaultPieDataset dataset = getDataset();   
// create the chart...   
JFreeChart chart = ChartFactory.createPie3DChart(   
  "Pie3D Chart Demo",  // chart title   
  dataset,             // data   
  true,                // include legend   
  true,   
  false   
);   
// set the background color for the chart...   
chart.setBackgroundPaint(Color.cyan);   
PiePlot plot = (PiePlot) chart.getPlot();   
plot.setNoDataMessage("No data available");   
// set drilldown capability...   
plot.setURLGenerator(new StandardPieURLGenerator("Bar3DDemo.jsp","section"));   
plot.setLabelGenerator(null);   
// OPTIONAL CUSTOMISATION COMPLETED.   
ChartRenderingInfo info = null;   
HttpSession session = request.getSession();   
try {   
//Create RenderingInfo object   
response.setContentType("text/html");   
info = new ChartRenderingInfo(new StandardEntityCollection());   
BufferedImage chartImage = chart.createBufferedImage(640, 400, info);   
// putting chart as BufferedImage in session,   
// thus making it available for the image reading action Action.   
session.setAttribute("chartImage", chartImage);   
PrintWriter writer = new PrintWriter(response.getWriter());   
ChartUtilities.writeImageMap(writer, "imageMap", info);   
writer.flush();   
} catch (Exception e) { }   
  
String pathInfo = "http://";   
pathInfo += request.getServerName();   
int port = request.getServerPort();   
pathInfo += ":"+String.valueOf(port);   
pathInfo += request.getContextPath();   
String chartViewer = pathInfo + "/servlet/ChartViewer";   
return chartViewer;   
}   

6. 页面

代码


   
<html>   
<head>   
<title>Pie Chart Demo</title>   
</head>   
<jsp:useBeanidjsp:useBeanid="myChart"scope="session"class="myapp.webwork.beans.Pie3DDemo" />   
<body>   
<h2>Pie Chart Demo</h2>   
<%String chartViewer = myChart.getChartViewer(request, response);%>   
<img src="<%=chartViewer%>" border=0 usemap="#imageMap">   
</body>   
</html>   

4)采用工具类ChartUtil和DisplayChart(jfreechart的servlet)输出

我用了上面的几个方法输出图片,发现页面里只能输出一个图片,

不过下面的方法可以输出多个图片,而且是几种方式中最简单的一个,推荐使用。

这种方式和上面的三种比较类似,是将javabean里的生成图片的方法写的一个工具类ChartUtil里面。

1 .添加工具类ChartUtil

public class ChartUtil {

// 产生时间序列图,返回图片名称

代码


public  static String generatePieChart(DefaultPieDataset dataset,String title,int width,int height,HttpSession session, PrintWriter pw) {   
  
String filename = null;   
try {   
if (session != null)   
{   
ChartDeleter deleter = (ChartDeleter)session.getAttribute("JFreeChart_Deleter");   
session.removeAttribute("JFreeChart_Deleter");   
session.setAttribute("JFreeChart_Deleter", deleter);   
}   
JFreeChart chart = ChartFactory.createPieChart3D(   
title,  // chart title   
dataset, // data   
true,  // include legend   
true,   
false );   
//  Write the chart image to the temporary directory   
ChartRenderingInfo info = new ChartRenderingInfo(new StandardEntityCollection());   
//If the last parameter is null, the chart is a "one time"-chart and will be deleted after the first serving.   
//If the last parameter is a session object, the chart remains until session time out.   
filename = ServletUtilities.saveChartAsPNG(chart, width, height, info, session);   
//  Write the image map to the PrintWriter   
ChartUtilities.writeImageMap(pw, filename, info,true);   
pw.flush();   
} catch (Exception e) {   
System.out.println("Exception - " + e.toString());   
e.printStackTrace(System.out);   
filename = "picture_error.png"; }   
return filename;   
}   
}   

2、在action里统计数据,设置好数据集dataset。传到页面

3、 在页面里取出

DefaultPieDataset piedataset=(DefaultPieDataset)request.getAttribute("piedataset");

// 用ChartUtil工具类产生图片

代码


String p = ChartUtil.generatePieChart(piedataset," 项目收支线图",500,300,null, new PrintWriter(out));   
String p1 = request.getContextPath() + "/servlet/DisplayChart?filename=" + p;   

通过以下方式输出

代码


<td><img src="<%= p1 %>" width=500 height=300 border=0 usemap="#<%= p %>"></td>  

4、在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>  

5) ApplicationFrame 方式

代码


public class PieChartDemo1 extends ApplicationFrame {   
public PieChartDemo1(String title) {   
super(title);   
setContentPane(createDemoPanel());   
}   
private static JFreeChart createChart(PieDataset dataset) {   
JFreeChart chart = ChartFactory.createPieChart(   
.......  
return chart;   
}   
  
public static JPanel createDemoPanel() {   
JFreeChart chart = createChart(createDataset());   
return new ChartPanel(chart);   
}   
  
public static void main(String[] args) {   
PieChartDemo1 demo = new PieChartDemo1("Pie Chart Demo 1");   
demo.pack();   
RefineryUtilities.centerFrameOnScreen(demo);   
demo.setVisible(true);   
}   
}   

文章引用自:



Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=1800159