当前位置: 代码迷 >> Web前端 >> JavaWeb图形表格饼状图
  详细解决方案

JavaWeb图形表格饼状图

热度:176   发布时间:2012-11-04 10:42:41.0
JavaWeb图形报表饼状图

步骤说明:
1,导入log4j.jar,jfreechart-0.9.18.jar,jcommon-0.9.3.jar三个jar包

2,在某个包中写入Pie.java类

?

package com.mengya.util;

import java.awt.Color;
import java.awt.Font;
import java.awt.Insets;
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.entity.StandardEntityCollection;
import org.jfree.chart.labels.StandardPieItemLabelGenerator;
import org.jfree.chart.plot.PiePlot;
import org.jfree.chart.servlet.ServletUtilities;
import org.jfree.chart.title.TextTitle;
import org.jfree.data.DefaultPieDataset;

public class Pie {
?private DefaultPieDataset data = new DefaultPieDataset();

?public void setValue(String key, double value) {
??data.setValue(key, value);

?}

?public String generatePieChart(String title, HttpSession session,
???PrintWriter pw, int wPhoto, int hPhoto) {

??String filename = null;
??try {
???JFreeChart chart = ChartFactory.createPieChart3D(title, data, true,
?????true, false);
???// 设置图片的背景色
???chart.setBackgroundPaint(new java.awt.Color(189, 235, 255));
???// 设置图片标题的字体和大小
???Font font = new Font("黑体", Font.CENTER_BASELINE, 16);
???TextTitle _title = new TextTitle(title);
???_title.setFont(font);
???chart.setTitle(_title);
???PiePlot plot = (PiePlot) chart.getPlot();
???plot.setInsets(new Insets(5, 5, 5, 5));
???// 在统计图片上建连结
???// plot.setURLGenerator(new StandardPieURLGenerator("link.jsp",
???// "section"));
???plot.setStartAngle(270);
???// 指定显示的饼图上圆形还椭圆形
???plot.setCircular(false);
???// 指定图片的透明度
???plot.setForegroundAlpha(1.0f);
???// 没有数据时显示的消息
???plot.setNoDataMessage("当天没有统计数据");
???plot.setNoDataMessageFont(new Font("黑体", Font.CENTER_BASELINE, 16));
???plot.setNoDataMessagePaint(Color.RED);

???// plot.setRadius(0.70);
???// 抽离一个 section 出来,不支持3D
???// plot.setExplodePercent(1, 1.00);
???plot.setSectionOutlinePaint(Color.BLACK);

???// 显示百分比
???plot.setLabelGenerator(new StandardPieItemLabelGenerator(
?????StandardPieItemLabelGenerator.DEFAULT_TOOLTIP_FORMAT));
???// 设置背景的透明度
???plot.setBackgroundAlpha(0.6f);
???// 把生成的图片放到临时目录
???ChartRenderingInfo info = new ChartRenderingInfo(
?????new StandardEntityCollection());
???// 500是图片长度,300是图片高度

???filename = ServletUtilities.saveChartAsJPEG(chart, wPhoto, hPhoto,
?????info, session);
???ChartUtilities.writeImageMap(pw, filename, info);
???pw.flush();

??} catch (Exception e) {
???System.out.println("Exception - " + e.toString());
???e.printStackTrace(System.out);
???filename = "public_error_500x300.png";
??}
??return filename;
?}
}

3,配置web.xml,在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>/DisplayChart</url-pattern>
?</servlet-mapping>

4,在要显示图片的jsp中添加内容:
??<%@ page import="com.mengya.util.Pie"%>
??<%@ page import="com.mengya.bean.VegetableBean"%>
??<%@ page import="java.io.PrintWriter"%>

??<%
???Pie biz = new Pie();
???List ar = (ArrayList) request.getAttribute("arr");
???for (int i = 0; i < ar.size(); i++) {
????VegetableBean bean = (VegetableBean) ar.get(i);
????biz.setValue(bean.getV_name(), bean.getV_count());
???}
???String filename = biz.generatePieChart("蔬菜品种例比图", session,
?????new PrintWriter(out), 700, 450);
???String url = request.getContextPath() + "/DisplayChart?filename="
?????+ filename;
??%>
??<table align="center" border="0">
???<tr>
????<td>
?????<P ALIGN="CENTER">
??????<img src="<%=url%>" width=700 height=450 border=1
???????usemap="#<%=filename%>">
?????</p>
????</td>
???</tr>
??</table>