package com.chart;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.io.FileOutputStream;
import java.io.PrintWriter;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.Date;
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.entity.StandardEntityCollection;
import org.jfree.chart.labels.StandardPieItemLabelGenerator;
import org.jfree.chart.plot.PiePlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.servlet.ServletUtilities;
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.time.Minute;
import org.jfree.data.time.TimePeriodValues;
import org.jfree.data.time.TimePeriodValuesCollection;
import org.jfree.data.xy.XYDataset;
public class ChartExample {
/*****
* 生成柱形图的实例,保存到Jpg文件中
*/
public void createChar3D()
{
CategoryDataset dataset = getDataSet2();
JFreeChart chart = ChartFactory.createBarChart3D(
"水果产量图", // 图表标题
"水果", // 目录轴的显示标签
"产量", // 数值轴的显示标签
dataset, // 数据集
PlotOrientation.VERTICAL, // 图表方向:水平、垂直
true, // 是否显示图例(对于简单的柱状图必须是false)
false, // 是否生成工具
false // 是否生成URL链接
);
FileOutputStream fos_jpg = null;
try
{
fos_jpg = new FileOutputStream("e:/123.jpg");
ChartUtilities.writeChartAsJPEG(fos_jpg,0.8f,chart,400,300);
}catch(Exception e)
{
e.printStackTrace();
}
finally
{
try
{
fos_jpg.close();
}catch(Exception e)
{
e.printStackTrace();
}
}
}
/**
* 获取一个演示用的组合数据集对象 (柱形图)
* @return
*/
private static CategoryDataset getDataSet2()
{
DefaultCategoryDataset dataset = new DefaultCategoryDataset();
dataset.addValue(100, "北京", "苹果");
dataset.addValue(100, "上海", "苹果");
dataset.addValue(100, "广州", "苹果");
dataset.addValue(200, "北京", "梨子");
dataset.addValue(200, "上海", "梨子");
dataset.addValue(200, "广州", "梨子");
dataset.addValue(300, "北京", "葡萄");
dataset.addValue(300, "上海", "葡萄");
dataset.addValue(300, "广州", "葡萄");
dataset.addValue(400, "北京", "香蕉");
dataset.addValue(400, "上海", "香蕉");
dataset.addValue(400, "广州", "香蕉");
dataset.addValue(500, "北京", "荔枝");
dataset.addValue(500, "上海", "荔枝");
dataset.addValue(500, "广州", "荔枝");
return dataset;
}
/***
* 根据时间生成曲线图,保存到jpg中
*
*/
public static void createTimeChart()
{
XYDataset xydataset = createDataset();
JFreeChart chart = ChartFactory.createTimeSeriesChart(
"nihao",
"hello",
"123",
xydataset,
true,
true,
true);
FileOutputStream fos_jpg = null;
try
{
fos_jpg = new FileOutputStream("e:/123.jpg");
ChartUtilities.writeChartAsJPEG(fos_jpg,0.8f,chart,400,300);
}catch(Exception e)
{
e.printStackTrace();
}
finally
{
try
{
fos_jpg.close();
}catch(Exception e)
{
e.printStackTrace();
}
}
}
/****
* 获取一个演示用的组合数据集对象 (时间曲线图)
* @return
*/
private static XYDataset createDataset()
{
TimePeriodValues timeseries1 = new TimePeriodValues("Chart Title");
//这地方可以使用循环,把x轴,y轴 的值 辅给timeseries1
TimePeriodValuesCollection timeseriescollection = new TimePeriodValuesCollection();
for(int i =1;i<10;i++)
{
Minute min = new Minute(new Date());
timeseries1.add(min, i);
timeseriescollection.addSeries(timeseries1);
}
return timeseriescollection;
}
/***********饼状图*********/
private DefaultPieDataset data =new DefaultPieDataset();
private Font titleFont = new Font("黑体",Font.CENTER_BASELINE,18);
/****
* 设置一个演示用的组合数据集对象 (饼状图)
* @return
*/
public void setValue(String key,double value)
{
data.setValue(key,value);
}
/***
* 根据时间生成病状图,显示到jsp页面
*
*/
public String generatePieChart(String title, HttpSession session, PrintWriter pw)
{
String filename = null;
String unitSytle = "{0}={1}({2})";
try {
JFreeChart chart = ChartFactory.createPieChart3D("饼型图", // chart title
data, // data
true, // include legend
true,
false
);
//设置图片的背景色
chart.setBackgroundPaint(new Color(215,215,215));
//设置透明度,好像对servlet没有用
chart.setBackgroundImageAlpha(0.9f);
//设置图片标题的字体和大小
TextTitle _title = new TextTitle(title);
_title.setFont(titleFont);
chart.setTitle(_title);
PiePlot plot = (PiePlot) chart.getPlot();
plot.setNoDataMessage("无对应的数据,请重新查询。");
plot.setNoDataMessagePaint(Color.red);
//指定 section 轮廓线的厚度(OutlinePaint不能为null)
plot.setOutlineStroke(new BasicStroke(0));
//设置第一个 section 的开始位置,默认是12点钟方向
plot.setStartAngle(90);
plot.setToolTipGenerator(new StandardPieItemLabelGenerator(unitSytle,
NumberFormat.getNumberInstance(),
new DecimalFormat("0.00%")));
//指定图片的透明度
plot.setForegroundAlpha(0.65f);
//引出标签显示样式
plot.setLabelGenerator(new StandardPieItemLabelGenerator(unitSytle,
NumberFormat.getNumberInstance(),
new DecimalFormat("0.00%")));
//图例显示样式
plot.setLegendLabelGenerator(new StandardPieItemLabelGenerator(unitSytle,
NumberFormat.getNumberInstance(),
new DecimalFormat("0.00%")));
//把生成的图片放到临时目录
ChartRenderingInfo info = new ChartRenderingInfo(new StandardEntityCollection());
//设置图片名称前缀
ServletUtilities.setTempFilePrefix("chart-");
//500是图片长度,300是图片高度
filename = ServletUtilities.saveChartAsPNG(chart, 500, 300, info, session);
ChartUtilities.writeImageMap(pw, filename, info, false);
pw.flush();
System.out.println(System.getProperty("java.io.tmpdir") + filename);
} catch (Exception e) {
System.out.println("Exception - " + e.toString());
e.printStackTrace(System.out);
filename = "public_error_600x350.png";
}
return filename;
}
/**
* @param args
*/
public static void main(String[] args)
{
createTimeChart();
}
}
/**************************饼形图的jsp页面***************************************/
<%@ page contentType="text/html;charset=GBK"%>
<%@ page import ="java.io.PrintWriter" %>
<%@ page import="com.chart.ChartExample"%>
<%
ChartExample chart = new ChartExample();
chart.setValue("六月",500);
chart.setValue("七月",580);
chart.setValue("八月",828);
String filename = chart.generatePieChart("www.SenTom.net 网站访问统计表",session, new PrintWriter(out));
String graphURL = request.getContextPath() +"/servlet/DisplayChart?filename=" + filename;
%>
<HTML>
<HEAD>
<TITLE>www.sentom.net</TITLE>
</HEAD>
<BODY>
<P ALIGN="CENTER">
<img src="<%= graphURL %>" width=500 height=300 border=0 usemap="#<%=filename%>"></P>
</BODY>
</HTML>