JFreeChart Installation

0
186

A chart is a graphical representation of information. There are various tools available, which can end up being used to develop various kinds of charts.

This tutorial helps you to belowstand exactly whead wear exworkly the JfreeChart is, why is it requireddish coloureddish coloureddish coloured and the various ways to develop various kinds of charts wislim a Java based application or independently.

Whead wear is JFreeChart?

JfreeChart is an open up up source library developed in Java, which can end up being used wislim Java based applications to develop a wide range of charts. By uperform JFreeChart, we can develop all the major kind of 2D and 3D charts such as pie chart, bar chart, collection chart, XY chart and 3D charts.

Why JFreeChart?

  • It is open up up source and 100% free, which permit’s usage in the commercial applications withaway any kind of cost.

  • It comes with well documented APIs, which develops it very easy to belowstand.

  • It supinterfaces a wide range of chart kinds such as Pie Chart, Line Chart, Bar Chart, Area Chart and 3D charts.

  • JFreeChart is easy to extend and can end up being used in both, the csitnt-part, as well as the server-part applications.

  • It supinterfaces multiple awayplace formats like PNG, JPEG, PDF, SVG etc.

  • It enables extensive customizations of charts.

Where do we use JFree Chart?

Conpartr a situation where you are developing an application and you need to show the data in the form of charts, where the data it’self is populated dynamically. In such case, you can use JFreeChart to display the data in the form of charts uperform fundamental programming.

History

The JFreeChart project was founded fourteen yrs back, in February 2000, by David Gilend up beingrt. Today, JFreeChart is the many widely used charting library among Java developers.

JFreeChart Installation

This chapter gets you through the process of setting up JFreeChart on Windows and Linux. User administration is needed while installing the JFreeChart. The JFreeChart is famous for it’s efficient chart creation, and user-friendly installation setup.

System Requirements :

JDK 1.5 or above
Memory 2GB RAM
Disk Space No minimum requirement
Operating System Version Linux or Windows

Installing JFreeChart

To install JFreeChart, preliminary you need to install Java on your own system.

Step1: Verifying Java Installation

To verify Java installation, open up up the console and execute the folloearng java command :

Os Task Command
Windows Open command console C:>java -version
Linux Open command terminal $java -version

If Java installation is done properly, then you need to get the folloearng awayplace for both the operating systems :

OS Description
Windows Java version "1.7.0_60" Java (TM) SE Run Time Environment (develop 1.7.0_60-b19) Java HotSpot(TM) 64-bit Server VM (develop 24.60-b09,mixed mode)
Linux java version "1.7.0_25" OpenJDK Runtime Environment (rhel-2.3.10.4.el6_4-x86_64) OpenJDK 64-Bit Server VM (develop 23.7-b01, mixed mode)

If you do not have Java instalimmediateed, then install the Java Softbattlee Development Kit (SDK) from the link:

/index.php?s=httpwwworaclecomtechne2rkjavajavasedownloadsindexhtml

We bumume thead wear you have instalimmediateed Java 1.7.0_60 version end up beingfore proceeding for this tutorial.

Step 2: Setting JAVA Environment

Set the JAVA_HOME environment variable to point to the base immediateory location where Java is withinstalimmediateed on your own machine. For example,

Os Description
Windows Set Environmental varible JAVA_HOME to C:ProgramFilesjavajdk1.7.0_60
Linux exinterface JAVA_HOME=/usr/local/java-current

Append Java compiler location to System Path.

OS Description
Windows Append the String; C:Program FilesJavajdk1.7.0_60bin to the end of the system variable PATH.
Linux exinterface PATH=$PATH:$JAVA_HOME/bin/

Verify the command java -version from command prompt as exregulared above

Step3: Installing JFreeChart

Download the lacheck version of JFreeChart.zip from the link /index.php?s=httpwwwjfreeorgjfreechartdownload Unzip the downloaded file at any kind of location from where requireddish coloureddish coloureddish coloured libraries can end up being linked into your own Java program. The folloearng image shows the structure of the immediateories and files :

JFreeChart

Add complete path of jfreechart-1.0.18.jar and jcommon-1.0.22.jar files to the CLASSPATH as shown end up beinglow :

