Abstract class 实现JFreeChar

  ChartAction类
package chart;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

public class ChartAction extends Action {

	private static final Class[] CONSTRUCTOR_PARAMS = new Class[] { HttpServletRequest.class,
			HttpServletResponse.class };

	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws Exception {
		String className = request.getParameter("chart");

		AbstractChart chart = (AbstractChart) Class.forName(
				"chart." + className).getConstructor(
				CONSTRUCTOR_PARAMS).newInstance(new Object[] { request, response });
		//如果类没有构造函数则直接.forName(类名)就可以了
		chart.execute();

		return null;
	}

}

 

 

AbstractChart 类

 

package chart;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.jfree.chart.ChartRenderingInfo;
import org.jfree.chart.ChartUtilities;

public abstract class AbstractChart {

	private HttpServletRequest request;

	private PrintWriter pw;

	public AbstractChart(HttpServletRequest request, HttpServletResponse response)
			throws IOException {
		this.request = request;
		this.pw = response.getWriter();
	}

	public void execute() throws IOException {
		String fileName;
		try {
			fileName = executeInternal(request);
		} catch (Exception e) {
			return;
		}
		ChartRenderingInfo info = getChartRenderingInfo();
		String usemap = "";
		if (info != null) {
			ChartUtilities.writeImageMap(pw, fileName, info, false);
			usemap = " usemap=\"#" + fileName + "\"";
		}
		pw.println("<img src=\"" + request.getContextPath() + "/DisplayChart?filename=" + fileName
				+ "\"" + usemap + "\"/>");

	}

	protected abstract String executeInternal(HttpServletRequest request) throws IOException;

	protected ChartRenderingInfo getChartRenderingInfo() {
		return null;
	}
}

 

 

SSBChart   extends AbstractChart

 

package chart;

import java.awt.Color;
import java.awt.Font;
import java.awt.RenderingHints;
import java.io.IOException;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.labels.StandardCategoryItemLabelGenerator;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.BarRenderer;
import org.jfree.chart.renderer.category.BarRenderer3D;
import org.jfree.chart.renderer.category.CategoryItemRenderer;
import org.jfree.chart.servlet.ServletUtilities;
import org.jfree.data.category.DefaultCategoryDataset;

public class SSBChart extends AbstractChart {

	public SSBChart(HttpServletRequest request, HttpServletResponse response) throws IOException {
		super(request, response);
	}

	protected String executeInternal(HttpServletRequest request) throws IOException {

		DefaultCategoryDataset dataset = new DefaultCategoryDataset();
		//jsp include 进去的页面共享一个request
		List data = (List) request.getAttribute("tableList");

		int len = data.size();
		for (int i = len - 1; i >= 0; i--) {
			Map elem = (Map) data.get(i);
			dataset.addValue((Number) elem.get("SSB"), "生师比", (String) elem.get("XN"));
		}

		JFreeChart chart = ChartFactory.createBarChart("历学年生师比", "学年", "生师比", dataset,
				PlotOrientation.VERTICAL, true, true, true);

		chart.setBackgroundPaint(Color.WHITE);

		chart.getRenderingHints().put(RenderingHints.KEY_TEXT_ANTIALIASING,
				RenderingHints.VALUE_TEXT_ANTIALIAS_OFF);
		chart.getTitle().setFont(new Font("serif", Font.TRUETYPE_FONT, 14));

		CategoryPlot plot = chart.getCategoryPlot();

		BarRenderer renderer = (BarRenderer) chart.getCategoryPlot().getRenderer();

		renderer.setMaximumBarWidth(0.1);

		renderer.setItemLabelGenerator(new StandardCategoryItemLabelGenerator());
		renderer.setItemLabelsVisible(true);

		plot.getRangeAxis().setUpperMargin(0.15d);

		String fileName = ServletUtilities.saveChartAsPNG(chart, 600, 250, request.getSession());

		return fileName;

	}
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值