最近做了个项目,需要在Struts2 Web 上使用JFreeChart并且在JFreeChart的柱状图上显示提示信息,也就是经常所说的热点信息。
步骤如下 :
1.首先创建一个Struts2的项目。
2.修改web.xml文件,在文中增加一个servlet。
代码如下
这个DisplayChart类是 JFreeChart自带的,不需要我们去创建的。
整个web.xml代码如下:
3.修改struts.xml文件,增加一个contant。代码如下:
整个struts.xml代码如下:
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
4.创建一个jsp页面:first.jsp
内容代码如下:
<%@ page language="java" import="java.util.*" pageEncoding="GBK"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
5.创建action: ChartAction 并继承ActionSupport
具体代码如下:
package com.action;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.awt.GradientPaint;
import java.awt.Polygon;
import java.io.IOException;
import java.text.NumberFormat;
import java.util.Random;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
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.StandardEntityCollection;
import org.jfree.chart.labels.StandardCategoryItemLabelGenerator;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.LineAndShapeRenderer;
import org.jfree.chart.servlet.ServletUtilities;
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 com.opensymphony.xwork2.ActionSupport;
public class ChartAction extends ActionSupport implements ServletRequestAware,
ServletResponseAware {
private Log log=LogFactory.getLog(ChartAction.class);
private HttpServletRequest request;
private HttpServletResponse response;
private String mapMessage;
private String src;
private String linemapMessage;
private String linesrc;
//创建图表的方法
public String getChart() throws IOException {
JFreeChart chart = ChartFactory.createBarChart(null, //图形标题名称
null, //domain轴 Lable,横坐标Lable
null, //range 轴 Lable,纵坐标Lable
defaultcategorydataset(), // dataset
PlotOrientation.VERTICAL, //垂直显示
true, // legend?
true, // tooltips?
false); //URLs?
//设置标题及标题字体
chart.setTitle(new TextTitle("图书销售统计图",new Font("黑体",Font.ITALIC,22)));
//建一个图例
LegendTitle legendTitle = chart.getLegend(0);
//设置图例字体
legendTitle.setItemFont(new Font("宋体",Font.BOLD,14));
StandardEntityCollection sec = new StandardEntityCollection();
// //生成RenderingInfo实例,info参数就是图片的热点信息
ChartRenderingInfo info = new ChartRenderingInfo(sec);
response.setCharacterEncoding("GBK");
String filename = null;
try {
filename = ServletUtilities.saveChartAsJPEG(chart, 700, 500, info, request.getSession());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
mapMessage = ChartUtilities.getImageMap("map0", info);
src = request.getContextPath()+"/servletDisplayChart?filename=" + filename;
System.out.println("mapMessage : "+mapMessage );
return super.SUCCESS;
}
//生成数据集的方法
private DefaultCategoryDataset defaultcategorydataset(){
String series1 = "JAVA";
String series2 = ".NET";
String series3 = "C++";
String type1 = "2009-01-01";
String type2 = "2009-02-01";
String type3 = "2009-03-01";
String type4 = "2009-04-01";
String type5 = "2009-05-01";
String type6 = "2009-06-01";
String type7 = "2009-07-01";
String type8 = "2009-08-01";
DefaultCategoryDataset defaultcategorydataset = new DefaultCategoryDataset();
defaultcategorydataset.addValue(1.0D, series1, type1);
defaultcategorydataset.addValue(2D, series1, type2);
defaultcategorydataset.addValue(3D, series1, type3);
defaultcategorydataset.addValue(5D, series1, type4);
defaultcategorydataset.addValue(5D, series1, type5);
defaultcategorydataset.addValue(7D, series1, type6);
defaultcategorydataset.addValue(7D, series1, type7);
defaultcategorydataset.addValue(8D, series1, type8);
defaultcategorydataset.addValue(12, series2, type1);
defaultcategorydataset.addValue(23, series2, type2);
defaultcategorydataset.addValue(33, series2, type3);
defaultcategorydataset.addValue(22, series2, type4);
defaultcategorydataset.addValue(12, series2, type5);
defaultcategorydataset.addValue(33, series2, type6);
defaultcategorydataset.addValue(65, series2, type7);
defaultcategorydataset.addValue(1.0D, series2, type8);
defaultcategorydataset.addValue(22, series3, type1);
defaultcategorydataset.addValue(43, series3, type2);
defaultcategorydataset.addValue(33, series3, type3);
defaultcategorydataset.addValue(26, series3, type4);
defaultcategorydataset.addValue(13, series3, type5);
defaultcategorydataset.addValue(23, series3, type6);
defaultcategorydataset.addValue(32, series3, type7);
defaultcategorydataset.addValue(14.0D, series3, type8);
return defaultcategorydataset;
}
//以及set和get方法
public void setServletRequest(HttpServletRequest request) {
this.request = request;
}
public void setServletResponse(HttpServletResponse response) {
this.response = response;
}
public String getMapMessage() {
return mapMessage;
}
public void setMapMessage(String mapMessage) {
this.mapMessage = mapMessage;
}
public String getSrc() {
return src;
}
public void setSrc(String src) {
this.src = src;
}
}
经过以上编辑后,启动tomcat。
在IE地址中输入:http://localhost:8080/项目名称/chart.action
这样就会得到一个页面,并且页面上会一个柱状图,而且鼠标经过时会出提示信息。
效果图如下:
希望以上的描述能对各位朋友有所帮助。如有疑问请给我留言或者QQ联系:308225695