action:
package com.segsec.chart.struts.action;
import java.awt.Color;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.AxisLocation;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.labels.StandardCategoryItemLabelGenerator;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.BarRenderer3D;
import org.jfree.chart.servlet.ServletUtilities;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.general.DatasetUtilities;
public class GraphAction extends Action {
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
//drawChartPicture dcp = new drawChartPicture();
//GraphLogic gl = new GraphLogic();
//gl.setChartInfo(request, response);
//String img = dcp.getDrawChartInfo(request, response);
HttpSession session = request.getSession();
double[][] data = new double[][] { { 672, 766, 223, 540, 126 },
{ 325, 521, 210, 340, 106 }, { 332, 256, 523, 240, 526 } };
String[] rowKeys = { "苹果", "梨子", "葡萄" };
String[] columnKeys = { "北京", "上海", "广州", "成都", "深圳" };
CategoryDataset dataset = DatasetUtilities.createCategoryDataset(
rowKeys, columnKeys, data);
JFreeChart chart = ChartFactory.createBarChart3D("水果销量图统计", null, null,
dataset, PlotOrientation.VERTICAL, true, false, false);
chart.setBackgroundPaint(Color.WHITE);
CategoryPlot plot = chart.getCategoryPlot();
CategoryAxis domainAxis = plot.getDomainAxis();
//domainAxis.setVerticalCategoryLabels(false);
plot.setDomainAxis(domainAxis);
ValueAxis rangeAxis = plot.getRangeAxis();
//设置最高的一个 Item 与图片顶端的距离
rangeAxis.setUpperMargin(0.15);
//设置最低的一个 Item 与图片底端的距离
rangeAxis.setLowerMargin(0.15);
plot.setRangeAxis(rangeAxis);
BarRenderer3D renderer = new BarRenderer3D();
renderer.setBaseOutlinePaint(Color.BLACK);
//设置 Wall 的颜色<BR>
renderer.setWallPaint(Color.gray);
//设置每种水果代表的柱的颜色
renderer.setSeriesPaint(0, new Color(0, 0, 255));
renderer.setSeriesPaint(1, new Color(0, 100, 255));
renderer.setSeriesPaint(2, Color.GREEN);
//设置每个地区所包含的平行柱的之间距离
renderer.setItemMargin(0.1);
//显示每个柱的数值,并修改该数值的字体属性<BR>
renderer
.setItemLabelGenerator(new StandardCategoryItemLabelGenerator());
renderer.setItemLabelsVisible(true);
plot.setRenderer(renderer);
//设置柱的透明度<BR>
plot.setForegroundAlpha(0.6f);
//设置地区、销量的显示位置<BR>
plot.setDomainAxisLocation(AxisLocation.TOP_OR_RIGHT);
plot.setRangeAxisLocation(AxisLocation.BOTTOM_OR_RIGHT);
String filename = ServletUtilities.saveChartAsPNG(chart, 500, 300,
null, session);
String graphURL = request.getContextPath() + "/DisplayChart?filename="+ filename;
System.out.println("graphURL:"+graphURL);
System.out.println("filename.length():"+filename.length());
if (filename.length() != 0) {
System.out.println("graphname:"+filename);
request.setAttribute("graphname", filename);
System.out.println("graphURL:"+graphURL);
request.setAttribute("graphURL", graphURL);
return mapping.findForward("success");
} else {
return mapping.findForward("fail");
}
}
}
显示图表的页面:
<%@ page language="java" import="java.util.*" pageEncoding="GBK"%>
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html"%>
<%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic"%>
<%@ taglib uri="http://struts.apache.org/tags-tiles" prefix="tiles"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
System.out.println("path:"+path);
System.out.println("basePath:"+basePath);
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html:html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GBK" />
<title></title>
</head>
<body>
图表名称:
<bean:write name="graphname" filter="false" />
<br>
图表链接:
<bean:write name="graphURL" filter="false" />
<img src="<bean:write name="graphURL" filter="false" />" border=0/>
</body>
</html:html>
配置文件:
<action path="/graph" type="com.segsec.chart.struts.action.GraphAction">
<forward name="success" path="/strutschart.jsp" />
<forward name="fail" path="/fail.jsp" />
</action>
当然你别忘了导入你要使用的包,我用的是:jfreechart 1.0.9,请你也注意与我的版本一致,jfreechart 不知是怎么回事,每一个版本的类的位置或方法都不一样,换来换去让人受不了。
Struts与JFreeChart绘制3D柱状图
本文介绍如何使用Struts框架结合JFreeChart库创建动态3D柱状图,包括数据集准备、图表样式设置及在Web应用中展示的方法。
611

被折叠的 条评论
为什么被折叠?



