JFreeChart線型圖


線圖或折線圖來顯示資訊為一系列由直線段連線的資料點(標記)。線圖顯示資料在相同的時間頻率如何變化。本章從一個給定的業務資料演示如何使用JFreeChart建立線型圖。

業務資料

下面的範例繪製折線圖顯示從1970年開始學校在不同年份開通數量。

給定的資料如下:

年份 學校數量
1970 15
1980 30
1990 60
2000 120
2013 240
2014 300

基於AWT的應用

以下是對從上述給定的資訊建立線型圖的程式碼。此程式碼可以幫助在AWT的應用程式嵌入一個折線圖。

import org.jfree.chart.ChartPanel;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.category.DefaultCategoryDataset;

public class LineChart_AWT extends ApplicationFrame
{
   public LineChart_AWT( String applicationTitle , String chartTitle )
   {
      super(applicationTitle);
      JFreeChart lineChart = ChartFactory.createLineChart(
         chartTitle,
         "Years","Number of Schools",
         createDataset(),
         PlotOrientation.VERTICAL,
         true,true,false);
         
      ChartPanel chartPanel = new ChartPanel( lineChart );
      chartPanel.setPreferredSize( new java.awt.Dimension( 560 , 367 ) );
      setContentPane( chartPanel );
   }

   private DefaultCategoryDataset createDataset( )
   {
      DefaultCategoryDataset dataset = new DefaultCategoryDataset( );
      dataset.addValue( 15 , "schools" , "1970" );
      dataset.addValue( 30 , "schools" , "1980" );
      dataset.addValue( 60 , "schools" ,  "1990" );
      dataset.addValue( 120 , "schools" , "2000" );
      dataset.addValue( 240 , "schools" , "2010" );
      dataset.addValue( 300 , "schools" , "2014" );
      return dataset;
   }
   public static void main( String[ ] args ) 
   {
      LineChart_AWT chart = new LineChart_AWT(
      "School Vs Years" ,
      "Numer of Schools vs years");

      chart.pack( );
      RefineryUtilities.centerFrameOnScreen( chart );
      chart.setVisible( true );
   }
}

儲存LineChart_AWT.java檔案如上面的Java程式碼,然後從命令提示字元下編譯並執行它,如下所示:

$javac LineChart_AWT.java 
$java LineChart_AWT 

如果一切順利,它會編譯並執行生成以下線圖:

JFreeChart Line Chart

建立JPEG影象

讓我們重新編寫上面的例子,在命令列執行生成JPEG影象。

import java.io.*;
import org.jfree.chart.JFreeChart; 
import org.jfree.chart.ChartFactory; 
import org.jfree.chart.ChartUtilities; 
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.category.DefaultCategoryDataset;

public class LineChart
{
   public static void main( String[ ] args ) throws Exception
   {
      DefaultCategoryDataset line_chart_dataset = new DefaultCategoryDataset();
      line_chart_dataset.addValue( 15 , "schools" , "1970" );
      line_chart_dataset.addValue( 30 , "schools" , "1980" );
      line_chart_dataset.addValue( 60 , "schools" , "1990" );
      line_chart_dataset.addValue( 120 , "schools" , "2000" );
      line_chart_dataset.addValue( 240 , "schools" , "2010" ); 
      line_chart_dataset.addValue( 300 , "schools" , "2014" );

      JFreeChart lineChartObject = ChartFactory.createLineChart(
         "Schools Vs Years","Year",
         "Schools Count",
         line_chart_dataset,PlotOrientation.VERTICAL,
         true,true,false);

      int width = 640; /* Width of the image */
      int height = 480; /* Height of the image */ 
      File lineChart = new File( "LineChart.jpeg" ); 
      ChartUtilities.saveChartAsJPEG(lineChart ,lineChartObject, width ,height);
   }
}

讓我們儲存LineChart.java檔案如上面的Java程式碼,然後從命令提示字元下編譯並執行它,如下所示:

$javac LineChart.java 
$java LineChart 

如果一切順利,它將編譯和執行在當前的目錄中建立JPEG影象檔案namedLineChart.jpeg。