JFreeChart是JAVA平台上的一个开放的第三方图表绘制类库。只要下载JFreeChart的类库,导入项目即可使用。下面是一个绘制折线图的实例。各处注释都已经写的比较清晰了。
1 package com.mvc.controller; 2 3 import java.util.HashMap; 4 import java.util.List; 5 import java.util.Map; 6 import javax.servlet.http.HttpServletRequest; 7 import java.awt.Color; 8 import java.awt.Font; 9 import java.awt.RenderingHints; 10 11 import org.jfree.chart.ChartColor; 12 import org.jfree.chart.ChartFactory; 13 import org.jfree.chart.JFreeChart; 14 import org.jfree.chart.axis.CategoryAxis; 15 import org.jfree.chart.axis.ValueAxis; 16 import org.jfree.chart.labels.StandardCategoryItemLabelGenerator; 17 import org.jfree.chart.plot.CategoryPlot; 18 import org.jfree.chart.plot.PlotOrientation; 19 import org.jfree.chart.renderer.category.LineAndShapeRenderer; 20 import org.jfree.chart.servlet.ServletUtilities; 21 import org.jfree.chart.title.TextTitle; 22 import org.jfree.data.category.DefaultCategoryDataset; 23 24 import org.springframework.stereotype.Controller; 25 import org.springframework.beans.factory.annotation.Autowired; 26 import org.springframework.web.bind.annotation.RequestMapping; 27 import org.springframework.web.bind.annotation.ResponseBody; 28 29 import com.alibaba.fastjson.JSON; 30 import com.mvc.po.User; 31 import com.mvc.service.UserService; 32 33 @Controller 34 public class UserController { 35 36 @Autowired 37 private UserService userService; 38 39 @RequestMapping("getChart.do") 40 public @ResponseBody String getChart(HttpServletRequest request) throws Exception{ 41 42 //1. 获得数据集合 43 DefaultCategoryDataset dataset = new DefaultCategoryDataset(); 44 List<Map<String, Object>> list = userService.query(); 45 Map<String, Object> map = new HashMap<String, Object>(); 46 47 for(int i = 0; i < list.size(); i++){ 48 map=list.get(i); 49 dataset.addValue(Double.parseDouble(map.get("orderMoney").toString()), "订单金额", map.get("orderDate").toString()); 50 } 51 52 //2. 创建柱状图 53 JFreeChart chart = ChartFactory.createLineChart("订单情况", // 图表标题 54 "日期", // 目录轴的显示标签 55 "金额", // 数值轴的显示标签 56 dataset, // 数据集 57 PlotOrientation.VERTICAL, // 图表方向:水平、垂直 58 false, // 是否显示图例(对于简单的柱状图必须是false) 59 true, // 是否生成工具 60 true // 是否生成URL链接 61 ); 62 63 //3. 设置整个柱状图的颜色和文字(char对象的设置是针对整个图形的设置) 64 chart.setBackgroundPaint(ChartColor.WHITE); // 设置总的背景颜色 65 chart.getLegend(0).setItemFont(new Font("宋体",Font.BOLD,20)); 66 chart.getLegend(0).setItemPaint(Color.BLUE); 67 68 //4. 获得图形对象,并通过此对象对图形的颜色文字进行设置 69 CategoryPlot p = chart.getCategoryPlot();// 获得图表对象 70 p.setNoDataMessage("当前没有数据统计!"); 71 p.setNoDataMessagePaint(Color.RED); 72 p.setNoDataMessageFont(new Font("宋体",Font.BOLD,20)); 73 p.setBackgroundPaint(ChartColor.WHITE);//图形背景颜色 74 p.setRangeGridlinePaint(ChartColor.DARK_GREEN);//图形表格颜色 75 p.setDomainGridlinesVisible(true); //设置背景网格线是否可见 76 p.setDomainGridlinePaint(Color.GRAY); //设置背景竖网格线颜色 77 p.setRangeGridlinePaint(Color.GRAY); //设置背景横网格线颜色 78 79 //显示节点数据 80 LineAndShapeRenderer renderer = (LineAndShapeRenderer) p.getRenderer(); 81 renderer.setBaseItemLabelsVisible(true); 82 renderer.setBaseShapesFilled(true); 83 renderer.setBaseItemLabelsVisible(true); 84 renderer.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator()); 85 renderer.setBaseItemLabelFont(new Font("Dialog", 1, 14)); 86 p.setRenderer(renderer); 87 88 //5.解决乱码问题 89 //(1)图形标题文字设置 90 TextTitle textTitle = chart.getTitle(); 91 textTitle.setFont(new Font("宋体",Font.BOLD,20)); 92 textTitle.setPaint(Color.BLACK); 93 94 //(2)图形X轴坐标文字的设置 95 CategoryPlot cPlot = (CategoryPlot) chart.getPlot(); 96 chart.getRenderingHints().put(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_OFF); 97 CategoryAxis axis = cPlot.getDomainAxis(); 98 axis.setLabelFont(new Font("宋体",Font.BOLD,15)); //设置X轴坐标上标题的文字 99 axis.setLabelPaint(Color.BLACK); 100 axis.setTickLabelFont(new Font("宋体",Font.BOLD,12)); //设置X轴坐标上的文字 101 axis.setTickLabelPaint(Color.BLACK); 102 103 //(3)图形Y轴坐标文字的设置 104 ValueAxis valueAxis = cPlot.getRangeAxis(); 105 valueAxis.setLabelFont(new Font("宋体",Font.BOLD,15)); //设置Y轴坐标上标题的文字 106 valueAxis.setLabelPaint(Color.BLACK); 107 valueAxis.setTickLabelFont(new Font("sans-serif",Font.BOLD,12));//设置Y轴坐标上的文字 108 valueAxis.setTickLabelPaint(Color.BLACK); 109 110 //6. 将图形转换为图片,传到前台 111 String fileName = ServletUtilities.saveChartAsJPEG(chart, 700, 400, null, request.getSession()); 112 String chartURL=request.getContextPath() + "/chart?filename="+fileName; 113 114 return JSON.toJSONString(chartURL); 115 }
时间: 2024-09-29 03:26:06