应项目的需求,要做一个折现统计图,具体的效果如下:
需求中可以在统计年度,季度,月,星期的。其中核心代码是:
/** * 绘图 * @return * @throws IOException * @throws ParseException * @throws IllegalArgumentException * @throws IllegalAccessException */ public String createChart() throws IOException, ParseException, IllegalArgumentException, IllegalAccessException { // 定义图标对象 JFreeChart chart = ChartFactory.createLineChart(null,// 报表题目,字符串类型 "交易时间", // 横轴 "交易数量", // 纵轴 this.createDataset(), // 获得数据集 PlotOrientation.VERTICAL, // 图标方向垂直 true, // 显示图例 false, // 不用生成工具 false // 不用生成URL地址 ); // 整个大的框架属于chart 可以设置chart的背景颜色 // 生成图形 CategoryPlot plot = chart.getCategoryPlot(); // 图像属性部分 plot.setBackgroundPaint(Color.white); plot.setDomainGridlinesVisible(false); // 设置背景网格线是否可见 plot.setDomainGridlinePaint(Color.BLACK); // 设置背景网格线颜色 plot.setRangeGridlinePaint(Color.GRAY); plot.setNoDataMessage("没有数据");// 没有数据时显示的文字说明。 // 数据轴属性部分 NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis(); rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits()); rangeAxis.setAutoRangeIncludesZero(true); // 自动生成 rangeAxis.setUpperMargin(0.20); rangeAxis.setLabelAngle(Math.PI / 2.0); rangeAxis.setAutoRange(false); // 数据渲染部分 主要是对折线做操作 LineAndShapeRenderer renderer = (LineAndShapeRenderer) plot .getRenderer(); renderer.setBaseItemLabelsVisible(true); renderer.setSeriesPaint(0, Color.black); // 设置折线的颜色 renderer.setBaseShapesFilled(true); renderer.setBaseItemLabelsVisible(true); renderer.setBasePositiveItemLabelPosition(new ItemLabelPosition( ItemLabelAnchor.OUTSIDE12, TextAnchor.BASELINE_LEFT)); renderer .setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator()); renderer.setBaseItemLabelFont(new Font("Dialog", 1, 14)); // 设置提示折点数据形状 plot.setRenderer(renderer); HttpSession session = request.getSession(); String filename = ServletUtilities.saveChartAsPNG(chart, 750, 600, null, session); String graphURL = request.getContextPath() + "/DisplayChart?filename=" + filename; ActionContext.getContext().put("graphURL", graphURL); return "icon"; }
/*************************** * 按星期统计交易统计 * * @throws IllegalAccessException * @throws IllegalArgumentException * * **************************/ private static final int FIRST_DAY = Calendar.MONDAY; public DefaultCategoryDataset createDatasetByWeek() throws ParseException, IllegalArgumentException, IllegalAccessException { DefaultCategoryDataset linedatasetByWeekByWeek = new DefaultCategoryDataset(); // 曲线名称 String series = "市场交易统计"; // series指的就是报表里的那条数据线 // 横轴名称(列名称) String[] time = new String[7]; Calendar calendar = Calendar.getInstance(); SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); while (calendar.get(Calendar.DAY_OF_WEEK) != FIRST_DAY) { calendar.add(Calendar.DATE, -1); } for (int i = 0; i < 7; i++) { time[i] = dateFormat.format(calendar.getTime()).toString(); calendar.add(Calendar.DATE, 1); } List purchaseinfonList = new ArrayList(); purchaseinfonList = tenant_purchaseinfoService.findResultListByWeek( "%Y-%m-%d", time[0], time[6]); int listlen = purchaseinfonList.size(); // 从数据库中查询出来的数据长度 int weeklen = time.length; // 月份的长度 // list 中的某条数据放到数组中 double[] arrList = new double[time.length]; String[] weekdate = new String[7]; // 取出星期 for (int i = 0; i < weeklen; i++) { if (listlen < i + 1) { weekdate[i] = ""; } else { Map map = (Map) purchaseinfonList.get(i); weekdate[i] = map.get("date").toString(); } } // 取出总数 for (int j = 0; j < listlen; j++) { for (int i = 0; i < weeklen; i++) { if (weekdate[j].equals(time[i])) { Map map = (Map) purchaseinfonList.get(j); arrList[i] = (Double) map.get("purchaseCount"); } } } // 添加数据值 for (int i = 0; i < time.length; i++) { linedatasetByWeekByWeek.addValue(arrList[i], series, // 值 time[i]); // 对应的横轴 } return linedatasetByWeekByWeek; }
按月统计的数据绑定:
/*** * 按月统计 * @return */ @SuppressWarnings("null") public DefaultCategoryDataset createDatasetByMonth() { DefaultCategoryDataset linedatasetByMonth = new DefaultCategoryDataset(); // 曲线名称 String series = "市场交易统计"; // series指的就是报表里的那条数据线 /********************************* 获取一个月的日期 ****************************************/ // 获取当前年份 Calendar cal = Calendar.getInstance(); // 横轴名称(列名称) int monthInt = cal.get(Calendar.MONTH) + 1; // 获取当前月份 // 横轴名称(列名称) String monthString = String.valueOf(monthInt); int yearString = cal.get(Calendar.YEAR); // 获取当前年份 String[] time = null; String[] monthdatestrStrings = null; String[] timeValue = { "01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31" }; switch (monthInt) { case 1: case 3: case 5: case 7: case 8: case 10: case 12: time = new String[31]; monthdatestrStrings = new String[31]; for (int i = 0; i < 31; i++) { time[i] = timeValue[i]; monthdatestrStrings[i] = timeValue[i]; } break; // 对于2月份需要判断是否为闰年 case 2: if ((yearString % 4 == 0 && yearString % 100 != 0) || (yearString % 400 == 0)) { time = new String[29]; monthdatestrStrings = new String[29]; for (int i = 0; i < 29; i++) { time[i] = timeValue[i]; monthdatestrStrings[i] = timeValue[i]; } break; } else { time = new String[28]; monthdatestrStrings = new String[28]; for (int i = 0; i < 28; i++) { time[i] = timeValue[i]; monthdatestrStrings[i] = timeValue[i]; } break; } case 4: case 6: case 9: case 11: time = new String[30]; monthdatestrStrings = new String[30]; for (int i = 0; i < 30; i++) { time[i] = timeValue[i]; monthdatestrStrings[i] = timeValue[i]; } break; } /*********************************** 获取一个月的日期结束 **************************************/ // 年度交易情况 List purchaseinfonList = new ArrayList(); purchaseinfonList = tenant_purchaseinfoService .findResultListByYearOrMonth("%d", "%m", monthString); int listlen = purchaseinfonList.size(); // 从数据库中查询出来的数据长度 int monthlen = time.length; // 月份的长度 int Dvalue = monthlen - listlen; // list 中的某条数据放到数组中 double[] arrList = new double[time.length]; String[] monthdate = new String[monthdatestrStrings.length]; // 取出日期 for (int i = 0; i < monthlen; i++) { if (listlen < i + 1) { monthdate[i] = ""; } else { Map map = (Map) purchaseinfonList.get(i); monthdate[i] = map.get("month").toString(); } } // 取出总数 for (int j = 0; j < listlen; j++) { for (int i = 0; i < monthlen; i++) { if (monthdate[j].equals(time[i])) { Map map = (Map) purchaseinfonList.get(j); arrList[i] = (Double)map.get("purchaseCount"); } } } // 添加数据值 for (int i = 0; i < time.length; i++) { linedatasetByMonth.addValue(arrList[i], series, // 值 monthdatestrStrings[i]); // 对应的横轴 } return linedatasetByMonth; }
按季度统计的数据绑定:
/** * 按季度统计 * @return */ @SuppressWarnings("null") public DefaultCategoryDataset createDatasetByQuarter() { DefaultCategoryDataset linedatasetByQuarter = new DefaultCategoryDataset(); // 曲线名称 String series = "市场交易统计"; // series指的就是报表里的那条数据线 /*********************************获取一个月的日期****************************************/ //获取当前年份 Calendar cal = Calendar.getInstance(); // 横轴名称(列名称) int monthInt=cal.get(Calendar.MONTH)+1; //获取当前月份 // 横轴名称(列名称) String monthString=String.valueOf(monthInt); String yearString=String.valueOf(cal.get(Calendar.YEAR)); //获取当前年份 String[] time = new String[3]; String[] monthdatestrStrings =new String[3]; switch (monthInt) { case 1: case 2: case 3: String[] timeValue = { "1", "2", "3" }; for (int i = 0; i < timeValue.length; i++) { time[i] = timeValue[i]; monthdatestrStrings[i] =monthString+"-"+timeValue[i]; } break; //对于2月份需要判断是否为闰年 case 4: case 5: case 6: String[] timeValue1 = { "4", "5", "6" }; for (int i = 0; i < timeValue1.length; i++) { time[i] = timeValue1[i]; monthdatestrStrings[i] =yearString+"-"+timeValue1[i]; } break; case 7: case 8: case 9: String[] timeValue2 = { "7", "8", "9" }; for (int i = 0; i < timeValue2.length; i++) { time[i] = timeValue2[i]; monthdatestrStrings[i] =yearString+"-"+timeValue2[i]; } break; case 10: case 11: case 12: String[] timeValue3 = { "10", "11", "12" }; for (int i = 0; i < timeValue3.length; i++) { time[i] = timeValue3[i]; monthdatestrStrings[i] =yearString+"-"+timeValue3[i]; } break; } /***********************************获取一个月的日期结束**************************************/ // 年度交易情况 List purchaseinfonList = new ArrayList(); purchaseinfonList = tenant_purchaseinfoService.findResultListByQuarter(yearString,time[0],time[2] ); int listlen = purchaseinfonList.size(); // 从数据库中查询出来的数据长度 int monthlen = time.length; // 月份的长度 int Dvalue = monthlen - listlen; //list 中的某条数据放到数组中 double[] arrList=new double[time.length]; String[] monthdate =new String[monthdatestrStrings.length]; // 取出日期 for (int i = 0; i < monthlen; i++) { if (listlen < i + 1) { monthdate[i] = ""; } else { Map map = (Map) purchaseinfonList.get(i); monthdate[i] = map.get("month").toString(); } } // 取出总数 for (int j = 0; j < listlen; j++) { for (int i = 0; i < monthlen; i++) { if (monthdate[j].equals(time[i])) { Map map = (Map) purchaseinfonList.get(j); arrList[i] = (Double)map.get("purchaseCount"); } } } // 添加数据值 for (int i = 0; i < time.length; i++) { linedatasetByQuarter.addValue(arrList[i], series, // 值 monthdatestrStrings[i]); // 对应的横轴 } return linedatasetByQuarter; }
按年统计的数据绑定:
/** * 按年统计 * @return */ @SuppressWarnings("null") public DefaultCategoryDataset createDatasetByYear() { DefaultCategoryDataset linedatasetByYear = new DefaultCategoryDataset(); // 曲线名称 String series = "市场交易统计"; // series指的就是报表里的那条数据线 String[] time = new String[12]; String[] month = new String[12]; //获取当前年份 Calendar cal = Calendar.getInstance(); // 横轴名称(列名称) String year=String.valueOf(cal.get(Calendar.YEAR)); System.out.println(cal.get(Calendar.YEAR)); System.out.println(year); //设置1年12个月,为从数据库中取出每个月对应的统计数据 String[] timeValue = { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12" }; for (int i = 0; i < timeValue.length; i++) { time[i] = timeValue[i]; month[i] =year+"-"+timeValue[i]; } // 年度交易情况 List purchaseinfonList = new ArrayList(); purchaseinfonList = tenant_purchaseinfoService.findResultListByYearOrMonth("%c","%Y", year); int listlen = purchaseinfonList.size(); // 从数据库中查询出来的数据长度 int monthlen = time.length; // 月份的长度 int Dvalue = monthlen - listlen; //list 中的某条数据放到数组中 double[] arrList=new double[time.length]; String[] monthdate = new String[12]; // 取出月份 for (int i = 0; i < monthlen; i++) { if (listlen < i + 1) { monthdate[i] = ""; } else { Map map = (Map) purchaseinfonList.get(i); monthdate[i] = map.get("month").toString(); } } // 取出总数 for (int j = 0; j < listlen; j++) { for (int i = 0; i < monthlen; i++) { if (monthdate[j].equals(time[i])) { Map map = (Map) purchaseinfonList.get(j); arrList[i] = (Double)map.get("purchaseCount"); } } } // 添加数据值 for (int i = 0; i < time.length; i++) { linedatasetByYear.addValue(arrList[i], series, // 值 month[i]); // 对应的横轴 } return linedatasetByYear; }