chartutil.java

来自「jfreechart不错的教程例子谢谢!」· Java 代码 · 共 49 行

JAVA
49
字号
package com.segsec.chart.common;

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.servlet.ChartDeleter;
import org.jfree.chart.servlet.ServletUtilities;
import org.jfree.data.general.DefaultPieDataset;

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

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?