标签:jfreechart database myeclipse
import org.jfree.ui.ApplicationFrame;
import iaqms.com.CustomRenderer;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.swing.JPanel;
import java.awt.Color;
import java.awt.Font;
import java.awt.GradientPaint;
import java.awt.RenderingHints;
import javax.swing.JPanel;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartFrame;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.StandardChartTheme;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.CategoryLabelPositions;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.axis.NumberTickUnit;
import org.jfree.chart.labels.StandardCategoryItemLabelGenerator;
import org.jfree.chart.labels.StandardPieSectionLabelGenerator;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PiePlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.RingPlot;
import org.jfree.chart.renderer.category.BarRenderer;
import org.jfree.chart.title.LegendTitle;
import org.jfree.chart.title.TextTitle;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.data.general.DefaultPieDataset;
import org.jfree.data.general.PieDataset;
import org.jfree.ui.RefineryUtilities;
public class PieChartDB extends ApplicationFrame {
private static final long serialVersionUID = 1L;
public static void main(String[] args) {
PieChartDB piechart = new PieChartDB("饼状图");
piechart.pack();
RefineryUtilities.centerFrameOnScreen(piechart);
piechart.setVisible(true);
}
//构造函数产生实例
public PieChartDB(String number){
super(number);
setContentPane(createpiePanel());
}
//生成报表的面板
public static JPanel createpiePanel() {
JFreeChart jfreechart = createChart(createDataset());
return new ChartPanel(jfreechart, false);
}
private static JFreeChart createChart(PieDataset dataset){
//解决乱码问题(不能显示中文的问题)
StandardChartTheme mChartTheme = new StandardChartTheme("CN");
mChartTheme.setLargeFont(new Font("黑体", Font.BOLD, 20));
mChartTheme.setExtraLargeFont(new Font("宋体", Font.PLAIN, 15)); //标题
mChartTheme.setRegularFont(new Font("宋体", Font.PLAIN, 15));
ChartFactory.setChartTheme(mChartTheme);
//生成饼状图的对象
JFreeChart chart = ChartFactory.createPieChart(
"图书销量统计图",//图标标题
dataset, //数据
true,//是否显示图例
false,//是否显示工具提示
false//是否生成url
);
//设置一系列的属性
//获得饼图的Plot对象
PiePlot plot = (PiePlot)chart.getPlot();
//用来显示标注的注解,{0}:{1}用来设置显示的格式
plot.setLabelGenerator(new StandardPieSectionLabelGenerator("{0}:{1}"));
plot.setBackgroundPaint(Color.gray);//设置背景色
//设置简单标签
plot.setSimpleLabels(true);
//标题
TextTitle texttitle=chart.getTitle();
texttitle.setFont(new Font("宋体", Font.BOLD, 30));
//图示
LegendTitle legendtitle =chart.getLegend();
legendtitle.setItemFont(new Font("宋体", Font.BOLD, 14));
//设定背景透明度(0-1.0之间)
plot.setBackgroundAlpha(0.5f);
//设定前景透明度(0-1.0之间)
plot.setForegroundAlpha(0.60f);
return chart;
}
private static PieDataset createDataset() {
double[][] table = null;
String[] name = null;
String[] number = null;
int result = 0;
try{
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/sale", "root", "fab");
ResultSet rs = conn.createStatement().executeQuery("select name,number from sale");
// 取得行数
if (rs.last()){
result = rs.getRow();
rs.beforeFirst(); // 指针回滚
}
table = new double[result][rs.getMetaData().getColumnCount() - 1];// 定义存放数据的二维数组
name = new String[result];
number = new String[rs.getMetaData().getColumnCount() - 1]; // 定义存放数据表头的一维数组
rs.next();
for (int j = 0; j < rs.getMetaData().getColumnCount() - 1; j++) {
number[j] = rs.getMetaData().getColumnName(j + 2); // 取出表头并存放数组
}
int i = 0;
rs.beforeFirst();
while (rs.next()) {
name[i] = rs.getString(1);
for (int j = 0; j < rs.getMetaData().getColumnCount() - 1; j++)
table[i][j] = rs.getDouble(j + 2); // 取出数据并存入二维数组
i++;
}
conn.close(); // 关闭连接
rs.close(); // 关闭查询
} catch (Exception ex) {
System.err.println("Exception:" + ex.getMessage());
}
//创建DefaultPieDataset类型的数据集,并行数据集中添加数据(从数据库中取的数据)
DefaultPieDataset dataset = new DefaultPieDataset();
for (int i = 0; i < number.length; i++)
for (int j = 0; j < result; j++) {
dataset.setValue(name[j],table[j][i] );
//调试用
System.out.println(table[j][i]);
System.out.println(name[j]);
}
return dataset;
}
}
数据库脚本:
CREATE DATABASE /*!32312 IF NOT EXISTS*/`sale` /*!40100 DEFAULT CHARACTER SET gbk */;
USE `sale`;
DROP TABLE IF EXISTS `sale`;
CREATE TABLE `sale` (
`name` varchar(150) default NULL COMMENT ‘书名‘,
`number` varchar(150) default NULL COMMENT ‘数量‘
) ENGINE=InnoDB DEFAULT CHARSET=gbk;
insert into `sale`(`name`,`number`) values (‘C++‘,‘18‘),(‘Java‘,‘20‘),(‘数据结构‘,‘30‘),(‘安桌‘,‘40‘),(‘IOS‘,‘25‘),(‘计算机网络‘,‘28‘);
运行结果图:
JFreeChart与MySQL数据库结合使用
标签:jfreechart database myeclipse
原文地址:http://blog.csdn.net/u010289197/article/details/42776179