Wednesday, September 8, 2010

Open Source chart libraries

Good Open Source chart libraries.

JfreeChart - http://www.jfree.org/jfreechart/
FusionChartsFree - http://www.fusioncharts.com/free/
JCCKit project (LGPL)
QN Plot project (BSD)
OpenChart2 project (LGPL)
PtPlot project (UC Berkeley copyright)
JRobin project (LGPL)
Java Chart Construction Kit (LGPL, works with JDK 1.1.8)
JOpenChart project (LGPL)
jCharts project (BSD-style)
JChart2D project (LGPL)
Chart2D project (LGPL)
ThunderGraph project (LGPL)
E-Gantt project (Q Public License)
MagPlot project (GPL)


I'm going to demonstrate code samples for bar chats and pie charts with JFreeChart

Note: jfreechart-1.0.13.jar and jcommon-1.0.16.jar should be available within your classpath.

1)  PieChart example....


package sample;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartFrame;
import org.jfree.chart.JFreeChart;
import org.jfree.data.general.DefaultPieDataset;

public class PieChart {

    public static void main(String[] args) {
        // create a dataset...
        DefaultPieDataset data = new DefaultPieDataset();
        data.setValue("Category 1", 43.2);
        data.setValue("Category 2", 27.9);
        data.setValue("Category 3", 79.5);
        // create a chart...
        JFreeChart chart = ChartFactory.createPieChart(
                "Sample Pie Chart",
                data,
                true,    // legend?
                true,    // tooltips?
                false    // URLs?
        );
        // create and display a frame...
        ChartFrame frame = new ChartFrame("First", chart);
        frame.pack();
        frame.setVisible(true);
    }
}


2) BarChart example...

package sample;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartFrame;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultIntervalCategoryDataset;

public class BarChart {

    public static void main(String[] args) {
        // create a dataset...
        double start[][] = new double[2][3];
        start[0][0] = 3;
        start[0][1] = 4;
        start[0][2] = 2;

        start[0][0] = 4;
        start[1][0] = 6;
       
        double end[][] = new double[2][3];

        end[0][0] = 2;
        end[0][1] = 5;
        end[0][2] = 1;

        end[0][0] = 5;
        end[1][0] = 2;

        CategoryDataset data = new DefaultIntervalCategoryDataset(start, end);

        JFreeChart chart = ChartFactory.createBarChart("title",
                "x axis", "y axis",
                data,
                PlotOrientation.VERTICAL,
                true,    // legend?
                true,    // tooltips?
                false    // URLs?
        );
        // create and display a frame...
        ChartFrame frame = new ChartFrame("Second", chart);
        frame.pack();
        frame.setVisible(true);
    }

}