饼状图

package com.web.utils;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.awt.Rectangle;
import java.awt.Shape;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.Locale;
import java.util.Random;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.jfree.chart.ChartRenderingInfo;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.entity.ChartEntity;
import org.jfree.chart.entity.StandardEntityCollection;
import org.jfree.chart.labels.CategoryToolTipGenerator;
import org.jfree.chart.labels.ItemLabelAnchor;
import org.jfree.chart.labels.ItemLabelPosition;
import org.jfree.chart.labels.StandardCategoryItemLabelGenerator;
import org.jfree.chart.labels.StandardCategoryToolTipGenerator;
import org.jfree.chart.labels.StandardPieSectionLabelGenerator;
import org.jfree.chart.labels.StandardPieToolTipGenerator;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PiePlot;
import org.jfree.chart.plot.PiePlot3D;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.category.BarRenderer;
import org.jfree.chart.renderer.category.BarRenderer3D;
import org.jfree.chart.renderer.category.LineAndShapeRenderer;
import org.jfree.chart.servlet.ServletUtilities;
import org.jfree.chart.title.TextTitle;
import org.jfree.chart.urls.StandardCategoryURLGenerator;
import org.jfree.chart.urls.StandardPieURLGenerator;
import org.jfree.ui.TextAnchor;

public class BarChart {
	public BarChart() {
	}
	
	/**
	 * 图形相关数据设置
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws IOException
	 */
	public String showBar(HttpServletResponse response,
			HttpSession session, String realPath) throws IOException {
		String filename = null;
		FileOperate fo = new FileOperate();
		PrintWriter pw = response.getWriter(); // 输出MAP信息
		int width = 1000;
		int height = 600;
		JFreeChart chart = (JFreeChart)FacesUtils.getValueInHashtableOfSession("chart");
		CategoryPlot plot = chart.getCategoryPlot();
		
		BarRenderer customBarRenderer = (BarRenderer) plot.getRenderer(); 
		//设定柱子上面的颜色 
		customBarRenderer.setSeriesPaint(0, Color.decode("#40A410")); // 给series1 Bar 
		customBarRenderer.setSeriesPaint(1, Color.decode("#40A410")); // 给series2 Bar 
		customBarRenderer.setSeriesPaint(2, Color.decode("#40A410")); // 给series3 Bar 
		customBarRenderer.setSeriesPaint(3, Color.decode("#40A410")); // 给series4 Bar 
		customBarRenderer.setSeriesPaint(4, Color.decode("#40A410")); // 给series5 Bar 
		customBarRenderer.setSeriesPaint(5, Color.decode("#40A410")); // 给series6 Bar 
		customBarRenderer.setSeriesOutlinePaint(0,Color.BLACK);//边框为黑色 
		customBarRenderer.setSeriesOutlinePaint(1,Color.BLACK);//边框为黑色 
		customBarRenderer.setSeriesOutlinePaint(2,Color.BLACK); //边框为黑色 
		customBarRenderer.setSeriesOutlinePaint(3,Color.BLACK);//边框为黑色 
		customBarRenderer.setSeriesOutlinePaint(4,Color.BLACK);//边框为黑色 
		customBarRenderer.setSeriesOutlinePaint(5,Color.BLACK); //边框为黑色
		customBarRenderer.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator());
		customBarRenderer.setBaseItemLabelsVisible(true);
		customBarRenderer.setBasePositiveItemLabelPosition(new ItemLabelPosition(ItemLabelAnchor.OUTSIDE12,TextAnchor.BASELINE_LEFT));
		customBarRenderer.setItemLabelAnchorOffset(10D);
		customBarRenderer.setMaximumBarWidth(0.03);
		customBarRenderer.setMinimumBarLength(0.03);
		String url=(String)FacesUtils.getValueInHashtableOfSession("url");
		customBarRenderer.setBaseItemURLGenerator(new StandardCategoryURLGenerator(url,"p1","p2"));
		CategoryToolTipGenerator generatorToolTip = new StandardCategoryToolTipGenerator("{0}{1}{2}", NumberFormat.getInstance(Locale.CHINESE));  
		customBarRenderer.setBaseToolTipGenerator(generatorToolTip);
		plot.setRenderer(customBarRenderer);	
		plot.setDomainGridlinePaint(Color.yellow);
		
