jfreechart采用TimeSeriesChart并更改热点内容

来源:互联网 发布:去杭州淘宝大学 编辑:程序博客网 时间:2024/06/10 00:25
[java] view plain copy
  1. /** 
  2.  *  
  3.  */  
  4. package com.huaxia.bank.test;  
  5.   
  6. import java.awt.Color;  
  7. import java.awt.Dimension;  
  8. import java.awt.Font;  
  9. import java.awt.Toolkit;  
  10. import java.awt.event.WindowAdapter;  
  11. import java.awt.event.WindowEvent;  
  12. import java.text.DateFormat;  
  13. import java.text.DecimalFormat;  
  14. import java.text.NumberFormat;  
  15. import java.text.SimpleDateFormat;  
  16.   
  17. import org.jfree.chart.ChartFactory;  
  18. import org.jfree.chart.ChartFrame;  
  19. import org.jfree.chart.JFreeChart;  
  20. import org.jfree.chart.axis.DateAxis;  
  21. import org.jfree.chart.axis.DateTickUnit;  
  22. import org.jfree.chart.axis.ValueAxis;  
  23. import org.jfree.chart.labels.StandardCategoryToolTipGenerator;  
  24. import org.jfree.chart.labels.StandardXYToolTipGenerator;  
  25. import org.jfree.chart.labels.XYToolTipGenerator;  
  26. import org.jfree.chart.plot.PlotOrientation;  
  27. import org.jfree.chart.plot.XYPlot;  
  28. import org.jfree.chart.renderer.xy.StandardXYItemRenderer;  
  29. import org.jfree.chart.renderer.xy.XYItemRenderer;  
  30. import org.jfree.chart.title.TextTitle;  
  31. import org.jfree.data.time.Day;  
  32. import org.jfree.data.time.Hour;  
  33. import org.jfree.data.time.Month;  
  34. import org.jfree.data.time.TimeSeries;  
  35. import org.jfree.data.time.TimeSeriesCollection;  
  36. import org.jfree.data.xy.XYDataset;  
  37. import org.jfree.ui.ApplicationFrame;  
  38. import org.jfree.ui.RectangleInsets;  
  39.   
  40. /** 
  41.  * @author cuiran 
  42.  * 
  43.  */  
  44. public class TimeSeriesTest  {  
  45.   
  46.       public final static String MONTH = "MONTH";  
  47.       public final static String DAY = "DAY";  
  48.       public final static String HOUR = "HOUR";  
  49.         
  50.       private JFreeChart rChart = null;     //图表对象  
  51.       public String chartTitle = "";        //图表标题  
  52.       public String chartXdesc = "";        //X轴标题  
  53.       public String chartYdesc = "";        //Y轴标题  
  54.       public String chartSeriesDesc = "";   //曲线说明  
  55.       public String chartSeriesDesc1 = "";   //曲线说明  
  56.       public int graphWidth = 600;          //默认宽度  
  57.       public int graphHigh = 400;           //默认高度  
  58.       public String timeFormat = "MM/yyyy"// 按日:MM-dd ,按小时:hh:mm  
  59.   
  60.       // 用于标志用户选择的是按哪种查询统计周期类型(年、月、天、小时).  
  61.       // 年:YEAR, 月:MONTH, 天:DAY, 小时:HOUR  
  62.       public String periodType = "";  
  63.   
  64.       // 用于确定时间间隔  
  65.       public int dateInterval = 1;  
  66.         
  67.       //统计结果数据集  
  68.       TimeSeriesCollection statDataset = new TimeSeriesCollection();  
  69.         
  70.       TimeSeries monthSeries = null;  //月份统计图数据集合  
  71.       TimeSeries monthSeries1 = null;  //月份统计图数据集合  
  72.       TimeSeries daySeries = null;    //天数统计图数据集合  
  73.       TimeSeries hourSeries = null;   //小时统计图数据集合  
  74.         
  75.         
  76.       
  77.       public void createTread(){  
  78.             setTimeSeriesStatType();  
  79.       }  
  80.        
  81.       /** 
  82.        * 创建趋势图表 
  83.        * @return JFreeChart 图表对象JFreeChart 
  84.        */  
  85.       private JFreeChart createTrendChart(){  
  86.         JFreeChart _freeChart = ChartFactory.createTimeSeriesChart(chartTitle, chartXdesc, chartYdesc,   
  87.             getTimeSeriesStatDataSet(), truetruetrue);  
  88.         _freeChart.setBackgroundPaint(Color.white);  
  89.           
  90.         XYPlot _xyplot = _freeChart.getXYPlot();  
  91.           
  92.         _xyplot.setOrientation(PlotOrientation.VERTICAL);  
  93.           
  94.         _xyplot.setBackgroundPaint(Color.lightGray);  
  95.         _xyplot.setDomainGridlinePaint(Color.white);  
  96.         _xyplot.setRangeGridlinePaint(Color.white);  
  97.          
  98.         _xyplot.setAxisOffset(new RectangleInsets(1.02.02.010.0));  
  99.        XYItemRenderer renderer= _xyplot.getRenderer();  
  100.   
  101.        DateFormat df=new SimpleDateFormat("yyyy年MM月");  
  102.        NumberFormat nf= NumberFormat.getNumberInstance();  
  103.        StandardXYToolTipGenerator toolg=new StandardXYToolTipGenerator(StandardXYToolTipGenerator.DEFAULT_TOOL_TIP_FORMAT,df,nf);  
  104.          
  105.        renderer.setToolTipGenerator(toolg);  
  106. //       
  107.          
  108.         DateAxis dateaxis = (DateAxis) _xyplot.getDomainAxis();  
  109.         if (periodType.equalsIgnoreCase("MONTH")){  
  110.           if (dateInterval > 0) {  
  111.             dateaxis.setTickUnit(new DateTickUnit(DateTickUnit.MONTH, dateInterval));  
  112.           }  
  113.         }else if (periodType.equalsIgnoreCase("DAY")){  
  114.           if (dateInterval > 0) {  
  115.             dateaxis.setTickUnit(new DateTickUnit(DateTickUnit.DAY, dateInterval));  
  116.           }  
  117.         }else if (periodType.equalsIgnoreCase("HOUR")){  
  118.           if (dateInterval > 0) {  
  119.             dateaxis.setTickUnit(new DateTickUnit(DateTickUnit.HOUR, dateInterval));  
  120.           }  
  121.         }  
  122.           
  123.         dateaxis.setDateFormatOverride(new SimpleDateFormat(timeFormat));  
  124.           
  125.         return _freeChart;  
  126.       }  
  127.         
  128.       /** 
  129.        * 增加走势图数据 
  130.        * @param periodType 区间类型 
  131.        * @param year  年份 
  132.        * @param month 月份 
  133.        * @param day   日期 
  134.        * @param hour  时间 
  135.        * @param statData 统计数据 
  136.        */  
  137.       public void addTimeSeriesUnitData(int year, int month,int statData) {  
  138.         if (periodType.equalsIgnoreCase("MONTH")){  
  139.           if (monthSeries == null){  
  140.             monthSeries = new TimeSeries(chartSeriesDesc,Month.class);  
  141.           }  
  142.           monthSeries.add(new Month(month, year), statData);  
  143.   
  144.           System.out.println("月");  
  145.         }else if (periodType.equalsIgnoreCase("DAY")){  
  146.           if (daySeries == null){  
  147.             daySeries = new TimeSeries(chartSeriesDesc, Day.class);  
  148.           }  
  149. //        daySeries.add(new Day(day, month, year), statData);  
  150.         }else if (periodType.equalsIgnoreCase("HOUR")){  
  151.           if (hourSeries == null){  
  152.             hourSeries = new TimeSeries(chartSeriesDesc, Hour.class);  
  153.           }  
  154. //        hourSeries.add(new Hour(hour, day, month, year), statData);  
  155.         }  
  156.       }  
  157.       /** 
  158.        * 增加走势图数据 
  159.        * @param periodType 区间类型 
  160.        * @param year  年份 
  161.        * @param month 月份 
  162.        * @param day   日期 
  163.        * @param hour  时间 
  164.        * @param statData 统计数据 
  165.        */  
  166.       public void addTimeSeriesUnitDataAll(int year, int month,int statData) {  
  167.         if (periodType.equalsIgnoreCase("MONTH")){  
  168.           if (monthSeries1 == null){  
  169.               monthSeries1 = new TimeSeries(chartSeriesDesc1,Month.class);  
  170.           }  
  171.           monthSeries1.add(new Month(month, year), statData);  
  172.   
  173.           System.out.println("月");  
  174.         }else if (periodType.equalsIgnoreCase("DAY")){  
  175.           if (daySeries == null){  
  176.             daySeries = new TimeSeries(chartSeriesDesc1, Day.class);  
  177.           }  
  178. //        daySeries.add(new Day(day, month, year), statData);  
  179.         }else if (periodType.equalsIgnoreCase("HOUR")){  
  180.           if (hourSeries == null){  
  181.             hourSeries = new TimeSeries(chartSeriesDesc1, Hour.class);  
  182.           }  
  183. //        hourSeries.add(new Hour(hour, day, month, year), statData);  
  184.         }  
  185.       }  
  186.       /** 
  187.        * 设置走势图统计的区间类型 
  188.        * @param periodType 区间类型 
  189.        */  
  190.       private void setTimeSeriesStatType() {  
  191.         if (periodType.equalsIgnoreCase("MONTH")){  
  192.           statDataset.addSeries(monthSeries);  
  193.           statDataset.addSeries(monthSeries1);  
  194.         }else if (periodType.equalsIgnoreCase("DAY")){  
  195.           statDataset.addSeries(daySeries);  
  196.         }else if (periodType.equalsIgnoreCase("HOUR")){  
  197.           statDataset.addSeries(hourSeries);  
  198.         }  
  199.       }  
  200.         
  201.       /** 
  202.        * 获得时序图的统计数据 
  203.        * @return XYDataset 统计数据 
  204.        */  
  205.       private XYDataset getTimeSeriesStatDataSet() {  
  206.         statDataset.setDomainIsPointsInTime(true);  
  207.         return statDataset;  
  208.       }  
  209.   
  210.       public int getDateInterval() {  
  211.         return dateInterval;  
  212.       }  
  213.        //字体配置方法(解决中文问题)  
  214.         private static void configFont(JFreeChart chart) {  
  215.             // 配置字体  
  216.             Font xfont = new Font("宋体", Font.PLAIN, 12);// X轴  
  217.             Font yfont = new Font("宋体", Font.PLAIN, 12);// Y轴  
  218.             Font kfont = new Font("宋体", Font.PLAIN, 12);// 底部  
  219.             Font titleFont = new Font("宋体", Font.BOLD, 25); // 图片标题  
  220.             XYPlot plot =  chart.getXYPlot();// 图形的绘制结构对象  
  221.              
  222.             // 图片标题  
  223.             chart.setTitle(new TextTitle(chart.getTitle().getText(), titleFont));  
  224.   
  225.             // 底部  
  226. //          chart.getLegend().setItemFont(kfont);  
  227.          // 横轴框里的标题字体  
  228.             chart.getLegend().setItemFont(kfont);  
  229.             // 横轴列表字体  
  230.             plot.getDomainAxis().setTickLabelFont(kfont);  
  231.             // 横轴小标题字体  
  232.             plot.getDomainAxis().setLabelFont(kfont);  
  233.   
  234.             // Y 轴  
  235.             ValueAxis rangeAxis = plot.getRangeAxis();  
  236.             rangeAxis.setLabelFont(yfont);  
  237.             rangeAxis.setLabelPaint(Color.BLUE); // 字体颜色  
  238.             rangeAxis.setTickLabelFont(yfont);  
  239.   
  240.         }  
  241.       public void setDateInterval(int dateInterval) {  
  242.         this.dateInterval = dateInterval;  
  243.       }  
  244.       public static void main(String arhs[]){  
  245.           TimeSeriesTest trendChart = new TimeSeriesTest();  
  246.           trendChart.chartTitle = "一年走势图";  
  247.           trendChart.chartSeriesDesc = "确认数量";  
  248.           trendChart.chartSeriesDesc1 = "风险数量";  
  249.           trendChart.chartXdesc = "月份";  
  250.           trendChart.chartYdesc = "数量";  
  251.           trendChart.graphHigh = 400;  
  252.           trendChart.graphWidth = 600;  
  253.           trendChart.timeFormat = "yyyy年MM月";  
  254.           trendChart.periodType = TimeSeriesTest.MONTH;  
  255.             
  256.           double baseData = 100.0;  
  257.           double rData = baseData;  
  258.             
  259.           trendChart.addTimeSeriesUnitData(201111,25);  
  260.           trendChart.addTimeSeriesUnitData(201112,45);  
  261.           trendChart.addTimeSeriesUnitData(2012150);  
  262.           trendChart.addTimeSeriesUnitData(20122,  80);  
  263.           trendChart.addTimeSeriesUnitData(2012330);  
  264.           trendChart.addTimeSeriesUnitData(20124,  10);  
  265.             
  266.           trendChart.addTimeSeriesUnitDataAll(201111,45);  
  267.           trendChart.addTimeSeriesUnitDataAll(201112,65);  
  268.           trendChart.addTimeSeriesUnitDataAll(2012170);  
  269.           trendChart.addTimeSeriesUnitDataAll(20122,  90);  
  270.           trendChart.addTimeSeriesUnitDataAll(2012350);  
  271.           trendChart.addTimeSeriesUnitDataAll(2012440);  
  272.             
  273.             
  274.           trendChart.createTread();  
  275.           JFreeChart chart =trendChart.createTrendChart();  
  276.           trendChart.configFont(chart);  
  277.           final ChartFrame preview = new ChartFrame("一年走势图",chart);  
  278.             preview.addWindowListener(new WindowAdapter() {  
  279.   
  280.                  public void windowClosing(final WindowEvent event) {  
  281.   
  282.                    preview.dispose();  
  283.   
  284.                  }  
  285.   
  286.                });  
  287.   
  288.                preview.pack();  
  289.   
  290.                //调整预览窗口的大小和位置,适合屏幕,并且居中  
  291.   
  292. //             Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();  
  293. //  
  294. //             preview.setSize(screenSize.width,screenSize.height-50);//适合屏幕,50表示把工具栏要考虑在内  
  295. //  
  296. //             Dimension frameSize = preview.getSize();  
  297. //  
  298. //             if (frameSize.height > screenSize.height) {  
  299. //  
  300. //               frameSize.height = screenSize.height;  
  301. //  
  302. //             }  
  303. //  
  304. //             if (frameSize.width > screenSize.width) {  
  305. //  
  306. //               frameSize.width = screenSize.width;  
  307. //  
  308. //             }  
  309. //  
  310. //             preview.setLocation( (screenSize.width - frameSize.width) / 2,  
  311. //  
  312. //                        (screenSize.height - frameSize.height-50) / 2);  
  313.   
  314.   
  315.   
  316.                //显示报表预览窗口  
  317.   
  318.                preview.setVisible(true);  
  319.       }  
  320.   
  321. }  


