jfreechart-1.0.2 生成柱状图报错

doverlee 发布于 2014/06/07 18:36
阅读 116
收藏 0
package com.bjpowernode.drp.statreport.web;


import java.awt.Color;
import java.awt.Font;
import java.io.IOException;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.Iterator;
import java.util.Map;


import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;


import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.labels.StandardPieSectionLabelGenerator;
import org.jfree.chart.plot.PiePlot3D;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.title.TextTitle;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.data.general.DefaultPieDataset;


import com.bjpowernode.drp.statreport.manager.StatReportManager;
import com.bjpowernode.drp.util.Constants;
import com.bjpowernode.drp.util.servlet.BaseServlet;


/**
 * 演示通过servlet直接输出图表
 * 
 * @author Winter Lau
 */
public class StatReportServlet extends BaseServlet {
private StatReportManager statReportManager;

@Override
public void init() throws ServletException {
statReportManager = (StatReportManager)getBeanFactory().getInstance().getServiceObject(StatReportManager.class);
}

public void service(ServletRequest request, ServletResponse response)
throws ServletException, IOException {
//设置传输数据类型!
response.setContentType("image/jpeg");
String command = request.getParameter("command");
if (Constants.SHOW_PIECHART.equals(command)) {
producePieChart(request, response);
} else if (Constants.SHOW_BARCHART.equals(command)) {
produceBarChart(request, response);
} else {

}

}


private void produceBarChart(ServletRequest request, ServletResponse response)
throws ServletException, IOException{

DefaultCategoryDataset dataset = getBarDataSet();
JFreeChart chart = ChartFactory.createBarChart3D("水果销量统计图", 
                 "水果",
                 "销量",
                 dataset,
                 PlotOrientation.VERTICAL,
                 false,
                 false,
                 false);
//使用response发送到客户端浏览器上
ChartUtilities.writeChartAsJPEG(response.getOutputStream(), 1.0f, chart, 400, 300, null);

}

/**
* 获取柱状图的数据集对象
* @return
*/
private DefaultCategoryDataset getBarDataSet() {
DefaultCategoryDataset dataset = new DefaultCategoryDataset();
dataset.addValue(510, "深圳", "苹果");
dataset.addValue(320, "深圳", "香蕉");
dataset.addValue(580, "深圳", "橘子");
dataset.addValue(390, "深圳", "梨子");
return dataset;
}

}

返回顶部
顶部