JFreeChart教程(二)

 

three创建折线图

//创建折线图(Category)数据对象

       String series1 = "First";

       String series2 = "Second";

       String series3 = "Third";

       String type1 = "2001";

       String type2 = "2002";

       String type3 = "2003";

       String type4 = "2004";

       String type5 = "2005";

       String type6 = "2006";

       String type7 = "2007";

       String type8 = "2008";

       DefaultCategoryDataset defaultcategorydataset = new DefaultCategoryDataset();

       defaultcategorydataset.addValue(1.0D, series1, type1);

       defaultcategorydataset.addValue(4D, series1, type2);

       defaultcategorydataset.addValue(3D, series1, type3);

       defaultcategorydataset.addValue(5D, series1, type4);

       defaultcategorydataset.addValue(5D, series1, type5);

       defaultcategorydataset.addValue(7D, series1, type6);

       defaultcategorydataset.addValue(7D, series1, type7);

       defaultcategorydataset.addValue(8D, series1, type8);

 

       defaultcategorydataset.addValue(5D, series2, type1);

       defaultcategorydataset.addValue(7D, series2, type2);

       defaultcategorydataset.addValue(6D, series2, type3);

       defaultcategorydataset.addValue(8D, series2, type4);

       defaultcategorydataset.addValue(4D, series2, type5);

       defaultcategorydataset.addValue(4D, series2, type6);

       defaultcategorydataset.addValue(2D, series2, type7);

       defaultcategorydataset.addValue(1.0D, series2, type8);

 

       defaultcategorydataset.addValue(4D, series3, type1);

       defaultcategorydataset.addValue(3D, series3, type2);

       defaultcategorydataset.addValue(2D, series3, type3);

       defaultcategorydataset.addValue(3D, series3, type4);

       defaultcategorydataset.addValue(6D, series3, type5);

       defaultcategorydataset.addValue(3D, series3, type6);

       defaultcategorydataset.addValue(4D, series3, type7);

       defaultcategorydataset.addValue(3D, series3, type8);

       JFreeChart jfreechart = ChartFactory.createLineChart("折线图 Demo 1",

              "Type","Value",

              defaultcategorydataset,PlotOrientation.VERTICAL,

              true,true,false);

       CategoryPlot categoryplot = (CategoryPlot)jfreechart.getPlot();

       categoryplot.setBackgroundPaint(Color.lightGray);

       categoryplot.setRangeGridlinePaint(Color.white);

       ChartFrame  frame=new ChartFrame ("折线图 ",jfreechart,true);

       frame.pack();

       frame.setVisible(true);

four:折线图

//  //折线图2

       XYSeries xyseries = new XYSeries("First"); //先产生XYSeries 对象

       xyseries.add(1.0D, 1.0D);

       xyseries.add(2D, 4D);

       xyseries.add(3D, 3D);

       xyseries.add(4D, 5D);

       xyseries.add(5D, 5D);

       xyseries.add(6D, 7D);

       xyseries.add(7D, 7D);

       xyseries.add(8D, 8D);

 

       XYSeries xyseries1 = new XYSeries("Second");

       xyseries1.add(1.0D, 5D);

       xyseries1.add(2D, 7D);

       xyseries1.add(3D, 6D);

       xyseries1.add(4D, 8D);

       xyseries1.add(5D, 4D);

       xyseries1.add(6D, 4D);

       xyseries1.add(7D, 2D);

        xyseries1.add(8D, 1.0D);

 

       XYSeries xyseries2 = new XYSeries("Third");

       xyseries2.add(3D, 4D);

       xyseries2.add(4D, 3D);

       xyseries2.add(5D, 2D);

       xyseries2.add(6D, 3D);

       xyseries2.add(7D, 6D);

       xyseries2.add(8D, 3D);

       xyseries2.add(9D, 4D);

       xyseries2.add(10D, 3D);

 

       XYSeriesCollection xyseriescollection = new XYSeriesCollection(); //再用XYSeriesCollection添加入XYSeries 对象

       xyseriescollection.addSeries(xyseries);

       xyseriescollection.addSeries(xyseries1);

       xyseriescollection.addSeries(xyseries2);

       JFreeChart jfreechart = ChartFactory.createXYLineChart("Line Chart Demo 2",

              "X",

              "Y",

              xyseriescollection,

              PlotOrientation.VERTICAL,

              true,

              true,

              false);

       ChartFrame  frame=new ChartFrame ("折线图 ",jfreechart,true);

       frame.pack();

        frame.setVisible(true);

