Thursday, December 30, 2010

Chart in Java

//this code creates chart with the hlep of jFree chart Library
// to work with this example you will need jFree chart Library that is available on
import java.awt.Color;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;

public class Main extends ApplicationFrame {

    public Main(final String title) {

        final XYDataset dataset = createDataset();
        final JFreeChart chart = createChart(dataset);
        final ChartPanel chartPanel = new ChartPanel(chart);
        chartPanel.setPreferredSize(new java.awt.Dimension(700, 470));

    private XYDataset createDataset() {

       //series no. 1 data elements
        final XYSeries series1 = new XYSeries("First");      
        series1.add(1.0, 1.0);
        series1.add(2.0, 4.0);
        series1.add(3.0, 3.0);
        series1.add(4.0, 5.0);
        series1.add(5.0, 5.0);
        series1.add(6.0, 7.0);
        series1.add(7.0, 7.0);
        series1.add(8.0, 8.0);

      // Series no. 2 data elements
      final XYSeries series2 = new XYSeries("Second");
        series2.add(1.0, 5.0);
        series2.add(2.0, 7.0);
        series2.add(3.0, 6.0);
        series2.add(4.0, 8.0);
        series2.add(5.0, 4.0);
        series2.add(6.0, 4.0);
        series2.add(7.0, 2.0);
        series2.add(8.0, 1.0);

         // Series no. 3 data elements
        final XYSeries series3 = new XYSeries("Third");
        series3.add(3.0, 7.0);
        series3.add(4.0, 4.0);
        series3.add(5.0, 1.0);
        series3.add(6.0, 3.0);
        series3.add(7.0, 6.0);
        series3.add(8.0, 3.0);
        series3.add(9.0, 6.0);
        series3.add(10.0, 4.0);

          // Series no. 4 data elements
        final XYSeries series4 = new XYSeries("Third");
        series4.add(1.0, 2.0);
        series4.add(2.0, 7.0);
        series4.add(3.0, 8.0);
        series4.add(4.0, 4.0);
        series4.add(7.0, 5.0);
        series4.add(8.0, 2.0);
        series4.add(9.0, 6.0);
        series4.add(10.0, 3.0);

        final XYSeriesCollection dataset = new XYSeriesCollection();
        dataset.addSeries(series1);// add series to dataset
        return dataset;      
   // createChart() returns chart
    private JFreeChart createChart(final XYDataset dataset) {
        final JFreeChart chart = ChartFactory.createXYLineChart(
            "Line Chart",    

        final XYPlot plot = chart.getXYPlot();
        final XYLineAndShapeRenderer renderer = new XYLineAndShapeRenderer();
        renderer.setSeriesLinesVisible(0, false);
        renderer.setSeriesShapesVisible(1, false);
        final NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
        return chart;      
 // main method
    public static void main(final String[] args) {
        final Main chart = new Main("Line Chart");



  1. if u get any problem then u can post ur comments here.

  2. hello sir, i need a line chart in java With Time, On X axis-Time and On Y-Axis-Numeric Value, Both the axis value will be retrieve from database , sir please help me , i am facing problem in it.. if possibel plz make it dyanamic..
    Thank You..

  3. hello Adish, i have another blog which is doing what u want. but its in JSP. i think u can have a fair idea of the functioning from this example. if u still face any problem then let me know i will try to explain you.