tl  tr
  Home | Tutorials | Articles | Videos | Products | Tools | Search
Interviews | Open Source | Tag Cloud | Follow Us | Bookmark | Contact   
 Charts > JFreeChart > JDBC Pie Chart

JDBC Pie Chart 

JFreeChart is a free and pure java chart library for creating professional quality charts. This requires the libraries jfreechart-1.0.13.jar and jcommon-1.0.8.jar to be in classpath. The following example shows creating a simple pie chart from JDBCPieDataset. It uses H2 database to fetch the data required for rendering the pie chart. The BUG_STAT table is shown below,

bug_stat.jpg

File Name  :  
com/bethecoder/tutorials/jfree_charts/tests/JDBCPieChart.java 
Author  :  Sudhakar KV
Email  :  [email protected]
   
package com.bethecoder.tutorials.jfree_charts.tests;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

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

public class JDBCPieChart{
  
  public static void main(String arg[]) throws ClassNotFoundException, SQLException{

    /**
     * Initialize H2 database driver and get connection for JDBCPieDataset
     */
    Class.forName("org.h2.Driver");
    Connection conn = DriverManager.getConnection(
        "jdbc:h2:tcp://localhost/~/test""sa""");
    DefaultPieDataset pieDataset = new JDBCPieDataset(
        conn, "select STATUS, COUNT from BUG_STAT");
    
    //Create the chart
    JFreeChart chart = ChartFactory.createPieChart(
      "Bug Stat Pie Chart", pieDataset, true, true, true);

    //Render the frame
    ChartFrame chartFrame = new ChartFrame("JDPC Pie Chart", chart);
    chartFrame.setVisible(true);
    chartFrame.setSize(420300);
  }
}
   

It gives the following output,
piejdbc.jpg


 
  


  
bl  br