five:时间序列图

//时间序列图

       TimeSeries timeseries = new TimeSeries("L&G European Index Trust",Month.class);

       timeseries.add(new Month(2, 2001), 181.8D);//这里用的是Month.class,同样还有Day.class Year.class 等等

       timeseries.add(new Month(3, 2001), 167.3D);

       timeseries.add(new Month(4, 2001), 153.8D);

       timeseries.add(new Month(5, 2001), 167.6D);

       timeseries.add(new Month(6, 2001), 158.8D);

       timeseries.add(new Month(7, 2001), 148.3D);

       timeseries.add(new Month(8, 2001), 153.9D);

       timeseries.add(new Month(9, 2001), 142.7D);

       timeseries.add(new Month(10, 2001), 123.2D);

       timeseries.add(new Month(11, 2001), 131.8D);

       timeseries.add(new Month(12, 2001), 139.6D);

       timeseries.add(new Month(1, 2002), 142.9D);

       timeseries.add(new Month(2, 2002), 138.7D);

       timeseries.add(new Month(3, 2002), 137.3D);

       timeseries.add(new Month(4, 2002), 143.9D);

       timeseries.add(new Month(5, 2002), 139.8D);

       timeseries.add(new Month(6, 2002), 137D);

       timeseries.add(new Month(7, 2002), 132.8D);

 

       TimeSeries timeseries1 = new TimeSeries("L&G UK Index Trust",Month.class);

       timeseries1.add(new Month(2, 2001), 129.6D);

       timeseries1.add(new Month(3, 2001), 123.2D);

       timeseries1.add(new Month(4, 2001), 117.2D);

       timeseries1.add(new Month(5, 2001), 124.1D);

       timeseries1.add(new Month(6, 2001), 122.6D);

       timeseries1.add(new Month(7, 2001), 119.2D);

       timeseries1.add(new Month(8, 2001), 116.5D);

       timeseries1.add(new Month(9, 2001), 112.7D);

       timeseries1.add(new Month(10, 2001), 101.5D);

       timeseries1.add(new Month(11, 2001), 106.1D);

       timeseries1.add(new Month(12, 2001), 110.3D);

       timeseries1.add(new Month(1, 2002), 111.7D);

       timeseries1.add(new Month(2, 2002), 111D);

       timeseries1.add(new Month(3, 2002), 109.6D);

       timeseries1.add(new Month(4, 2002), 113.2D);

       timeseries1.add(new Month(5, 2002), 111.6D);

       timeseries1.add(new Month(6, 2002), 108.8D);

       timeseries1.add(new Month(7, 2002), 101.6D);

       TimeSeriesCollection timeseriescollection = new TimeSeriesCollection();

       timeseriescollection.addSeries(timeseries);

        timeseriescollection.addSeries(timeseries1);

       timeseriescollection.setDomainIsPointsInTime(true); //domain轴上的刻度点代表的是时间点而不是时间段

       JFreeChart jfreechart = ChartFactory.createTimeSeriesChart("Legal & General Unit Trust Prices",

              "Date",

              "Price Per Unit",

              timeseriescollection,

              true,

              true,

              false);

              jfreechart.setBackgroundPaint(Color.white);

              XYPlot xyplot = (XYPlot)jfreechart.getPlot(); //获得 plot : XYPlot!!

              xyplot.setBackgroundPaint(Color.lightGray);

              xyplot.setDomainGridlinePaint(Color.white);

              xyplot.setRangeGridlinePaint(Color.white);

              xyplot.setAxisOffset(new RectangleInsets(5D, 5D, 5D, 5D));

              xyplot.setDomainCrosshairVisible(true);

              xyplot.setRangeCrosshairVisible(true);

              ChartFrame  frame=new ChartFrame ("折线图 ",jfreechart,true);

              frame.pack();

              frame.setVisible(true);

 

你可能感兴趣的:(java,Component)