Discuss JFreeChart ”; Previous Next This tutorial describes various ways to incorporate JFreeChart in Java-based standalone and web-based applications. The tutorial is categorized into various chapters to provide a comprehensive and easy understanding of JFreeChart programming with Java applications. Print Page Previous Next Advertisements ”;
Category: jfreechart
JFreeChart – Useful Resources ”; Previous Next The following resources contain additional information on JFreeChart. Please use them to get more in-depth knowledge on this topic. Python Programming Certification 2024 Most Popular 9 Courses 1 eBooks Tutorialspoint More Detail Artificial Intelligence and Machine Learning Certification 2024 Most Popular 7 Courses 1 eBooks Tutorialspoint More Detail Java Certification 2024 Best Seller 7 Courses 1 eBooks Tutorialspoint More Detail Print Page Previous Next Advertisements ”;
JFreeChart – Pie Chart
JFreeChart – Pie Chart ”; Previous Next In a pie chart, the arc length of each sector is proportional to the quantity that it represents. This chapter demonstrates — how we can use JFreeChart to create Pie Chart from a given set of business data. Business data The following example depicts mobile sale with the help of a 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 AWT Based Application Following is the code to create a Pie Chart by using the above given information. This code helps you to embed a pie chart in any AWT based application. import javax.swing.JPanel; import org.jfree.chart.ChartFactory; import org.jfree.chart.ChartPanel; import org.jfree.chart.JFreeChart; import org.jfree.data.general.DefaultPieDataset; import org.jfree.data.general.PieDataset; import org.jfree.ui.ApplicationFrame; import org.jfree.ui.RefineryUtilities; public class PieChart_AWT extends ApplicationFrame { public PieChart_AWT( String title ) { super( title ); setContentPane(createDemoPanel( )); } private static PieDataset createDataset( ) { 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 ) ); return dataset; } private static JFreeChart createChart( PieDataset dataset ) { JFreeChart chart = ChartFactory.createPieChart( “Mobile Sales”, // chart title dataset, // data true, // include legend true, false); return chart; } public static JPanel createDemoPanel( ) { JFreeChart chart = createChart(createDataset( ) ); return new ChartPanel( chart ); } public static void main( String[ ] args ) { PieChart_AWT demo = new PieChart_AWT( “Mobile Sales” ); demo.setSize( 560 , 367 ); RefineryUtilities.centerFrameOnScreen( demo ); demo.setVisible( true ); } } Let us keep the above Java code in PieChart_AWT.java file, and then compile and run it from the command prompted as − $javac PieChart_AWT.java $java PieChart_AWT If everything is fine, it will compile and run to generate the following Pie Graph − If you do not need to embed your chart in any application, then you can create chart images at command prompt. JFreeChart allows you to save chart images in either JPG or PNG formats. JPEG Image Creation Let us re-write the above example to generate a JPEG image from a command line. Following are the two APIs provided by JFreeChart library, which you can use to generate either PNG or JPEG image as per your requirement. saveChartAsPNG() − API to save image in PNG format. saveChartAsJPEG() − API to save image in JPEG format. import java.io.*; import org.jfree.chart.ChartUtilities; import org.jfree.chart.ChartFactory; import org.jfree.chart.JFreeChart; import org.jfree.data.general.DefaultPieDataset; public class PieChart { 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.createPieChart( “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 = new File( “PieChart.jpeg” ); ChartUtilities.saveChartAsJPEG( pieChart , chart , width , height ); } } Let us keep the above Java code in PieChart.java file, and then compile and run it from the command prompted as − $javac PieChart.java $java PieChart If everything is fine, it will compile and run to create a JPEG image file named PieChart.jpeg in your current directory. Print Page Previous Next Advertisements ”;
JFreeChart – Referenced APIs
JFreeChart – Referenced APIs ”; Previous Next In this chapter, we will discuss about some of the important packages, classes, and methods from JFreeChart library. These packages, classes, and methods are the most frequently used while creating a variety of charts using JFreeChart library. ChartFactory Class ChartFactory is an abstract class under the org.jfree.chart package. It provides a collection of utility methods for generating standard charts. Following is a list of few of the important methods − Class Constructor S.No Description 1 ChartFactory() Default constructor of ChartFactory class. Class Methods S.No Methods & Description 1 createPieChart(java.lang.String title, PieDataset dataset, boolean legend, boolean tooltips, boolean urls) This method creates a pie chart with default settings. It returns JfreeChart type object. 2 createPieChart3D(java.lang.String title, PieDataset dataset, boolean legend, boolean tooltips, boolean urls This method creates a 3D pie chart using the specified dataset. 3 createBarChart(java.lang.String title, java.lang.String categoryAxisLabel, java.lang.String valueAxisLabel, CategoryDataset dataset, PlotOrientation orientation, boolean legend, boolean tooltips, boolean urls) The argument java.lang.String categoryAxisLabel is the label for values placed on X-axis. The argument java.lang.String valueAxisLabel is the label for values placed on Y-axis. This method creates a bar chart. 4 createBarChart3D(java.lang.String title, java.lang.String categoryAxisLabel, java.lang.String valueAxisLabel, CategoryDataset dataset, PlotOrientation orientation, boolean legend, boolean tooltips, boolean urls) This Method Creates a bar chart with a 3D effect. It returns JfreeChart type object. 5 createLineChart(java.lang.String title, java.lang.String categoryAxisLabel, java.lang.String valueAxisLabel, CategoryDataset dataset, PlotOrientation orientation, boolean legend, boolean tooltips, boolean urls) This method creates a line chart with default settings. 6 createLineChart3D(java.lang.String title, java.lang.String categoryAxisLabel, java.lang.String valueAxisLabel, CategoryDataset dataset, PlotOrientation orientation, boolean legend, boolean tooltips, boolean urls) This method creates a line chart with 3D effect. 7 createXYLineChart(java.lang.String title, java.lang.String xAxisLabel, java.lang.String yAxisLabel, XYDataset dataset, PlotOrientation orientation, boolean legend, boolean tooltips, boolean urls) This method creates a line chart based on XYDataset with default settings. ChartFrame Class ChartFrame class under the org.jfree.chart package, provides all frame related functions and utilities. ChartFrame class inherits functionalities from parent classes such as Frame, Window, Container, and Component classes. Class Constructor S.No Constructor and Description 1 ChartFrame (java.lang.Frame String, JfreeChart chart) It constructs a frame. 2 Chart Frame (java.lang.Frame String, JfreeChart chart, boolean scrollpane) It constructs a frame. Class Method S.No Method and Description 1 getChartPanel() This method returns the chart panel for a frame. ChartPanel Class ChartPanel class from the org.jfree.chart package is used as a swing GUI component for displaying JfreeChart object. Class Constructor S.No Constructor and Description 1 ChartPanel(JFreeChart chart) This constructor constructs a panel that displays the specified chart. 2 ChartPanel(JFreeChart chart, boolean useBuffer) This constructor constructs a panel containing a chart. 3 ChartPanel(JFreeChart chart, boolean properties, boolean save, boolean print, boolean zoom, boolean tooltips) This constructor constructs a JFreeChart panel. Class Method S.No Method and Description 1 setPreferredSize(java.awt.Dimension) This method is used to set the frame size using java.awt. Dimension class object as an argument. This method is taken from javax.swing.JComponent. ChartUtilities Class CharUtilites class from the org.jfree.chart package provides a collection of utility methods of JFreeCharts including methods for converting charts into image file format such as PNG, JPEG, and creating HTML image maps. Class Constructor S.No Constructor and Description 1 ChartUtilities() This is a default constructor of a class Class Method S.No Method and Description 1 saveChartAsPNG(java.io.File file, JfreeChart chart, int width, int height) This method converts and saves a chart to the specified file in PNG format. 2 saveChartAsJPEG(java.io.File file, JfreeChart chart, int width, int height) This method converts and saves a chart to the specified file in JPEG format. JFreeChart Class JFreeChart class is the core class under the org.jfree.chart package. This class provides JFreeChart method to create bar charts, line charts, pie charts, and xy plots including time series data. Class Constructor S.No Constructor and Description 1 JfreeChart(Plot plot) This constructor creates a new chart based on the supplied plot. 2 JfreeChart(java.lang.String title, java.awt.Font titleFont, Plot plot, boolean createLegend) This constructor creates a new chart with the given title and plot. 3 JfreeChart(java.lang.String title, Plot plot) This constructor creates a new chart with the given title and plot. Class Method S.No Method and Description 1 getXYPlot() This method Returns the plot chart as XYPlot. Using XYPolt, we can do some utility operations on xy charts. PiePlot Class This class is a part of org.jfree.chart.plot package and extends Plot class from the same package. This class provides methods to create Pie Plots. Class Constructor S.No Constructor and Description 1 PiePlot() It creates a new plot. 2 PiePlot(PieDataset dataset) It creates a plot that draws a pie chart for the specified dataset. Class Method S.No Method and Description 1 setStartAngle(double angle) This Method sets the starting angle and sends a PlotChangeEvent to all registered listeners PiePlot3D Class PiePlot3D class is a subclass of PiePlot class under the same package. Hence, this class has the same features as PiePlot class, except it is used to create 3D plots. Class Constructor S.No Constructor and Description 1 PiePlot3D() This constructor creates a new instance with no dataset. 2 PiePlot3D(PieDataset dataset) This constructor creates a pie chart with three dimensional effect using a specified dataset. Class Method S.No Method and Description 1 setForegroundAlpha(float alpha) It sets the alpha-transparency for the plot and sends a PlotChangeEvent to all registered listeners. This is taken from one of the parent Plot classes. 2 setInteriorGap(double percent) It sets the interior gap and sends a PlotChangeEvent to all registered listeners. This controls the space between the edges of the pie plot and the plot area itself (i. e., the region where the section labels appear). This method is taken from the parent class PiePlot. PlotOrientation Class This is a serialized class available in org.jfree.chart.plot package and it is used to show the orientation of a 2D plot. The orientation can either be vertical or horizontal. It sets the orientation of Y-axis. A conventional plot has a vertical Y- axis. Field summary S.No Type Field & Description 1 PlotOrientation HORIZONTAL For a plot where the range axis(Y-axis) is horizontal. 2 PlotOrientation VERTICAL For a plot where
JFreeChart – Quick Guide
JFreeChart – Quick Guide ”; Previous Next JFreeChart – Overview 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. JFreeChart – Installation JFreeChart is popular for its efficient chart creation and user-friendly installation setup. This chapter describes the process of setting up JFreeChart on Windows and Linux. User administration is needed while installing JFreeChart. 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, there are three following steps viz… Step 1: Verifying Java Installation To verify Java installation, open the console and execute the following java command − Os Task Command Windows Open command console C:>java -version Linux Open command terminal $java -version Once Java installation is done properly, then you should get the following output for both the operating systems − S.No OS & Description 1 Windows Java version “1.7.0_60” Java (TM) SE Run Time Environment (build 1.7.0_60-b19) Java HotSpot(TM) 64-bit Server VM (build 24.60-b09,mixed mode) 2 Linux java version “1.7.0_25” OpenJDK Runtime Environment (rhel2.3.10.4.el6_4-x86_64) OpenJDK 64-Bit Server VM (build 23.7-b01, mixed mode) If you do not have Java installed, then install the Java Software Development Kit (SDK) from the link − https://www.oracle.com/technetwork/java/javase/downloads/index.html We assume that you have installed Java 1.7.0_60 version before proceeding for this tutorial. Step 2: Setting JAVA Environment Set the JAVA_HOME environment variable to point to the base directory location where Java is installed on your machine. For example, S.No Os & Description 1 Windows Set Environmental variable JAVA_HOME to C:ProgramFilesjavajdk1.7.0_60 2 Linux export JAVA_HOME=/usr/local/java-current Append Java compiler location to System Path. S.No OS & Description 1 Windows Append the String; C:Program FilesJavajdk1.7.0_60bin to the end of the system variable PATH. 2 Linux export PATH=$PATH:$JAVA_HOME/bin/ Verify the command java -version from the command prompted as explained above. Step 3: Installing JFreeChart Download the latest version of JFreeChart.zip from the link http://www.jfree.org/jfreechart/download/ Unzip the downloaded file at any location from where required libraries can be linked into your Java program. The following image shows the structure of the directories and files − Add complete path of jfreechart-1.0.18.jar and jcommon-1.0.22.jar files to the CLASSPATH as shown below − S.No OS & Description 1 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 2 Linux Export CLASSPATH=$CLASSPATH: /usr/share/jfreechart1.0.18/lib/jfreechart-1.0.18.jar: /usr/share/jfreechart-1.0.18/lib/jcommon1.0.22.jar Note − Inorder to communicate with MySql database you need to set the classpath to mysql-connector-java-5.0.8-bin.jar too. JFreeChart – Architecture 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. JFreeChart – Referenced APIs In this chapter, we will discuss about some of the important packages, classes, and methods from JFreeChart library. These packages, classes, and methods are the most frequently used
JFreeChart – Installation
JFreeChart – Installation ”; Previous Next JFreeChart is popular for its efficient chart creation and user-friendly installation setup. This chapter describes the process of setting up JFreeChart on Windows and Linux. User administration is needed while installing JFreeChart. 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, there are three following steps viz… Step 1: Verifying Java Installation To verify Java installation, open the console and execute the following java command − Os Task Command Windows Open command console C:>java -version Linux Open command terminal $java -version Once Java installation is done properly, then you should get the following output for both the operating systems − S.No OS & Description 1 Windows Java version “1.7.0_60” Java (TM) SE Run Time Environment (build 1.7.0_60-b19) Java HotSpot(TM) 64-bit Server VM (build 24.60-b09,mixed mode) 2 Linux java version “1.7.0_25” OpenJDK Runtime Environment (rhel2.3.10.4.el6_4-x86_64) OpenJDK 64-Bit Server VM (build 23.7-b01, mixed mode) If you do not have Java installed, then install the Java Software Development Kit (SDK) from the link − https://www.oracle.com/technetwork/java/javase/downloads/index.html We assume that you have installed Java 1.7.0_60 version before proceeding for this tutorial. Step 2: Setting JAVA Environment Set the JAVA_HOME environment variable to point to the base directory location where Java is installed on your machine. For example, S.No Os & Description 1 Windows Set Environmental variable JAVA_HOME to C:ProgramFilesjavajdk1.7.0_60 2 Linux export JAVA_HOME=/usr/local/java-current Append Java compiler location to System Path. S.No OS & Description 1 Windows Append the String; C:Program FilesJavajdk1.7.0_60bin to the end of the system variable PATH. 2 Linux export PATH=$PATH:$JAVA_HOME/bin/ Verify the command java -version from the command prompted as explained above. Step 3: Installing JFreeChart Download the latest version of JFreeChart.zip from the link http://www.jfree.org/jfreechart/download/ Unzip the downloaded file at any location from where required libraries can be linked into your Java program. The following image shows the structure of the directories and files − Add complete path of jfreechart-1.0.18.jar and jcommon-1.0.22.jar files to the CLASSPATH as shown below − S.No OS & Description 1 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 2 Linux Export CLASSPATH=$CLASSPATH: /usr/share/jfreechart1.0.18/lib/jfreechart-1.0.18.jar: /usr/share/jfreechart-1.0.18/lib/jcommon1.0.22.jar Note − Inorder to communicate with MySql database you need to set the classpath to mysql-connector-java-5.0.8-bin.jar too. Print Page Previous Next Advertisements ”;
JFreeChart – TimeSeries Chart ”; Previous Next A time series chart displays sequence of data points, which varies at equal intervals of time. This chapter demonstrates — how we can use JFreeChart to create Time Series Chart from a given set of business data. Business Data Let us consider various random numbers generated by using standard Java API Math.random(). We use these numbers to generate a Time Series Chart. You can generate similar chart for total number of errors occurring in your website at a given interval of time. AWT Based Application Following is the code to create Time Series Chart from the numbers generated by Math.random() at a given time internal. import org.jfree.chart.ChartFactory; import org.jfree.chart.ChartPanel; import org.jfree.chart.JFreeChart; import org.jfree.data.general.SeriesException; import org.jfree.data.time.Second; import org.jfree.data.time.TimeSeries; import org.jfree.data.time.TimeSeriesCollection; import org.jfree.data.xy.XYDataset; import org.jfree.ui.ApplicationFrame; import org.jfree.ui.RefineryUtilities; public class TimeSeries_AWT extends ApplicationFrame { public TimeSeries_AWT( final String title ) { super( title ); final XYDataset dataset = createDataset( ); final JFreeChart chart = createChart( dataset ); final ChartPanel chartPanel = new ChartPanel( chart ); chartPanel.setPreferredSize( new java.awt.Dimension( 560 , 370 ) ); chartPanel.setMouseZoomable( true , false ); setContentPane( chartPanel ); } private XYDataset createDataset( ) { final TimeSeries series = new TimeSeries( “Random Data” ); Second current = new Second( ); double value = 100.0; for (int i = 0; i < 4000; i++) { try { value = value + Math.random( ) – 0.5; series.add(current, new Double( value ) ); current = ( Second ) current.next( ); } catch ( SeriesException e ) { System.err.println(“Error adding to series”); } } return new TimeSeriesCollection(series); } private JFreeChart createChart( final XYDataset dataset ) { return ChartFactory.createTimeSeriesChart( “Computing Test”, “Seconds”, “Value”, dataset, false, false, false); } public static void main( final String[ ] args ) { final String title = “Time Series Management”; final TimeSeries_AWT demo = new TimeSeries_AWT( title ); demo.pack( ); RefineryUtilities.positionFrameRandomly( demo ); demo.setVisible( true ); } } Let us keep the above Java code in TimeSeries_AWT.java file, and then compile and run it from the command prompted as − $javac TimeSeries_AWT.java $java TImeSeries_AWT If everything is fine, it will compile and run to generate the following Time Series Graph − JPEG Image Creation Let us re-write the above example to generate a JPEG image from a command line. import java.io.*; import org.jfree.chart.ChartFactory; import org.jfree.chart.JFreeChart; import org.jfree.data.general.SeriesException; import org.jfree.data.time.Second; import org.jfree.data.time.TimeSeries; import org.jfree.data.time.TimeSeriesCollection; import org.jfree.data.xy.XYDataset; import org.jfree.chart.ChartUtilities; public class TimeSeriesChart { public static void main( final String[ ] args )throws Exception { final TimeSeries series = new TimeSeries( “Random Data” ); Second current = new Second(); double value = 100.0; for ( int i = 0 ; i < 4000 ; i++ ) { try { value = value + Math.random( ) – 0.5; series.add( current , new Double( value ) ); current = ( Second ) current.next( ); } catch ( SeriesException e ) { System.err.println( “Error adding to series” ); } } final XYDataset dataset=( XYDataset )new TimeSeriesCollection(series); JFreeChart timechart = ChartFactory.createTimeSeriesChart( “Computing Test”, “Seconds”, “Value”, dataset, false, false, false); int width = 560; /* Width of the image */ int height = 370; /* Height of the image */ File timeChart = new File( “TimeChart.jpeg” ); ChartUtilities.saveChartAsJPEG( timeChart, timechart, width, height ); } } Let us keep the above Java code in TimeSeriesChart.java file, and then compile and run it from the command prompted as − $javac TimeSeriesChart.java $java TimeSeriesChart If everything is fine with your environment, it will compile and run to create a JPEG image file TimeChart.jpeg file in your current directory. Print Page Previous Next Advertisements ”;
JFreeChart – Bar Chart
JFreeChart – Bar Chart ”; Previous Next This chapter demonstrates how you can use JFreeChart to create Bar Chart from a given set of business data. A bar chart uses different orientation (horizontal or vertical) bars to show comparisons in various categories. One axis (domain axis) of the chart shows the specific domain being compared, and the other axis (range axis) represents discrete values. Business Data The following example depicts various car statistics with the help of a 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 AWT Based Application Following is the code to create a Bar Chart from the above given information. This code helps you to embed a bar chart in any AWT based application. import org.jfree.chart.ChartFactory; import org.jfree.chart.ChartPanel; import org.jfree.chart.JFreeChart; import org.jfree.chart.plot.PlotOrientation; import org.jfree.data.category.CategoryDataset; import org.jfree.data.category.DefaultCategoryDataset; import org.jfree.ui.ApplicationFrame; import org.jfree.ui.RefineryUtilities; public class BarChart_AWT extends ApplicationFrame { public BarChart_AWT( String applicationTitle , String chartTitle ) { super( applicationTitle ); JFreeChart barChart = ChartFactory.createBarChart( chartTitle, “Category”, “Score”, createDataset(), PlotOrientation.VERTICAL, true, true, false); ChartPanel chartPanel = new ChartPanel( barChart ); chartPanel.setPreferredSize(new java.awt.Dimension( 560 , 367 ) ); setContentPane( chartPanel ); } private CategoryDataset createDataset( ) { 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 safety = “safety”; final DefaultCategoryDataset dataset = 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 , safety ); dataset.addValue( 5.0 , audi , speed ); dataset.addValue( 6.0 , audi , userrating ); dataset.addValue( 10.0 , audi , millage ); dataset.addValue( 4.0 , audi , safety ); dataset.addValue( 4.0 , ford , speed ); dataset.addValue( 2.0 , ford , userrating ); dataset.addValue( 3.0 , ford , millage ); dataset.addValue( 6.0 , ford , safety ); return dataset; } public static void main( String[ ] args ) { BarChart_AWT chart = new BarChart_AWT(“Car Usage Statistics”, “Which car do you like?”); chart.pack( ); RefineryUtilities.centerFrameOnScreen( chart ); chart.setVisible( true ); } } Let us keep the above Java code in BarChart_AWT.java file, and then compile and run it from the command prompted as − $javac BarChar_AWT.java $java BarChart_AWT If everything is fine, it will compile and run to generate the following Bar Graph − JPEG Image Creation Let us re-write the above example to generate a JPEG image from a command line. 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 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 safety = “safety”; final DefaultCategoryDataset dataset = 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 , safety ); dataset.addValue( 5.0 , audi , speed ); dataset.addValue( 6.0 , audi , userrating ); dataset.addValue( 10.0 , audi , millage ); dataset.addValue( 4.0 , audi , safety ); dataset.addValue( 4.0 , ford , speed ); dataset.addValue( 2.0 , ford , userrating ); dataset.addValue( 3.0 , ford , millage ); dataset.addValue( 6.0 , ford , safety ); JFreeChart barChart = ChartFactory.createBarChart( “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 = new File( “BarChart.jpeg” ); ChartUtilities.saveChartAsJPEG( BarChart , barChart , width , height ); } } Let us keep the above Java code in BarChart.java file, and then compile and run it from the command prompted as − $javac BarChart.java $java BarChart If everything is fine, it will compile and run to create a JPEG image file named BarChart.jpeg in your current directory. Print Page Previous Next Advertisements ”;
JFreeChart – Line Chart
JFreeChart – Line Chart ”; Previous Next A line chart or line graph displays information as a series of data points (markers) connected by straight line 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 line 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 Application Following is the code to create Line Chart from the information above given. This code helps you to embed a line chart in any AWT based application. 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 ); } } 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 − JPEG Image Creation Let us re-write the above example to generate a JPEG image from a command line. 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); } } 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. Print Page Previous Next Advertisements ”;
JFreeChart – Overview
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 ”;