java代码
package frdc.moss.jfreechart;
import java.awt.Color;
import java.awt.Font;
import java.awt.GradientPaint;
import java.awt.Rectangle;
import java.awt.Shape;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.servlet.jsp.JspWriter;
import javax.xml.ws.Response;
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.CategoryAxis;
import org.jfree.chart.axis.CategoryLabelPositions;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.entity.ChartEntity;
import org.jfree.chart.entity.StandardEntityCollection;
import org.jfree.chart.labels.ItemLabelAnchor;
import org.jfree.chart.labels.ItemLabelPosition;
import org.jfree.chart.labels.StandardCategoryItemLabelGenerator;
import org.jfree.chart.labels.StandardCategoryToolTipGenerator;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.BarRenderer;
import org.jfree.chart.servlet.ServletUtilities;
import org.jfree.chart.title.TextTitle;
import org.jfree.chart.urls.StandardCategoryURLGenerator;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.ui.TextAnchor;
/**
* Servlet implementation class Pie
*/
public class Histogram extends HttpServlet {
private static final long serialVersionUID = 1L;
int width;// 图象宽度
int height; // 图象高度
String chartTitle;// 图表标题
String subtitle;// 副标题
String xTitle;// X轴标题
String yTitle;// Y轴标题
String[] cutline;// 图例名称
String category[]; // 统计种类
Double[][] data;// 绘图数据
String servletURI ="/DisplayChart";// 映射路径
public Histogram() {
this.width = 500;
this.height = 400;
this.chartTitle = "编程类图书年销量柱形图分析";
this.subtitle = "------统计时间:2008年";
this.xTitle = "销售季度";
this.yTitle = "销售量 单位:万册";
this.cutline = new String[] { "ASP", "JSP", "PHP" };
this.category = new String[] { "第1季度", "第2季度", "第3季度", "第4季度" };
this.data = new Double[cutline.length][category.length];
for (int m = 0; m < cutline.length; m++) {
for (int n = 0; n < category.length; n++) {
data[m][n] = 1 + Math.random() * 100;
}
}
}
public Histogram( int width, int height, String chartTitle, String subtitle, String xTitle, String yTitle, String[] cutline, String[] category, Double[][] data ) {
this.width = width;
this.height = height;
this.chartTitle = chartTitle;
this.subtitle = subtitle;
this.xTitle = xTitle;
this.yTitle = yTitle;
this.cutline = cutline;
this.category = category;
this.data = data;
}
public Histogram(int width, int height, String chartTitle,String subtitle, String xTitle, String yTitle, String[] cutline, String[] category, Double[][] data, String servletURI) {
this.width = width;
this.height = height;
this.chartTitle = chartTitle;
this.subtitle = subtitle;
this.xTitle = xTitle;
this.yTitle = yTitle;
this.cutline = cutline;
this.category = category;
this.data = data;
this.servletURI = servletURI;
}
public String draw(HttpSession session, String contextPath,PrintWriter out) {
// 创建绘图数据集
DefaultCategoryDataset dataset = new DefaultCategoryDataset();
for (int m = 0; m < cutline.length; m++) {
for (int n = 0; n < category.length; n++) {
dataset.addValue(data[m][n], cutline[m], category[n]);
}
}
// 创建图表对象
JFreeChart chart = ChartFactory.createBarChart(
chartTitle, // 图表标题
xTitle, // X轴标题
yTitle, // Y轴标题
dataset, // 绘图数据集
PlotOrientation.VERTICAL,// 柱形图绘制方向
true, // 显示图例
true, // 采用标准生成器
true// 生成链接
);
// 自定义图表开始:
if (subtitle.length() > 0) {
chart.addSubtitle(new TextTitle(subtitle));// 添加副标题
}
GradientPaint chartGP = new GradientPaint(0, 0, new Color(219, 227, 251), 0, height, Color.WHITE, false);// 创建渐变色对象
chart.setBackgroundPaint(chartGP); // 设置图片背景色
// 通过绘图区对象,可以设置更多的绘图属性
CategoryPlot plot = chart.getCategoryPlot();
plot.setBackgroundPaint(new Color(241, 219, 127));// 设置绘图区背景色
plot.setRangeGridlinePaint(Color.RED);// 设置水平方向背景线颜色
plot.setRangeGridlinesVisible(true);// 设置是否显示水平方向背景线,默认值为true
plot.setDomainGridlinePaint(Color.RED);// 设置垂直方向背景线颜色
plot.setDomainGridlinesVisible(true);// 设置是否显示垂直方向背景线,默认值为false
// 通过柱形对象,可以设置柱形的绘图属性
BarRenderer renderer = (BarRenderer) plot.getRenderer();
renderer.setDrawBarOutline(true);// 设置是否绘制柱形的轮廓线,默认值为true
//Shape shape = new Rectangle(20, 10);
// ChartEntity entity = new ChartEntity(shape);
//StandardEntityCollection coll = new StandardEntityCollection();
//coll.add(entity);
// 设置填充柱形的渐进色
Color color[] = new Color[cutline.length];
color[0] = new Color(99, 99, 0);
color[1] = new Color(255, 169, 66);
color[2] = new Color(33, 255, 66);
for (int i = 0; i < color.length; i++) {
GradientPaint gp = new GradientPaint(0, 0, color[i].brighter(), 0, height, color[i].darker());
renderer.setSeriesPaint(i, gp);
}
// 设置横轴标题文字的旋转方向
CategoryAxis domainAxis = plot.getDomainAxis();
//domainAxis.setCategoryLabelPositions(CategoryLabelPositions.UP_90);
// 自定义图表结束! // 固定用法
//ChartRenderingInfo info = new ChartRenderingInfo(new StandardEntityCollection());
// 生成指定格式的图片,并返回图片名称
/*以下是解决字体乱码代码 */
//设置字体
Font font = new Font("宋体",10,20);
TextTitle tt = chart.getTitle(); //设置标题
tt.setFont(font);
//CategoryPlot plot = chart.getCategoryPlot(); //获取图标对象
//CategoryAxis domainAxis=plot.getDomainAxis();
//水平底部列表
domainAxis.setLabelFont(new Font("黑体",Font.BOLD,14));
//水平底部标题
domainAxis.setTickLabelFont(new Font("宋体",Font.BOLD,12));
//垂直标题
ValueAxis rangeAxis=plot.getRangeAxis();//获取柱状
rangeAxis.setLabelFont(new Font("黑体",Font.BOLD,15));
chart.getLegend().setItemFont(new Font("黑体", Font.BOLD, 15));
//设置网格背景颜色
plot.setBackgroundPaint(Color.white);
//设置网格竖线颜色
plot.setDomainGridlinePaint(Color.pink);
//设置网格横线颜色
plot.setRangeGridlinePaint(Color.pink);
//显示每个柱的数值,并修改该数值的字体属性
//BarRenderer renderers = new BarRenderer();
renderer.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator());
renderer.setBaseItemLabelsVisible(true);
//默认的数字显示在柱子中,通过如下两句可调整数字的显示
//注意:此句很关键,若无此句,那数字的显示会被覆盖,给人数字没有显示出来的问题
renderer.setBasePositiveItemLabelPosition(new ItemLabelPosition(ItemLabelAnchor.OUTSIDE12, TextAnchor.BASELINE_CENTER));
renderer.setItemLabelAnchorOffset(0D);
//设置每个地区所包含的平行柱的之间距离
renderer.setItemMargin(0.001);
renderer.setMaximumBarWidth(0.6);
plot.setRenderer(renderer);
/*以上是解决字体乱码代码 */
/*热点图表*/
//CategoryPlot plott = new CategoryPlot(dataset);//3D饼图
//plot.setURLGenerator(new StandardPieURLGenerator("barview.jsp"));//设定链接
renderer.setBaseItemURLGenerator(new StandardCategoryURLGenerator("test.jsp"));// 生成热点,用于链接
//JFreeChart chartt = new JFreeChart("",JFreeChart.DEFAULT_TITLE_FONT, plot, true);
//chart.setBackgroundPaint(java.awt.Color.white);//可选,设置图片背景色
//chart.setTitle("程序员学历情况调查表");//可选,设置图片标题
renderer.setBaseToolTipGenerator(new StandardCategoryToolTipGenerator());//
StandardEntityCollection sec = new StandardEntityCollection();
ChartRenderingInfo info = new ChartRenderingInfo(sec);
PrintWriter w = new PrintWriter(out);//输出MAP信息
//500是图片长度,300是图片高度
String fileName ="";
try {
fileName =ServletUtilities.saveChartAsPNG(chart, width, height, info, session);
System.out.println(fileName);
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
ChartUtilities.writeImageMap(w, fileName, info, false);
//out.close();
//w.close();
} catch (IOException e1) {
System.out.println("------ 在绘制图片时抛出异常,内容如下:");
e1.printStackTrace();
}
/**/
//设置地区、销量的显示位置
//将下方的“水果”放到上方
//plot.setDomainAxisLocation(AxisLocation.TOP_OR_RIGHT);
//将默认放在左边的“销量”放到右方
//plot.setRangeAxisLocation(AxisLocation.BOTTOM_OR_RIGHT);
//
//try {
//fileName = ServletUtilities.saveChartAsPNG(chart, width, height, info, session);
//} catch (Exception e) {
//System.out.println("------ 在绘制图片时抛出异常,内容如下:");
//e.printStackTrace();
//}
// 组织图片浏览路径
String graphURL =contextPath+servletURI+"?filename="+fileName;
System.out.println(graphURL);
//System.exit(0);
// 返回图片浏览路径
return graphURL;
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
}
jsp代码
<%@page import="frdc.moss.jfreechart.Histogram"%>
<%@page import="frdc.moss.jfreechart.Histogram"%>
<%@page import="java.io.*"%>
<%@page import="org.jfree.chart.JFreeChart"%>
<%@page import="org.jfree.chart.servlet.ServletUtilities"%>
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<html>
<head>
<META http-equiv=Content-Type content="text/html; charset=utf-8">
</head>
<body>
<%
Histogram histogramPositive=new Histogram();
PrintWriter w = response.getWriter();
//JspWriter out1=response.;
String mapname=histogramPositive.draw(session,request.getContextPath(),w).split("=")[1].toString();
%>
<img src="<%=histogramPositive.draw(session,request.getContextPath(),w)%>" style="width:500px;height:400px;" usemap="#<%=mapname %>"/>
</body>
</html>