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

創建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。