import java.awt.BasicStroke; import java.awt.Color; import java.awt.Font; import java.io.IOException; import java.text.DecimalFormat; import java.text.NumberFormat; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpSession; import org.apache.struts2.ServletActionContext; import org.jfree.chart.ChartFactory; import org.jfree.chart.JFreeChart; import org.jfree.chart.axis.CategoryAxis; import org.jfree.chart.axis.NumberAxis; import org.jfree.chart.axis.ValueAxis; import org.jfree.chart.labels.ItemLabelAnchor; import org.jfree.chart.labels.ItemLabelPosition; import org.jfree.chart.labels.StandardCategoryItemLabelGenerator; import org.jfree.chart.labels.StandardPieSectionLabelGenerator; import org.jfree.chart.plot.CategoryPlot; import org.jfree.chart.plot.PiePlot; import org.jfree.chart.plot.PlotOrientation; import org.jfree.chart.renderer.category.BarRenderer3D; import org.jfree.chart.renderer.category.CategoryItemRenderer; import org.jfree.chart.renderer.category.LineAndShapeRenderer; import org.jfree.chart.servlet.ServletUtilities; import org.jfree.chart.title.TextTitle; import org.jfree.data.category.CategoryDataset; import org.jfree.data.category.DefaultCategoryDataset; import org.jfree.data.general.DefaultPieDataset; import org.jfree.ui.TextAnchor; public class JFreeChartUtil { /** * 根据参数创建曲线图 * * @author HeCheng * @time 2010-12-13 15:56:42 * * @return */ public static String[] createLineChartByCategoryDataset( CategoryDataset dataset, String firstTitle, String subTitle, int width, int height, int min, int max) { int pieWidth = getLineWidth(width); int pieHeight = getlineHeight(height); String fileName = ""; String fileUrl = ""; JFreeChart chart = ChartFactory.createLineChart(firstTitle, "", "", dataset, PlotOrientation.VERTICAL, true, true, true); chart.setBackgroundPaint(Color.white); // 设置子标题 TextTitle subtitle = new TextTitle(subTitle, new Font("黑体", Font.BOLD, 12)); chart.addSubtitle(subtitle); Font titleFont = new Font("宋体", Font.PLAIN, 25); // 图片标题 Font itemfont = new Font("宋体", Font.PLAIN, 15);// 图示字体 chart.getTitle().setFont(titleFont); chart.getLegend().setItemFont(itemfont); // 设置最底下方框内的字体(图示) chart.getLegend().setVisible(false);// 不显示图示 // 其他参数 final CategoryPlot plot = (CategoryPlot) chart.getPlot(); CategoryItemRenderer cgir = plot.getRenderer(); // 在数量点上显示数值 cgir.setBaseItemLabelsVisible(true); cgir.setBasePositiveItemLabelPosition(new ItemLabelPosition( ItemLabelAnchor.OUTSIDE12, TextAnchor.BASELINE_LEFT)); cgir .setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator()); // 设置数值字体 cgir.setBaseItemLabelFont(new Font("Dialog", 0, 12)); // 是否显示数值 cgir.setBaseItemLabelsVisible(true); // 回传 plot.setRenderer(cgir); // 背景色,线条色 plot.setBackgroundPaint(Color.white); plot.setRangeGridlinePaint(Color.pink); // 线条属性 NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis(); rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits()); rangeAxis.setLowerBound(min); rangeAxis.setUpperBound(max); rangeAxis.setAutoRangeIncludesZero(false); // 其他属性 final LineAndShapeRenderer renderer = (LineAndShapeRenderer) plot .getRenderer(); renderer.setDrawOutlines(true); renderer.setSeriesStroke(0, new BasicStroke(2.0f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND, 1.0f, new float[] { 10.0f, 6.0f }, 0.0f)); renderer.setSeriesStroke(1, new BasicStroke(2.0f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND, 1.0f, new float[] { 6.0f, 6.0f }, 0.0f)); renderer.setSeriesStroke(2, new BasicStroke(2.0f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND, 1.0f, new float[] { 2.0f, 6.0f }, 0.0f)); HttpServletRequest request = ServletActionContext.getRequest(); HttpSession session = request.getSession(); try { fileName = ServletUtilities.saveChartAsPNG(chart, pieWidth, pieHeight, null, session); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } fileUrl = request.getContextPath() + "/DisplayChart?filename=" + fileName; return new String[] { fileName, fileUrl }; } /** * 获取曲线图默认宽度 * * @param width * @return */ public static int getLineWidth(int width) { switch (width) { case 0: return 350; case 1: return 1000; default: return width; } } /** * 获取曲线图默认高度 * * @param width * @return */ public static int getlineHeight(int height) { switch (height) { case 0: return 300; case 1: return 560; default: return height; } } /** * 根据参数创建饼图 * * @author HeCheng * @time 2010-12-10 09:57:29 * @param dataset * @return */ public static String[] createPieChartByDefaultPieDataset( DefaultPieDataset dataset, String imageName, int width, int height) { int pieWidth = getPieWidth(width); int pieHeight = getPieHeight(height); String fileName = ""; String fileUrl = ""; JFreeChart chart = ChartFactory.createPieChart(imageName, dataset, true, false, false); PiePlot pieplot = (PiePlot) chart.getPlot(); // 设置标题栏字体 chart.getTitle() .setFont(new Font("宋体", com.lowagie.text.Font.BOLD, 20)); // 设置图块标识字体 pieplot.setLabelFont(new Font("宋体", com.lowagie.text.Font.BOLD, 12)); // 设置背景颜色 pieplot.setBackgroundPaint(Color.white); // 设置饼图边框是否显示 pieplot.setOutlineVisible(false); // 设置下方类别字体 chart.getLegend().setItemFont( new Font("宋体", com.lowagie.text.Font.BOLD, 12)); // 设置每一个方块里显示的内容,为name:数量(百分比),第2和第3个参数为格式化 pieplot.setLabelGenerator(new StandardPieSectionLabelGenerator( "{0}:{1}({2}) ", NumberFormat.getNumberInstance(), new DecimalFormat("0.0000%"))); HttpServletRequest request = ServletActionContext.getRequest(); HttpSession session = request.getSession(); try { fileName = ServletUtilities.saveChartAsPNG(chart, pieWidth, pieHeight, null, session); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } fileUrl = request.getContextPath() + "/DisplayChart?filename=" + fileName; return new String[] { fileName, fileUrl }; } /** * 获取饼图默认宽度 * * @param width * @return */ public static int getPieWidth(int width) { switch (width) { case 0: return 700; case 1: return 1000; default: return width; } } /** * 获取饼图默认高度 * * @param width * @return */ public static int getPieHeight(int height) { switch (height) { case 0: return 450; case 1: return 560; default: return height; } } /** * 根据参数创建柱形图 * * @author HeCheng * @time 2010-12-10 09:59:46 * @return */ public String[] createBarChartByDefaultCategoryDataset( DefaultCategoryDataset dataset) { String fileName = ""; String fileUrl = ""; // 第3个boolean值表示是否显示dataset的第二项 JFreeChart chart = ChartFactory.createBarChart3D("肉类销量统计图", "四省对比情况", "销量", dataset, PlotOrientation.VERTICAL, true, true, true); CategoryPlot plot = chart.getCategoryPlot();// 获取图表区域对象 CategoryAxis domainAxis = plot.getDomainAxis(); // 设置标题字体 chart.getTitle().setFont(new Font("宋体", Font.BOLD, 20)); // 小标题字体 domainAxis.setLabelFont(new Font("黑体", Font.BOLD, 14)); // 各项字体(柱形下方) domainAxis.setTickLabelFont(new Font("宋体", Font.BOLD, 12)); // 垂直标题 ValueAxis rangeAxis = plot.getRangeAxis();// 获取柱状 // 数值单位字体 rangeAxis.setLabelFont(new Font("黑体", Font.BOLD, 15)); // 各项字体(最下方) chart.getLegend().setItemFont(new Font("黑体", Font.BOLD, 15)); CategoryPlot plot1 = chart.getCategoryPlot(); // 设置网格背景颜色 // plot1.setBackgroundPaint(Color.white); // 设置网格竖线颜色 // plot1.setDomainGridlinePaint(Color.pink); // 设置网格横线颜色 plot1.setRangeGridlinePaint(Color.pink); // 显示每个柱的数值,并修改该数值的字体属性 BarRenderer3D renderer = new BarRenderer3D(); renderer .setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator()); renderer.setBaseItemLabelsVisible(true); // 默认的数字显示在柱子中,通过如下两句可调整数字的显示 // 将数字显示出来 renderer.setBasePositiveItemLabelPosition(new ItemLabelPosition( ItemLabelAnchor.OUTSIDE12, TextAnchor.BASELINE_LEFT)); renderer.setItemLabelAnchorOffset(10D); // 设置每个地区所包含的平行柱的之间距离 renderer.setItemMargin(0.2); plot1.setRenderer(renderer); HttpServletRequest request = ServletActionContext.getRequest(); HttpSession session = request.getSession(); try { fileName = ServletUtilities.saveChartAsPNG(chart, 500, 300, null, session); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } fileUrl = request.getContextPath() + "/DisplayChart?filename=" + fileName; return new String[] { fileName, fileUrl }; } }