jfreechart一&二维报表导出为word&pdf&html-报表上显示百分比或数字(二)

jfreechart是java平台上一个开放的绘制图表的类库,完全使用java语言编写.
报表导出涉及的重要问题:无论是针对饼状图,柱状图,还是折线图,现在报表上显示的都是数据的名称,
根据实际需求,导出的报表饼状图上显示的应该是百分比,柱状图和折线图报表显示的应该是数据数量.

重点:1.通过Plot的子类操作各种报表上显示的数据.

以导出pdf为例:

<span style="font-size:12px;">package com.ilucky.chart.jfreechart.export.one;

import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.DecimalFormat;

import javax.imageio.ImageIO;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.StandardChartTheme;
import org.jfree.chart.labels.StandardCategoryItemLabelGenerator;
import org.jfree.chart.labels.StandardPieSectionLabelGenerator;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PiePlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.BarRenderer;
import org.jfree.chart.renderer.category.LineAndShapeRenderer;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.data.general.DefaultPieDataset;

import com.lowagie.text.Document;
import com.lowagie.text.Image;
import com.lowagie.text.Rectangle;
import com.lowagie.text.pdf.PdfWriter;

/**
 * @author IluckySi
 * @date 20140723
 */
public class ExportToPdf {

	private static final String PDF = ".pdf";
	
	private String destination;
	
	private String name;
	
	private int imageWidth;
	
	private int imageHeight;
	
	private String imageStyle;
	
	private Document document;
	
	private int countsPerPage;
	
	private StandardChartTheme standardChartTheme;
	
	public ExportToPdf() {
		this.imageWidth = 900;
		this.imageHeight = 540;
		this.imageStyle = "png";
		this.countsPerPage = 2;
	}
	
	public void setDestination(String destination) {
		this.destination = destination;
	}

	public void setName(String name) {
		this.name = name;
	}

	public void setImageWidth(int imageWidth) {
		this.imageWidth = imageWidth;
	}

	public void setImageHeight(int imageHeight) {
		this.imageHeight = imageHeight;
	}

	public void setImageStyle(String imageStyle) {
		this.imageStyle = imageStyle;
	}

	public void setCountsPerPage(int countsPerPage) {
		this.countsPerPage = countsPerPage;
	}
	
	public void setStandardChartTheme(StandardChartTheme standardChartTheme) {
		this.standardChartTheme = standardChartTheme;
	}
	
	public void createDocument() {
		try {
			//创建目标文件.
			File file = new File(destination + "/" + name + PDF);
			
			//创建Document,并设置页边距.
			Rectangle pagesize = new Rectangle(imageWidth + 100, imageHeight * countsPerPage + 150);  
			document = new Document(pagesize, 50, 50, 50, 50);
			
			//将目标文件和Document关联书写器, 注意: 书写器和导出的文件格式息息相关.
			PdfWriter.getInstance(document, new FileOutputStream(file));
			
			//打开document.
			document.open();
			
			//应用主题样式.
			ChartFactory.setChartTheme(standardChartTheme);
		} catch (Exception e) {
			System.out.println("创建Document发生错误!");
		}
	}
	
	//一维2D饼状图参数: 图表标题-数据集-是否显示图例-是否采用标准生成器-是否生成链接.
	public void addPie2D(String title, DefaultPieDataset pieDataSet) {
		//创建报表.
		JFreeChart pieChart = ChartFactory.createPieChart(title, pieDataSet, true, false, false);
		
		//在报表上显示百分比.
		PiePlot piePlot = (PiePlot)pieChart.getPlot();
		piePlot.setLabelGenerator(new StandardPieSectionLabelGenerator("{2}", new DecimalFormat("0.0"), new DecimalFormat("0.0%")));//1-显示实际数值, 2-显示百分比.
		
		//将报表添加到Document中.
		this.add(pieChart);
	}
	
	//一维3D饼状图参数: 图表标题-数据集-是否显示图例-是否采用标准生成器-是否生成链接.
	public void addPie3D(String title, DefaultPieDataset pieDataSet) {
		//创建报表.
		JFreeChart pieChart = ChartFactory.createPieChart3D(title, pieDataSet, true, false, false);
		
		//在报表上显示百分比.
		PiePlot piePlot = (PiePlot)pieChart.getPlot();
		piePlot.setLabelGenerator(new StandardPieSectionLabelGenerator("{2}", new DecimalFormat("0.0"), new DecimalFormat("0.0%")));//1-显示实际数值, 2-显示百分比.
		
		//将报表添加到Document中.
		this.add(pieChart);
	}

