JFreeChart - Gráfico XY

El gráfico XY (dispersión) se basa en una serie de datos que consta de una lista de valores X e Y. Cada par de valores (X, Y) es un punto en un sistema de coordenadas. Aquí, un valor determina la posición horizontal (X) y el otro determina la posición vertical (Y). Este capítulo demuestra cómo podemos usar JFreeChart para crearXY Chart a partir de un conjunto determinado de datos empresariales.

Datos comerciales

Considere un ejemplo en el que queremos crear un gráfico XY para todos los navegadores principales. Aquí, se recopilan diferentes puntajes de desempeño de diferentes categorías de personas, como se muestra a continuación:

Firefox Categoría (X) Puntuación (Y)
1.0 1.0
2.0 4.0
3,0 3,0
Chrome Categoría (X) Puntuación (Y)
1.0 4.0
2.0 5,0
3,0 6.0
IE Categoría (X) Puntuación (Y)
3,0 4.0
4.0 5,0
5,0 4.0

Aplicación basada en AWT

A continuación se muestra el código para crear un gráfico XY a partir de la información proporcionada anteriormente. Este código le ayuda a incrustar un gráfico XY en cualquier aplicación basada en AWT.

import java.awt.Color; 
import java.awt.BasicStroke; 

import org.jfree.chart.ChartPanel; 
import org.jfree.chart.JFreeChart; 
import org.jfree.data.xy.XYDataset; 
import org.jfree.data.xy.XYSeries; 
import org.jfree.ui.ApplicationFrame; 
import org.jfree.ui.RefineryUtilities; 
import org.jfree.chart.plot.XYPlot; 
import org.jfree.chart.ChartFactory; 
import org.jfree.chart.plot.PlotOrientation; 
import org.jfree.data.xy.XYSeriesCollection; 
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;

public class XYLineChart_AWT extends ApplicationFrame {

   public XYLineChart_AWT( String applicationTitle, String chartTitle ) {
      super(applicationTitle);
      JFreeChart xylineChart = ChartFactory.createXYLineChart(
         chartTitle ,
         "Category" ,
         "Score" ,
         createDataset() ,
         PlotOrientation.VERTICAL ,
         true , true , false);
         
      ChartPanel chartPanel = new ChartPanel( xylineChart );
      chartPanel.setPreferredSize( new java.awt.Dimension( 560 , 367 ) );
      final XYPlot plot = xylineChart.getXYPlot( );
      
      XYLineAndShapeRenderer renderer = new XYLineAndShapeRenderer( );
      renderer.setSeriesPaint( 0 , Color.RED );
      renderer.setSeriesPaint( 1 , Color.GREEN );
      renderer.setSeriesPaint( 2 , Color.YELLOW );
      renderer.setSeriesStroke( 0 , new BasicStroke( 4.0f ) );
      renderer.setSeriesStroke( 1 , new BasicStroke( 3.0f ) );
      renderer.setSeriesStroke( 2 , new BasicStroke( 2.0f ) );
      plot.setRenderer( renderer ); 
      setContentPane( chartPanel ); 
   }
   
   private XYDataset createDataset( ) {
      final XYSeries firefox = new XYSeries( "Firefox" );          
      firefox.add( 1.0 , 1.0 );          
      firefox.add( 2.0 , 4.0 );          
      firefox.add( 3.0 , 3.0 );          
      
      final XYSeries chrome = new XYSeries( "Chrome" );          
      chrome.add( 1.0 , 4.0 );          
      chrome.add( 2.0 , 5.0 );          
      chrome.add( 3.0 , 6.0 );          
      
      final XYSeries iexplorer = new XYSeries( "InternetExplorer" );          
      iexplorer.add( 3.0 , 4.0 );          
      iexplorer.add( 4.0 , 5.0 );          
      iexplorer.add( 5.0 , 4.0 );          
      
      final XYSeriesCollection dataset = new XYSeriesCollection( );          
      dataset.addSeries( firefox );          
      dataset.addSeries( chrome );          
      dataset.addSeries( iexplorer );
      return dataset;
   }

   public static void main( String[ ] args ) {
      XYLineChart_AWT chart = new XYLineChart_AWT("Browser Usage Statistics",
         "Which Browser are you using?");
      chart.pack( );          
      RefineryUtilities.centerFrameOnScreen( chart );          
      chart.setVisible( true ); 
   }
}

Mantengamos el código Java anterior en XYLineChart_AWT.java archivo, y luego compílelo y ejecútelo desde el comando solicitado como:

$javac XYLineChart_AWT.java  
$java XYLineChart_AWT

Si todo está bien, se compilará y ejecutará para generar el siguiente gráfico XY:

Creación de imágenes JPEG

Reescribamos el ejemplo anterior para generar una imagen JPEG desde la línea de comando.

import java.io.*;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.data.xy.XYSeries;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.xy.XYSeriesCollection;
import org.jfree.chart.ChartUtilities; 

public class XYLineChart_image {

   public static void main( String[ ] args )throws Exception {
      final XYSeries firefox = new XYSeries( "Firefox" );
      firefox.add( 1.0 , 1.0 );
      firefox.add( 2.0 , 4.0 );
      firefox.add( 3.0 , 3.0 );
      
      final XYSeries chrome = new XYSeries( "Chrome" );
      chrome.add( 1.0 , 4.0 );
      chrome.add( 2.0 , 5.0 );
      chrome.add( 3.0 , 6.0 );
      
      final XYSeries iexplorer = new XYSeries( "InternetExplorer" );
      iexplorer.add( 3.0 , 4.0 );
      iexplorer.add( 4.0 , 5.0 );
      iexplorer.add( 5.0 , 4.0 );
      
      final XYSeriesCollection dataset = new XYSeriesCollection( );
      dataset.addSeries( firefox );
      dataset.addSeries( chrome );
      dataset.addSeries( iexplorer );

      JFreeChart xylineChart = ChartFactory.createXYLineChart(
         "Browser usage statastics", 
         "Category",
         "Score", 
         dataset,
         PlotOrientation.VERTICAL, 
         true, true, false);
      
      int width = 640;   /* Width of the image */
      int height = 480;  /* Height of the image */ 
      File XYChart = new File( "XYLineChart.jpeg" ); 
      ChartUtilities.saveChartAsJPEG( XYChart, xylineChart, width, height);
   }
}

Mantengamos el código Java anterior en XYLineChart_image.java archivo, y luego compílelo y ejecútelo desde el comando solicitado como -

$javac XYLineChart_image.java  
$java XYLineChart_image

Si todo está bien, se compilará y se ejecutará para crear un archivo de imagen JPEG llamado XYLineChart.jpeg en su directorio actual.