`

[转]JFreeChart生成饼状图

阅读更多


  利用JFreeChart jar包可以绘制pie charts 饼图,bar charts 柱状图,line and area charts曲线图,scatter plots and bubble charts 散列图,time series 时序图,Area Charts区域图,Difference Chart差异图,Step Chart步骤图,Multiple Axis Charts 混合图,Gantt charts甘特图,combination charts 复合图.

其主要的核心类如下:
  两个大包:
  org.jfree.chart.* 主要与图形本身有关
  org.jfree.data.* 与图形显示的数据有关

核心类主要有:
org.jfree.chart.JFreeChart:图表对象,任何类型的图表的最终表现形式都是在该对象进行一些属性的定制。JFreeChart 引擎本身提供了一个工厂类用于创建不同类型的图表对象
org.jfree.data.category.XXXDataSet:数据集对象,用于提供显示图表所用的数据。根据不同类型的图表对应着很多类型的数据集对象类
org.jfree.chart.plot.XXXPlot:图表区域对象,基本上这个对象决定着什么样式的图表,创建该对象的时候需要Axis、 Renderer以及数据集对象的支持
org.jfree.chart.axis.XXXAxis:用于处理图表的两个轴:纵轴和横轴
org.jfree.chart.render.XXXRender:负责如何显示一个图表对象
org.jfree.chart.urls.XXXURLGenerator:用于生成Web图表中每个项目的鼠标点击链接
XXXXXToolTipGenerator:用于生成图象的帮助提示,不同类型图表对应不同类型的工具提示类

  下面通过生成一个JFreeChart的 Pie饼状图,来熟悉JFreeChart的框架以及如何编写代码生成想要的图形。下面例子是在网上找到的,我稍微修改、加了一点注释。

package com.javachen.jfreechart;
import java.awt.Color;
import java.awt.Font;
import java.io.FileOutputStream;
import java.text.DecimalFormat;
import java.text.NumberFormat;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.labels.StandardPieSectionLabelGenerator;
import org.jfree.chart.labels.StandardPieToolTipGenerator;
import org.jfree.chart.plot.PiePlot;
import org.jfree.chart.plot.PiePlot3D;
import org.jfree.chart.title.TextTitle;
import org.jfree.data.general.DefaultPieDataset;
import org.jfree.data.general.PieDataset;

public class PieChart {
	public static void main(String[] args) {
		createPieChart();
		createPieChart3D();
	}

	public static void createPieChart3D(){
		PieDataset dataset = getDataSet();
		JFreeChart chart = ChartFactory.createPieChart3D(
				" 项目进度分布",// 图表标题
				dataset,// data
				true,// include legend
				true, true);
		PiePlot3D plot = (PiePlot3D) chart.getPlot();
		// 图片中显示百分比:默认方式
		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));

		// 输出图片到文件
		FileOutputStream fos_jpg = null;
		try {
			fos_jpg = new FileOutputStream("D:\\项目状态分布-3D.jpg");
			ChartUtilities.writeChartAsJPEG(fos_jpg, 0.8f, chart, 800, 600,
					null);
			fos_jpg.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static void createPieChart(){
		PieDataset dataset = getDataSet();
		JFreeChart chart = ChartFactory.createPieChart(" 项目进度分布",// 图表标题
				dataset,// data
				true,// include legend
				true, true);
		PiePlot plot = (PiePlot) chart.getPlot();
		// 图片中显示百分比:默认方式
		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);
		//设置起始角度,默认值为90,当为0时,起始值在3点钟方向
		plot.setStartAngle(45);

		/**
		 * 设置某一块凸出,第一个参数为dataSet的key,此方法只在PiePlot中有效
		 */
		plot.setExplodePercent(" 运维", 0.2);

		// 设置图标题的字体,解决中文乱码
		TextTitle textTitle = chart.getTitle();
		textTitle.setFont(new Font("黑体", Font.PLAIN, 20));

		// 设置背景色为白色
		chart.setBackgroundPaint(Color.white);
		// 设置Legend部分字体,解决中文乱码
		chart.getLegend().setItemFont(new Font("宋体", Font.PLAIN, 12));

		// 输出图片到文件
		FileOutputStream fos_jpg = null;
		try {
			fos_jpg = new FileOutputStream("D:\\项目状态分布.jpg");
			ChartUtilities.writeChartAsJPEG(fos_jpg, 0.8f, chart, 800, 600,
					null);
			fos_jpg.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private static PieDataset getDataSet() {
		DefaultPieDataset dataset = new DefaultPieDataset();
		dataset.setValue(" 市场前期", new Double(10));
		dataset.setValue(" 立项", new Double(15));
		dataset.setValue(" 计划", new Double(10));
		dataset.setValue(" 需求与设计", new Double(10));
		dataset.setValue(" 执行控制", new Double(35));
		dataset.setValue(" 收尾", new Double(10));
		dataset.setValue(" 运维", new Double(10));
		return dataset;
	}
}


  最后生成的图如下:


  查阅api相关的方法,可以美化上图,关于Pie的相关类和方法如下:
引用

Plot类:
void setBackgroundImage(Image image) 数据区的背景图片
void setBackgroundImageAlignment(int alignment) 数据区的背景图片对齐方式(参数常量在org.jfree.ui.Align类中定义)
void setBackgroundPaint(Paint paint) 数据区的背景图片背景色
void setBackgroundAlpha(float alpha) 数据区的背景透明度(0.0~1.0)
void setForegroundAlpha(float alpha) 数据区的前景透明度(0.0~1.0)
void setDataAreaRatio(double ratio) 数据区占整个图表区的百分比
void setOutLinePaint(Paint paint) 数据区的边界线条颜色
void setOutLineStroke(Stroke stroke) 数据区的边界线条笔触
void setNoDataMessage(String message) 没有数据时显示的消息
void setNoDataMessageFont(Font font) 没有数据时显示的消息字体
void setNoDataMessagePaint(Paint paint) 没有数据时显示的消息颜色
——————————————————————————————————–
CategoryPlot(Plot)类:
void setDataset(CategoryDataset dataset) 数据区的2维数据表
void setColumnRenderingOrder(SortOrder order) 数据分类的排序方式
void setAxisOffset(Spacer offset) 坐标轴到数据区的间距
void setOrientation(PlotOrientation orientation) 数据区的方向(PlotOrientation.HORIZONTAL或PlotOrientation.VERTICAL)
void setDomainAxis(CategoryAxis axis) 数据区的分类轴
void setDomainAxisLocation(AxisLocation location) 分类轴的位置(参数常量在org.jfree.chart.axis.AxisLocation类中定义)
void setDomainGridlinesVisible(boolean visible) 分类轴网格是否可见
void setDomainGridlinePaint(Paint paint) 分类轴网格线条颜色
void setDomainGridlineStroke(Stroke stroke) 分类轴网格线条笔触
void setRangeAxis(ValueAxis axis) 数据区的数据轴
void setRangeAxisLocation(AxisLocation location) 数据轴的位置(参数常量在org.jfree.chart.axis.AxisLocation类中定义)
void setRangeGridlinesVisible(boolean visible) 数据轴网格是否可见
void setRangeGridlinePaint(Paint paint) 数据轴网格线条颜色
void setRangeGridlineStroke(Stroke stroke) 数据轴网格线条笔触
void setRenderer(CategoryItemRenderer renderer) 数据区的表示者(详见Renderer组)
void addAnnotation(CategoryAnnotation annotation) 给数据区加一个注释
void addRangeMarker(Marker marker,Layer layer) 给数据区加一个数值范围区域
———————————————————————————————————
PiePlot(Plot)类:
void setDataset(PieDataset dataset) 数据区的1维数据表
void setIgnoreNullValues(boolean flag) 忽略无值的分类
void setCircular(boolean flag) 饼图是否一定是正圆
void setStartAngle(double angle) 饼图的初始角度
void setDirection(Rotation direction) 饼图的旋转方向
void setExplodePercent(int section,double percent) 抽取的那块(1维数据表的分类下标)以及抽取出来的距离(0.0~1.0),3D饼图无效
void setLabelBackgroundPaint(Paint paint) 分类标签的底色
void setLabelFont(Font font) 分类标签的字体
void setLabelPaint(Paint paint) 分类标签的字体颜色
void setLabelLinkMargin(double margin) 分类标签与图的连接线边距
void setLabelLinkPaint(Paint paint) 分类标签与图的连接线颜色
void setLabelLinkStroke(Stroke stroke) 分类标签与图的连接线笔触
void setLabelOutlinePaint(Paint paint) 分类标签边框颜色
void setLabelOutlineStroke(Paint paint) 分类标签边框笔触
void setLabelShadowPaint(Paint paint) 分类标签阴影颜色
void setMaximumLabelWidth(double width) 分类标签的最大长度(0.0~1.0)
void setPieIndex(int index) 饼图的索引(复合饼图中用到)
void setSectionOutlinePaint(int section,Paint paint) 指定分类饼的边框颜色
void setSectionOutlineStroke(int section,Stroke stroke) 指定分类饼的边框笔触
void setSectionPaint(int section,Paint paint) 指定分类饼的颜色
void setShadowPaint(Paint paint) 饼图的阴影颜色
void setShadowXOffset(double offset) 饼图的阴影相对图的水平偏移
void setShadowYOffset(double offset) 饼图的阴影相对图的垂直偏移
void setLabelGenerator(PieSectionLabelGenerator generator) 分类标签的格式,设置成null则整个标签包括连接线都不显示
void setToolTipGenerator(PieToolTipGenerator generator) MAP中鼠标移上的显示格式
void setURLGenerator(PieURLGenerator generator) MAP中钻取链接格式
———————————————————————————————————
PiePlot3D(PiePlot)类:
void setDepthFactor(double factor) 3D饼图的Z轴高度(0.0~1.0)
  • 大小: 35.3 KB
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics