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;
	}
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章