	//一维2D柱状图参数: 图表标题-x轴标题-y轴标题-数据集-显示方向-是否显示图例-是否采用标准生成器-是否生成链接.
	public void addBar2D(String title, String x, String y, DefaultCategoryDataset categoryDataSet) {
		//创建报表.
		JFreeChart barChart = ChartFactory.createBarChart(title, x, y, categoryDataSet, PlotOrientation.VERTICAL, true, false, false);
		
		//在报表上显示数据数量.
		CategoryPlot categoryPlot = (CategoryPlot)barChart.getPlot();
		BarRenderer barRenderer = (BarRenderer)categoryPlot.getRenderer();
		barRenderer.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator());
		barRenderer.setBaseItemLabelsVisible(true);
		
		//将报表添加到Document中.
		this.add(barChart);
	}
	
	//一维3D柱状图参数: 图表标题-x轴标题-y轴标题-数据集-显示方向-是否显示图例-是否采用标准生成器-是否生成链接.
	public void addBar3D(String title, String x, String y, DefaultCategoryDataset categoryDataSet) {
		//创建报表.
		JFreeChart barChart = ChartFactory.createBarChart3D(title, x, y, categoryDataSet, PlotOrientation.VERTICAL, true, false, false);

		//在报表上显示数据数量.
		CategoryPlot categoryPlot = (CategoryPlot)barChart.getPlot();
		BarRenderer barRenderer = (BarRenderer)categoryPlot.getRenderer();
		barRenderer.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator());
		barRenderer.setBaseItemLabelsVisible(true);
		
		//将报表添加到Document中.
		this.add(barChart);
	}

	//一维2D折线图参数: 图表标题-x轴标题-y轴标题-数据集-显示方向-是否显示图例-是否采用标准生成器-是否生成链接.
	public void addLine2D(String title, String x, String y, DefaultCategoryDataset lineDataSet) {
		//创建报表.
		JFreeChart lineChart = ChartFactory.createLineChart(title, x, y, lineDataSet, PlotOrientation.VERTICAL, true, false, false);
		
		//在报表上显示数据数量.
		CategoryPlot categoryPlot = (CategoryPlot)lineChart.getPlot();
		LineAndShapeRenderer lineAndShapeRenderer = (LineAndShapeRenderer)categoryPlot.getRenderer();
		lineAndShapeRenderer.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator());
		lineAndShapeRenderer.setBaseItemLabelsVisible(true);
		
		//将报表添加到Document中.
		this.add(lineChart);
	}
	
	//一维3D折线图参数: 图表标题-x轴标题-y轴标题-数据集-显示方向-是否显示图例-是否采用标准生成器-是否生成链接.
	public void addLine3D(String title, String x, String y, DefaultCategoryDataset lineDataSet) {
		//创建报表.
		JFreeChart lineChart = ChartFactory.createLineChart3D(title, x, y, lineDataSet, PlotOrientation.VERTICAL, true, false, false);
		
		//在报表上显示数据数量.
		CategoryPlot categoryPlot = (CategoryPlot)lineChart.getPlot();
		LineAndShapeRenderer lineAndShapeRenderer = (LineAndShapeRenderer)categoryPlot.getRenderer();
		lineAndShapeRenderer.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator());
		lineAndShapeRenderer.setBaseItemLabelsVisible(true);
				
		//将报表添加到Document中.
		this.add(lineChart);
	}
	
	private  void add(JFreeChart chart) {
		ByteArrayOutputStream baos = null;
		try {
			//将报表写入到图片缓冲区中,并设置大小.
			BufferedImage bi = chart.createBufferedImage(imageWidth, imageHeight);
			
			//将缓冲区中的图片写入到字节输出流.
			baos = new ByteArrayOutputStream();
			ImageIO.write(bi, imageStyle, baos);
			
			//将字节输出流转换为字节数组.
			byte[] data = baos.toByteArray();
			
			//将字节数组转换为Image,并写入到Document.
			Image image = Image.getInstance(data);
			document.add(image);
		} catch (Exception e) {
			System.out.println("向pdf添加报表发生错误!");
		} finally {
			try {
				if(baos != null) {
					baos.close();
					baos = null;
				}
			} catch (IOException e) {
				System.out.println("关闭流发生错误!");
			}
		}
	}
	
	public void closeDocument() {
		try {
			if(document != null) {
				document.close();
				document = null;
			} 
		} catch (Exception e) {
			System.out.println("关闭Document发生错误!");
		}
	}
}
</span>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值