您的位置:首页 > 编程语言 > Java开发

[六]JFreeChart实践五之与Struts2整合

2015-12-27 22:43 639 查看
1.Action,返回Chart

package com.java1234.chart.bar;

import java.awt.Color;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.labels.ItemLabelAnchor;
import org.jfree.chart.labels.ItemLabelPosition;
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.data.category.CategoryDataset;
import org.jfree.data.general.DatasetUtilities;
import org.jfree.ui.TextAnchor;

import com.opensymphony.xwork2.ActionSupport;

public class BarCharAction extends ActionSupport{

/**
*
*/
private static final long serialVersionUID = 1L;

private JFreeChart chart;

public JFreeChart getChart() {
return chart;
}

@Override
public String execute() throws Exception {
double [][]data=new double[][]{{1320,1110,1123,321},{720,210,1423,1321},{830,1310,123,521},{400,1110,623,321}};
String []rowKeys={"苹果","香蕉","橘子","梨子"};
String []columnKeys={"深圳","北京","上海","南京"};
CategoryDataset dataset=DatasetUtilities.createCategoryDataset(rowKeys,columnKeys ,data);
chart=ChartFactory.createBarChart3D("水果销售统计图", "水果", "销售", dataset,
PlotOrientation.VERTICAL, true, true, true);

CategoryPlot plot=chart.getCategoryPlot();
// 设置网格背景颜色
plot.setBackgroundPaint(Color.white);
// 设置网格竖线颜色
plot.setDomainGridlinePaint(Color.pink);
// 设置网格横线颜色
plot.setRangeGridlinePaint(Color.pink);

// 显示每个柱的数值,并修改该数值的字体属性
BarRenderer3D renderer=new BarRenderer3D();
renderer.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator());
renderer.setBaseItemLabelsVisible(true);

renderer.setBasePositiveItemLabelPosition(new ItemLabelPosition(ItemLabelAnchor.OUTSIDE12, TextAnchor.BASELINE_LEFT));
renderer.setItemLabelAnchorOffset(10D);

// 设置平行柱的之间距离
renderer.setItemMargin(0.4);

plot.setRenderer(renderer);

return SUCCESS;
}

}

2.配置返回的chart

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">

<struts>

<package name="jfreechart" extends="jfreechart-default">
<action name="barChart" class="com.java1234.chart.bar.BarCharAction">
<result name="success" type="chart">
<param name="value">chart</param>
<param name="type">png</param>
<param name="width">700</param>
<param name="height">500</param>
</result>
</action>
</package>

</struts>

3.配置struts2拦截器

<filter>
<filter-name>StrutsPrepareAndExecuteFilter</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>

<filter-mapping>
<filter-name>StrutsPrepareAndExecuteFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: