JFreeChart(与Struts2整合)

package com.ygl;


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>
  //*****************************************************************



  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ygl6150373

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值