package org.hz.util; import org.jfree.chart.ChartFactory; import org.jfree.chart.JFreeChart; import org.jfree.chart.axis.CategoryAxis; import org.jfree.chart.axis.CategoryLabelPositions; import org.jfree.chart.plot.CategoryPlot; import org.jfree.chart.plot.PlotOrientation; import org.jfree.chart.renderer.category.BarRenderer3D; import org.jfree.data.category.CategoryDataset; import org.jfree.data.category.DefaultCategoryDataset; public class FreeChartUtil { public static CategoryDataset createDataset() { DefaultCategoryDataset localDefaultCategoryDataset = new DefaultCategoryDataset(); localDefaultCategoryDataset.addValue(10.0D, "Series 1", "Category 1"); localDefaultCategoryDataset.addValue(4.0D, "Series 1", "Category 2"); localDefaultCategoryDataset.addValue(15.0D, "Series 1", "Category 3"); localDefaultCategoryDataset.addValue(14.0D, "Series 1", "Category 4"); localDefaultCategoryDataset.addValue(-5.0D, "Series 2", "Category 1"); localDefaultCategoryDataset.addValue(-7.0D, "Series 2", "Category 2"); localDefaultCategoryDataset.addValue(14.0D, "Series 2", "Category 3"); localDefaultCategoryDataset.addValue(-3.0D, "Series 2", "Category 4"); localDefaultCategoryDataset.addValue(6.0D, "Series 3", "Category 1"); localDefaultCategoryDataset.addValue(17.0D, "Series 3", "Category 2"); localDefaultCategoryDataset.addValue(-12.0D, "Series 3", "Category 3"); localDefaultCategoryDataset.addValue(7.0D, "Series 3", "Category 4"); localDefaultCategoryDataset.addValue(7.0D, "Series 4", "Category 1"); localDefaultCategoryDataset.addValue(15.0D, "Series 4", "Category 2"); localDefaultCategoryDataset.addValue(11.0D, "Series 4", "Category 3"); localDefaultCategoryDataset.addValue(0D, "Series 4", "Category 4"); localDefaultCategoryDataset.addValue(-8.0D, "Series 5", "Category 1"); localDefaultCategoryDataset.addValue(-6.0D, "Series 5", "Category 2"); localDefaultCategoryDataset.addValue(10.0D, "Series 5", "Category 3"); localDefaultCategoryDataset.addValue(-9.0D, "Series 5", "Category 4"); localDefaultCategoryDataset.addValue(9.0D, "Series 6", "Category 1"); localDefaultCategoryDataset.addValue(8.0D, "Series 6", "Category 2"); localDefaultCategoryDataset.addValue(0D, "Series 6", "Category 3"); localDefaultCategoryDataset.addValue(6.0D, "Series 6", "Category 4"); localDefaultCategoryDataset.addValue(-10.0D, "Series 7", "Category 1"); localDefaultCategoryDataset.addValue(9.0D, "Series 7", "Category 2"); localDefaultCategoryDataset.addValue(7.0D, "Series 7", "Category 3"); localDefaultCategoryDataset.addValue(7.0D, "Series 7", "Category 4"); localDefaultCategoryDataset.addValue(11.0D, "Series 8", "Category 1"); localDefaultCategoryDataset.addValue(13.0D, "Series 8", "Category 2"); localDefaultCategoryDataset.addValue(9.0D, "Series 8", "Category 3"); localDefaultCategoryDataset.addValue(9.0D, "Series 8", "Category 4"); localDefaultCategoryDataset.addValue(-3.0D, "Series 9", "Category 1"); localDefaultCategoryDataset.addValue(7.0D, "Series 9", "Category 2"); localDefaultCategoryDataset.addValue(11.0D, "Series 9", "Category 3"); localDefaultCategoryDataset.addValue(-10.0D, "Series 9", "Category 4"); return localDefaultCategoryDataset; } public static JFreeChart createChart(CategoryDataset paramCategoryDataset) { JFreeChart localJFreeChart = ChartFactory.createBarChart3D("3D Bar Chart Demo", "Category", "Value", paramCategoryDataset, PlotOrientation.VERTICAL, true, true, false); CategoryPlot localCategoryPlot = (CategoryPlot)localJFreeChart.getPlot(); localCategoryPlot.setOutlineVisible(false); localCategoryPlot.setDomainGridlinesVisible(true); CategoryAxis localCategoryAxis = localCategoryPlot.getDomainAxis(); localCategoryAxis.setCategoryLabelPositions(CategoryLabelPositions.createUpRotationLabelPositions(0.39269908169872414D)); localCategoryAxis.setCategoryMargin(0D); BarRenderer3D localBarRenderer3D = (BarRenderer3D)localCategoryPlot.getRenderer(); localBarRenderer3D.setDrawBarOutline(false); return localJFreeChart; } }
Servlet
package org.hz.servlets; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.hz.util.FreeChartUtil; import org.jfree.chart.ChartUtilities; /** * Servlet implementation class FreeChartServlet */ public class FreeChartServlet extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public FreeChartServlet() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stubre FreeChartUtil u=new FreeChartUtil(); ChartUtilities.writeChartAsJPEG(response.getOutputStream(), u.createChart(u.createDataset()), 640, 400); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub this.doGet(request, response); } }图表示例之一,文档中还有更多示例