JAVA – JFreeChart – graphs and charts in JAVA

 

I came across a fantastic tool this week for creating graphs and charts in JAVA – JFreeChart.

The JARs and API can be downloaded from the following link:
http://www.jfree.org/jfreechart/download.html

 

STEP 1:
Once downloaded, the first step is to unzip the content and look for 2 files called –
jcommon-1.0.14.jar and jfreechart-1.0.11.jar
The file names may change slightly with new releases.
Copy these 2 files into the working directory of your JAVA application – adding then to the class path. 

 
 

 

If using Netbeans, copy jcommon-1.0.14.jar andjfreechart-1.0.11.jar to the project folder in windows explorer.
In Netbeans – right-click the project in the projects window and select properties.
Select the Libraries category.
On the Compile tab click Add JAR/Folder.
Select jcommon-1.0.14.jar andjfreechart-1.0.11.jar  and click open.
Click OK.
jcommon-1.0.14.jar andjfreechart-1.0.11.jar are now in the class path of the current JAVA application.

 

STEP 2 Creating a pie chart in a swing application:

I have found that jScrollPane is a use full object to hold the chart. When building your Jframe add an empty JScrollPane somewhere on your window that will hold the chart.

The following example assumes that the JFrame is already created and Pack() has been called.

Add the following 3 methods into the GUI class:

   // CREATE THE PIE CHART
   public static JPanel createPiePanel() {

        JFreeChart pieChart = createPieChart(createPieDataset());

        return new ChartPanel(pieChart);

    }

   
    // BUILD THE PIE CHART

    private static JFreeChart createPieChart(PieDataset dataset) {

 

        JFreeChart chart = ChartFactory.createPieChart(

            “Chart Title”,  // chart title

            dataset,             // data

            true,               // include legend

            true,

            false

        );

 

        PiePlot plot = (PiePlot) chart.getPlot();

        plot.setSectionOutlinesVisible(false);

        plot.setNoDataMessage(“No data available”);

 

        return chart;

 

    }

   

    // CREATE THE PIE CHART DATA

    private static PieDataset createPieDataset() {

        DefaultPieDataset dataset = new DefaultPieDataset();

       

        dataset.setValue(“One”, new Double(43.2));

        dataset.setValue(“Two”, new Double(10.0));

        dataset.setValue(“Three”, new Double(27.5));

        dataset.setValue(“Four”, new Double(17.5));

        dataset.setValue(“Five”, new Double(11.0));

        dataset.setValue(“Six”, new Double(19.4));

 

         return dataset;

    }

 

 

 

 

 

STEP 3: Add the created pie chart into an already existing JScrollPane:

Assuming the there is a valid JScrollPane in the main GUI of the Swing application, use the following code to add the chart to it.

// REMOVE ANY ITEM CURRENTLY IN THE JCROLLPANE
JScrollPane.getViewport().removeAll();


// BUILD AND ADD THE CHART TO THE JSCROLLPANE

JScrollPane.getViewport().add(createPiePanel());

When this is teamed with a JDBC (https://robbamforth.wordpress.com/2008/10/28/java-sql-server-2005-odbc-jdbc-connection/) you can build up graphs based on SQL data.

 

You can learn how wo export the chart to a JPEG feil here: https://robbamforth.wordpress.com/2008/11/05/java-jfreechart-how-to-save-a-jcfreechart-to-jpeg-file/

Advertisements

2 thoughts on “JAVA – JFreeChart – graphs and charts in JAVA

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s