JFreeChar各种图画法


import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.awt.Rectangle;
import java.awt.geom.Ellipse2D;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;

import javax.servlet.http.HttpSession;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartRenderingInfo;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.AxisLocation;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.CategoryLabelPositions;
import org.jfree.chart.axis.DateAxis;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.block.BlockBorder;
import org.jfree.chart.entity.StandardEntityCollection;
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.DatasetRenderingOrder;
import org.jfree.chart.plot.MultiplePiePlot;
import org.jfree.chart.plot.PiePlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.category.BarRenderer3D;
import org.jfree.chart.renderer.category.LineAndShapeRenderer;
import org.jfree.chart.renderer.category.StackedBarRenderer3D;
import org.jfree.chart.renderer.xy.XYItemRenderer;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.chart.servlet.ServletUtilities;
import org.jfree.chart.title.LegendTitle;
import org.jfree.chart.title.TextTitle;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.data.time.Month;
import org.jfree.data.time.TimeSeries;
import org.jfree.data.time.TimeSeriesCollection;
import org.jfree.data.xy.XYDataset;
import org.jfree.ui.RectangleInsets;
import org.jfree.util.TableOrder;


/**
 * 
 * @author Bingo汇集生成曲线、柱形、堆积柱形、饼形图的数据集,图形生成

方法。
 */
public class CreateChart {

	private Font font = new Font("黑体", Font.BOLD, 14);
	private Font fontText = new Font("黑体", Font.PLAIN, 12);
	private Font fontAxis = new Font("黑体", Font.PLAIN, 12);
	// 定义文件名称
	private String fileName = "";

	/**
	 * 生成曲线图数据集
	 */
	public XYDataset createLineDateSet(String strTtimePointZone, 

String[][] arr) {
		TimeSeriesCollection dataset = new 

TimeSeriesCollection();
		// 得到传值。时间和值。
		// String[] str = strTtimePointZone.split(",");//应该是

指标周期
		// 处理传值。根据传值设置曲线数。
		int ty = 0;
		int tm = 0;
		for (int i = 1; i < arr[0].length; i++) {
			TimeSeries s_i = new TimeSeries(arr[0][i], 

Month.class);
			// Day.class);
			// 创建时间数据源,每一个//TimeSeries在图上是一

条曲线
			for (int j = 3; j < arr.length; j++) {
				if (!arr[j][i].equals("--")) {
					ty = Integer.parseInt(arr[j]

[0].substring(0, 4));
					tm = Integer.parseInt(arr[j]

[0].substring(4, 6));//怎么比较同月数据
					s_i.add(new Month(tm, ty), 

Double.parseDouble(arr[j][i]));// 时间点,数值
				}
			}
			dataset.addSeries(s_i);
		}
		return dataset;
	}

	/**
	 * 生成堆栈柱状图数据集
	 * 一个时间轴点上有多个数据。所以废数据不能删除
	 */
	public CategoryDataset createStackedBarDateSet(String 

strTtimePointZone,
			String[][] arr) {
		DefaultCategoryDataset dataset = new 

DefaultCategoryDataset();// 定义一个柱图的数据集合。
		int rowindex = arr[0].length;
		int columindex = arr.length;
		String ty = "";
		String tm = "";
		String[] rowKeys = new String[rowindex-1];//定义每个柱

子,指标名称
		for (int i = 1; i < rowindex; i++) {
			rowKeys[i - 1] = arr[0][i];//指标名称
		}

		String[] columnKeys = new String[columindex-3];//定义统

计时间
		double[][] data = new double[rowindex-1][columindex-3];
		for (int j = 3; j < columindex; j++) {
			ty = arr[j][0].substring(2, 4);
			tm = arr[j][0].substring(4, 6);
			columnKeys[j-3] = ty + "/" + tm;//指标时间
			for (int i = 1; i < rowindex; i++) {
				if (!arr[j][i].equals("--")) {
					data[i - 1][j-3] = 

Double.parseDouble(arr[j][i]);
				}
			}
		}	
		for(int n=0;n<data.length;n++){
			for(int m = 0;m<data[0].length;m++){
				dataset.addValue(data[n][m], rowKeys

[n], columnKeys[m]);
			}
		}
		return dataset;
	}

