JFreeChart - Grafico a linee

Un grafico a linee o un grafico a linee visualizza le informazioni come una serie di punti dati (indicatori) collegati da segmenti di linea retta. Il grafico a linee mostra come i dati cambiano alla stessa frequenza temporale.

Questo capitolo mostra come possiamo usare JFreeChart per creare Line Chart da un determinato insieme di dati aziendali.

Dati aziendali

L'esempio seguente traccia un grafico a linee per mostrare l'andamento del numero di scuole aperte in anni diversi a partire dal 1970.

I dati forniti sono i seguenti:

Anno Numero di scuole
1970 15
1980 30
1990 60
2000 120
2013 240
2014 300

Applicazione basata su AWT

Di seguito è riportato il codice per creare un grafico a linee dalle informazioni fornite sopra. Questo codice ti aiuta a incorporare un grafico a linee in qualsiasi applicazione basata su 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 );
   }
}

Manteniamo il codice Java sopra in formato LineChart_AWT.java file, quindi compilarlo ed eseguirlo dal comando richiesto come -

$javac LineChart_AWT.java  
$java LineChart_AWT

Se tutto va bene, verrà compilato ed eseguito per generare il seguente grafico a linee:

Creazione di immagini JPEG

Riscriviamo l'esempio precedente per generare un'immagine JPEG da una riga di comando.

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);
   }
}

Manteniamo il codice Java sopra in formato LineChart.java file, quindi compilarlo ed eseguirlo dal comando richiesto come -

$javac LineChart.java  
$java LineChart

Se tutto va bene, verrà compilato ed eseguito per creare un file immagine JPEG denominato LineChart.jpeg nella directory corrente.