jfreeChart 例子
package demo;
import java.awt.Color;
import java.awt.Font;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Random;
import javax.servlet.http.HttpSession;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartRenderingInfo;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.DateAxis;
import org.jfree.chart.axis.DateTickUnit;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.axis.NumberTickUnit;
import org.jfree.chart.entity.StandardEntityCollection;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.servlet.ServletUtilities;
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.data.time.Day;
import org.jfree.data.time.TimeSeries;
import org.jfree.data.time.TimeSeriesCollection;
import org.jfree.data.xy.XYDataset;
public class PieDemo {
/**
* @param args
* 做一个饼状图
*/
private static PieDataset getDataSet() {
DefaultPieDataset dataset = new DefaultPieDataset();
dataset.setValue("收油量", 100111);
dataset.setValue("特殊用油量", 320220);
dataset.setValue("生产用油量", 30220);
return dataset;
}
//柱状图的数据集合
//堆栈图的数据集合
//线性图的数据集合
private static CategoryDataset getDataSet1() {
DefaultCategoryDataset dataset1 = new DefaultCategoryDataset();
dataset1.addValue(50000, "生产用油量", "2010-08-20");
dataset1.addValue(1000, "特殊用油量", "2010-08-20");
dataset1.addValue(1000, "收油量", "2010-08-20");
dataset1.addValue(4000, "生产用油量", "2010-08-24");
dataset1.addValue(6000, "特殊用油量", "2010-08-24");
dataset1.addValue(7000, "收油量", "2010-08-24");
dataset1.addValue(4000, "生产用油量", "2010-08-28");
dataset1.addValue(6000, "特殊用油量", "2010-08-28");
dataset1.addValue(7000, "收油量", "2010-08-28");
return dataset1;
}
//线性图
public static String generateLineChart(HttpSession session ,PrintWriter pw, int w,int h){
String filename = null;
JFreeChart chart = ChartFactory.createLineChart(
"井队月统计报表", //图表标题
"", //横轴标题
"", //纵轴标题
getDataSet1(), //绘图数据集
PlotOrientation.VERTICAL, //定义区域图的方向为纵向
true, //是否显示图例标识
true, //是否显示tooltips
false); //是否支持超链接 //设置标题字体
chart.getTitle().setFont(new Font("隶书",Font.BOLD, 25));
//设置图例类别字体
chart.getLegend().setItemFont(new Font("宋体", Font.BOLD, 15));
//设置背景色
chart.setBackgroundPaint(new Color(160,214,248));
//获取绘图区对象
CategoryPlot plot = chart.getCategoryPlot();
try {
//------得到chart的保存路径----
ChartRenderingInfo info = new ChartRenderingInfo
(new StandardEntityCollection());
filename = ServletUtilities.saveChartAsJPEG(chart, w, h, info,session);
//------使用printWriter将文件写出----
ChartUtilities.writeImageMap(pw, filename, info, true);
pw.flush();
} catch (IOException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
return filename;
}
//堆栈图
public static String generateStockChart(HttpSession session ,PrintWriter pw, int w,int h){
String filename = null;
JFreeChart chart = ChartFactory.createStackedBarChart3D(
"井队月统计报表", //图表标题
"", //横轴标题
"", //纵轴标题
getDataSet1(), //绘图数据集
PlotOrientation.VERTICAL, //定义区域图的方向为纵向
true, //是否显示图例标识
true, //是否显示tooltips
false); //是否支持超链接 //设置标题字体
chart.getTitle().setFont(new Font("隶书", Font.BOLD, 25));
//设置图例类别字体
chart.getLegend().setItemFont(new Font("宋体", Font.BOLD, 15));
//设置背景色
chart.setBackgroundPaint(new Color(160,214,248));
//获取绘图区对象
CategoryPlot plot = chart.getCategoryPlot();
try {
//------得到chart的保存路径----
ChartRenderingInfo info = new ChartRenderingInfo
(new StandardEntityCollection());
filename = ServletUtilities.saveChartAsJPEG(chart, w, h, info,session);
//------使用printWriter将文件写出----
ChartUtilities.writeImageMap(pw, filename, info, true);
pw.flush();
} catch (IOException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
return filename;
}
//时序图的数据集合
private static XYDataset getDataSet2(){
//实例化TimeSeries对象
TimeSeries timeseries = new TimeSeries("Data");
Day day = new Day(1, 1, 2008); //实例化Day
double d = 3000D;
//添加一年365天的数据
for (int i = 0; i < 365; i++) {
d = d + (Math.random() - 0.5) * 10;
//创建随机数据
timeseries.add(day, d);
//向数据集合中添加数据
day = (Day) day.next();
}
//创建TimeSeriesCollection集合对象
TimeSeriesCollection timeSeriesCollection =new TimeSeriesCollection(timeseries);
//返回数据集合对象
return timeSeriesCollection;
}
//区域图的数据集合
public static CategoryDataset createDataset() {
//实例化DefaultCategoryDataset对象
DefaultCategoryDataset defaultcategorydataset= new DefaultCategoryDataset();
Random random = new Random(); //创建Random对象
//向数据集合加入6个月的数据
for (int i = 1; i < 7; i++) {
defaultcategorydataset.addValue(random.nextInt(5000) + 5000, "收油量", i + "月份");
defaultcategorydataset.addValue(random.nextInt(5000) + 5000, "剩余油量", i + "月份");
defaultcategorydataset.addValue(random.nextInt(5000) + 5000, "特殊用油量", i + "月份");
}
return defaultcategorydataset;
}
//堆栈图的数据集合
//饼图
/* public static String generatePieChart(HttpSession session, PrintWriter pw,
int w, int h){
String filename = null;
PieDataset dataset = getDataSet();
//2D图
JFreeChart chart = ChartFactory.createPieChart(
"生产用油量", // 图表标题
dataset, // 数据集
true, // 是否显示图例
true, // 是否生成工具
false // 是否生成URL链接
);
//3D图
JFreeChart chart = ChartFactory.createPieChart3D(
"生产用油量", // 图表标题
dataset, // 数据集
true, // 是否显示图例
true, // 是否生成工具
false // 是否生成URL链接
);
chart.setBackgroundPaint(Color.pink);
try {
------得到chart的保存路径----
ChartRenderingInfo info = new ChartRenderingInfo(new StandardEntityCollection());
filename = ServletUtilities.saveChartAsJPEG(chart, w, h, info,session);
------使用printWriter将文件写出----
ChartUtilities.writeImageMap(pw, filename, info, true);
pw.flush();
} catch (IOException e) {
e.printStackTrace();
}
return filename;
}*/
//柱状图
public static String generateBarChart(HttpSession session, PrintWriter pw,
int w, int h){
String filename = null;
CategoryDataset dataset = getDataSet1();
JFreeChart chart = ChartFactory.createBarChart3D(
"井队日报表", // 图表标题
"2010-08-17", // 目录轴的显示标签
"50", // 数值轴的显示标签
dataset, // 数据集
PlotOrientation.VERTICAL, // 图表方向:水平、垂直
true, // 是否显示图例(对于简单的柱状图必须是false)
false, // 是否生成工具
false // 是否生成URL链接
);
try {
//------得到chart的保存路径----
ChartRenderingInfo info = new ChartRenderingInfo
(new StandardEntityCollection());
CategoryPlot plot = chart.getCategoryPlot();
NumberAxis vn = (NumberAxis) plot.getRangeAxis();//数据轴精度
vn.setAutoRangeIncludesZero(true);//设置刻度必须从0开始
NumberTickUnit ntu= new NumberTickUnit(100);
vn.setTickUnit(ntu);
filename = ServletUtilities.saveChartAsPNG(chart, w, h, info,session);
//------使用printWriter将文件写出----
ChartUtilities.writeImageMap(pw, filename, info, true);
pw.flush();
} catch (IOException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
return filename;
}
//时序图
public static String generateXYChart(HttpSession session ,PrintWriter pw, int w,int h){
String filename = null;
//创建时序图对象
JFreeChart chart = ChartFactory.createTimeSeriesChart(
"编程词典全国销量统计", //标题
"销售月份", //时间轴标签
"销量(份)", //数据轴标签
getDataSet2(), //数据集合
false, //是否显示图例标识
false, //是否显示tooltips
false); //是否支持超链接 //设置标题字体
chart.getTitle().setFont(new Font("隶书", Font.BOLD, 26)); //设置背景色
chart.setBackgroundPaint(new Color(252,175,134));
XYPlot plot = chart.getXYPlot(); //获取图表的绘制属性
plot.setDomainGridlinesVisible(false); //设置网格不显示
//获取时间轴对象
DateAxis dateAxis = (DateAxis) plot.getDomainAxis();
//dateAxis.setLabelFont(PLOT_FONT); //设置时间轴字体
//设置时间轴标尺值字体
dateAxis.setTickLabelFont(new Font("宋体",Font.PLAIN,12));
dateAxis.setLowerMargin(0.0); //设置时间轴上显示的最小值
//获取数据轴对象 ValueAxis valueAxis = plot.getRangeAxis();
//valueAxis.setLabelFont(PLOT_FONT); //设置数据字体
DateFormat format = new SimpleDateFormat("MM月份"); //创建日期格式对象
//创建DateTickUnit对象
DateTickUnit dtu = new DateTickUnit(DateTickUnit.DAY,29,format);
dateAxis.setTickUnit(dtu); //设置日期轴的日期标签
try {
//------得到chart的保存路径----
ChartRenderingInfo info = new ChartRenderingInfo
(new StandardEntityCollection());
filename = ServletUtilities.saveChartAsPNG(chart, w, h, info,session);
//------使用printWriter将文件写出----
ChartUtilities.writeImageMap(pw, filename, info, true);
pw.flush();
} catch (IOException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
return filename;
}
//区域图
public static String generateAddressChart(HttpSession session ,PrintWriter pw, int w,int h){
String filename = null;
JFreeChart chart = ChartFactory.createAreaChart(
"井队月统计报表", //图表标题
"", //横轴标题
"", //纵轴标题
createDataset(), //绘图数据集
PlotOrientation.VERTICAL, //定义区域图的方向为纵向
true, //是否显示图例标识
true, //是否显示tooltips
false); //是否支持超链接 //设置标题字体
chart.getTitle().setFont(new Font("隶书", Font.BOLD, 25));
//设置图例类别字体
chart.getLegend().setItemFont(new Font("宋体", Font.BOLD, 15));
//设置背景色
chart.setBackgroundPaint(new Color(160,214,248));
//获取绘图区对象
CategoryPlot plot = chart.getCategoryPlot();
try {
//------得到chart的保存路径----
ChartRenderingInfo info = new ChartRenderingInfo
(new StandardEntityCollection());
filename = ServletUtilities.saveChartAsPNG(chart, w, h, info,session);
//------使用printWriter将文件写出----
ChartUtilities.writeImageMap(pw, filename, info, true);
pw.flush();
} catch (IOException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
return filename;
}
}