	/**
	 * 生成柱形图所需的数据集
	 * 
	 * @return 返回数据集
	 */
	public DefaultCategoryDataset createBarDataSet(String 

strTtimePointZone, String[][] arr) {
		DefaultCategoryDataset dataset = new 

DefaultCategoryDataset();// 定义一个柱图的数据集合。
		int rowindex = arr[0].length;
		int columindex = arr.length;
		String ty = "";
		String tm = "";
		String[] rowKeys = new String[rowindex-1];//定义每个柱

子,指标名称
		for (int i = 1; i < rowindex; i++) {
			rowKeys[i - 1] = arr[0][i];//指标名称
		}

		String[] columnKeys = new String[columindex-3];//定义统

计时间
		double[][] data = new double[rowindex-1][columindex-3];
		for (int j = 3; j < columindex; j++) {
			ty = arr[j][0].substring(2, 4);
			tm = arr[j][0].substring(4, 6);
			columnKeys[j-3] = ty + "/" + tm;//指标时间
			for (int i = 1; i < rowindex; i++) {
				if (!arr[j][i].equals("--")) {
					data[i - 1][j-3] = 

Double.parseDouble(arr[j][i]);
				}
			}
		}	
		for(int n=0;n<data.length;n++){
			for(int m = 0;m<data[0].length;m++){
				dataset.addValue(data[n][m], rowKeys

[n], columnKeys[m]);
			}
		}
		return dataset;
	}
/**
 * 混合图所需数据集同上(折线)
 * @return
 */
	public DefaultCategoryDataset createDataSet(String 

strTtimePointZone, String[][] arr){//柱形和折线所用数据集一样?
		DefaultCategoryDataset dataset = new 

DefaultCategoryDataset();// 定义一个柱图的数据集合。
		int rowindex = arr[0].length;
		int columindex = arr.length;
		String ty = "";
		String tm = "";
		String[] rowKeys = new String[rowindex-1];//定义每个柱

子,指标名称
		for (int i = 1; i < rowindex; i++) {
			rowKeys[i - 1] = arr[0][i];//指标名称
		}

		String[] columnKeys = new String[columindex-3];//定义统

计时间
		double[][] data = new double[rowindex-1][columindex-3];
		for (int j = 3; j < columindex; j++) {
			ty = arr[j][0].substring(2, 4);
			tm = arr[j][0].substring(4, 6);
			columnKeys[j-3] = ty + "/" + tm;//指标时间
			for (int i = 1; i < rowindex; i++) {
				if (!arr[j][i].equals("--")) {
					data[i - 1][j-3] = 

Double.parseDouble(arr[j][i]);
				}
			}
		}	
		for(int n=0;n<data.length;n++){
			for(int m = 0;m<data[0].length;m++){
				dataset.addValue(data[n][m], rowKeys

[n], columnKeys[m]);
			}
		}
		return dataset;
	}
	/**
	 * 生成饼形图所需的数据集
	 * 
	 * @return 返回数据集
	 */
	public CategoryDataset createPieDataSet(String 

strTtimePointZone, String[][] arr) {
		DefaultCategoryDataset dataset = new 

DefaultCategoryDataset();// 定义一个柱图的数据集合。
		int rowindex = arr[0].length;
		int columindex = arr.length;
		String ty = "";
		String tm = "";
		String[] rowKeys = new String[rowindex-1];//定义每个柱

子,指标名称
		for (int i = 1; i < rowindex; i++) {
			rowKeys[i - 1] = arr[0][i];//指标名称
		}
		String[] columnKeys = null;//指标时间,年或者月
		double[][] data = null;//存指标值
		double sum = 0.0;//计算指标值
		int sYear = Integer.parseInt(arr[3][0].substring(0, 

4));
		int year = Integer.parseInt(arr[columindex-1]

[0].substring(0, 4))-sYear;
		int month = Integer.parseInt(arr[columindex-1]

[0].substring(4, 6))-Integer.parseInt(arr[3][0].substring(4, 6));
		if(year==0){
			columnKeys = new String[month+1];//定义统计时间
			data = new double[rowindex-1][month+1];//不跨年
			for (int j = 3; j < columindex; j++) {
				ty = arr[j][0].substring(2, 4);
				tm = arr[j][0].substring(4, 6);
				columnKeys[j-3] = ty + "/" + tm;//指标

时间
				for (int i = 1; i < rowindex; i++) {
					if (!arr[j][i].equals("--")) {
						data[i - 1][j-3] = 

Math.abs(Double.parseDouble(arr[j][i]));
					}
				}
			}
		}else if(year>0){
			columnKeys = new String[year+1];
			data = new double[rowindex-1][year+1];//跨年
			for(int j = 0;j<year+1;j++){
				columnKeys[j] = sYear+j+"";
				for(int i = 1;i<rowindex;i++){
					for(int m = 

3;m<columindex;m++){
						if(columnKeys

[j].equals(arr[m][0].substring(0, 4))){
							if(arr[m]

[i].equals("--")){
								arr[m]

[i]="0.0";
							}
							sum += 

Double.parseDouble(arr[m][i]);
						}
					}
					data[i-1][j] = Math.abs(sum);//

不能显示负数
					sum = 0.0;
				}
			}
		}
		for(int n=0;n<data.length;n++){
			for(int m = 0;m<data[0].length;m++){
				dataset.addValue(data[n][m], rowKeys

[n], columnKeys[m]);
			}
		}
		return dataset;
	}