		// 图片输出流,图片的保存路径
		Random rand = new Random();
		int i = rand.nextInt();
		String imgPath = realPath + "chartTemp\\" + System.nanoTime()+Tools.getRandom(i)+ ".png";
		fo.newFolder(realPath + "chartTemp\\");
		FileOutputStream fout = new FileOutputStream(imgPath);
		// shape是指热点的区域
		Shape shape = new Rectangle(20, 10);
		ChartEntity entity = new ChartEntity(shape);
		StandardEntityCollection coll = new StandardEntityCollection();
		coll.add(entity);
		ChartRenderingInfo info = new ChartRenderingInfo(coll);
		// 并将图片信息写入到info对象中
		ChartUtilities.writeChartAsPNG(fout, chart, width, height, info);
		// 生成图片,并返回图片名称
		filename = ServletUtilities.saveChartAsPNG(chart, width, height, info, session);
		// 读取ChartRenderingInfo对象,生成Map信息,这些信息写在pw的输出流中,这里的输出流就是Response.out,也就是直接输出到页面了。
		ChartUtilities.writeImageMap(pw, filename, info, false);
		pw.flush(); // 调用pw.flush()方法关闭IO流
		return filename;
	}
	
	public String showBarana(HttpServletResponse response,
			HttpSession session, String realPath) throws IOException {
		String filename = null;
		FileOperate fo = new FileOperate();
		PrintWriter pw = response.getWriter(); // 输出MAP信息
		int width = 1000;
		int height = 600;
		JFreeChart chart = (JFreeChart)FacesUtils.getValueInHashtableOfSession("chart");
		CategoryPlot plot = chart.getCategoryPlot();
		
		BarRenderer customBarRenderer = (BarRenderer) plot.getRenderer(); 
		//设定柱子上面的颜色 
		customBarRenderer.setSeriesPaint(0, Color.decode("#40A410")); // 给series1 Bar 
		customBarRenderer.setSeriesPaint(1, Color.decode("#40A410")); // 给series2 Bar 
		customBarRenderer.setSeriesPaint(2, Color.decode("#40A410")); // 给series3 Bar 
		customBarRenderer.setSeriesPaint(3, Color.decode("#40A410")); // 给series4 Bar 
		customBarRenderer.setSeriesPaint(4, Color.decode("#40A410")); // 给series5 Bar 
		customBarRenderer.setSeriesPaint(5, Color.decode("#40A410")); // 给series6 Bar 
		customBarRenderer.setSeriesOutlinePaint(0,Color.BLACK);//边框为黑色 
		customBarRenderer.setSeriesOutlinePaint(1,Color.BLACK);//边框为黑色 
		customBarRenderer.setSeriesOutlinePaint(2,Color.BLACK); //边框为黑色 
		customBarRenderer.setSeriesOutlinePaint(3,Color.BLACK);//边框为黑色 
		customBarRenderer.setSeriesOutlinePaint(4,Color.BLACK);//边框为黑色 
		customBarRenderer.setSeriesOutlinePaint(5,Color.BLACK); //边框为黑色
		customBarRenderer.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator());
		customBarRenderer.setBaseItemLabelsVisible(true);
		customBarRenderer.setBasePositiveItemLabelPosition(new ItemLabelPosition(ItemLabelAnchor.OUTSIDE12,TextAnchor.BASELINE_LEFT));
		customBarRenderer.setItemLabelAnchorOffset(10D);
		customBarRenderer.setMaximumBarWidth(0.03);
		customBarRenderer.setMinimumBarLength(0.03);
		String url=(String)FacesUtils.getValueInHashtableOfSession("url"); 
		customBarRenderer.setBaseItemURLGenerator(new StandardCategoryURLGenerator(url,"p1","p2"));
		CategoryToolTipGenerator generatorToolTip = new StandardCategoryToolTipGenerator("{0}{1}{2}", NumberFormat.getInstance(Locale.CHINESE));  
		customBarRenderer.setBaseToolTipGenerator(generatorToolTip);
		plot.setRenderer(customBarRenderer);	
		plot.setDomainGridlinePaint(Color.yellow);
		
		// 图片输出流,图片的保存路径
		Random rand = new Random();
		int i = rand.nextInt();
		String imgPath = realPath + "chartTemp\\" + System.nanoTime()+Tools.getRandom(i)+ ".png";
		fo.newFolder(realPath + "chartTemp\\");
		FileOutputStream fout = new FileOutputStream(imgPath);
		// shape是指热点的区域
		Shape shape = new Rectangle(20, 10);
		ChartEntity entity = new ChartEntity(shape);
		StandardEntityCollection coll = new StandardEntityCollection();
		coll.add(entity);
		ChartRenderingInfo info = new ChartRenderingInfo(coll);
		// 并将图片信息写入到info对象中
		ChartUtilities.writeChartAsPNG(fout, chart, width, height, info);
		// 生成图片,并返回图片名称
		filename = ServletUtilities.saveChartAsPNG(chart, width, height, info, session);
		// 读取ChartRenderingInfo对象,生成Map信息,这些信息写在pw的输出流中,这里的输出流就是Response.out,也就是直接输出到页面了。
		ChartUtilities.writeImageMap(pw, filename, info, false);
		pw.flush(); // 调用pw.flush()方法关闭IO流
		return filename;
	}
	
	
	
	
	public String showpie(HttpServletResponse response,
			HttpSession session, String realPath) throws IOException {
		
		String filename = null;
		FileOperate fo = new FileOperate();
		PrintWriter pw = response.getWriter(); // 输出MAP信息
		int width = 800;
		int height = 600;
		JFreeChart chart = (JFreeChart)FacesUtils.getValueInHashtableOfSession("chart");
		PiePlot plot = (PiePlot) chart.getPlot(); 
		// requestUrl为链接地址
		String url=(String)FacesUtils.getValueInHashtableOfSession("url");
		  plot.setURLGenerator(new StandardPieURLGenerator(url, "name"));
		  // 图片中显示百分比:默认方式
		  plot.setLabelGenerator(new StandardPieSectionLabelGenerator(
		    StandardPieToolTipGenerator.DEFAULT_TOOLTIP_FORMAT));
		  // 图片中显示百分比:自定义方式,{0} 表示选项, {1} 表示数值,
		  //{2} 表示所占比例 ,小数点后两位
		  plot.setLabelGenerator(new StandardPieSectionLabelGenerator(
		    "{0}={1}({2})", NumberFormat.getNumberInstance(),
		    new DecimalFormat("0.00%")));
		  // 图例显示百分比:自定义方式, {0} 表示选项, {1} 表示数值, {2} 表示所占比例
		  plot.setLegendLabelGenerator(new StandardPieSectionLabelGenerator(
		      "{0}"));
		  // 指定图片的透明度(0.0-1.0)
		  plot.setForegroundAlpha(1.0f);
		  // 指定显示的饼图上圆形(true)还椭圆形(false)
		  plot.setCircular(true);
		  // 设置图上分类标签label的字体,解决中文乱码
		  plot.setLabelFont(new Font("黑体", Font.PLAIN, 12));
		  // 设置图上分类标签label的最大宽度,相对与plot的百分比
		  plot.setMaximumLabelWidth(0.2);
		  // 设置3D饼图的Z轴高度(0.0~1.0)
		//  plot.setDepthFactor(0.07);
		  //设置起始角度,默认值为90,当为0时,起始值在3点钟方向
		  plot.setStartAngle(45);
		  // 设置图标题的字体,解决中文乱码
		  TextTitle textTitle = chart.getTitle();
		  textTitle.setFont(new Font("黑体", Font.PLAIN, 20));
		  // 设置背景色为白色
		  chart.setBackgroundPaint(Color.white);
		  // 设置Legend部分字体,解决中文乱码
		  chart.getLegend().setItemFont(new Font("宋体", Font.PLAIN, 12));
		  // 输出图片到文件
		Random rand = new Random();
		int i = rand.nextInt();
		String imgPath = realPath + "chartTemp\\" + System.nanoTime()+Tools.getRandom(i)+ ".png";
		fo.newFolder(realPath + "chartTemp\\");
		FileOutputStream fout = new FileOutputStream(imgPath);
		// shape是指热点的区域
		Shape shape = new Rectangle(20, 10);
		ChartEntity entity = new ChartEntity(shape);
		StandardEntityCollection coll = new StandardEntityCollection();
		coll.add(entity);
		ChartRenderingInfo info = new ChartRenderingInfo(coll);
		// 并将图片信息写入到info对象中
		ChartUtilities.writeChartAsPNG(fout, chart, width, height, info);
		// 生成图片,并返回图片名称
		filename = ServletUtilities.saveChartAsPNG(chart, width, height, info, session);
		// 读取ChartRenderingInfo对象,生成Map信息,这些信息写在pw的输出流中,这里的输出流就是Response.out,也就是直接输出到页面了。
		ChartUtilities.writeImageMap(pw, filename, info, false);
		pw.flush(); // 调用pw.flush()方法关闭IO流
		return filename;
	}
	public String showpie1(HttpServletResponse response,
			HttpSession session, String realPath) throws IOException {
		
		String filename = null;
		FileOperate fo = new FileOperate();
		PrintWriter pw = response.getWriter(); // 输出MAP信息
		int width = 800;
		int height = 600;
		JFreeChart chart = (JFreeChart)FacesUtils.getValueInHashtableOfSession("chart");
		PiePlot plot = (PiePlot) chart.getPlot(); 
		// requestUrl为链接地址
		//String url=(String)FacesUtils.getValueInHashtableOfSession("url");
		 // plot.setURLGenerator(new StandardPieURLGenerator(url, "name"));
		  // 图片中显示百分比:默认方式
		  plot.setLabelGenerator(new StandardPieSectionLabelGenerator(
		    StandardPieToolTipGenerator.DEFAULT_TOOLTIP_FORMAT));
		  // 图片中显示百分比:自定义方式,{0} 表示选项, {1} 表示数值,
		  //{2} 表示所占比例 ,小数点后两位
		  plot.setLabelGenerator(new StandardPieSectionLabelGenerator(
		    "{0}={1}({2})", NumberFormat.getNumberInstance(),
		    new DecimalFormat("0.00%")));
		  // 图例显示百分比:自定义方式, {0} 表示选项, {1} 表示数值, {2} 表示所占比例
		  plot.setLegendLabelGenerator(new StandardPieSectionLabelGenerator(
		      "{0}"));
		  // 指定图片的透明度(0.0-1.0)
		  plot.setForegroundAlpha(1.0f);
		  // 指定显示的饼图上圆形(true)还椭圆形(false)
		  plot.setCircular(true);
		  // 设置图上分类标签label的字体,解决中文乱码
		  plot.setLabelFont(new Font("黑体", Font.PLAIN, 12));
		  // 设置图上分类标签label的最大宽度,相对与plot的百分比
		  plot.setMaximumLabelWidth(0.2);
		  // 设置3D饼图的Z轴高度(0.0~1.0)
		//  plot.setDepthFactor(0.07);
		  //设置起始角度,默认值为90,当为0时,起始值在3点钟方向
		  plot.setStartAngle(45);
		  // 设置图标题的字体,解决中文乱码
		  TextTitle textTitle = chart.getTitle();
		  textTitle.setFont(new Font("黑体", Font.PLAIN, 20));
		  // 设置背景色为白色
		  chart.setBackgroundPaint(Color.white);
		  // 设置Legend部分字体,解决中文乱码
		  chart.getLegend().setItemFont(new Font("宋体", Font.PLAIN, 12));
		  // 输出图片到文件
		Random rand = new Random();
		int i = rand.nextInt();
		String imgPath = realPath + "chartTemp\\" + System.nanoTime()+Tools.getRandom(i)+ ".png";
		fo.newFolder(realPath + "chartTemp\\");
		FileOutputStream fout = new FileOutputStream(imgPath);
		// shape是指热点的区域
		Shape shape = new Rectangle(20, 10);
		ChartEntity entity = new ChartEntity(shape);
		StandardEntityCollection coll = new StandardEntityCollection();
		coll.add(entity);
		ChartRenderingInfo info = new ChartRenderingInfo(coll);
		// 并将图片信息写入到info对象中
		ChartUtilities.writeChartAsPNG(fout, chart, width, height, info);
		// 生成图片,并返回图片名称
		filename = ServletUtilities.saveChartAsPNG(chart, width, height, info, session);
		// 读取ChartRenderingInfo对象,生成Map信息,这些信息写在pw的输出流中,这里的输出流就是Response.out,也就是直接输出到页面了。
		ChartUtilities.writeImageMap(pw, filename, info, false);
		pw.flush(); // 调用pw.flush()方法关闭IO流
		return filename;
	}
	
	public String showline(HttpServletResponse response,
			HttpSession session, String realPath) throws IOException {
		
		String filename = null;
		FileOperate fo = new FileOperate();
		PrintWriter pw = response.getWriter(); // 输出MAP信息
		int width = 800;
		int height = 300;
		JFreeChart chart = (JFreeChart)FacesUtils.getValueInHashtableOfSession("chart");
		 CategoryPlot plot = chart.getCategoryPlot();  
	        plot.setBackgroundPaint(Color.LIGHT_GRAY); // 设置绘图区背景色  
	        plot.setRangeGridlinePaint(Color.WHITE); // 设置水平方向背景线颜色  
	        plot.setRangeGridlinesVisible(true);// 设置是否显示水平方向背景线,默认值为true  
	        plot.setDomainGridlinePaint(Color.WHITE); // 设置垂直方向背景线颜色  
	        plot.setDomainGridlinesVisible(true); // 设置是否显示垂直方向背景线,默认值为false  
	          
	          
	        CategoryAxis domainAxis = plot.getDomainAxis();    
	        domainAxis.setLowerMargin(0.01);// 左边距 边框距离  
	        domainAxis.setUpperMargin(0.06);// 右边距 边框距离,防止最后边的一个数据靠近了坐标轴。  
	        domainAxis.setMaximumCategoryLabelLines(2);  
	          
	        ValueAxis rangeAxis = plot.getRangeAxis();  
	        rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());//Y轴显示整数  
	        rangeAxis.setAutoRangeMinimumSize(1);   //最小跨度  
	        rangeAxis.setUpperMargin(0.18);//上边距,防止最大的一个数据靠近了坐标轴。     
	        rangeAxis.setLowerBound(0);   //最小值显示0  
	        rangeAxis.setAutoRange(false);   //不自动分配Y轴数据  
	        rangeAxis.setTickMarkStroke(new BasicStroke(1.6f));     // 设置坐标标记大小  
	        rangeAxis.setTickMarkPaint(Color.GREEN);     // 设置坐标标记颜色  
	  
	          
	          
	     // 获取折线对象  
	        LineAndShapeRenderer renderer = (LineAndShapeRenderer) plot.getRenderer();  
	        BasicStroke realLine = new BasicStroke(3.0f); // 设置实线  
	        // 设置虚线  
	        float dashes[] = { 5.0f };   
	        BasicStroke brokenLine = new BasicStroke(5.2f, // 线条粗细  
	                BasicStroke.JOIN_MITER, // 端点风格  
	                BasicStroke.CAP_BUTT, // 折点风格  
	                8f, dashes, 1.6f);   
	          
	        plot.setNoDataMessage("无对应的数据,请重新查询。");  
	        plot.setNoDataMessagePaint(Color.RED);
		  // 设置图标题的字体,解决中文乱码
		  TextTitle textTitle = chart.getTitle();
		  textTitle.setFont(new Font("黑体", Font.PLAIN, 20));
		  // 设置背景色为白色
		  chart.setBackgroundPaint(Color.white);
		  // 设置Legend部分字体,解决中文乱码
		  chart.getLegend().setItemFont(new Font("宋体", Font.PLAIN, 12));
		  // 输出图片到文件
		Random rand = new Random();
		int i = rand.nextInt();
		String imgPath = realPath + "chartTemp\\" + System.nanoTime()+Tools.getRandom(i)+ ".png";
		fo.newFolder(realPath + "chartTemp\\");
		FileOutputStream fout = new FileOutputStream(imgPath);
		// shape是指热点的区域
		Shape shape = new Rectangle(20, 10);
		ChartEntity entity = new ChartEntity(shape);
		StandardEntityCollection coll = new StandardEntityCollection();
		coll.add(entity);
		ChartRenderingInfo info = new ChartRenderingInfo(coll);
		// 并将图片信息写入到info对象中
		ChartUtilities.writeChartAsPNG(fout, chart, width, height, info);
		// 生成图片,并返回图片名称
		filename = ServletUtilities.saveChartAsPNG(chart, width, height, info, session);
		// 读取ChartRenderingInfo对象,生成Map信息,这些信息写在pw的输出流中,这里的输出流就是Response.out,也就是直接输出到页面了。
		ChartUtilities.writeImageMap(pw, filename, info, false);
		pw.flush(); // 调用pw.flush()方法关闭IO流
		return filename;
	}
	
}


你可能感兴趣的:(饼状图)