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