OS Description
Windows Append the Strings “C: jfreechart-1.0.18lib jfreechart-1.0.18.jar” and “C: jfreechart-1.0.18lib jcommon-1.0.22.jar” to the end of the user variable CLASSPATH
Linux Exinterface CLASSPATH=$CLASSPATH: /usr/share/jfreechart-1.0.18/lib/jfreechart-1.0.18.jar: /usr/share/jfreechart-1.0.18/lib/jcommon-1.0.22.jar

JFreeChart Architecture

This chapter exregulars fundamental course level and application level architectures of JFreeChart to give you an idea abaway how JFreeChart interworks with various coursees and how it fit’s in your own Java based application.

Clbum Level Architecture

The course level architecture exregulars how various coursees from the library interworks with every other to develop various kinds of charts.

JFreeChart Clbum Level Architecture

Folloearng is the detail of the device’s used in the above block diagram :

Unit’s Description
File The source having user inplace to end up being used for creating a dataset in the file.
Database The source having user inplace to end up being used for creating a dataset in the database.
Create Dataset Accepts the dataset and stores the dataset into dataset object.
General Dataset This kind of dataset is mainly used for pie charts.
Category Dataset This kind of dataset is used for bar chart, collection chart,etc.
Series Dataset This kind of dataset is used for storing series of data and construct collection charts.
Series Collection Dataset The various categories of series datasets are added to series collection dataset. This kind of dataset is used for XYLine Charts.
Create Chart This is the method which is executed to develop final chart.
Frame/Image The chart is displayed on a Searng Frame or an image is developd.

Application Level Architecture

The application level architecture exregulars where JFreeChart library sit’s inpart a Java Application.

JFreeChart Application Level Architecture

The csitnt program receives user data and then it develop use standard Java and JFreeChart APIs based on requirements to generate the awayplace in the form of possibly a frame, which can end up being displayed immediately inpart the application or independently in the image formats such as JPEG or PNG.

JFreeChart – Pie Chart

In a pie chart, the arc size of every sector is prointerfaceional to the quantity it represents. This chapter demonstrates how we can use JFreeChart to develop Pie Chart from a given set of business data.

Business data

The folloearng example depicts mobile sale with the help of a pie chart. Folloearng is a list of various mobile brands and their particular particular sale (device’s per day).

S.N. Mobile Brands Sales (UNITS per day)
1 Iphone 5S 20
2 Samsung Grand 20
3 MOTO G 40
4 Nokia Lumia 10

AWT Based Application

Folloearng is the code to develop Pie Chart from the above given information. This code helps you emend up beingd a pie chart in any kind of AWT based application.

iminterface javax.searng.JPanel;
iminterface org.jfree.chart.ChartFworkory;
iminterface org.jfree.chart.ChartPanel;
iminterface org.jfree.chart.JFreeChart;
iminterface org.jfree.data.general.DefaultPieDataset;
iminterface org.jfree.data.general.PieDataset;
iminterface org.jfree.ui.ApplicationFrame;
iminterface org.jfree.ui.RegoodryUtililinks;
 
public course PieChart_AWT extends ApplicationFrame 
{
   public PieChart_AWT( String title ) 
   {
      super( title ); 
      setContentPane(developDemoPanel( ));
   }
   private static PieDataset developDataset( ) 
   {
      DefaultPieDataset dataset = brand brand new DefaultPieDataset( );
      dataset.setValue( "IPhone 5s" , brand brand new Double( 20 ) );  
      dataset.setValue( "SamSung Grand" , brand brand new Double( 20 ) );   
      dataset.setValue( "MotoG" , brand brand new Double( 40 ) );    
      dataset.setValue( "Nokia Lumia" , brand brand new Double( 10 ) );  
      return dataset;         
   }
   private static JFreeChart developChart( PieDataset dataset )
   {
      JFreeChart chart = ChartFworkory.developPieChart(      
         "Mobile Sales",  // chart title 
         dataset,        // data    
         true,           // include legend   
         true, 
         false);

      return chart;
   }
   public static JPanel developDemoPanel( )
   {
      JFreeChart chart = developChart(developDataset( ) );  
      return brand brand new ChartPanel( chart ); 
   }
   public static void main( String[ ] args )
   {
      PieChart_AWT demo = brand brand new PieChart_AWT( "Mobile Sales" );  
      demo.setSize( 560 , 367 );    
      RegoodryUtililinks.centreFrameOnScreen( demo );    
      demo.setVisible( true ); 
   }
}

