import java.awt.Color;
import javax.servlet.http.HttpSession;
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 BarChartAction extends ActionSupport {
private static final long serialVersionUID = 1L;
private JFreeChart chart;
public void setChart(JFreeChart chart) {
this.chart = chart;
}
public JFreeChart getChart() throws Exception {
//构造数据集(默认的无颜色)
//DefaultCategoryDataset dataset=new DefaultCategoryDataset();
double [][]data=new double[][]{{1320,1110,1123,321},{720,210,1423,1321},{830,1310,1256,700},{400,256,369,800}};
String []rowKeys={"苹果","香蕉","橘子","梨子"};
String []columnKeys={"苏州","北京","上海","广州"};
CategoryDataset dataset= DatasetUtilities.createCategoryDataset(rowKeys,columnKeys,data);
//创建图片
JFreeChart 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 chart;
}
}
//********************************************************************
<body>
<img src="barchart"/>//地址指向 <action name="barchart" class="com.ygl.BarChartAction">
</body>
//***************************struts2.xml****************************************
<package name="default" namespace="/" extends="jfreechart-default">
<action name="barchart" class="com.ygl.BarChartAction">
<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>
//***********************web.xml****************************************************
<!-- 配置 Struts2 的 Filter -->
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
//*****************************************************************