	/**
	 * 生成曲线图形的方法。
	 * 
	 * @param session
	 * @param strTtimePointZone
	 * @param arr
	 * @param pw
	 * @return
	 */
	public String getLineChart(HttpSession session, String 

strTtimePointZone,
			String[][] arr, PrintWriter pw) {

		XYDataset dataset = this.createLineDateSet

(strTtimePointZone, arr);// 建立数据集
		JFreeChart chart = ChartFactory.createTimeSeriesChart("

指标曲线图", // title
				"时间", // x-axis label
				"值", // y-axis label
				dataset, // data
				true, // create legend?
				true, // generate tooltips?
				false // generate URLs?
				);
		// 设置JFreeChart的显示属性,对图形外部部分进行调整
		chart.setBackgroundPaint(Color.white);// 设置曲线图背景

色
		// 设置字体大小,形状
		TextTitle title = new TextTitle("指标曲线图", font);
		chart.setTitle(title);

		XYPlot plot = (XYPlot) chart.getPlot();// 获取图形的画

布
		plot.setBackgroundPaint(Color.white);// 设置网格背景色
		plot.setDomainGridlinePaint(Color.black);// 设置网格Y

(Domain轴)颜色
		plot.setRangeGridlinePaint(Color.black);// 设置网格X横

线颜色
		plot.setRangeGridlineStroke(new BasicStroke(0.2f)); // 

数据X轴网格线条笔触
		plot.setDomainGridlineStroke(new BasicStroke(0.1f)); // 

数据Y轴网格线条笔触
		plot.setAxisOffset(new RectangleInsets(1.0, 1.0, 1.0, 

1.0));// 设置曲线图与xy轴的距离
		plot.setDomainCrosshairVisible(true);
		plot.setRangeCrosshairVisible(true);

		XYItemRenderer r = plot.getRenderer();
		if (r instanceof XYLineAndShapeRenderer) {
			XYLineAndShapeRenderer renderer = 

(XYLineAndShapeRenderer) r;
			renderer.setBaseLegendTextFont(fontText);
			renderer.setShapesVisible(true);
			renderer.setShapesFilled(true);
			renderer.setShapesVisible(false);// 设置曲线是

否显示数据点
			processRenderer(renderer, dataset);
		}
		// 设置Y轴
		NumberAxis numAxis = (NumberAxis) plot.getRangeAxis();
		numAxis.setLabelFont(fontAxis);
		numAxis.setStandardTickUnits

(NumberAxis.createIntegerTickUnits()); 
		numAxis.setAutoRangeIncludesZero(false); //设置Y轴的数

据自动范围,不从0开始 

		NumberFormat numFormater = 

NumberFormat.getNumberInstance();
		numFormater.setMinimumFractionDigits(2);
		numAxis.setNumberFormatOverride(numFormater);

		SimpleDateFormat mm = new SimpleDateFormat("MM");
		SimpleDateFormat yyyy = new SimpleDateFormat("yyyy");

		// 设置X轴(日期轴)
		DateAxis axis = (DateAxis) plot.getDomainAxis();
		axis.setLabelFont(fontAxis);
		axis.setDateFormatOverride(new SimpleDateFormat

("yy/MM"));
		
		ChartRenderingInfo info = new ChartRenderingInfo(
				new StandardEntityCollection());
		try {
			fileName = ServletUtilities.saveChartAsPNG

(chart, 720, 540, info,
					session);// 生成图片,放到服务器

的临时文件夹下
			// Write the image map to the PrintWriter
			ChartUtilities.writeImageMap(pw, fileName, 

info, false);
		} catch (IOException e) {
			e.printStackTrace();
		}
		pw.flush();
		return fileName;
	}