运行效果如图:

 

部分代码:

[java] view plain copy
  1. DateFormat df=new SimpleDateFormat("yyyy年MM月");  
  2.    NumberFormat nf= NumberFormat.getNumberInstance();  
  3.    StandardXYToolTipGenerator toolg=new StandardXYToolTipGenerator(StandardXYToolTipGenerator.DEFAULT_TOOL_TIP_FORMAT,df,nf);  
  4.      
  5.    renderer.setToolTipGenerator(toolg);  

 

加上代码可以显示对应的图点

[java] view plain copy
  1. XYLineAndShapeRenderer xylineandshaperenderer = (XYLineAndShapeRenderer)_xyplot.getRenderer();  
  2. //设置网格背景颜色  
  3.   _xyplot.setBackgroundPaint(Color.white);  
  4. //设置网格竖线颜色  
  5.   _xyplot.setDomainGridlinePaint(Color.pink);  
  6. //设置网格横线颜色  
  7.   _xyplot.setRangeGridlinePaint(Color.pink);  
  8. //设置曲线图与xy轴的距离  
  9.   _xyplot.setAxisOffset(new RectangleInsets(0D, 0D, 0D, 10D));  
  10. //设置曲线是否显示数据点  
  11. xylineandshaperenderer.setBaseShapesVisible(true);  
  12. //设置曲线显示各数据点的值  


原创粉丝点击