在网上找了关于Struts2下JFreechart的使用,然后加到了自己写的项目中。
最后倒是可以生成饼图,但是有些不明白,而且遇到了些问题,就是 执行完生成饼图的Action后跳到JSP页面,样式什么的都没了~~~~~请问原因?!下面贴代码
这是src下chart-struts.xml 文件的代码<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"><struts>
<package name="jCuckoo" namespace="/chart" extends="jfreechart-default">
<action name="pieChartAction" class="com.neverlose.bean.PieChartAction">
<result type="chart">
<param name="width">600</param>
<param name="height">450</param>
</result>
</action> </package></struts>下面贴出PieChartAction类的代码package com.neverlose.bean;import java.awt.Font;
import java.io.Serializable;
import java.sql.SQLException;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.List;
import org.apache.struts2.ServletActionContext;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.labels.StandardPieSectionLabelGenerator;
import org.jfree.chart.plot.PiePlot;
import org.jfree.chart.title.LegendTitle;
import org.jfree.chart.title.TextTitle;
import org.jfree.data.general.DefaultPieDataset;
import com.opensymphony.xwork2.ActionSupport;public class PieChartAction extends ActionSupport implements Serializable { /**
*
*/
private static final long serialVersionUID = 7853070220887551126L;
private JFreeChart chart; public String execute(){
int t_id =Integer.parseInt(ServletActionContext.getRequest().getParameter("t_id")) ;
List choices = null;
try {
choices = Choice.getChoicesByTid(t_id);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//生成JFreeChart对象
chart = ChartFactory.createPieChart3D(
"饼状图", // 图表标题
getDataSet(choices), //数据
true, // 是否显示图例
false, //是否显示工具提示
false //是否生成URL
); //重新设置图标标题,改变字体
chart.setTitle(new TextTitle("饼状图", new Font("黑体", Font.ITALIC , 22)));
//取得统计图标的第一个图例
LegendTitle legend = chart.getLegend(0);
//修改图例的字体
legend.setItemFont(new Font("宋体", Font.BOLD, 14));
//获得饼图的Plot对象
PiePlot plot = (PiePlot)chart.getPlot();
//设置饼图各部分的标签字体
plot.setLabelFont(new Font("宋体", Font.BOLD, 12));
//设定背景透明度(0-1.0之间)
plot.setBackgroundAlpha(0.9f);
//设定前景透明度(0-1.0之间)
plot.setForegroundAlpha(0.50f);
// 图片中显示百分比:自定义方式,{0} 表示选项, {1} 表示数值, {2} 表示所占比例 ,小数点后两位
plot.setLabelGenerator(new StandardPieSectionLabelGenerator("{0}:{1}({2})", NumberFormat.getNumberInstance(),
new DecimalFormat("0.00%"))); // 图例显示百分比:自定义方式, {0} 表示选项, {1} 表示数值, {2} 表示所占比例
plot.setLegendLabelGenerator(new StandardPieSectionLabelGenerator("{0}"));
return SUCCESS;
} public DefaultPieDataset getDataSet(List choices){ DefaultPieDataset data = new DefaultPieDataset();
/* data.setValue("Java", new Double(43.2));
data.setValue("Visual Basic", new Double(1.0));
data.setValue("C/C++", new Double(17.5));
data.setValue("tangjun书", new Double(60.0));*/
for(int i=0;i<choices.size();i++)
{
Choice temp = (Choice)choices.get(i);
String tempType = temp.getItem_type() ;
String tempName = temp.getItem();
int tempAmount = temp.getAmount() ;
if(tempType.equals("text")) data.setValue(tempName, tempAmount);
else data.setValue("选项 "+(i+1), tempAmount) ;
}
return data;
} public JFreeChart getChart() {
return chart;
} }
下面是showPieChart.jsp的代码<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>显示饼状图</title>
<link style="text/css" rel="stylesheet" href="<%=basePath%>/css/style.css"> </head>
<body class="body2">
测试:样式!
</body>
</html>就是执行完Action后跳到这个页面,然而这个页面样式什么都失效了!
因为我还需要在这个页面添加比如返回之类的按钮的,所以请知道的朋友指点下
我希望执行完Action后生成的饼图可以像普通图片一样可以让我在JSP中用<img>这样的标签来显示,怎么弄???
最后倒是可以生成饼图,但是有些不明白,而且遇到了些问题,就是 执行完生成饼图的Action后跳到JSP页面,样式什么的都没了~~~~~请问原因?!下面贴代码
这是src下chart-struts.xml 文件的代码<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"><struts>
<package name="jCuckoo" namespace="/chart" extends="jfreechart-default">
<action name="pieChartAction" class="com.neverlose.bean.PieChartAction">
<result type="chart">
<param name="width">600</param>
<param name="height">450</param>
</result>
</action> </package></struts>下面贴出PieChartAction类的代码package com.neverlose.bean;import java.awt.Font;
import java.io.Serializable;
import java.sql.SQLException;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.List;
import org.apache.struts2.ServletActionContext;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.labels.StandardPieSectionLabelGenerator;
import org.jfree.chart.plot.PiePlot;
import org.jfree.chart.title.LegendTitle;
import org.jfree.chart.title.TextTitle;
import org.jfree.data.general.DefaultPieDataset;
import com.opensymphony.xwork2.ActionSupport;public class PieChartAction extends ActionSupport implements Serializable { /**
*
*/
private static final long serialVersionUID = 7853070220887551126L;
private JFreeChart chart; public String execute(){
int t_id =Integer.parseInt(ServletActionContext.getRequest().getParameter("t_id")) ;
List choices = null;
try {
choices = Choice.getChoicesByTid(t_id);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//生成JFreeChart对象
chart = ChartFactory.createPieChart3D(
"饼状图", // 图表标题
getDataSet(choices), //数据
true, // 是否显示图例
false, //是否显示工具提示
false //是否生成URL
); //重新设置图标标题,改变字体
chart.setTitle(new TextTitle("饼状图", new Font("黑体", Font.ITALIC , 22)));
//取得统计图标的第一个图例
LegendTitle legend = chart.getLegend(0);
//修改图例的字体
legend.setItemFont(new Font("宋体", Font.BOLD, 14));
//获得饼图的Plot对象
PiePlot plot = (PiePlot)chart.getPlot();
//设置饼图各部分的标签字体
plot.setLabelFont(new Font("宋体", Font.BOLD, 12));
//设定背景透明度(0-1.0之间)
plot.setBackgroundAlpha(0.9f);
//设定前景透明度(0-1.0之间)
plot.setForegroundAlpha(0.50f);
// 图片中显示百分比:自定义方式,{0} 表示选项, {1} 表示数值, {2} 表示所占比例 ,小数点后两位
plot.setLabelGenerator(new StandardPieSectionLabelGenerator("{0}:{1}({2})", NumberFormat.getNumberInstance(),
new DecimalFormat("0.00%"))); // 图例显示百分比:自定义方式, {0} 表示选项, {1} 表示数值, {2} 表示所占比例
plot.setLegendLabelGenerator(new StandardPieSectionLabelGenerator("{0}"));
return SUCCESS;
} public DefaultPieDataset getDataSet(List choices){ DefaultPieDataset data = new DefaultPieDataset();
/* data.setValue("Java", new Double(43.2));
data.setValue("Visual Basic", new Double(1.0));
data.setValue("C/C++", new Double(17.5));
data.setValue("tangjun书", new Double(60.0));*/
for(int i=0;i<choices.size();i++)
{
Choice temp = (Choice)choices.get(i);
String tempType = temp.getItem_type() ;
String tempName = temp.getItem();
int tempAmount = temp.getAmount() ;
if(tempType.equals("text")) data.setValue(tempName, tempAmount);
else data.setValue("选项 "+(i+1), tempAmount) ;
}
return data;
} public JFreeChart getChart() {
return chart;
} }
下面是showPieChart.jsp的代码<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>显示饼状图</title>
<link style="text/css" rel="stylesheet" href="<%=basePath%>/css/style.css"> </head>
<body class="body2">
测试:样式!
</body>
</html>就是执行完Action后跳到这个页面,然而这个页面样式什么都失效了!
因为我还需要在这个页面添加比如返回之类的按钮的,所以请知道的朋友指点下
我希望执行完Action后生成的饼图可以像普通图片一样可以让我在JSP中用<img>这样的标签来显示,怎么弄???
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货