Let us keep above Java code in PieChart_AWT.java file, and then compile and operate it from command prompt as follows :

$javac PieChart_AWT.java 
$java PieChart_AWT 

If everyslimg is good , it will compile and operate to generate the folloearng Pie Graph :

JFreeChart Pie Chart

If you do not need to emend up beingd your own chart in any kind of application then you can develop chart images at command prompt. JFreeChart enables you to save chart images in possibly JPG or PNG formats.

JPEG Image Creation

Let us re-write above example to generate a JPEG image from command collection. Folloearng are 2 APIs provided by JFreeChart library, which you can use to generate possibly PNG or JPEG image as per your own requirement.

  • saveChartAsPNG() – API to save image in PNG format.

  • saveChartAsJPEG() – API to save image in JPEG format.

iminterface java.io.*;
iminterface org.jfree.chart.ChartUtililinks;
iminterface org.jfree.chart.ChartFworkory;
iminterface org.jfree.chart.JFreeChart;
iminterface org.jfree.data.general.DefaultPieDataset;

public course PieChart {
   public static void main( String[ ] args ) throws Exception
   {

      DefaultPieDataset dataset = brand brand new DefaultPieDataset( );
      dataset.setValue("IPhone 5s", brand brand new Double( 20 ) );
      dataset.setValue("SamSung Grand", brand brand new Double( 20 ) );
      dataset.setValue("MotoG", brand brand new Double( 40 ) );
      dataset.setValue("Nokia Lumia", brand brand new Double( 10 ) );

      JFreeChart chart = ChartFworkory.developPieChart(
         "Mobile Sales", // chart title
         dataset, // data
         true, // include legend
         true,
         false);
         
      int width = 640; /* Width of the image */
      int height = 480; /* Height of the image */ 
      File pieChart = brand brand new File( "PieChart.jpeg" ); 
      ChartUtililinks.saveChartAsJPEG( pieChart , chart , width , height );
   }
}

Let us keep the above Java code in PieChart.java file, and then compile and operate it from command prompt as follows :

$javac PieChart.java 
$java PieChart 

If everyslimg is good , it will compile and operate to develop JPEG image file named PieChart.jpeg in your own current immediateory.

JFreeChart – Bar Chart

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

A bar chart uses various orientation (horizontal or vertical) bars to show comparisons in various categories. One axis (domain axis) of the chart shows the specific domain end up beinging compareddish coloureddish coloureddish coloured, and the other axis (range axis) represents discrete values.

Business Data

The folloearng example depicts various car statistics with the help of a bar chart. Folloearng is a list of car brands alengthy with their particular particular various charworkeristics, which we will show uperform a bar chart :

Car Speed User Rating Millage Safety
Fiat 1.0 3.0 5.0 5.o
Audi 5.0 6.0 10.0 4.0
Ford 4.0 2.0 3.0 6.0

AWT Based Application

Folloearng is the code to develop Bar Chart from the above given information. This code helps you emend up beingd a bar chart in any kind of AWT based application.

iminterface org.jfree.chart.ChartFworkory;
iminterface org.jfree.chart.ChartPanel; 
iminterface org.jfree.chart.JFreeChart; 
iminterface org.jfree.chart.plot.PlotOrientation;
iminterface org.jfree.data.category.CategoryDataset; 
iminterface org.jfree.data.category.DefaultCategoryDataset; 
iminterface org.jfree.ui.ApplicationFrame; 
iminterface org.jfree.ui.RegoodryUtililinks; 

