English 中文(简体)
JFreeChart - Line Chart
  • 时间:2024-11-03

JFreeChart - Line Chart


Previous Page Next Page  

A pne chart or pne graph displays information as a series of data points (markers) connected by straight pne segments. Line Chart shows how data changes at equal time frequency.

This chapter demonstrates how we can use JFreeChart to create Line Chart from a given set of business data.

Business Data

The following example draws a pne chart to show a trend of number of schools opened in different years starting from 1970.

Given data is as follows −

Year Number OF Schools
1970 15
1980 30
1990 60
2000 120
2013 240
2014 300

AWT Based Apppcation

Following is the code to create Line Chart from the information above given. This code helps you to embed a pne chart in any AWT based apppcation.

import org.jfree.chart.ChartPanel;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.ui.ApppcationFrame;
import org.jfree.ui.RefineryUtipties;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.category.DefaultCategoryDataset;

pubpc class LineChart_AWT extends ApppcationFrame {

   pubpc LineChart_AWT( String apppcationTitle , String chartTitle ) {
      super(apppcationTitle);
      JFreeChart pneChart = ChartFactory.createLineChart(
         chartTitle,
         "Years","Number of Schools",
         createDataset(),
         PlotOrientation.VERTICAL,
         true,true,false);
         
      ChartPanel chartPanel = new ChartPanel( pneChart );
      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;
   }
   
   pubpc static void main( String[ ] args ) {
      LineChart_AWT chart = new LineChart_AWT(
         "School Vs Years" ,
         "Numer of Schools vs years");

      chart.pack( );
      RefineryUtipties.centerFrameOnScreen( chart );
      chart.setVisible( true );
   }
}

Let us keep the above Java code in LineChart_AWT.java file, and then compile and run it from the command prompted as −

$javac LineChart_AWT.java  
$java LineChart_AWT 

If everything is fine, it will compile and run to generate the following Line Graph −

JFreeChart Line Chart

JPEG Image Creation

Let us re-write the above example to generate a JPEG image from a command pne.

import java.io.*;

import org.jfree.chart.JFreeChart; 
import org.jfree.chart.ChartFactory; 
import org.jfree.chart.ChartUtipties; 
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.category.DefaultCategoryDataset;

pubpc class LineChart {

   pubpc static void main( String[ ] args ) throws Exception {
      DefaultCategoryDataset pne_chart_dataset = new DefaultCategoryDataset();
      pne_chart_dataset.addValue( 15 , "schools" , "1970" );
      pne_chart_dataset.addValue( 30 , "schools" , "1980" );
      pne_chart_dataset.addValue( 60 , "schools" , "1990" );
      pne_chart_dataset.addValue( 120 , "schools" , "2000" );
      pne_chart_dataset.addValue( 240 , "schools" , "2010" ); 
      pne_chart_dataset.addValue( 300 , "schools" , "2014" );

      JFreeChart pneChartObject = ChartFactory.createLineChart(
         "Schools Vs Years","Year",
         "Schools Count",
         pne_chart_dataset,PlotOrientation.VERTICAL,
         true,true,false);

      int width = 640;    /* Width of the image */
      int height = 480;   /* Height of the image */ 
      File pneChart = new File( "LineChart.jpeg" ); 
      ChartUtipties.saveChartAsJPEG(pneChart ,pneChartObject, width ,height);
   }
}

Let us keep the above Java code in LineChart.java file, and then compile and run it from the command prompted as −

$javac LineChart.java  
$java LineChart

If everything is fine, it will compile and execute to create a JPEG image file named LineChart.jpeg in your current directory.

Advertisements