JFreeChart最佳實(shí)踐:時(shí)序圖
作者:zhangzi
本文將介紹作者通過(guò)Java最佳圖形解決方案JFreeChart實(shí)現(xiàn)時(shí)序圖的詳細(xì)過(guò)程。
1、準(zhǔn)備Jar包:jfreechart-1.0.13.jar和jcommon-1.0.16.jar
2、配置web.xml
- <!-- JFreeChart -->
- <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>/DisplayChart</url-pattern>
- </servlet-mapping>
3、Action(Struts2)
- /**
- * 曲線圖
- * */
- @SuppressWarnings("deprecation")
- public String quxiantu() throws Exception {
- String plateNum = request.getParameter("plateNum");
- String startDateTimeStr = request.getParameter("startTime");
- String stopDateTimeStr = request.getParameter("stopTime");
- List<Object[]> dataSet = mileageStatService.getVehicleFuelByPlateNum(plateNum, startDateTimeStr, stopDateTimeStr);
- //訪問(wèn)量統(tǒng)計(jì)時(shí)間線
- TimeSeries timeSeries = new TimeSeries(plateNum+"油耗統(tǒng)計(jì)", Minute.class);
- //時(shí)間曲線數(shù)據(jù)集合
- TimeSeriesCollection lineDataset = new TimeSeriesCollection();
- //構(gòu)造數(shù)據(jù)集合
- for(Object[] obj : dataSet){
- timeSeries.addOrUpdate(new Minute(dateformat.parse(obj[0].toString())), Long.valueOf(obj[1].toString()));
- }
- lineDataset.addSeries(timeSeries);
- //**************************************************************************************
- //解決中文亂碼
- StandardChartTheme standardChartTheme = new StandardChartTheme("JFree"); //或者為L(zhǎng)egacy
- standardChartTheme.setLargeFont(new Font("宋體", Font.BOLD, 12));
- standardChartTheme.setRegularFont(new Font("宋體", Font.BOLD, 12));
- standardChartTheme.setExtraLargeFont(new Font("宋體", Font.BOLD, 12));
- standardChartTheme.setSmallFont(new Font("宋體", Font.BOLD, 12));
- ChartFactory.setChartTheme(standardChartTheme);
- //**************************************************************************************
- JFreeChart chart = ChartFactory.createTimeSeriesChart("油耗統(tǒng)計(jì)時(shí)間線", "時(shí)間", "油耗", lineDataset, true, true, true);
- //***********************************************************************************
- XYPlot plot = chart.getXYPlot();
- //設(shè)置網(wǎng)格背景顏色
- plot.setBackgroundPaint(Color.white);
- //設(shè)置網(wǎng)格豎線顏色
- plot.setDomainGridlinePaint(Color.LIGHT_GRAY);
- //設(shè)置網(wǎng)格橫線顏色
- plot.setRangeGridlinePaint(Color.LIGHT_GRAY);
- //設(shè)置曲線圖與xy軸的距離
- plot.setAxisOffset(new RectangleInsets(0D, 0D, 0D, 10D));
- //設(shè)置X軸(日期軸)
- DateAxis dateaxis = (DateAxis) plot.getDomainAxis();
- //時(shí)間軸間距是5分鐘,格式為小時(shí):分鐘
- dateaxis.setTickUnit(new DateTickUnit(DateTickUnit.MINUTE, 30));
- dateaxis.setDateFormatOverride(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
- //X軸日期垂直顯示(沒找到傾斜多少度顯示)
- dateaxis.setVerticalTickLabels(true);
- //***********************************************************************************
- //設(shè)置子標(biāo)題
- TextTitle subtitle = new TextTitle(startDateTimeStr+" 到 "+stopDateTimeStr, new Font("宋體", Font.PLAIN, 12));
- chart.addSubtitle(subtitle);
- //設(shè)置主標(biāo)題
- chart.setTitle(new TextTitle(plateNum+"油耗統(tǒng)計(jì)", new Font("黑體", Font.BOLD, 15)));
- chart.setAntiAlias(true);
- String filename = ServletUtilities.saveChartAsPNG(chart, 700, 500, null, request.getSession());
- String graphURL = request.getContextPath() + "/DisplayChart?filename=" + filename;
- request.setAttribute("graphURL", graphURL);
- request.setAttribute("filename", filename);
- return "quxiantu";
- }
4、JSP
- <img src="${graphURL}" width="550" height="350" border="0" usemap="#${filename}" >
附圖:
原文鏈接:http://zhangzi.iteye.com/blog/1130051
【編輯推薦】
責(zé)任編輯:林師授
來(lái)源:
zhangzi的博客