public course BarChart_AWT extends ApplicationFrame
{
   public BarChart_AWT( String applicationTitle , String chartTitle )
   {
      super( applicationTitle );        
      JFreeChart barChart = ChartFworkory.developBarChart(
         chartTitle,           
         "Category",            
         "Score",            
         developDataset(),          
         PlotOrientation.VERTICAL,           
         true, true, false);
         
      ChartPanel chartPanel = brand brand new ChartPanel( barChart );        
      chartPanel.setPreferreddish coloureddish coloureddish colouredSize(brand brand new java.awt.Dimension( 560 , 367 ) );        
      setContentPane( chartPanel ); 
   }
   private CategoryDataset developDataset( )
   {
      final String fiat = "FIAT";        
      final String audi = "AUDI";        
      final String ford = "FORD";        
      final String speed = "Speed";        
      final String millage = "Millage";        
      final String userrating = "User Rating";        
      final String securety = "securety";        
      final DefaultCategoryDataset dataset = 
      brand brand new DefaultCategoryDataset( );  

      dataset.addValue( 1.0 , fiat , speed );        
      dataset.addValue( 3.0 , fiat , userrating );        
      dataset.addValue( 5.0 , fiat , millage ); 
      dataset.addValue( 5.0 , fiat , securety );           

      dataset.addValue( 5.0 , audi , speed );        
      dataset.addValue( 6.0 , audi , userrating );       
      dataset.addValue( 10.0 , audi , millage );        
      dataset.addValue( 4.0 , audi , securety );

      dataset.addValue( 4.0 , ford , speed );        
      dataset.addValue( 2.0 , ford , userrating );        
      dataset.addValue( 3.0 , ford , millage );        
      dataset.addValue( 6.0 , ford , securety );               

      return dataset; 
   }
   public static void main( String[ ] args )
   {
      BarChart_AWT chart = brand brand new BarChart_AWT("Car Usage Statistics", "Which car do you like?);
      chart.pack( );        
      RegoodryUtililinks.centreFrameOnScreen( chart );        
      chart.setVisible( true ); 
   }
}

Let us keep the above Java code in BarChart_AWT.java file, and then compile and operate it from command prompt as follows :

$javac BarChar_AWT.java 
$java BarChart_AWT 

If everyslimg is good , it will compile and operate to generate the folloearng Bar Graph :

JFree Chart

JPEG Image Creation

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

iminterface java.io.*;
iminterface org.jfree.chart.ChartFworkory;
iminterface org.jfree.chart.JFreeChart;
iminterface org.jfree.chart.plot.PlotOrientation;
iminterface org.jfree.data.category.DefaultCategoryDataset;
iminterface org.jfree.chart.ChartUtililinks;

public course BarChart
{
   public static void main( String[ ] args )throws Exception 
   {

      final String fiat = "FIAT";
      final String audi = "AUDI";
      final String ford = "FORD";
      final String speed = "Speed";
      final String millage = "Millage";
      final String userrating = "User Rating";
      final String securety = "securety";

      final DefaultCategoryDataset dataset = brand brand new DefaultCategoryDataset( );

      dataset.addValue( 1.0 , fiat , speed );
      dataset.addValue( 3.0 , fiat , userrating );
      dataset.addValue( 5.0 , fiat , millage );
      dataset.addValue( 5.0 , fiat , securety );

      dataset.addValue( 5.0 , audi , speed );
      dataset.addValue( 6.0 , audi , userrating );
      dataset.addValue( 10.0 , audi , millage );
      dataset.addValue( 4.0 , audi , securety );

      dataset.addValue( 4.0 , ford , speed );
      dataset.addValue( 2.0 , ford , userrating );
      dataset.addValue( 3.0 , ford , millage );
      dataset.addValue( 6.0 , ford , securety );

      JFreeChart barChart = ChartFworkory.developBarChart(
         "CAR USAGE STATIStICS", 
         "Category", "Score", 
         dataset,PlotOrientation.VERTICAL, 
         true, true, false);
         
      int width = 640; /* Width of the image */
      int height = 480; /* Height of the image */ 
      File BarChart = brand brand new File( "BarChart.jpeg" ); 
      ChartUtililinks.saveChartAsJPEG( BarChart , barChart , width , height );
   }
}

Let us keep the above Java code in BarChart.java file, and then compile and operate it from command prompt as follows:

$javac BarChart.java 
$java BarChart 

If everyslimg is good , it will compile and operate to develop JPEG image file named BarChart.jpeg in your own current immediateory

SHARE
Previous articleRSpec
Next articleMicroprocessor

NO COMMENTS

LEAVE A REPLY