java代码
@Controller public class CityAction { @Autowired private CityBiz cityBiz; //柱状图 @RequestMapping(value = "getColumnChart") public ModelAndView getColumnChart(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) throws Exception { CategoryDataset dataset = getDataSet(cityBiz.findAll()); JFreeChart chart = ChartFactory.createBarChart3D("用户统计报表(所属单位)", // 主标题的名称 "所属单位名称", // X轴的标签 "数量", // Y轴的标签 dataset, // 图标显示的数据集合 PlotOrientation.VERTICAL, // 图像的显示形式(水平或者垂直) true, // 是否显示子标题 true, // 是否生成提示的标签 true); // 是否生成URL链接 JfreeUtil.setJfreeChart(chart); // 6. 将图形转换为图片,传到前台 String fileName = ServletUtilities.saveChartAsJPEG(chart, 700, 400, null, request.getSession()); String chartURL = request.getContextPath() + "/chart?filename=" + fileName; modelMap.put("chartURLBar", chartURL); return new ModelAndView("index", modelMap); } //饼状图 @RequestMapping(value = "getColumnpie") public ModelAndView getColumnpie(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) throws Exception { DefaultPieDataset dataset = getDataSetpie(cityBiz.findAll()); JFreeChart chart = ChartFactory.createPieChart3D("用户统计报表(所属单位)", // 主标题的名称 dataset, // 图标显示的数据集合 true, // 是否显示子标题 true, // 是否生成提示的标签 true); // 是否生成URL链接 JfreeUtil.setJfreePie(chart); // 6. 将图形转换为图片,传到前台 String fileName = ServletUtilities.saveChartAsJPEG(chart, 700, 400, null, request.getSession()); String chartURL = request.getContextPath() + "/chart?filename=" + fileName; modelMap.put("chartURLPie", chartURL); return new ModelAndView("index", modelMap); } //折线图 @RequestMapping(value = "getColumnLine") public ModelAndView getColumnLine(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) throws Exception { DefaultCategoryDataset dataset = getDataSetLine(cityBiz.findAll()); JFreeChart chart = ChartFactory.createLineChart("用户统计报表(所属单位)", // 主标题的名称 "所属单位名称", // X轴的标签 "数量", // Y轴的标签 dataset, // 图标显示的数据集合 PlotOrientation.VERTICAL, // 图像的显示形式(水平或者垂直) true, // 是否显示子标题 true, // 是否生成提示的标签 true); // 是否生成URL链接 JfreeUtil.setJfreeLine(chart); // 6. 将图形转换为图片,传到前台 String fileName = ServletUtilities.saveChartAsJPEG(chart, 700, 400, null, request.getSession()); String chartURL = request.getContextPath() + "/chart?filename=" + fileName; modelMap.put("chartURLLine", chartURL); return new ModelAndView("index", modelMap); } // 获取柱状图数据集 private CategoryDataset getDataSet(List<City> cityList) { DefaultCategoryDataset dataset = new DefaultCategoryDataset(); if (cityList != null && cityList.size() > 0) { for (City city : cityList) { dataset.addValue(city.getCount(), city.getCityname(), city.getFruitname()); } } return dataset; } // 获取饼状图数据集 private DefaultPieDataset getDataSetpie(List<City> cityList) { DefaultPieDataset dataset = new DefaultPieDataset(); // if (cityList != null && cityList.size() > 0) { // for (City city : cityList) { // dataset.addValue(city.getCount(), city.getCityname(), // city.getFruitname()); // } // } dataset.setValue("北京", 13); dataset.setValue("深圳", 6); dataset.setValue("上海", 2); return dataset; } // 获取折线图数据集 private DefaultCategoryDataset getDataSetLine(List<City> cityList) { DefaultCategoryDataset dataset = new DefaultCategoryDataset(); // if (cityList != null && cityList.size() > 0) { // for (City city : cityList) { // dataset.addValue(city.getCount(), city.getCityname(), // city.getFruitname()); // } // } dataset.addValue(13, "所属单位", "北京"); dataset.addValue(6, "所属单位", "深圳"); dataset.addValue(2, "所属单位", "上海"); dataset.addValue(24, "水果", "香蕉"); dataset.addValue(15, "水果", "梨子"); dataset.addValue(27, "水果", "苹果"); return dataset; } }
public class JfreeUtil { // 柱状图 public static void setJfreeChart(JFreeChart chart) { // 处理图形上的乱码 // 处理主标题的乱码 chart.getTitle().setFont(new Font("宋体", Font.BOLD, 18)); // 处理子标题乱码 chart.getLegend().setItemFont(new Font("宋体", Font.BOLD, 15)); // 获取图表区域对象 CategoryPlot categoryPlot = (CategoryPlot) chart.getPlot(); // 获取X轴的对象 CategoryAxis3D categoryAxis3D = (CategoryAxis3D) categoryPlot.getDomainAxis(); // 获取Y轴的对象 NumberAxis3D numberAxis3D = (NumberAxis3D) categoryPlot.getRangeAxis(); // 处理X轴上的乱码 categoryAxis3D.setTickLabelFont(new Font("宋体", Font.BOLD, 15)); // 处理X轴外的乱码 categoryAxis3D.setLabelFont(new Font("宋体", Font.BOLD, 15)); // 处理Y轴上的乱码 numberAxis3D.setTickLabelFont(new Font("宋体", Font.BOLD, 15)); // 处理Y轴外的乱码 numberAxis3D.setLabelFont(new Font("宋体", Font.BOLD, 15)); // 处理Y轴上显示的刻度,以1作为1格 numberAxis3D.setAutoTickUnitSelection(false); NumberTickUnit unit = new NumberTickUnit(1); numberAxis3D.setTickUnit(unit); // 获取绘图区域对象 BarRenderer3D barRenderer3D = (BarRenderer3D) categoryPlot.getRenderer(); // 设置柱形图的宽度 barRenderer3D.setMaximumBarWidth(0.07); // 在图形上显示数字 barRenderer3D.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator()); barRenderer3D.setBaseItemLabelsVisible(true); barRenderer3D.setBaseItemLabelFont(new Font("宋体", Font.BOLD, 15)); } // 饼状图 public static void setJfreePie(JFreeChart chart) { // 处理图形上的乱码 // 处理主标题的乱码 chart.getTitle().setFont(new Font("宋体", Font.BOLD, 18)); // 处理子标题乱码 chart.getLegend().setItemFont(new Font("宋体", Font.BOLD, 15)); // 获取图表区域对象 PiePlot3D categoryPlot = (PiePlot3D) chart.getPlot(); // 处理图像上的乱码 categoryPlot.setLabelFont(new Font("宋体", Font.BOLD, 15)); // 设置图形的生成格式为(上海 2 (10%)) String format = "{0} {1} ({2})"; categoryPlot.setLabelGenerator(new StandardPieSectionLabelGenerator(format)); // 使用ChartFrame对象显示图像 } // 折线图 public static void setJfreeLine(JFreeChart chart) { // 处理图形上的乱码 // 处理主标题的乱码 chart.getTitle().setFont(new Font("宋体", Font.BOLD, 18)); // 处理子标题乱码 chart.getLegend().setItemFont(new Font("宋体", Font.BOLD, 15)); // 获取图表区域对象 CategoryPlot categoryPlot = (CategoryPlot) chart.getPlot(); // 获取X轴的对象 CategoryAxis categoryAxis = (CategoryAxis) categoryPlot.getDomainAxis(); // 获取Y轴的对象 NumberAxis numberAxis = (NumberAxis) categoryPlot.getRangeAxis(); // 处理X轴上的乱码 categoryAxis.setTickLabelFont(new Font("宋体", Font.BOLD, 15)); // 处理X轴外的乱码 categoryAxis.setLabelFont(new Font("宋体", Font.BOLD, 15)); // 处理Y轴上的乱码 numberAxis.setTickLabelFont(new Font("宋体", Font.BOLD, 15)); // 处理Y轴外的乱码 numberAxis.setLabelFont(new Font("宋体", Font.BOLD, 15)); // 处理Y轴上显示的刻度,以1作为1格 numberAxis.setAutoTickUnitSelection(false); NumberTickUnit unit = new NumberTickUnit(1); numberAxis.setTickUnit(unit); // 获取绘图区域对象 LineAndShapeRenderer lineAndShapeRenderer = (LineAndShapeRenderer) categoryPlot.getRenderer(); // 在图形上显示数字 lineAndShapeRenderer.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator()); lineAndShapeRenderer.setBaseItemLabelsVisible(true); lineAndShapeRenderer.setBaseItemLabelFont(new Font("宋体", Font.BOLD, 15)); // 在图形上添加转折点(使用小矩形显示) Rectangle shape = new Rectangle(10, 10); lineAndShapeRenderer.setSeriesShape(0, shape); lineAndShapeRenderer.setSeriesShapesVisible(0, true); } }
jsp页面
<div style="text-align: center"> jfreechart _3D柱状图 <br> <br> 点击显示柱状图<a href="getColumnChart.action">getMajorChart</a> <br> <br> <c:if test="${not empty chartURLBar}"> <img src="${chartURLBar}" width=600 height=400 border=0 color=gray> </c:if> </div> <div style="text-align: center"> jfreechart _3D饼状图 <br> <br> 点击显示饼状图<a href="getColumnpie.action">getMajorChart</a> <br> <br> <c:if test="${not empty chartURLPie}"> <img src="${chartURLPie}" width=600 height=400 border=0 color=gray> </c:if> </div> <div style="text-align: center"> jfreechart _3D折线状图 <br> <br> 点击显示折线图<a href="getColumnLine.action">getMajorChart</a> <br> <br> <c:if test="${not empty chartURLLine}"> <img src="${chartURLLine}" width=600 height=400 border=0 color=gray> </c:if> </div>
web.xml
//插入下面部分 <servlet> <servlet-name>DisplayChart</servlet-name> <servlet-class>org.jfree.chart.servlet.DisplayChart</servlet-class> </servlet> <servlet-mapping> <servlet-name>DisplayChart</servlet-name> <url-pattern>/chart</url-pattern> </servlet-mapping> </web-app>
jar包jfreechart1.0.13