JFreeChart – Overview ”; Previous Next A chart is a graphical representation of information. There are various tools available, which can be used to create different types of charts. The JFreeChart project was founded in February 2000, by David Gilbert. Today, it is the most widely used charting library among Java developers. This tutorial will help you understand what exactly JFreeChart is, why is it required, and the various ways to create different types of charts within a Java-based application or independently. What is JFreeChart? JfreeChart is an open source library developed in Java. It can be used within Java based applications to create a wide range of charts. By using JFreeChart, we can create all the major type of 2D and 3D charts such as pie chart, bar chart, line chart, XY chart and 3D charts. Why JFreeChart? JFreeChart is open source and 100% free, which permits usage in the commercial applications without any cost. We have enlisted here some more points in favor of why you should use JFreeChart − It comes with well documented APIs, which makes it quite easy to understand. It supports a wide range of chart types such as Pie Chart, Line Chart, Bar Chart, Area Chart and 3D charts. JFreeChart is easy to extend and can be used in both, the client-side, as well as the server-side applications. It supports multiple output formats like PNG, JPEG, PDF, SVG etc. It allows extensive customizations of charts. Consider a situation where you are developing an application and you need to show the data in the form of charts, and the data itself is populated dynamically. In such case, displaying the data in the form of charts using JFreeChart programming is very simple. Print Page Previous Next Advertisements ”;
Category: jfreechart
JFreeChart – 3D Pie/Bar Chart ”; Previous Next The 3D charts are the ones, which appear in a three-dimensional format. You can use these charts to provide better display and clear information. A 3D Pie chart is same as the pie chart additionally with a nice 3D effect. A 3D effect can be achieved by adding a little extra code, which will take care of creating 3D effect in a pie chart. 3D Pie chart Consider the following example to illustrate mobile sale with the help of a 3D pie chart. Following is a list of different mobile brands and their sale (units per day). S.No Mobile Brands Sales (UNITS per day) 1 Iphone 5S 20 2 Samsung Grand 20 3 MOTO G 40 4 Nokia Lumia 10 Following is the code to create 3D Pie Chart from the above given information. This code helps you to embed a pie chart in any AWT based application. import java.io.*; import org.jfree.chart.ChartFactory; import org.jfree.chart.JFreeChart; import org.jfree.chart.plot.PiePlot3D; import org.jfree.data.general.DefaultPieDataset; import org.jfree.chart.ChartUtilities; public class PieChart3D { public static void main( String[ ] args )throws Exception { DefaultPieDataset dataset = new DefaultPieDataset( ); dataset.setValue( “IPhone 5s” , new Double( 20 ) ); dataset.setValue( “SamSung Grand” , new Double( 20 ) ); dataset.setValue( “MotoG” , new Double( 40 ) ); dataset.setValue( “Nokia Lumia” , new Double( 10 ) ); JFreeChart chart = ChartFactory.createPieChart3D( “Mobile Sales” , // chart title dataset , // data true , // include legend true, false); final PiePlot3D plot = ( PiePlot3D ) chart.getPlot( ); plot.setStartAngle( 270 ); plot.setForegroundAlpha( 0.60f ); plot.setInteriorGap( 0.02 ); int width = 640; /* Width of the image */ int height = 480; /* Height of the image */ File pieChart3D = new File( “pie_Chart3D.jpeg” ); ChartUtilities.saveChartAsJPEG( pieChart3D , chart , width , height ); } } Let us keep the above Java code in PieChart3D.java file, and then compile and run it from the command prompted as − $javac PieChart3D.java $java PieChart3D If everything is fine, it will compile and run to create a JPEG image file named PieChart3D.jpeg having the following 3D Pie Chart − 3D Bar Chart A 3D Bar chart is same as the bar chart additionally with a nice 3D effect. A 3D effect can achieved by adding a little extra code, which will take care of creating 3D effect in a bar chart. Consider the following example that depicts various car statistics with the help of a 3D bar chart. Following is a list of car brands along with their different characteristics, which we will show using a bar chart − Car Speed User Rating Millage Safety FIAT 1.0 3.0 5.0 5.0 AUDI 5.0 6.0 10.0 4.0 FORD 4.0 2.0 3.0 6.0 The following code creates 3D Bar Chart from the above given information. This code helps you to embed a bar chart in any AWT based application. import java.io.*; import org.jfree.chart.ChartFactory; import org.jfree.chart.JFreeChart; import org.jfree.chart.plot.PlotOrientation; import org.jfree.data.category.DefaultCategoryDataset; import org.jfree.chart.ChartUtilities; public class BarChart3D { public static void main( String[ ] args )throws Exception { final String fait = “FAIT”; final String audi = “AUDI”; final String ford = “FORD”; final String speed = “Speed”; final String popular = “Popular”; final String mailage = “Mailage”; final String userrating = “User Rating”; final String safety = “safety”; final DefaultCategoryDataset dataset = new DefaultCategoryDataset( ); dataset.addValue( 1.0 , fait , speed ); dataset.addValue( 4.0 , fait , popular ); dataset.addValue( 3.0 , fait , userrating ); dataset.addValue( 5.0 , fait , mailage ); dataset.addValue( 5.0 , fait , safety ); dataset.addValue( 5.0 , audi , speed ); dataset.addValue( 7.0 , audi , popular ); dataset.addValue( 6.0 , audi , userrating ); dataset.addValue( 10.0 , audi , mailage ); dataset.addValue( 4.0 , audi , safety ); dataset.addValue( 4.0 , ford , speed ); dataset.addValue( 3.0 , ford , popular ); dataset.addValue( 2.0 , ford , userrating ); dataset.addValue( 3.0 , ford , mailage ); dataset.addValue( 6.0 , ford , safety ); JFreeChart barChart = ChartFactory.createBarChart3D( “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 barChart3D = new File( “barChart3D.jpeg” ); ChartUtilities.saveChartAsJPEG( barChart3D, barChart, width, height); } } Let us keep the above Java code in BarChart3D.java file, and then compile and run it from the command prompted as − $javac BarChart3D.java $java BarChart3 If everything is fine with your environment, it will compile and run to create a JPEG image file BarChart3D.jpeg having the following 3D Bar Chart − Print Page Previous Next Advertisements ”;
JFreeChart- Bubble Chart
JFreeChart – Bubble Chart ”; Previous Next This chapter demonstrates how you can use JFreeChart to create Bubble Chart from a given set of business data. A bubble chart displays information in three-dimensional way. A bubble is plotted at the place where (x, y) coordinate intersect. The size of the bubble is considered as range or quantity of X and Y axis. Business Data Let us consider different persons along with their age, weight, and work capacities. The wok capacity can be treated as number of hours that is plotted as bubbles in the chart. WEIGHT AGE 30 40 50 60 70 80 10 4 WORK 20 5 30 10 40 8 50 9 60 6 AWT Based Application Following is the code to create Bubble Chart from the above given information. This code helps you to embed a Bubble chart in any AWT based application. import java.awt.Color; import java.awt.Dimension; import javax.swing.JPanel; import org.jfree.chart.*; import org.jfree.chart.axis.NumberAxis; import org.jfree.chart.plot.PlotOrientation; import org.jfree.chart.plot.XYPlot; import org.jfree.chart.renderer.xy.XYItemRenderer; import org.jfree.data.xy.DefaultXYZDataset; import org.jfree.data.xy.XYZDataset; import org.jfree.ui.ApplicationFrame; import org.jfree.ui.RefineryUtilities; public class BubbleChart_AWT extends ApplicationFrame { public BubbleChart_AWT( String s ) { super( s ); JPanel jpanel = createDemoPanel( ); jpanel.setPreferredSize(new Dimension( 560 , 370 ) ); setContentPane( jpanel ); } private static JFreeChart createChart( XYZDataset xyzdataset ) { JFreeChart jfreechart = ChartFactory.createBubbleChart( “AGE vs WEIGHT vs WORK”, “Weight”, “AGE”, xyzdataset, PlotOrientation.HORIZONTAL, true, true, false); XYPlot xyplot = ( XYPlot )jfreechart.getPlot( ); xyplot.setForegroundAlpha( 0.65F ); XYItemRenderer xyitemrenderer = xyplot.getRenderer( ); xyitemrenderer.setSeriesPaint( 0 , Color.blue ); NumberAxis numberaxis = ( NumberAxis )xyplot.getDomainAxis( ); numberaxis.setLowerMargin( 0.2 ); numberaxis.setUpperMargin( 0.5 ); NumberAxis numberaxis1 = ( NumberAxis )xyplot.getRangeAxis( ); numberaxis1.setLowerMargin( 0.8 ); numberaxis1.setUpperMargin( 0.9 ); return jfreechart; } public static XYZDataset createDataset( ) { DefaultXYZDataset defaultxyzdataset = new DefaultXYZDataset(); double ad[ ] = { 30 , 40 , 50 , 60 , 70 , 80 }; double ad1[ ] = { 10 , 20 , 30 , 40 , 50 , 60 }; double ad2[ ] = { 4 , 5 , 10 , 8 , 9 , 6 }; double ad3[][] = { ad , ad1 , ad2 }; defaultxyzdataset.addSeries( “Series 1” , ad3 ); return defaultxyzdataset; } public static JPanel createDemoPanel( ) { JFreeChart jfreechart = createChart( createDataset( ) ); ChartPanel chartpanel = new ChartPanel( jfreechart ); chartpanel.setDomainZoomable( true ); chartpanel.setRangeZoomable( true ); return chartpanel; } public static void main( String args[ ] ) { BubbleChart_AWT bubblechart = new BubbleChart_AWT( “Bubble Chart_frame” ); bubblechart.pack( ); RefineryUtilities.centerFrameOnScreen( bubblechart ); bubblechart.setVisible( true ); } } Let us keep the above Java code in BubbleChart_AWT.java file, and then compile and run it from the command prompted as − $javac BubbleChart_AWT.java $java BubbleChart_AW If everything is fine, it will compile and run to generate the following Bubble Graph − JPEG Image Creation Let us re-write the above example to generate a JPEG image from a command line. import java.io.*; import java.awt.Color; import org.jfree.chart.*; import org.jfree.chart.axis.NumberAxis; import org.jfree.chart.plot.PlotOrientation; import org.jfree.chart.plot.XYPlot; import org.jfree.chart.renderer.xy.XYItemRenderer; import org.jfree.data.xy.DefaultXYZDataset; import org.jfree.chart.ChartUtilities; public class BubbleChart_image { public static void main( String args[ ] )throws Exception { DefaultXYZDataset defaultxyzdataset = new DefaultXYZDataset( ); double ad[ ] = { 30 , 40 , 50 , 60 , 70 , 80 }; double ad1[ ] = { 10 , 20 , 30 , 40 , 50 , 60 }; double ad2[ ] = { 4 , 5 , 10 , 8 , 9 , 6 }; double ad3[ ][ ] = { ad , ad1 , ad2 }; defaultxyzdataset.addSeries( “Series 1” , ad3 ); JFreeChart jfreechart = ChartFactory.createBubbleChart( “AGE vs WEIGHT vs WORK”, “Weight”, “AGE”, defaultxyzdataset, PlotOrientation.HORIZONTAL, true, true, false); XYPlot xyplot = ( XYPlot )jfreechart.getPlot( ); xyplot.setForegroundAlpha( 0.65F ); XYItemRenderer xyitemrenderer = xyplot.getRenderer( ); xyitemrenderer.setSeriesPaint( 0 , Color.blue ); NumberAxis numberaxis = ( NumberAxis )xyplot.getDomainAxis( ); numberaxis.setLowerMargin( 0.2 ); numberaxis.setUpperMargin( 0.5 ); NumberAxis numberaxis1 = ( NumberAxis )xyplot.getRangeAxis( ); numberaxis1.setLowerMargin( 0.8 ); numberaxis1.setUpperMargin( 0.9 ); int width = 560; /* Width of the image */ int height = 370; /* Height of the image */ File bubbleChart = new File(“BubbleChart.jpeg”); ChartUtilities.saveChartAsJPEG(bubbleChart,jfreechart,width,height); } } Let us keep the above Java code in BubbleChart_image.java file, and then compile and run it from the command prompted as − $javac BubbleChart_image.java $java BubbleChart_image If everything is fine, it will compile and run to create a JPEG image file named BubbleChart.jpeg in your current directory. Print Page Previous Next Advertisements ”;
JFreeChart – Database Interface ”; Previous Next This chapter explains how you can read simple data from a database table and then use JFreeChart to create a chart of your choice. Business Data Consider we have the following MySQL table mobile_tbl(mobile_brand VARCHAR(100) NOT NULL, unit_sale INT NO NULL); Consider this table is having the following records − Mobile Brands Unit Sales IPhone5S 20 Samsung Grand 20 MotoG 40 Nokia Lumia 10 Chart Generation Using Database Following is the code to create a Pie Chart based on the information provided in mobile_tbl table available in test_db in a MySQL database. Based on your requirements, you can use any other database. import java.io.*; import java.sql.*; import org.jfree.chart.ChartUtilities; import org.jfree.chart.ChartFactory; import org.jfree.chart.JFreeChart; import org.jfree.data.general.DefaultPieDataset; public class PieChart_DB { public static void main( String[ ] args )throws Exception { String mobilebrands[] = { “IPhone 5s”, “SamSung Grand”, “MotoG”, “Nokia Lumia” }; /* Create MySQL Database Connection */ Class.forName( “com.mysql.jdbc.Driver” ); Connection connect = DriverManager.getConnection( “jdbc:mysql://localhost:3306/jf_testdb” , “root”, “root123”); Statement statement = connect.createStatement( ); ResultSet resultSet = statement.executeQuery(“select * from mobile_data” ); DefaultPieDataset dataset = new DefaultPieDataset( ); while( resultSet.next( ) ) { dataset.setValue( resultSet.getString( “mobile_brand” ) , Double.parseDouble( resultSet.getString( “unit_sale” ))); } JFreeChart chart = ChartFactory.createPieChart( “Mobile Sales”, // chart title dataset, // data true, // include legend true, false ); int width = 560; /* Width of the image */ int height = 370; /* Height of the image */ File pieChart = new File( “Pie_Chart.jpeg” ); ChartUtilities.saveChartAsJPEG( pieChart , chart , width , height ); } } Let us keep the above Java code in PieChart_DB.java file, and then compile and run it from the command prompted as − $javac PieChart_DB.java $java PieChart_DB If everything is fine, it will compile and run to create a JPEG image file named Pie_Chart.jpeg having the following chart. Print Page Previous Next Advertisements ”;
JFreeChart – File Interface
JFreeChart – File Interface ”; Previous Next So far we studied how to create various types of charts using JFreeChart APIs using static data. But in production environment, data is provided in the form of text file with a predefined format, or it comes directly from the database. This chapter will explain — how we can read a simple data from a given text file from a given location and then use JFreeChart to create a chart of your choice. Business Data Consider we have a file named mobile.txt, having different mobile brands and their sale (units per day) separated by a simple comma (,) − Iphone 5S, 20 Samsung Grand, 20 MOTO G, 40 Nokia Lumia, 10 Chart Generation Based on File Following is the code to create a Pie Chart based on the information provided in mobile.txt − import java.io.*; import java.util.StringTokenizer; import org.jfree.chart.ChartUtilities; import org.jfree.chart.ChartFactory; import org.jfree.chart.JFreeChart; import org.jfree.data.general.DefaultPieDataset; public class PieChart_File { public static void main( String[ ] args )throws Exception { String mobilebrands[ ] = { “IPhone 5s” , “SamSung Grand” , “MotoG” , “Nokia Lumia” }; InputStream in = new FileInputStream( new File( “C:/temp/test.txt” ) ); BufferedReader reader = new BufferedReader(new InputStreamReader(in ) ); StringBuilder out = new StringBuilder(); String line; DefaultPieDataset dataset = new DefaultPieDataset(); while (( line = reader.readLine() ) != null ) { out.append( line ); } StringTokenizer s = new StringTokenizer( out.toString(), “,” ); int i = 0; while( s.hasMoreTokens( ) && ( mobilebrands [i] != null ) ) { dataset.setValue(mobilebrands[i], Double.parseDouble( s.nextToken( ) )); i++; } JFreeChart chart = ChartFactory.createPieChart( “Mobile Sales”, // chart title dataset, // data true, // include legend true, false); int width = 560; /* Width of the image */ int height = 370; /* Height of the image */ File pieChart = new File( “pie_Chart.jpeg” ); ChartUtilities.saveChartAsJPEG( pieChart, chart, width, height); } } Let us keep the above Java code in PieChart_File.java file, and then compile and run it from the command prompted as − $javac PieChart_File.java $java PieChart_File If everything is fine, it will compile and run to create a JPEG image file named PieChart.jpeg that contains the following chart. Print Page Previous Next Advertisements ”;
JFreeChart – XY Chart
JFreeChart – XY Chart ”; Previous Next The XY chart (scatter) is based on one data series consisting of a list of X and Y values. Each value pair (X,Y) is a point in a coordinate system. Here, one value determines the horizontal (X) position, and the other determines the vertical (Y) position. This chapter demonstrates — how we can use JFreeChart to create XY Chart from a given set of business data. Business Data Consider an example where we want to create an XY chart for all major browsers. Here, different performance scores are gathered from different categories of people as shown below − Firefox Category(X) Score(Y) 1.0 1.0 2.0 4.0 3.0 3.0 Chrome Category(X) Score(Y) 1.0 4.0 2.0 5.0 3.0 6.0 IE Category(X) Score(Y) 3.0 4.0 4.0 5.0 5.0 4.0 AWT Based Application Following is the code to create an XY Chart from the above given information. This code helps you to embed an XY chart in any AWT based application. 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 ); } } Let us keep the above Java code in XYLineChart_AWT.java file, and then compile and run it from the command prompted as: $javac XYLineChart_AWT.java $java XYLineChart_AWT If everything is fine, it will compile and run to generate the following XY Graph − JPEG Image Creation Let us re-write the above example to generate a JPEG image from the command line. 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); } } Let us keep the above Java code in XYLineChart_image.java file, and then compile and run it from the command prompted as − $javac XYLineChart_image.java $java XYLineChart_image If everything is fine, it will compile and run to create a JPEG image file named XYLineChart.jpeg in your current directory. Print Page Previous Next Advertisements ”;
JFreeChart – Architecture
JFreeChart – Architecture ”; Previous Next This chapter explains basic class level and application level architectures of JFreeChart to give you an idea about how JFreeChart interacts with different classes and how it fits in your Java based application. Class Level Architecture The class level architecture explains how various classes from the library interact with each other to create various types of charts. Following is the detail of the units used in the above block diagram − S.No Units & Description 1 File The source having user input to be used for creating a dataset in the file. 2 Database The source having user input to be used for creating a dataset in the database. 3 Create Dataset Accepts the dataset and stores the dataset into dataset object. 4 General Dataset This type of dataset is mainly used for pie charts. 5 Category Dataset This type of dataset is used for bar chart, line chart,etc. 6 Series Dataset This type of dataset is used for storing series of data and construct line charts. 7 Series Collection Dataset The different categories of series datasets are added to series collection dataset. This type of dataset is used for XYLine Charts. 8 Create Chart This is the method which is executed to create final chart. 9 Frame/Image The chart is displayed on a Swing Frame or an image is created. Application Level Architecture The application level architecture explains where JFreeChart library sits inside a Java Application. The client program receives user data and then it uses standard Java and JFreeChart APIs based on requirements to generate the output in the form of either a frame, which can be displayed directly inside the application or independently in the image formats such as JPEG or PNG. Print Page Previous Next Advertisements ”;