例子:
http://blog.csdn.net/Ami121/category/394379.aspx
饼图:
class BChart{
public static void main(String[] args){
PieDataset dataset = getDataSet();//设置数据源
JFreeChart chart = ChartFactory.createPieChart3D(
"时延分布统计图", // chart title
dataset,// data
true,// include legend
true,
false
);
PiePlot3D plot=(PiePlot3D)chart.getPlot();
// 图片中显示百分比:默认方式
//plot.setLabelGenerator(new StandardPieSectionLabelGenerator(StandardPieToolTipGenerator.DEFAULT_TOOLTIP_FORMAT));
// 图片中显示百分比:自定义方式,{0} 表示选项, {1} 表示数值, {2} 表示所占比例 ,小数点后两位
plot.setLabelGenerator(new StandardPieSectionLabelGenerator("{0}={1}({2})", NumberFormat.getNumberInstance(), new DecimalFormat("0.00%")));
// 图例显示百分比:自定义方式, {0} 表示选项, {1} 表示数值, {2} 表示所占比例
plot.setLegendLabelGenerator(new StandardPieSectionLabelGenerator("{0}={1}({2})"));
// 设置背景色为白色
chart.setBackgroundPaint(Color.white);
// 指定图片的透明度(0.0-1.0)
plot.setForegroundAlpha(1.0f);
// 指定显示的饼图上圆形(false)还椭圆形(true)
plot.setCircular(true);
// 设置图标题的字体
Font font = new Font(" 黑体",Font.CENTER_BASELINE,20);
TextTitle title = new TextTitle(" 时延分布统计图");
title.setFont(font);
chart.setTitle(title);
FileOutputStream fos_jpg = null;
try {
fos_jpg=new FileOutputStream("D:\\时延分布统计图.jpg");
ChartUtilities.writeChartAsJPEG(fos_jpg,100,chart,640,480,null);
fos_jpg.close();
} catch (Exception e) {
}
}
private static PieDataset getDataSet()//数据源构造
{
DefaultPieDataset dataset = new DefaultPieDataset();
dataset.setValue("login.jsp",50);
dataset.setValue("reg.jsp",60);
return dataset;
}
}
折线图:
class ZChart{
public static void main(String[] args)
{
JFreeChart chart = ChartFactory.createXYLineChart("固定uri时延分布图", "历次访问点",
"时延/ms", createDataSet(special), PlotOrientation.VERTICAL, true,
true, false);
FileOutputStream fos_jpg = null;
try {
fos_jpg=new FileOutputStream("D:\\mao.jpg");
ChartUtilities.writeChartAsJPEG(fos_jpg,100,chart,640,480,null);
fos_jpg.close();
} catch (Exception e) {
}
}
private static XYSeriesCollection createDataSet() {//数据源构造方法
XYSeriesCollection seriesCollection = new XYSeriesCollection();
XYSeries series1 = new XYSeries("login.jsp");
seriesCollection.addSeries(series1);
series1.add(1, 75);
series1.add(2, 41);
series1.add(3, 39);
series1.add(4, 33);
series1.add(5, 25);
series1.add(6, 36);
seriesCollection.addSeries(series1);
return seriesCollection;
}
}
曲线图:
import java.awt.Color;
import java.awt.Font;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
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.NumberAxis;
import org.jfree.chart.entity.StandardEntityCollection;
import org.jfree.chart.labels.StandardXYToolTipGenerator;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYItemRenderer;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.chart.servlet.ServletUtilities;
import org.jfree.chart.title.LegendTitle;
import org.jfree.chart.title.TextTitle;
import org.jfree.data.time.Day;
import org.jfree.data.time.TimeSeries;
import org.jfree.data.time.TimeSeriesCollection;
import org.jfree.data.xy.XYDataset;
import org.jfree.ui.RectangleInsets;
/**
* 曲线图的绘制
*/
public class LineXYChart {
/**
* 返回生成图片的文件名
*
* @param session
* @param pw
* @return 生成图片的文件名
*/
public String getLineXYChart(HttpSession session, PrintWriter pw,ArrayList data) {
XYDataset dataset = this.createDateSet(data);// 建立数据集
String fileName = null;
// 建立JFreeChart
JFreeChart chart = ChartFactory.createTimeSeriesChart(
"销售时间曲线序列图", // title
"Date", // x-axis label
"Price", // y-axis label
dataset, // data
true, // create legend?
true, // generate tooltips?
false // generate URLs?
);
// 设置JFreeChart的显示属性,对图形外部部分进行调整
chart.setBackgroundPaint(Color.red);// 设置曲线图背景色
// 设置字体大小,形状
Font font = new Font("宋体", Font.BOLD, 12);
TextTitle title = new TextTitle("销售时间曲线序列图", font);
chart.setTitle(title);
// 副标题
//TextTitle subtitle = new TextTitle(deptname, new Font("黑体", Font.BOLD, 12));
//chart.addSubtitle(subtitle);
chart.setTitle(title); // 标题
// 设置图示标题字符
// TimeSeries s1 = new TimeSeries("历史曲线", Day.class);该中文字符
LegendTitle legengTitle = chart.getLegend();
legengTitle.setItemFont(font);
XYPlot plot = (XYPlot) chart.getPlot();// 获取图形的画布
plot.setBackgroundPaint(Color.lightGray);// 设置网格背景色
plot.setDomainGridlinePaint(Color.green);// 设置网格竖线(Domain轴)颜色
plot.setRangeGridlinePaint(Color.white);// 设置网格横线颜色
plot.setAxisOffset(new RectangleInsets(5.0, 5.0, 5.0, 5.0));// 设置曲线图与xy轴的距离
plot.setDomainCrosshairVisible(true);
plot.setRangeCrosshairVisible(true);
XYItemRenderer r = plot.getRenderer();
if (r instanceof XYLineAndShapeRenderer) {
XYLineAndShapeRenderer renderer = (XYLineAndShapeRenderer) r;
renderer.setBaseShapesVisible(true);
renderer.setBaseShapesFilled(true);
renderer.setShapesVisible(true);// 设置曲线是否显示数据点
}
// 设置Y轴
NumberAxis numAxis = (NumberAxis) plot.getRangeAxis();
NumberFormat numFormater = NumberFormat.getNumberInstance();
numFormater.setMinimumFractionDigits(2);
numAxis.setNumberFormatOverride(numFormater);
// 设置提示信息
StandardXYToolTipGenerator tipGenerator = new StandardXYToolTipGenerator(
"历史信息{1} 16:00,{2})", new SimpleDateFormat("MM-dd"),
numFormater);
r.setToolTipGenerator(tipGenerator);
// 设置X轴(日期轴)
DateAxis axis = (DateAxis) plot.getDomainAxis();
axis.setDateFormatOverride(new SimpleDateFormat("MM-dd"));
ChartRenderingInfo info = new ChartRenderingInfo(
new StandardEntityCollection());
try {
fileName = ServletUtilities.saveChartAsPNG(chart, 500, 300, info,
session);// 生成图片
// Write the image map to the PrintWriter
ChartUtilities.writeImageMap(pw, fileName, info, false);
} catch (IOException e) {
e.printStackTrace();
}
pw.flush();
return fileName;// 返回生成图片的文件名
}
private XYDataset createDateSet(ArrayList al) {
TimeSeriesCollection dataset = new TimeSeriesCollection();// 时间曲线数据集合
TimeSeries s1 = new TimeSeries("销售曲线", Day.class);// 创建时间数据源,每一个//TimeSeries在图上是一条曲线
// s1.add(new Day(day,month,year),value),添加数据点信息
HashMap hm = null;
for (Iterator it = al.iterator(); it.hasNext();) {
hm = (HashMap) it.next();
String day=hm.get("day_nr").toString();
if(day.indexOf("总计")>-1){
}else{
int day_nr=Integer.parseInt(day);
int zmonth=Integer.parseInt(hm.get("zmonth").toString());
int zyear=Integer.parseInt(hm.get("zyear").toString());
int value=Integer.parseInt(hm.get("netwr").toString());
s1.add(new Day(day_nr, zmonth, zyear),value );
}
}
// s1.add(new Day(1, 2, 2006), 123.51);
// s1.add(new Day(2, 2, 2006), 122.1);
// s1.add(new Day(3, 2, 2006), 120.86);
// s1.add(new Day(4, 2, 2006), 122.50);
// s1.add(new Day(5, 2, 2006), 123.12);
// s1.add(new Day(6, 2, 2006), 123.9);
// s1.add(new Day(7, 2, 2006), 124.47);
// s1.add(new Day(8, 2, 2006), 124.08);
// s1.add(new Day(9, 2, 2006), 123.55);
// s1.add(new Day(10, 2, 2006), 122.53);
dataset.addSeries(s1);
dataset.setDomainIsPointsInTime(true);
return dataset;
}
}