2016년 4월 12일 화요일

JFreechart JDBC Bar Chart Example

Java and JFreechart with JDBC (which is MySQL java driver)

1) Download following JAR files

  • jfreechart-x.y.z.jar   (with jcommon-x.y.z.jar)
  • ojdbc6.jar
  • jdbc driver for MySQL Version x.y


2) Create sample DB  table
/* Create Chart table */
create table chart_Data
(
category varchar2(20),
marks number
)
/* Insert data - Need COMMIT */
insert into chart_data values ('English',67)
insert into chart_data values ('Physics',67)
insert into chart_data values ('Chemistry',67)
insert into chart_data values ('Biology',67)
insert into chart_data values ('C',67)
/* Randomize mark values - Needs COMMIT */
update chart_data set marks=TRUNC(DBMS_RANDOM.value(40,99))


3) Make a JAVA code like a following.

================================================================
import java.sql.*;
import java.io.*;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.ChartUtilities;


public class BarChartJDBCExample {

    public static void main(String[] args) throws Exception{

        Class.forName("com.mysql.jdbc.Driver");
        Connection conn = DriverManager.getConnection("jdbc:mysql://10.10.2.11:3306/andrew_db", "andrew", "andrew");
        DefaultCategoryDataset my_bar_chart_dataset = new DefaultCategoryDataset();
        Statement stmt = conn.createStatement();
        try {
            ResultSet query_set = stmt.executeQuery("select * from MSS_RAW_MSG_5M_TBL where STAT_TIME > '2016-04-09';");
            while (query_set.next()) {
                String stat_time = query_set.getString("STAT_TIME");
                int call_setup = query_set.getInt("CALL_SETUP");
                int call_term  = query_set.getInt("CALL_TERM");
                int regi       = query_set.getInt("REGI");
                my_bar_chart_dataset.addValue(call_setup, "CALL_SETUP", stat_time);
                my_bar_chart_dataset.addValue(call_term, "CALL_TERM", stat_time);
                my_bar_chart_dataset.addValue(regi, "REGISTER", stat_time);
            }
            JFreeChart BarChartObject = ChartFactory.createLineChart("INA System", "Date", "TPS", my_bar_chart_dataset, PlotOrientation.VERTICAL, true, true, false);

            query_set.close();
            stmt.close();
            conn.close();
            int width  = 1920; /* Width of the image */
            int height = 1080; /* Height of the image */
            File BarChart = new File("output_chart.png");
            ChartUtilities.saveChartAsPNG(BarChart, BarChartObject, width, height);
        }
        catch (Exception i)
        {
            System.out.println(i);
        }

    }
}
==================================================================


I referred following blog.

< Java Chart Example >
http://thinktibits.blogspot.kr/p/java-chart-example-programs.html

http://thinktibits.blogspot.kr/2013/01/JFreeChart-XYDataset-Line-Chart-Java-Example.html

http://thinktibits.blogspot.kr/2013/01/JFreeChart-SVG-XY-Line-Chart-Graph-Java-Example.html




< Bar Chart >
http://thinktibits.blogspot.kr/2012/11/jfreechart-java-jdbc-bar-chart-example.html


< Pie Chart >
http://thinktibits.blogspot.kr/2012/11/jfreechart-jdbc-pie-chart-java-example.html

댓글 없음:

댓글 쓰기