浏览 3912 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (1) :: 隐藏帖 (0)
|
|
---|---|
作者 | 正文 |
发表时间:2009-09-25
最后修改:2009-09-25
jFreeChart与Struts2的组合: <struts> <package name="jfreechart-default" extends="struts-default"> <result-types> <result-type name="chart" class="org.apache.struts2.dispatcher.ChartResult"> <param name="height">150</param> <param name="width">200</param> </result-type> </result-types> </package> </struts> 若该package没有继承struts-default则需要手动加上extends="struts-default"再重新用jdk打包 /** * 生成图形报表 * * @author 张明学 * */ @SuppressWarnings("serial") public class ViewResultAction extends ActionSupport { /** * 这里的JFreeChart的变量名称必须是chart(这是jfreeChart插件不足的地方) */ private JFreeChart chart = null; private List<String> interest = null; @SuppressWarnings("unchecked") @Override public String execute() throws Exception { ActionContext context = ActionContext.getContext(); Map application = context.getApplication(); for (String inter : interest) { if (application.get(inter) == null) { application.put(inter, 1); } else { application.put(inter, (Integer) application.get(inter) + 1); } } DefaultCategoryDataset dataset = new DefaultCategoryDataset(); dataset.setValue((Integer) application.get("football"), "", "足球"); dataset.setValue((Integer) application.get("volleyball"), "", "排球"); dataset.setValue((Integer) application.get("basketball"), "", "蓝球"); dataset.setValue((Integer) application.get("badminton"), "", "羽毛球"); // 设置标题样式 chart = ChartFactory.createBarChart3D("兴趣统计结果", "项目", "结果", dataset, PlotOrientation.VERTICAL, true, false, false); chart.setTitle(new TextTitle("兴趣统计结果", new Font("黑体", Font.BOLD, 22))); // 报表中间部分 CategoryPlot plot = (CategoryPlot) chart.getPlot(); // 设置水平方向的样式 CategoryAxis categoryAxis = plot.getDomainAxis(); categoryAxis.setLabelFont(new Font("宋体", Font.BOLD, 20)); categoryAxis.setCategoryLabelPositions(CategoryLabelPositions.UP_45); // 设置垂直方向的样式 NumberAxis numberAxis = (NumberAxis) plot.getRangeAxis(); numberAxis .setLabelFont(new Font("仿宋", Font.LAYOUT_NO_LIMIT_CONTEXT, 18)); // 获得报表下面部分 // chart.getLegend(); return SUCCESS; } public List<String> getInterest() { return interest; } public void setInterest(List<String> interest) { this.interest = interest; } public JFreeChart getChart() { return chart; } public void setChart(JFreeChart chart) { this.chart = chart; } } <struts> <package name="mengya" extends="jfreechart-default"> <action name="viewResult" class="com.mengya.action.ViewResultAction"> <result name="success" type="chart"> <!-- 指定图形报表的大小 --> <param name="height">600</param> <param name="width">800</param> </result> </action> </package> </struts>
声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |