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 

如果一切正常,它将编译并运行以生成以下时间序列图 −

JFreeChart TimeSeries Chart

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

广告