strut2+jfreechart+mysql页面的图形无法显示
我用strut2+jfreechart+mysql生成折线图(jfreechart和plugin都导入到lib目录下了),页面无法显示折线图,当程序也没报错。页面显示的是一个叉叉,不知道是什么原因。代码如下:struts.xml配置文件为:
<package name="chart" extends="jfreechart-default">
<action name="displayChart" class="wit.cl.action.DisplayChartAction">
<result name="success" type="chart">
<param name="width">700</param>
<param name="height">400</param>
</result>
</action>
</package>
action的代码如下:(数据集的数据是从数据库取出来的,用的是hibernate,数据表很简单,横坐标是时间,纵坐标是位移)
package wit.cl.action;
import java.awt.Color;
import java.awt.Font;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
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.NumberAxis;
import org.jfree.chart.axis.NumberTickUnit;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.entity.StandardEntityCollection;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.servlet.ServletUtilities;
import org.jfree.chart.title.LegendTitle;
import org.jfree.chart.title.TextTitle;
import org.jfree.data.Range;
import org.jfree.data.category.DefaultCategoryDataset;
import wit.cl.model.Wittable1;
import wit.cl.service.Wittable1Service;
import wit.cl.service.impl.Wittable1ServiceImpl;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class DisplayChartAction extends ActionSupport {
Wittable1Service service=new Wittable1ServiceImpl();
private JFreeChart chart;
public String execute()throws Exception
{
chart = createChart(createDataset(service.findAll()));
return "success";
}
//创建数据集对象
private DefaultCategoryDataset createDataset(List<Wittable1> list) {
DefaultCategoryDataset defaultCategoryDataset = new DefaultCategoryDataset();
String s = "";
if (!list.isEmpty()) {
for (int i = 0; i < list.size(); i++) {
Wittable1 w = list.get(i);
String text = "2011-1-1";
Date d = w.getDate();
System.out.println("位移数据"+w.getMove());
if (d != null) {
SimpleDateFormat f = new SimpleDateFormat("yyyy/MM/dd");
text = f.format(d);
}
defaultCategoryDataset.addValue(w.getMove(), s,text);
}
}
return defaultCategoryDataset;
}
//生成JFreeChart对象
private JFreeChart createChart(DefaultCategoryDataset defaultCategoryDataset) {
JFreeChart jfreechart = ChartFactory.createLineChart("图1-1-1 左岸坝基1041m高程0+59.4坝纵多点位移计M41LBP绝对位移过程线",
"日期", "位移", defaultCategoryDataset, PlotOrientation.VERTICAL, true, true, false);
jfreechart.setBackgroundPaint(Color.white);//设置背景颜色
//TextTitle t=new TextTitle();
//对折线图进行标绘的类CategoryPlot
CategoryPlot categoryplot = jfreechart.getCategoryPlot();
categoryplot.setBackgroundPaint(Color.white);
categoryplot.setDomainGridlinePaint(Color.white);
categoryplot.setDomainGridlinesVisible(true);
Font font = new Font("宋体", Font.BOLD, 20);
//设置标题字体格式
TextTitle t = jfreechart.getTitle();
t.setFont(font);
//设置下标字体格式
LegendTitle l = jfreechart.getLegend();
l.setItemFont(font);
//设置x轴字体格式
CategoryAxis x = categoryplot.getDomainAxis();
x.setTickLabelFont(font);
x.setLabelFont(font);
//设置y轴字体格式
ValueAxis y = categoryplot.getRangeAxis();
NumberAxis ny = (NumberAxis) y;
y.setTickLabelFont(font);
y.setLabelFont(font);
//y.setTickMarkInsideLength( );
ny.setRange(new Range(-1d,5));//y轴最大最小刻度值
//设置y轴刻度
ny.setTickUnit(new NumberTickUnit(1.0d));//最小刻度跨度设置为1
ny.setAutoRangeIncludesZero(true);
DecimalFormat df = new DecimalFormat("#0.0");
ny.setNumberFormatOverride(df);
//lineandshaperenderer.setBaseShapesVisible(true);
categoryplot.setDomainGridlinesVisible(true);
categoryplot.setRangeGridlinePaint(Color.blue);
return jfreechart;
}
}
页面的代码如下:
<img src="chart/displayChart"/>
但是最后页面显示的确实一个红色的叉叉,不知道是什么问题,请大侠们指教! --------------------编程问答-------------------- 楼主看看你的配置文件,以前遇到过配置中有问题会导致显示问题的 --------------------编程问答-------------------- debug呀 Wittable1Service service=new Wittable1ServiceImpl();
service有值吗
补充:Java , Web 开发