jfreechart采用TimeSeriesChart并更改热点内容



jfreechart采用TimeSeriesChart并更改热点内容

标签: jfreechartstringnull网格plot报表
4664人阅读 评论(1) 收藏 举报
分类:
 
  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. }  
/**
 * 
 */
package com.huaxia.bank.test;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Toolkit;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.text.DateFormat;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartFrame;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.DateAxis;
import org.jfree.chart.axis.DateTickUnit;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.labels.StandardCategoryToolTipGenerator;
import org.jfree.chart.labels.StandardXYToolTipGenerator;
import org.jfree.chart.labels.XYToolTipGenerator;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.StandardXYItemRenderer;
import org.jfree.chart.renderer.xy.XYItemRenderer;
import org.jfree.chart.title.TextTitle;
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.ApplicationFrame;
import org.jfree.ui.RectangleInsets;

/**
 * @author cuiran
 *
 */
public class TimeSeriesTest  {

	  public final static String MONTH = "MONTH";
	  public final static String DAY = "DAY";
	  public final static String HOUR = "HOUR";
	  
	  private JFreeChart rChart = null;     //图表对象
	  public String chartTitle = "";        //图表标题
	  public String chartXdesc = "";        //X轴标题
	  public String chartYdesc = "";        //Y轴标题
	  public String chartSeriesDesc = "";   //曲线说明
	  public String chartSeriesDesc1 = "";   //曲线说明
	  public int graphWidth = 600;          //默认宽度
	  public int graphHigh = 400;           //默认高度
	  public String timeFormat = "MM/yyyy"; // 按日:MM-dd ,按小时:hh:mm

	  // 用于标志用户选择的是按哪种查询统计周期类型(年、月、天、小时).
	  // 年:YEAR, 月:MONTH, 天:DAY, 小时:HOUR
	  public String periodType = "";

	  // 用于确定时间间隔
	  public int dateInterval = 1;
	  
	  //统计结果数据集
	  TimeSeriesCollection statDataset = new TimeSeriesCollection();
	  
	  TimeSeries monthSeries = null;  //月份统计图数据集合
	  TimeSeries monthSeries1 = null;  //月份统计图数据集合
	  TimeSeries daySeries = null;    //天数统计图数据集合
	  TimeSeries hourSeries = null;   //小时统计图数据集合
	  
	  
	
	  public void createTread(){
		    setTimeSeriesStatType();
	  }
	 
	  /**
	   * 创建趋势图表
	   * @return JFreeChart 图表对象JFreeChart
	   */
	  private JFreeChart createTrendChart(){
	    JFreeChart _freeChart = ChartFactory.createTimeSeriesChart(chartTitle, chartXdesc, chartYdesc, 
	        getTimeSeriesStatDataSet(), true, true, true);
	    _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));
	   XYItemRenderer renderer= _xyplot.getRenderer();

	   DateFormat df=new SimpleDateFormat("yyyy年MM月");
	   NumberFormat nf= NumberFormat.getNumberInstance();
	   StandardXYToolTipGenerator toolg=new StandardXYToolTipGenerator(StandardXYToolTipGenerator.DEFAULT_TOOL_TIP_FORMAT,df,nf);
	   
	   renderer.setToolTipGenerator(toolg);
