SpringMVC JFreeChart 实例

配置文件:web-context.xml

<bean name="/plan.it.report.showChartWin.do" class="com.test.web.controller.BudgetContrastiveJFreeChartController">
  	<property name="viewName">
  		<description>预算对比分析</description>
  		<value>budgetContrastiveChartVeiw</value>
  	</property>
</bean>

java代码:

public class BudgetContrastiveJFreeChartController extends PlanAbstractController {
	
	@Override
	protected ModelAndView formShow(HttpServletRequest request, HttpServletResponse response) throws Exception {
		double[] amountArray = new double[yearSize];
		double[] amountFeeArray = new double[yearSize];
		double[] AmountHrArray = new double[yearSize];
		List amountList = new ArrayList();
		List amountFeeList = new ArrayList();
		List amountHrList = new ArrayList();
		double[][] data = new double[][]{};
		String[] rowKeys = {"全成本","人力","收费"};
		String[] columnKeys = new String[yearSize];
		int year = startYear;
		for(int i = 0;i < yearSize;i++){
			amountArray[i] = Double.parseDouble(amountList.get(i).toString());
			amountFeeArray[i] = Double.parseDouble(amountFeeList.get(i).toString());
			AmountHrArray[i] = Double.parseDouble(amountHrList.get(i).toString());
			columnKeys[i] = Integer.toString(year);
			year++;
		}
		data = new double[][]{amountArray,AmountHrArray,amountFeeArray};
		CategoryDataset dataset = DatasetUtilities.createCategoryDataset(rowKeys,columnKeys,data);
		//DefaultCategoryDataset dataset = new DefaultCategoryDataset();
		
		JFreeChart chart = ChartFactory.createBarChart3D(
				"预算对比分析统计图", //??图表标题
				"年份", // 目录轴的显示标签
				"预算", // 数值轴的显示标签
				dataset, // 数据集
				PlotOrientation.VERTICAL, // 图表方向:水平、垂直
				true, // 是否显示图例(对于简单的柱状图必须是false) 
				true, // 是否生成工具 
				false // 是否生成URL链接
		);
		CategoryPlot plot = chart.getCategoryPlot();//获取图表区域对象
		plot.setBackgroundPaint(Color.white);//设置网格背景颜色  
		plot.setDomainGridlinePaint(Color.pink);//设置网格竖线颜色  
		plot.setRangeGridlinePaint(Color.pink);//设置网格横线颜色 
		
		plot.setOutlineVisible(true);
		plot.setDomainGridlinesVisible(true);
		plot.setRangeGridlinesVisible(true);
		plot.setNoDataMessage("没有数据");
		plot.setNoDataMessageFont(new Font("宋体",Font.BOLD,12));
		plot.setNoDataMessagePaint(Color.red);
		//将下方的“年份”放到上方
		//plot.setDomainAxisLocation(AxisLocation.TOP_OR_RIGHT);
		//将默认放在左边的“销量”放到右方
		//plot.setRangeAxisLocation(AxisLocation.BOTTOM_OR_RIGHT);
		CategoryAxis domainAxis = plot.getDomainAxis();
		domainAxis.setCategoryMargin(0.3);
        domainAxis.setLabelFont(new Font("宋体",Font.BOLD,14));//水平底部列表
        domainAxis.setTickLabelFont(new Font("宋体",Font.BOLD,12));//水平底部标题
        
        ValueAxis rangeAxis = plot.getRangeAxis();//获取柱状
        rangeAxis.setLabelFont(new Font("宋体",Font.BOLD,15));//垂直标题
        
        chart.getLegend().setItemFont(new Font("宋体", Font.BOLD, 15));//设置说明的字体
        chart.getTitle().setFont(new Font("宋体", Font.BOLD, 20));//设置说明的字体
		  
		//显示每个柱的数值,并修改该数值的字体属性
		BarRenderer3D renderer = new BarRenderer3D();
		//renderer.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator("{2}",NumberFormat.getNumberInstance()));
		renderer.setBaseItemLabelsVisible(true);
		//默认的数字显示在柱子中,通过如下两句可调整数字的显示
		//注意:此句很关键,若无此句,那数字的显示会被覆盖,给人数字没有显示出来的问题
		renderer.setBasePositiveItemLabelPosition(new ItemLabelPosition(ItemLabelAnchor.OUTSIDE12, TextAnchor.BASELINE_LEFT));  
		renderer.setItemLabelAnchorOffset(10D);
		//组内柱子间隔为组宽的30%
		renderer.setItemMargin(0.4);
		
		plot.setRenderer(renderer);
		
		response.setContentType("image/jpeg");
		ChartUtilities.writeChartAsJPEG(response.getOutputStream(), 1, chart, 400, 400, null);
		return null;
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值