	/**
	 * 生成堆栈柱状图的方法
	 * @param String[][] arr
	 * @return
	 */
	public String getStackedBarChart(HttpSession session,
			String strTtimePointZone, String[][] arr, 

PrintWriter pw) {
		// 1:得到 CategoryDataset
		// 2:JFreeChart对象
		CategoryDataset dataset = this.createStackedBarDateSet(
				strTtimePointZone, arr);// 数据集
		JFreeChart chart = ChartFactory.createStackedBarChart("

堆积柱图", // 图表标题
				"时间", // x-axis label
				"值", // y-axis label
				dataset, // 数据集
				PlotOrientation.VERTICAL, // 图表方向:

水平、垂直
				true, // 是否显示图例(对于简单的柱状图

必须是false)
				false, // 是否生成工具
				false // 是否生成URL链接
				);
		// 设置JFreeChart的显示属性,对图形外部部分进行调整
		chart.setBackgroundPaint(Color.white);// 设置图背景色
		TextTitle title = new TextTitle("指标堆积图", font);// 

设置字体大小,形状
		chart.setTitle(title);
		// 图例字体清晰
		chart.setTextAntiAlias(true);
		CategoryPlot plot = chart.getCategoryPlot();//画布属性
		plot.setNoDataMessage("NO Data!"); 
		plot.setRangeGridlinesVisible(true);// 设置横虚线可见
		plot.setRangeGridlinePaint(Color.gray);// 虚线色彩
		plot.setBackgroundPaint(Color.WHITE);// 设置网格背景色
		plot.setDomainGridlinePaint(Color.DARK_GRAY);// 设置网

格Y(Domain轴)颜色
		plot.setRangeGridlinePaint(Color.DARK_GRAY);// 设置网格

X横线颜色
		plot.setRangeGridlineStroke(new BasicStroke(0.2f)); // 

数据X轴网格线条笔触
		plot.setDomainGridlineStroke(new BasicStroke(0.1f)); // 

数据Y轴网格线条笔触
		plot.setAxisOffset(new RectangleInsets(1.0, 1.0, 1.0, 

1.0));// 设置曲线图与xy轴的距离
		plot.setDomainCrosshairVisible(true);
		plot.setRangeCrosshairVisible(true);
		
		NumberAxis vn = (NumberAxis) plot.getRangeAxis();// 数

据轴精度
		vn.setAutoRangeIncludesZero(true);// 设置数据轴坐标从0

开始
		vn.setLabelFont(fontAxis);// 轴标题
		
		NumberFormat numFormater = 

NumberFormat.getNumberInstance();
		numFormater.setMinimumFractionDigits(2);//显示小数点后2

位
		
		DecimalFormat df = new DecimalFormat("");// 数据显示格

式是百分比
		vn.setNumberFormatOverride(df); // 数据轴数据标签的显示

格式
		// DomainAxis (区域轴,相当于 x 轴), RangeAxis (范

围轴,相当于 y 轴)
		CategoryAxis domainAxis = plot.getDomainAxis();
		domainAxis.setLabelFont(fontAxis);// X轴标题
		domainAxis.setTickLabelFont(fontAxis);// X轴数值
		domainAxis.setMaximumCategoryLabelWidthRatio(0.6f);// 

横轴上的 Lable 是否完整显示
//		domainAxis.setCategoryLabelPositions

(CategoryLabelPositions.createUpRotationLabelPositions(Math.PI / 7.0));
		domainAxis.setCategoryLabelPositions

(CategoryLabelPositions.DOWN_90);//X轴显示位置为90度
		plot.setDomainAxis(domainAxis);
		
		ValueAxis rangeAxis = plot.getRangeAxis();// y轴设置
		rangeAxis.setLabelFont(fontText);
		rangeAxis.setTickLabelFont(fontText);
		rangeAxis.setUpperMargin(0.15);// 设置最高的一个 Item 

与图片顶端的距离
		rangeAxis.setLowerMargin(0.15);// 设置最低的一个 Item 

与图片底端的距离
		((NumberAxis) rangeAxis).setNumberFormatOverride

(numFormater);
		plot.setRangeAxis(rangeAxis);

		// Renderer 对象是图形的绘制单元
//		StackedBarRenderer renderer = (StackedBarRenderer) 

plot.getRenderer();
		StackedBarRenderer3D renderer = new 

StackedBarRenderer3D();//just test
		renderer.setItemLabelFont(fontText);
		renderer.setMaximumBarWidth(0.05);// 设置柱子宽度
		renderer.setMinimumBarLength(0.1);// 设置柱子高度
		renderer.setBaseOutlinePaint(Color.BLACK);// 设置柱的边

框颜色
		renderer.setDrawBarOutline(true);// 设置柱的边框可见
        renderer.setItemLabelGenerator(new 

StandardCategoryItemLabelGenerator());   
		renderer.setItemMargin(0.05);// 设置每个地区所包含的平

