急!!求高手帮忙啊,JFreeChart整了一周了,没有进展啊!!
要求:同时生成柱状图和折线图。折线图是柱状图的值的增减百分数,并在折线图的折点出显示相应的百分数。如能帮忙,小弟感激不尽,愿把所有的积分奉上!
------解决方案--------------------package com.weboa.system.util;
import java.awt.Color;
import java.awt.Font;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.data.general.DefaultPieDataset;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.ChartRenderingInfo;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PiePlot;
import org.jfree.chart.plot.PiePlot3D;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.axis.AxisLocation;
import org.jfree.chart.entity.StandardEntityCollection;
import org.jfree.chart.servlet.*;
import com.weboa.system.model.DrawerContent;
import org.jfree.chart.labels.StandardCategoryItemLabelGenerator;
import org.jfree.chart.labels.StandardPieSectionLabelGenerator;
/**
* @author 石洲祥
*
*通过JFreeChart会图统计图表
*/
public class Drawer {
/**
* 生成2D饼图
* @param request
* @param response
* @param title 图表标题
* @param contents 图表内容
* @param Width 图表宽度
* @param Height 图表高度
* @return String 图表临时文件存放路径
*/
public String getImageURL_2DPie(HttpServletRequest request,HttpServletResponse response,
String Title,List Contents,Integer Width,Integer Height){
String fileName = null;
//默认宽度和高度,500是图片长度,300是图片高度
if(Width == null)Width = 500;
if(Height == null)Height = 300;
try{
PiePlot plot = new PiePlot(setDataset(Contents));
JFreeChart chart = new JFreeChart("", JFreeChart.DEFAULT_TITLE_FONT, plot, true);
//可选,设置图片背景色
chart.setBackgroundPaint(java.awt.Color.white);
//可选,设置图片标题
chart.setTitle(Title);
ChartRenderingInfo info = new ChartRenderingInfo(new StandardEntityCollection());
fileName = ServletUtilities.saveChartAsPNG(chart, Width, Height, info, request.getSession());
}catch (Exception ex) {
System.out.print(ex.fillInStackTrace());
return null;
}
return request.getContextPath() + "/servlet/DisplayChart?filename=" + fileName;
}
/**
* 生成3D饼图
* @param request
* @param response
* @param title 图表标题
* @param contents 图表内容
* @param Width 图表宽度
* @param Height 图表高度
* @return String 图表临时文件存放路径
*/
public String getImageURL_3DPie(HttpServletRequest request,HttpServletResponse response,
String Title,List Contents,String unit,Integer Width,Integer Height){
if(unit == null)unit = "RMB";
String fileName = null;
//默认宽度和高度,500是图片长度,300是图片高度
if(Width == null)Width = 500;
if(Height == null)Height = 300;
try{
//3D饼图
PiePlot3D plot = new PiePlot3D(setDataset(Contents));
//设定链接 ("link.jsp","section"));//section为参数,不填写则默认为category
// plot.setURLGenerator(new StandardPieURLGenerator("link.jsp","section"));
//指定图片的透明度
plot.setForegroundAlpha(0.5f);
//指定显示的饼图上圆形(false)还椭圆形(true)
plot.setCircular(false);
//显示百分比
plot.setLabelGenerator(new StandardPieSectionLabelGenerator("{0}: ({1}"+unit+", {2})"));
JFreeChart chart = new JFreeChart("",JFreeChart.DEFAULT_TITLE_FONT, plot, true);
//设置图片背景色
chart.setBackgroundPaint(java.awt.Color.white);
//设置图片标题属性
chart.setTitle(Title);