Aqui a Base de Dados utilizada:
CREATE DATABASE jf_testdb;
USE jf_testdb;
CREATE TABLE mobile_tbl(
id INT( 20 ) AUTO_INCREMENT,
mobile_brand VARCHAR(100) NOT NULL,
unit_sale INT(20) NOT NULL,
PRIMARY KEY (id)
);
INSERT INTO mobile_tbl (mobile_brand, unit_sale) VALUES ('IPhone5S', 20);
INSERT INTO mobile_tbl (mobile_brand, unit_sale) VALUES ('Samsung Grand', 20);
INSERT INTO mobile_tbl (mobile_brand, unit_sale) VALUES ('MotoG', 40);
INSERT INTO mobile_tbl (mobile_brand, unit_sale) VALUES ('Nokia Lumia', 10);
SELECT * FROM mobile_tbl;
Aqui o código para gerar Pizza (pie):
package modulojfreechart3;
//
import java.io.*;
import java.sql.*;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.data.general.DefaultPieDataset;
/**
*
* @Marcel
*/
public class ModuloJFreeChart3 {
/**
* @param args the command line arguments
*/
public static void main(String[] args) throws Exception{
String mobilebrands[] = {
"IPhone 5s",
"SamSung Grand",
"MotoG",
"Nokia Lumia"
};
/* Create MySQL Database Connection */
Class.forName( "com.mysql.jdbc.Driver" );
Connection connect = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/jf_testdb" , "root", "123");
Statement statement = connect.createStatement( );
ResultSet resultSet = statement.executeQuery("select * from mobile_tbl" );
DefaultPieDataset dataset = new DefaultPieDataset( );
while( resultSet.next( ) ) {
dataset.setValue(
resultSet.getString( "mobile_brand" ) ,
Integer.parseInt( resultSet.getString( "unit_sale" )));
}
JFreeChart chart = ChartFactory.createPieChart(
"Mobile Sales", // chart title
dataset, // data
true, // include legend
true,
false );
int width = 560; /* Width of the image */
int height = 370; /* Height of the image */
File pieChart = new File( "Pie_Chart.jpeg" );
ChartUtilities.saveChartAsJPEG( pieChart , chart , width , height );
}
}
Aqui a jpeg gerada:
http://i66.tinypic.com/15wcg8m.jpgc.com/15wcg8m.jpg[/IMG]