JFreeChart(八)之時序圖
阿新 • • 發佈:2018-12-29
轉載自 JFreeChart時序圖
時序圖表顯示的資料點在相等的時間間隔序列變化。本章演示瞭如何從一個給定的業務資料使用JFreeChart,建立時序圖。
業務資料
讓我們考慮通過使用標準 Java API 的 Math.random()產生的各種隨機數。我們使用這些數字產生一個時間序列圖。可以生成用於發生錯誤的總數在自己的網站給定的時間間隔類似的圖表。
基於AWT的應用
下面是建立由Math.random()產生的數字時間的程式碼在給定時間內的序列圖。
import org.jfree.chart.ChartFactory; import org.jfree.chart.ChartPanel; import org.jfree.chart.JFreeChart; import org.jfree.data.general.SeriesException; import org.jfree.data.time.Second; 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.RefineryUtilities; public class TimeSeries_AWT extends ApplicationFrame { public TimeSeries_AWT( final String title ) { super( title ); final XYDataset dataset = createDataset( ); final JFreeChart chart = createChart( dataset ); final ChartPanel chartPanel = new ChartPanel( chart ); chartPanel.setPreferredSize( new java.awt.Dimension( 560 , 370 ) ); chartPanel.setMouseZoomable( true , false ); setContentPane( chartPanel ); } private XYDataset createDataset( ) { final TimeSeries series = new TimeSeries( "Random Data" ); Second current = new Second( ); double value = 100.0; for (int i = 0; i < 4000; i++) { try { value = value + Math.random( ) - 0.5; series.add(current, new Double( value ) ); current = ( Second ) current.next( ); } catch ( SeriesException e ) { System.err.println("Error adding to series"); } } return new TimeSeriesCollection(series); } private JFreeChart createChart( final XYDataset dataset ) { return ChartFactory.createTimeSeriesChart( "Computing Test", "Seconds", "Value", dataset, false, false, false); } public static void main( final String[ ] args ) { final String title = "Time Series Management"; final TimeSeries_AWT demo = new TimeSeries_AWT( title ); demo.pack( ); RefineryUtilities.positionFrameRandomly( demo ); demo.setVisible( true ); } }
儲存上面的Java程式碼在TimeSeries_AWT.java檔案,然後從命令提示符下編譯並執行它,如下所示:
$javac TimeSeries_AWT.java $java TImeSeries_AWT
如果一切順利,它會編譯並執行生成以下時序圖:
建立JPEG影象
讓我們重新編寫上面的例子,在命令列生成JPEG影象。
import java.io.*; import org.jfree.chart.ChartFactory; import org.jfree.chart.JFreeChart; import org.jfree.data.general.SeriesException; import org.jfree.data.time.Second; import org.jfree.data.time.TimeSeries; import org.jfree.data.time.TimeSeriesCollection; import org.jfree.data.xy.XYDataset; import org.jfree.chart.ChartUtilities; public class TimeSeriesChart { public static void main( final String[ ] args )throws Exception { final TimeSeries series = new TimeSeries( "Random Data" ); Second current = new Second(); double value = 100.0; for ( int i = 0 ; i < 4000 ; i++ ) { try { value = value + Math.random( ) - 0.5; series.add( current , new Double( value ) ); current = ( Second ) current.next( ); } catch ( SeriesException e ) { System.err.println( "Error adding to series" ); } } final XYDataset dataset=( XYDataset )new TimeSeriesCollection(series); JFreeChart timechart = ChartFactory.createTimeSeriesChart( "Computing Test", "Seconds", "Value", dataset, false, false, false); int width = 560; /* Width of the image */ int height = 370; /* Height of the image */ File timeChart = new File( "TimeChart.jpeg" ); ChartUtilities.saveChartAsJPEG( timeChart, timechart, width, height ); } }
繼續上面的Java程式碼儲存在TimeSeriesChart.java檔案中,然後從命令提示符下編譯並執行它,如下所示:
$javac TimeSeriesChart.java $java TimeSeriesChart
如果一切正常,它會編譯並執行在當前的目錄中建立JPEG影象檔案TimeChart.jpeg檔案。