Abstract class 实现JFreeChar jfreechartStrutsJavaServletApache
程序员文章站
2022-03-07 23:19:25
...
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; } }
上一篇: JS时间校验
下一篇: spring MVC上传文件中的问题