posts - 18,  comments - 0,  trackbacks - 0

          import java.awt.Color;
          import java.io.IOException;
          import java.text.SimpleDateFormat;

          import javax.servlet.ServletRequest;
          import javax.servlet.ServletResponse;

          import org.jfree.chart.ChartFactory;
          import org.jfree.chart.ChartUtilities;
          import org.jfree.chart.JFreeChart;
          import org.jfree.chart.axis.DateAxis;
          import org.jfree.chart.axis.DateTickUnit;
          import org.jfree.chart.plot.PlotOrientation;
          import org.jfree.chart.plot.XYPlot;
          import org.jfree.data.time.Day;
          import org.jfree.data.time.Hour;
          import org.jfree.data.time.Month;
          import org.jfree.data.time.TimeSeries;
          import org.jfree.data.time.TimeSeriesCollection;
          import org.jfree.data.xy.XYDataset;
          import org.jfree.ui.RectangleInsets;

          /**
           * 用于將統(tǒng)計(jì)結(jié)果顯示成走勢(shì)圖
           * @author lord
           * 調(diào)用方法:
             TrendChart trendChart = new TrendChart();
             trendChart.chartTitle = "走勢(shì)圖";
             trendChart.chartSeriesDesc = "走勢(shì)曲線";
             trendChart.chartXdesc = "時(shí)間";
             trendChart.chartYdesc = "價(jià)格";
             trendChart.graphHigh = 400;
             trendChart.graphWidth = 600;
             trendChart.timeFormat = "yyyy-MM";
             trendChart.periodType = TrendChart.DAY;
             ......
             trendChart.addTimeSeriesUnitData(mYear, mMonth, mDay, mHour, (int)rData);
             trendChart.createTrendGraphByServlet(request, response);
           */

          public class TrendChart {
            public final static String MONTH = "MONTH";
            public final static String DAY = "DAY";
            public final static String HOUR = "HOUR";
           
            private JFreeChart rChart = null;     //圖表對(duì)象
            public String chartTitle = "";        //圖表標(biāo)題
            public String chartXdesc = "";        //X軸標(biāo)題
            public String chartYdesc = "";        //Y軸標(biāo)題
            public String chartSeriesDesc = "";   //曲線說(shuō)明
            public int graphWidth = 600;          //默認(rèn)寬度
            public int graphHigh = 400;           //默認(rèn)高度
            public String timeFormat = "MM/yyyy"; // 按日:MM-dd ,按小時(shí):hh:mm

            // 用于標(biāo)志用戶選擇的是按哪種查詢統(tǒng)計(jì)周期類型(年、月、天、小時(shí)).
            // 年:YEAR, 月:MONTH, 天:DAY, 小時(shí):HOUR
            public String periodType = "";

            // 用于確定時(shí)間間隔
            public int dateInterval = 0;
           
            //統(tǒng)計(jì)結(jié)果數(shù)據(jù)集
            TimeSeriesCollection statDataset = new TimeSeriesCollection();
           
            TimeSeries monthSeries = null;  //月份統(tǒng)計(jì)圖數(shù)據(jù)集合
            TimeSeries daySeries = null;    //天數(shù)統(tǒng)計(jì)圖數(shù)據(jù)集合
            TimeSeries hourSeries = null;   //小時(shí)統(tǒng)計(jì)圖數(shù)據(jù)集合
            
            
            /**
             * 創(chuàng)建Servlet方式走勢(shì)圖表
             * @param req
             * @param res
             * @throws IOException
             */
            public void createTrendGraphByServlet(ServletRequest req, ServletResponse res) throws IOException {
              res.setContentType("image/jpeg");
              setTimeSeriesStatType();
              rChart = createTrendChart();
              ChartUtilities.writeChartAsJPEG(res.getOutputStream(), 1, rChart, graphWidth, graphHigh, null);
            }
           
            /**
             * 創(chuàng)建趨勢(shì)圖表
             * @return JFreeChart 圖表對(duì)象JFreeChart
             */
            private JFreeChart createTrendChart(){
              JFreeChart _freeChart = ChartFactory.createTimeSeriesChart(chartTitle, chartXdesc, chartYdesc,
                  getTimeSeriesStatDataSet(), true, false, false);
              _freeChart.setBackgroundPaint(Color.white);
             
              XYPlot _xyplot = _freeChart.getXYPlot();
              _xyplot.setOrientation(PlotOrientation.VERTICAL);
              _xyplot.setBackgroundPaint(Color.lightGray);
              _xyplot.setDomainGridlinePaint(Color.white);
              _xyplot.setRangeGridlinePaint(Color.white);
              _xyplot.setAxisOffset(new RectangleInsets(1.0, 2.0, 2.0, 10.0));
             
              DateAxis dateaxis = (DateAxis) _xyplot.getDomainAxis();
              if (periodType.equalsIgnoreCase("MONTH")){
                if (dateInterval > 0) {
                  dateaxis.setTickUnit(new DateTickUnit(DateTickUnit.MONTH, dateInterval));
                }
              }else if (periodType.equalsIgnoreCase("DAY")){
                if (dateInterval > 0) {
                  dateaxis.setTickUnit(new DateTickUnit(DateTickUnit.DAY, dateInterval));
                }
              }else if (periodType.equalsIgnoreCase("HOUR")){
                if (dateInterval > 0) {
                  dateaxis.setTickUnit(new DateTickUnit(DateTickUnit.HOUR, dateInterval));
                }
              }
              dateaxis.setDateFormatOverride(new SimpleDateFormat(timeFormat));
              return _freeChart;
            }
           
            /**
             * 增加走勢(shì)圖數(shù)據(jù)
             * @param periodType 區(qū)間類型
             * @param year  年份
             * @param month 月份
             * @param day   日期
             * @param hour  時(shí)間
             * @param statData 統(tǒng)計(jì)數(shù)據(jù)
             */
            public void addTimeSeriesUnitData(int year, int month, int day, int hour, int statData) {
              if (periodType.equalsIgnoreCase("MONTH")){
                if (monthSeries == null){
                  monthSeries = new TimeSeries(chartSeriesDesc, Month.class);
                }
                monthSeries.add(new Month(month, year), statData);
              }else if (periodType.equalsIgnoreCase("DAY")){
                if (daySeries == null){
                  daySeries = new TimeSeries(chartSeriesDesc, Day.class);
                }
                daySeries.add(new Day(day, month, year), statData);
              }else if (periodType.equalsIgnoreCase("HOUR")){
                if (hourSeries == null){
                  hourSeries = new TimeSeries(chartSeriesDesc, Hour.class);
                }
                hourSeries.add(new Hour(hour, day, month, year), statData);
              }
            }
           
            /**
             * 設(shè)置走勢(shì)圖統(tǒng)計(jì)的區(qū)間類型
             * @param periodType 區(qū)間類型
             */
            private void setTimeSeriesStatType() {
              if (periodType.equalsIgnoreCase("MONTH")){
                statDataset.addSeries(monthSeries);
              }else if (periodType.equalsIgnoreCase("DAY")){
                statDataset.addSeries(daySeries);
              }else if (periodType.equalsIgnoreCase("HOUR")){
                statDataset.addSeries(hourSeries);
              }
            }
           
            /**
             * 獲得時(shí)序圖的統(tǒng)計(jì)數(shù)據(jù)
             * @return XYDataset 統(tǒng)計(jì)數(shù)據(jù)
             */
            private XYDataset getTimeSeriesStatDataSet() {
              statDataset.setDomainIsPointsInTime(true);
              return statDataset;
            }

            public int getDateInterval() {
              return dateInterval;
            }

            public void setDateInterval(int dateInterval) {
              this.dateInterval = dateInterval;
            }


          JSP調(diào)用代碼如下:
           TrendChart trendChart = new TrendChart();
           trendChart.chartTitle = "一年走勢(shì)圖";
           trendChart.chartSeriesDesc = "走勢(shì)曲線";
           trendChart.chartXdesc = "時(shí)間";
           trendChart.chartYdesc = "價(jià)格";
           trendChart.graphHigh = 400;
           trendChart.graphWidth = 600;
           trendChart.timeFormat = "yyyy-MM";
           trendChart.periodType = TrendChart.DAY;
           
           double baseData = 100.0;
           double rData = baseData;
           java.util.Calendar calendar = java.util.Calendar.getInstance();
           calendar.set(2007, 0, 0);
           for (int i = 1; i <= 365; i++){
            rData = rData * (1 + (Math.random() - 0.495) / 10.0);
            calendar.add(java.util.Calendar.DAY_OF_MONTH, 1);
            System.out.println(calendar.get(java.util.Calendar.YEAR) + "||" +
             (calendar.get(java.util.Calendar.MONTH) + 1) + "||" + calendar.get(java.util.Calendar.DAY_OF_MONTH) + "||" +
             (calendar.get(java.util.Calendar.HOUR_OF_DAY) + "||" + calendar.get(java.util.Calendar.MINUTE))
            );
            int mYear = calendar.get(java.util.Calendar.YEAR);
            int mMonth = calendar.get(java.util.Calendar.MONTH) + 1;
            int mDay = calendar.get(java.util.Calendar.DAY_OF_MONTH);
            int mHour = calendar.get(java.util.Calendar.HOUR_OF_DAY);
            trendChart.addTimeSeriesUnitData(mYear, mMonth, mDay, mHour, (int)rData);
           }
          trendChart.createTrendGraphByServlet(request, response);

          顯示圖結(jié)果如下:













          posted on 2007-05-10 16:29 LORD BLOG 閱讀(4206) 評(píng)論(0)  編輯  收藏 所屬分類: JAVA

          只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 明光市| 富平县| 化隆| 揭西县| 集贤县| 景东| 青岛市| 天峻县| 泗水县| 乐昌市| 高台县| 西吉县| 疏勒县| 华池县| 汉阴县| 罗甸县| 兴安县| 胶州市| 林口县| 抚宁县| 延庆县| 广汉市| 丰都县| 沾化县| 潍坊市| 正宁县| 渑池县| 龙胜| 册亨县| 建湖县| 静乐县| 成都市| 科技| 福建省| 玛纳斯县| 和顺县| 崇仁县| 大新县| 丽江市| 江达县| 儋州市|