| 发表于:2008-01-23 12:12:55 楼主 |
目标:用struts2+jfreechart+struts2-jfreechart-plugin-2.0.9插件做有链接的饼状图 我的代码: 1.web.xml文件添加struts2的过滤器 2.struts.xml文件中加- xml code
<include file="struts-jfreechart.xml" />
3.这是struts-jfreechart.xml文件内容 - xml code
<!doctype struts public
"-//apache software foundation//dtd struts configuration 2.0//en"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="jfreechartdemonstration" extends="struts-default">
<result-types>
<result-type name="chart" class="org.apache.struts2.dispatcher.chartresult"></result-type>
</result-types>
<action name="jfreechartaction" class="com.coraise.action.jfreechartaction">
<result type="chart">
<param name="width">400</param>
<param name="height">300</param>
</result>
</action>
</package>
</struts>
4.这是struts2-jfreechart-plugin-2.0.9插件中chartresult.java源代码 - java code
import com.opensymphony.xwork2.actioncontext;
import com.opensymphony.xwork2.actioninvocation;
import com.opensymphony.xwork2.result;
import com.opensymphony.xwork2.util.valuestack;
import java.io.outputstream;
import java.io.printwriter;
import javax.servlet.http.httpservletresponse;
import org.apache.struts2.servletactioncontext;
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.servlet.servletutilities;
public class chartresult
implements result
{
public chartresult()
{
chartset = false;
}
public chartresult(jfreechart chart, int height, int width)
{
chartset = false;
setchart(chart);
this.height = height;
this.width = width;
}
public chartresult setchart(jfreechart chart)
{
this.chart = chart;
chartset = true;
return this;
}
public chartresult setheight(int height)
{
this.height = height;
return this;
}
public chartresult setwidth(int width)
{
this.width = width;
return this;
}
public void EXECute(actioninvocation invocation)
throws exception
{
jfreechart chart = null;
if(chartset)
chart = this.chart;
else
chart = (jfreechart)invocation.getstack().findvalue("chart");
if(chart == null)
{
throw new nullpointerexception("no chart found");
} else
{
httpservletresponse response = servletactioncontext.getresponse();
outputstream os = response.getoutputstream();
chartutilities.writechartaspng(os, chart, width, height);
//以下为个人加的代码
standardentitycollection entitycollection = new standardentitycollection();
chartrenderinginfo info = new chartrenderinginfo(entitycollection);
printwriter pw = new printwriter(os);
chartutilities.writeimagemap(pw,"map0", info,false);
//以上为个人加的代码
os.flush();
return;
}
}
private static final long serialversionuid = 0xa6017f6fda50aa4cl;
jfreechart chart;
boolean chartset;
private int height;
private int width;
}
5.这是我的jfreechartaction的代码 - java code
import java.awt.*;
import org.jfree.chart.chartfactory;
import org.jfree.chart.chartrenderinginfo;
import org.jfree.chart.jfreechart;
import org.jfree.chart.entity.standardentitycollection;
import org.jfree.chart.labels.standardpietooltipgenerator;
import org.jfree.chart.plot.pieplot;
import org.jfree.chart.title.legendtitle;
import org.jfree.chart.urls.standardpieurlgenerator;
import org.jfree.data.general.defaultpiedataset;
import com.opensymphony.xwork2.actionsupport;
public class jfreechartaction extends actionsupport{
private static final long serialversionuid = 5752180822913527064l;
private jfreechart chart;
private defaultpiedataset getdata() {
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));
return data;
}
@override
public string EXECute() throws exception {
return success;
}
public jfreechart getchart() {
chart = chartfactory.createpiechart("",getdata(),true,true,true);
legendtitle legend = chart.getlegend(0);
legend.setitemfont(new font("宋体", font.bold, 13));
pieplot plot = (pieplot) chart.getplot();
plot.setlabelfont(new font("隶书", font.bold, 26));
plot.seturlgenerator(new standardpieurlgenerator("index.jsp"));
plot.settooltipgenerator(new standardpietooltipgenerator());
return chart;
}
}
6.myjsp.jsp页面嵌入的代码 <img src="jfreechartaction.action"> 结果:myjsp.jsp页面出统计图,但这个统计图不是动态的,既没提示,也无链接. 请问怎么该才能成为有提示和链接的效果啊 |
|
|
|
|