//	   
	   
	    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;
	  }
	  
	  /**
	   * 增加走势图数据
	   * @param periodType 区间类型
	   * @param year  年份
	   * @param month 月份
	   * @param day   日期
	   * @param hour  时间
	   * @param statData 统计数据
	   */
	  public void addTimeSeriesUnitData(int year, int month,int statData) {
	    if (periodType.equalsIgnoreCase("MONTH")){
	      if (monthSeries == null){
	        monthSeries = new TimeSeries(chartSeriesDesc,Month.class);
	      }
	      monthSeries.add(new Month(month, year), statData);

	      System.out.println("月");
	    }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);
	    }
	  }
	  /**
	   * 增加走势图数据
	   * @param periodType 区间类型
	   * @param year  年份
	   * @param month 月份
	   * @param day   日期
	   * @param hour  时间
	   * @param statData 统计数据
	   */
	  public void addTimeSeriesUnitDataAll(int year, int month,int statData) {
	    if (periodType.equalsIgnoreCase("MONTH")){
	      if (monthSeries1 == null){
	    	  monthSeries1 = new TimeSeries(chartSeriesDesc1,Month.class);
	      }
	      monthSeries1.add(new Month(month, year), statData);

	      System.out.println("月");
	    }else if (periodType.equalsIgnoreCase("DAY")){
	      if (daySeries == null){
	        daySeries = new TimeSeries(chartSeriesDesc1, Day.class);
	      }
//	      daySeries.add(new Day(day, month, year), statData);
	    }else if (periodType.equalsIgnoreCase("HOUR")){
	      if (hourSeries == null){
	        hourSeries = new TimeSeries(chartSeriesDesc1, Hour.class);
	      }
//	      hourSeries.add(new Hour(hour, day, month, year), statData);
	    }
	  }
	  /**
	   * 设置走势图统计的区间类型
	   * @param periodType 区间类型
	   */
	  private void setTimeSeriesStatType() {
	    if (periodType.equalsIgnoreCase("MONTH")){
	      statDataset.addSeries(monthSeries);
	      statDataset.addSeries(monthSeries1);
	    }else if (periodType.equalsIgnoreCase("DAY")){
	      statDataset.addSeries(daySeries);
	    }else if (periodType.equalsIgnoreCase("HOUR")){
	      statDataset.addSeries(hourSeries);
	    }
	  }
	  
	  /**
	   * 获得时序图的统计数据
	   * @return XYDataset 统计数据
	   */
	  private XYDataset getTimeSeriesStatDataSet() {
	    statDataset.setDomainIsPointsInTime(true);
	    return statDataset;
	  }

	  public int getDateInterval() {
	    return dateInterval;
	  }
	   //字体配置方法(解决中文问题)
	    private static void configFont(JFreeChart chart) {
	        // 配置字体
	        Font xfont = new Font("宋体", Font.PLAIN, 12);// X轴
	        Font yfont = new Font("宋体", Font.PLAIN, 12);// Y轴
	        Font kfont = new Font("宋体", Font.PLAIN, 12);// 底部
	        Font titleFont = new Font("宋体", Font.BOLD, 25); // 图片标题
	        XYPlot plot =  chart.getXYPlot();// 图形的绘制结构对象
	       
	        // 图片标题
	        chart.setTitle(new TextTitle(chart.getTitle().getText(), titleFont));

	        // 底部
//	        chart.getLegend().setItemFont(kfont);
	     // 横轴框里的标题字体
	        chart.getLegend().setItemFont(kfont);
	        // 横轴列表字体
	        plot.getDomainAxis().setTickLabelFont(kfont);
	        // 横轴小标题字体
	        plot.getDomainAxis().setLabelFont(kfont);

	        // Y 轴
	        ValueAxis rangeAxis = plot.getRangeAxis();
	        rangeAxis.setLabelFont(yfont);
	        rangeAxis.setLabelPaint(Color.BLUE); // 字体颜色
	        rangeAxis.setTickLabelFont(yfont);

	    }
	  public void setDateInterval(int dateInterval) {
	    this.dateInterval = dateInterval;
	  }
	  public static void main(String arhs[]){
		  TimeSeriesTest trendChart = new TimeSeriesTest();
		  trendChart.chartTitle = "一年走势图";
		  trendChart.chartSeriesDesc = "确认数量";
		  trendChart.chartSeriesDesc1 = "风险数量";
		  trendChart.chartXdesc = "月份";
		  trendChart.chartYdesc = "数量";
		  trendChart.graphHigh = 400;
		  trendChart.graphWidth = 600;
		  trendChart.timeFormat = "yyyy年MM月";
		  trendChart.periodType = TimeSeriesTest.MONTH;
		  
		  double baseData = 100.0;
		  double rData = baseData;
		  
		  trendChart.addTimeSeriesUnitData(2011, 11,25);
		  trendChart.addTimeSeriesUnitData(2011, 12,45);
		  trendChart.addTimeSeriesUnitData(2012, 1, 50);
		  trendChart.addTimeSeriesUnitData(2012, 2,  80);
		  trendChart.addTimeSeriesUnitData(2012, 3, 30);
		  trendChart.addTimeSeriesUnitData(2012, 4,  10);
		  
		  trendChart.addTimeSeriesUnitDataAll(2011, 11,45);
		  trendChart.addTimeSeriesUnitDataAll(2011, 12,65);
		  trendChart.addTimeSeriesUnitDataAll(2012, 1, 70);
		  trendChart.addTimeSeriesUnitDataAll(2012, 2,  90);
		  trendChart.addTimeSeriesUnitDataAll(2012, 3, 50);
		  trendChart.addTimeSeriesUnitDataAll(2012, 4, 40);
		  
		  
		  trendChart.createTread();
		  JFreeChart chart =trendChart.createTrendChart();
		  trendChart.configFont(chart);
		  final ChartFrame preview = new ChartFrame("一年走势图",chart);
		    preview.addWindowListener(new WindowAdapter() {

		         public void windowClosing(final WindowEvent event) {

		           preview.dispose();

		         }

		       });

		       preview.pack();

		       //调整预览窗口的大小和位置,适合屏幕,并且居中

//		       Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
//
//		       preview.setSize(screenSize.width,screenSize.height-50);//适合屏幕,50表示把工具栏要考虑在内
//
//		       Dimension frameSize = preview.getSize();
//
//		       if (frameSize.height > screenSize.height) {
//
//		         frameSize.height = screenSize.height;
//
//		       }
//
//		       if (frameSize.width > screenSize.width) {
//
//		         frameSize.width = screenSize.width;
//
//		       }
//
//		       preview.setLocation( (screenSize.width - frameSize.width) / 2,
//
//		                  (screenSize.height - frameSize.height-50) / 2);



		       //显示报表预览窗口

		       preview.setVisible(true);
	  }

}


运行效果如图:

 

部分代码:

  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);  
 DateFormat df=new SimpleDateFormat("yyyy年MM月");
	   NumberFormat nf= NumberFormat.getNumberInstance();
	   StandardXYToolTipGenerator toolg=new StandardXYToolTipGenerator(StandardXYToolTipGenerator.DEFAULT_TOOL_TIP_FORMAT,df,nf);
	   
	   renderer.setToolTipGenerator(toolg);

 

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

  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. //设置曲线显示各数据点的值  
   XYLineAndShapeRenderer xylineandshaperenderer = (XYLineAndShapeRenderer)_xyplot.getRenderer();
	  //设置网格背景颜色
	    _xyplot.setBackgroundPaint(Color.white);
	  //设置网格竖线颜色
	    _xyplot.setDomainGridlinePaint(Color.pink);
	  //设置网格横线颜色
	    _xyplot.setRangeGridlinePaint(Color.pink);
	  //设置曲线图与xy轴的距离
	    _xyplot.setAxisOffset(new RectangleInsets(0D, 0D, 0D, 10D));
	  //设置曲线是否显示数据点
	  xylineandshaperenderer.setBaseShapesVisible(true);
	  //设置曲线显示各数据点的值



 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值