行柱的之间距离
		plot.setRenderer(renderer);
		
		// 取得统计图表的第一个图例
		LegendTitle legend = chart.getLegend(0);
		 //设置图例的字体 
		legend.setItemFont(fontText);

		ChartRenderingInfo info = new ChartRenderingInfo(
				new StandardEntityCollection());
		try {
			fileName = ServletUtilities.saveChartAsPNG

(chart, 720, 540, info,
					session);// 生成图片,放到服务器

的临时文件夹下
			// Write the image map to the PrintWriter
			ChartUtilities.writeImageMap(pw, fileName, 

info, false);
		} catch (IOException e) {
			e.printStackTrace();
		}
		pw.flush();
		return fileName;
	}

	/**
	 * 生成(混合图形)柱形图+曲线图的方法
	 *
	 * @return 返回文件名称
	 */
	public String getBarChart(HttpSession session, String 

strTimePointZone,
			String[][] arr, PrintWriter pw) {
		
		CategoryDataset categorydataset1 = 

this.createBarDataSet(strTimePointZone, arr);//柱线数据集
		CategoryDataset categorydataset = 

this.createBarDataSet(strTimePointZone, arr);//折线数据集(相同方法)
		
		JFreeChart chart = ChartFactory.createBarChart("指标混

合图",
				"时间", 
				"值", 
				categorydataset1,
				PlotOrientation.VERTICAL, 
				true, 
				false, 
				false);
		chart.setBackgroundPaint(Color.white);// 设置曲线图背景

色
		TextTitle title = new TextTitle("指标混合图", font);// 

设置字体大小,形状
		chart.setTitle(title);
		chart.setTextAntiAlias(true);
		
		CategoryPlot categoryplot = (CategoryPlot) 

chart.getPlot();
		categoryplot.setNoDataMessage("NO Data!"); 
		categoryplot.setDomainAxisLocation

(AxisLocation.BOTTOM_OR_RIGHT);
		//映射折线数据集
		categoryplot.setDataset(1, categorydataset);
		categoryplot.mapDatasetToRangeAxis(1, 1);
		categoryplot.setBackgroundPaint(Color.white);
		categoryplot.setRangeGridlinesVisible(true);// 设置横虚

线可见
		categoryplot.setRangeGridlinePaint(Color.gray);// 虚线

色彩
		categoryplot.setDomainGridlinePaint(Color.DARK_GRAY);// 

设置网格Y(Domain轴)颜色
		categoryplot.setRangeGridlinePaint(Color.DARK_GRAY);// 

设置网格X横线颜色
		categoryplot.setRangeGridlineStroke(new BasicStroke

(0.2f)); // 数据X轴网格线条笔触
		categoryplot.setDomainGridlineStroke(new BasicStroke

(0.1f)); // 数据Y轴网格线条笔触
		categoryplot.setAxisOffset(new RectangleInsets(1.0, 

1.0, 1.0, 1.0));// 设置图与xy轴的距离
		categoryplot.setDomainCrosshairVisible(true);
		categoryplot.setRangeCrosshairVisible(true);
		
		CategoryAxis categoryaxis = categoryplot.getDomainAxis

();//X轴
		categoryaxis.setLabelFont(fontText);
		categoryaxis.setTickLabelFont(fontText);
//		categoryaxis.setCategoryLabelPositions

(CategoryLabelPositions.createUpRotationLabelPositions(Math.PI / 7.0));
		categoryaxis.setCategoryLabelPositions

(CategoryLabelPositions.DOWN_90);//X轴显示位置为90度
		
		NumberFormat numFormater = 

NumberFormat.getNumberInstance();
		numFormater.setMinimumFractionDigits(2);//显示小数点后2

位
		
		NumberAxis left = new NumberAxis("值");//Y轴
		NumberAxis numberaxis = new NumberAxis("比率");//Y轴
		numberaxis.setLabelFont(fontText);
		left.setLabelFont(fontText);
		left.setNumberFormatOverride(numFormater);
		numberaxis.setNumberFormatOverride(numFormater);
		
		categoryplot.setRangeAxis(0, left);//左侧Y轴
		categoryplot.setRangeAxis(1, numberaxis);//右侧Y轴
		
		BarRenderer3D renderer = new BarRenderer3D();
		renderer.setBaseOutlinePaint(Color.BLACK); 
//		设置每个地区所包含的平行柱之间的距离

renderer.setItemMargin(0.1);
		renderer.setMaximumBarWidth(0.05);// 设置柱子宽度
		renderer.setMinimumBarLength(0.1);// 设置柱子高度
		renderer.setBaseOutlinePaint(Color.BLACK);// 设置柱的边

框颜色
		renderer.setDrawBarOutline(true);// 设置柱的边框可见
        renderer.setItemLabelGenerator(new 

StandardCategoryItemLabelGenerator());  
		renderer.setItemMargin(0.05);
		
		LineAndShapeRenderer lineandshaperenderer = new 

LineAndShapeRenderer();//折线对象
		lineandshaperenderer.setToolTipGenerator(new 

StandardCategoryToolTipGenerator());
		lineandshaperenderer.setSeriesPaint(0, new Color(0, 0, 

255)); 

		categoryplot.setRenderer(0,renderer);
		categoryplot.setRenderer(1, lineandshaperenderer);
		categoryplot.setDatasetRenderingOrder

(DatasetRenderingOrder.FORWARD);
		//图例各个属性
		LegendTitle legendtitle = chart.getLegend(0);
		legendtitle.setMargin(new RectangleInsets(2D, 2D, 2D, 

2D));
		legendtitle.setBorder(new BlockBorder());
		legendtitle.setItemFont(fontText);
/*		
		LegendTitle legendtitle1 = chart.getLegend(1);
		legendtitle1.setMargin(new RectangleInsets(2D, 2D, 2D, 

2D));
		legendtitle1.setItemFont(new Font

("SimSun",Font.BOLD,10));
		legendtitle1.setBorder(new BlockBorder());
		
		BlockContainer blockcontainer = new BlockContainer(new 

BorderArrangement());
		blockcontainer.add(legendtitle, RectangleEdge.LEFT);
		blockcontainer.add(legendtitle1, RectangleEdge.RIGHT);
		blockcontainer.add(new EmptyBlock(2000D, 0.0D));
		CompositeTitle compositetitle = new CompositeTitle

(blockcontainer);
		compositetitle.setPosition(RectangleEdge.BOTTOM);  
      	chart.addSubtitle(compositetitle); 
 *  可能以后会用到的功能
 */ 
		ChartRenderingInfo info = new ChartRenderingInfo(new 

StandardEntityCollection());
		try {
			fileName = ServletUtilities.saveChartAsPNG

(chart, 720, 540, info,
					session);// 生成图片
			ChartUtilities.writeImageMap(pw, fileName, 

info, false);
		} catch (IOException e) {
			e.printStackTrace();
		}
		pw.flush();
		return fileName;// 返回生成图片的文件名
	}

	/**
	 * 饼状图
	 * 
	 * @param String[][]
	 *            arr
	 * @return
	 */
	public String getPieChart(HttpSession session, String 

strTtimePointZone,
			String[][] arr, PrintWriter pw) {
		CategoryDataset dataset = this.createPieDataSet

(strTtimePointZone, arr);// 建立数据集
		JFreeChart chart = ChartFactory.createMultiplePieChart

("指标饼图",  dataset, TableOrder.BY_ROW, true, false, false);
		
		chart.setBackgroundPaint(Color.white);// 设置图背景色
		TextTitle title = new TextTitle("指标饼图", font);// 设

置字体大小,形状
		chart.setTitle(title);
		chart.setTextAntiAlias(true);
		
		MultiplePiePlot multiplepieplot = (MultiplePiePlot)

chart.getPlot();
		JFreeChart jfreechart1 = multiplepieplot.getPieChart();
		jfreechart1.getTitle().setFont(fontText);
		PiePlot plot = (PiePlot) jfreechart1.getPlot();
		//设置背景颜色
		plot.setBackgroundPaint(Color.GRAY);
		//设置透明度
		//plot.setForegroundAlpha(0.65f);
		// 指定饼图轮廓线的颜色 
		plot.setBaseSectionOutlinePaint(Color.BLACK);
		plot.setBaseSectionPaint(Color.BLACK);
		//设置饼图为圆形,默认分饼从12点钟方向开始。
		plot.setCircular(false);
		plot.setLabelLinkPaint(Color.BLUE);
		plot.setSectionOutlinesVisible(false);
		//无数据是显示提示和颜色。
		plot.setNoDataMessage("无数据!");
		plot.setNoDataMessagePaint(Color.BLACK);
		plot.setToolTipGenerator(new 

StandardPieToolTipGenerator());
		plot.setInteriorGap(0.30);

		//设置扇区分离显示   
		plot.setExplodePercent("", 0.2D);
		//设置扇区边框不可见   
		plot.setSectionOutlinesVisible(false);
		// 图片中显示百分比:自定义方式,{0} 表示选项, {1} 表示

数值, {2} 表示所占比例 ,小数点后两位   
		plot.setLabelGenerator(new 

StandardPieSectionLabelGenerator(
				"{1}", NumberFormat.getNumberInstance

(),
				new DecimalFormat("0.0%")));
		// 图例显示百分比:自定义方式, 同上说明
		plot.setLegendLabelGenerator(new 

StandardPieSectionLabelGenerator(
				"{0}"));
		plot.setLabelFont(fontText);
		//图例各个属性
		LegendTitle legendtitle = new LegendTitle

(jfreechart1.getPlot());
		legendtitle.setMargin(new RectangleInsets(2D, 2D, 2D, 

2D));
		legendtitle.setBorder(new BlockBorder());
		legendtitle.setItemFont(fontText);

		ChartRenderingInfo info = new ChartRenderingInfo(
				new StandardEntityCollection());
		try {
			fileName = ServletUtilities.saveChartAsPNG

(chart, 720, 540, info,
					session);// 生成图片,放到服务器

的临时文件夹下
			// Write the image map to the PrintWriter
			ChartUtilities.writeImageMap(pw, fileName, 

info, false);
		} catch (IOException e) {
			e.printStackTrace();
		}
		pw.flush();
		return fileName;
	}

	// 曲线图形的微调方法。
	private void processRenderer(XYLineAndShapeRenderer renderer,
			XYDataset dataset) {
		Ellipse2D ell = new Ellipse2D.Float(-4, -4, 8, 8);
		if (dataset.getSeriesCount() == 8) {
			renderer.setSeriesStroke(2, new BasicStroke

(2.0F, 1, 1, 1.0F,
					new float[] { 6F, 6F }, 

0.0F));// 虚线
			renderer.setSeriesStroke(3, new BasicStroke

(2.0F, 1, 1, 1.0F,
					new float[] { 6F, 6F }, 

0.0F));// 虚线
			renderer.setSeriesShape(2, ell); // 设置第一个

XYSeries数据点大小
			renderer.setSeriesPaint(2, Color.cyan);
			renderer.setSeriesShape(3, new Rectangle(-3, -

3, 6, 6));// 设置第二个XYSeries数据点大小
			renderer.setSeriesPaint(3, Color.green);

			renderer.setSeriesStroke(4, new BasicStroke

(2.0F, 1, 1, 1.0F,
					new float[] { 6F, 6F }, 

0.0F));// 虚线
			renderer.setSeriesStroke(5, new BasicStroke

(2.0F, 1, 1, 1.0F,
					new float[] { 6F, 6F }, 

0.0F));// 虚线
			renderer.setSeriesShape(4, ell); // 设置第一个

XYSeries数据点大小
			renderer.setSeriesPaint(4, Color.cyan);
			renderer.setSeriesShape(5, new Rectangle(-3, -

3, 6, 6));// 设置第二个XYSeries数据点大小
			renderer.setSeriesPaint(5, Color.green);

			renderer.setSeriesStroke(6, new BasicStroke

(2.0F, 1, 1, 1.0F,
					new float[] { 6F, 6F }, 

0.0F));// 虚线
			renderer.setSeriesStroke(7, new BasicStroke

(2.0F, 1, 1, 1.0F,
					new float[] { 6F, 6F }, 

0.0F));// 虚线
			renderer.setSeriesShape(6, ell); // 设置第一个

XYSeries数据点大小
			renderer.setSeriesPaint(6, Color.pink);
			renderer.setSeriesShape(7, new Rectangle(-3, -

3, 6, 6));// 设置第二个XYSeries数据点大小
			renderer.setSeriesPaint(7, Color.yellow);

			renderer.setSeriesStroke(8, new BasicStroke

(2.0F, 1, 1, 1.0F,
					new float[] { 6F, 6F }, 

0.0F));// 虚线
			renderer.setSeriesStroke(9, new BasicStroke

(2.0F, 1, 1, 1.0F,
					new float[] { 6F, 6F }, 

0.0F));// 虚线
			renderer.setSeriesShape(8, ell); // 设置第一个

XYSeries数据点大小
			renderer.setSeriesPaint(8, Color.pink);
			renderer.setSeriesShape(9, new Rectangle(-3, -

3, 6, 6));// 设置第二个XYSeries数据点大小
			renderer.setSeriesPaint(9, Color.yellow);

		}
	}
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值