---
title: "jfreechart 生成报表  （1.0.6）"
author: "CodeToMyLaw"
date: "2012-05-21T20:18:11Z"
tags: []
categories: ["Web前端"]
slug: "jfreechart-1-0-6"
draft: false
lastmod: "2012-04-24T15:31:08Z"
id: 1496761
url: "https://codetomylaw.iteye.com/blog/1496761"
---

if("bzt".equals(tu)){
	        // 生成3D饼图
			PiePlot3D plot = new PiePlot3D(getDataset());
	        plot.setToolTipGenerator(new StandardPieToolTipGenerator());
	        // 设置图片链接
	        plot.setURLGenerator(new StandardPieURLGenerator(
	                ""));

	        JFreeChart chart = new JFreeChart("",
	                JFreeChart.DEFAULT_TITLE_FONT, plot, true);
	        plot.setLabelGenerator(new StandardPieSectionLabelGenerator("{0}={1}({2})", NumberFormat.getNumberInstance(), new DecimalFormat("0.00%")));	        
	        
	        chart.setBackgroundPaint(Color.gray);
	        ChartUtilities.saveChartAsJPEG(new File(path), chart, 950, 600);
	        
		}else{
			//柱状图
			
			CategoryDataset dataset = getDataSet2();

			JFreeChart chart = ChartFactory.createBarChart3D(
								"", // 图表标题
								"" , // 目录轴的显示标签
								"", // 数值轴的显示标签
								dataset, // 数据集
								PlotOrientation.VERTICAL, // 图表方向：水平、垂直
								true, 	// 是否显示图例(对于简单的柱状图必须是false)
								true, 	// 是否生成工具
								false 	// 是否生成URL链接
								);
			//CategoryPlot plot = (CategoryPlot) chart.getPlot();	
			CategoryPlot plot = chart.getCategoryPlot(); 
			//设置网格背景颜色   
			plot.setBackgroundPaint(Color.white);  
			//设置网格竖线颜色   
			plot.setDomainGridlinePaint(Color.pink);  
			//设置网格横线颜色   
			plot.setRangeGridlinePaint(Color.pink);			
			
			plot.setRangeAxisLocation(AxisLocation.TOP_OR_LEFT);
			//plot.setDomainAxisLocation(AxisLocation.TOP_OR_RIGHT);
			
			
			CategoryAxis domainAxis = plot.getDomainAxis();
			domainAxis.setCategoryLabelPositions(CategoryLabelPositions.UP_45); // X轴上的Lable让其45度倾斜 
			
			BarRenderer3D renderer = new BarRenderer3D();  
			renderer.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator());  
			renderer.setBasePositiveItemLabelPosition(new ItemLabelPosition(ItemLabelAnchor.OUTSIDE12, TextAnchor.BASELINE_LEFT));
			renderer.setItemLabelFont(new   Font("黑体",Font.PLAIN,13)); 
			//数据显示在柱上的位置
			renderer.setItemLabelAnchorOffset(12D); 
			//每个分类之间的间隔 
			renderer.setItemMargin(0.3); 
			renderer.setSeriesVisibleInLegend(true);
			
			renderer.setBaseItemLabelsVisible(true);
			plot.setRenderer(renderer);  

			FileOutputStream fos_jpg = null;
			try {
				fos_jpg = new FileOutputStream(new File(path));
				ChartUtilities.writeChartAsJPEG(fos_jpg,1.0f,chart,950,600,null);
			} finally {
				try {
					fos_jpg.close();
				} catch (Exception e) {}
			}			
			
		}