`

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的框架以及如何编写代码生成想要的图形。下面例子是在网上找到的,我稍微修改、加了一点注释。

Java代码 复制代码 收藏代码
  1. package com.javachen.jfreechart;   
  2. import java.awt.Color;   
  3. import java.awt.Font;   
  4. import java.io.FileOutputStream;   
  5. import java.text.DecimalFormat;   
  6. import java.text.NumberFormat;   
  7.   
  8. import org.jfree.chart.ChartFactory;   
  9. import org.jfree.chart.ChartUtilities;   
  10. import org.jfree.chart.JFreeChart;   
  11. import org.jfree.chart.labels.StandardPieSectionLabelGenerator;   
  12. import org.jfree.chart.labels.StandardPieToolTipGenerator;   
  13. import org.jfree.chart.plot.PiePlot;   
  14. import org.jfree.chart.plot.PiePlot3D;   
  15. import org.jfree.chart.title.TextTitle;   
  16. import org.jfree.data.general.DefaultPieDataset;   
  17. import org.jfree.data.general.PieDataset;   
  18.   
  19. public class PieChart {   
  20.     public static void main(String[] args) {   
  21.         createPieChart();   
  22.         createPieChart3D();   
  23.     }   
  24.   
  25.     public static void createPieChart3D(){   
  26.         PieDataset dataset = getDataSet();   
  27.         JFreeChart chart = ChartFactory.createPieChart3D(   
  28.                 " 项目进度分布",// 图表标题   
  29.                 dataset,// data   
  30.                 true,// include legend   
  31.                 truetrue);   
  32.         PiePlot3D plot = (PiePlot3D) chart.getPlot();   
  33.         // 图片中显示百分比:默认方式   
  34.         plot.setLabelGenerator(new StandardPieSectionLabelGenerator(   
  35.                 StandardPieToolTipGenerator.DEFAULT_TOOLTIP_FORMAT));   
  36.         // 图片中显示百分比:自定义方式,{0} 表示选项, {1} 表示数值,   
  37.         //{2} 表示所占比例 ,小数点后两位   
  38.         plot.setLabelGenerator(new StandardPieSectionLabelGenerator(   
  39.                 "{0}={1}({2})", NumberFormat.getNumberInstance(),   
  40.                 new DecimalFormat("0.00%")));   
  41.         // 图例显示百分比:自定义方式, {0} 表示选项, {1} 表示数值, {2} 表示所占比例   
  42.         plot.setLegendLabelGenerator(new StandardPieSectionLabelGenerator(   
  43.                         "{0}"));   
  44.         // 指定图片的透明度(0.0-1.0)   
  45.         plot.setForegroundAlpha(1.0f);   
  46.         // 指定显示的饼图上圆形(true)还椭圆形(false)   
  47.         plot.setCircular(true);   
  48.         // 设置图上分类标签label的字体,解决中文乱码   
  49.         plot.setLabelFont(new Font("黑体", Font.PLAIN, 12));   
  50.         // 设置图上分类标签label的最大宽度,相对与plot的百分比   
  51.         plot.setMaximumLabelWidth(0.2);   
  52.         // 设置3D饼图的Z轴高度(0.0~1.0)   
  53.         plot.setDepthFactor(0.07);   
  54.         //设置起始角度,默认值为90,当为0时,起始值在3点钟方向   
  55.         plot.setStartAngle(45);   
  56.   
  57.         // 设置图标题的字体,解决中文乱码   
  58.         TextTitle textTitle = chart.getTitle();   
  59.         textTitle.setFont(new Font("黑体", Font.PLAIN, 20));   
  60.   
  61.         // 设置背景色为白色   
  62.         chart.setBackgroundPaint(Color.white);   
  63.         // 设置Legend部分字体,解决中文乱码   
  64.         chart.getLegend().setItemFont(new Font("宋体", Font.PLAIN, 12));   
  65.   
  66.         // 输出图片到文件   
  67.         FileOutputStream fos_jpg = null;   
  68.         try {   
  69.             fos_jpg = new FileOutputStream("D:\\项目状态分布-3D.jpg");   
  70.             ChartUtilities.writeChartAsJPEG(fos_jpg, 0.8f, chart, 800600,   
  71.                     null);   
  72.             fos_jpg.close();   
  73.         } catch (Exception e) {   
  74.             e.printStackTrace();   
  75.         }   
  76.     }   
  77.   
  78.     public static void createPieChart(){   
  79.         PieDataset dataset = getDataSet();   
  80.         JFreeChart chart = ChartFactory.createPieChart(" 项目进度分布",// 图表标题   
  81.                 dataset,// data   
  82.                 true,// include legend   
  83.                 truetrue);   
  84.         PiePlot plot = (PiePlot) chart.getPlot();   
  85.         // 图片中显示百分比:默认方式   
  86.         plot.setLabelGenerator(new StandardPieSectionLabelGenerator(   
  87.                 StandardPieToolTipGenerator.DEFAULT_TOOLTIP_FORMAT));   
  88.         // 图片中显示百分比:自定义方式,{0} 表示选项, {1} 表示数值,   
  89.         //{2} 表示所占比例 ,小数点后两位   
  90.         plot.setLabelGenerator(new StandardPieSectionLabelGenerator(   
  91.                 "{0}={1}({2})", NumberFormat.getNumberInstance(),   
  92.                 new DecimalFormat("0.00%")));   
  93.         // 图例显示百分比:自定义方式, {0} 表示选项, {1} 表示数值, {2} 表示所占比例   
  94.         plot.setLegendLabelGenerator(new StandardPieSectionLabelGenerator(   
  95.                         "{0}"));   
  96.         // 指定图片的透明度(0.0-1.0)   
  97.         plot.setForegroundAlpha(1.0f);   
  98.         // 指定显示的饼图上圆形(true)还椭圆形(false)   
  99.         plot.setCircular(true);   
  100.         // 设置图上分类标签label的字体,解决中文乱码   
  101.         plot.setLabelFont(new Font("黑体", Font.PLAIN, 12));   
  102.         // 设置图上分类标签label的最大宽度,相对与plot的百分比   
  103.         plot.setMaximumLabelWidth(0.2);   
  104.         //设置起始角度,默认值为90,当为0时,起始值在3点钟方向   
  105.         plot.setStartAngle(45);   
  106.   
  107.         /**  
  108.          * 设置某一块凸出,第一个参数为dataSet的key,此方法只在PiePlot中有效  
  109.          */  
  110.         plot.setExplodePercent(" 运维"0.2);   
  111.   
  112.         // 设置图标题的字体,解决中文乱码   
  113.         TextTitle textTitle = chart.getTitle();   
  114.         textTitle.setFont(new Font("黑体", Font.PLAIN, 20));   
  115.   
  116.         // 设置背景色为白色   
  117.         chart.setBackgroundPaint(Color.white);   
  118.         // 设置Legend部分字体,解决中文乱码   
  119.         chart.getLegend().setItemFont(new Font("宋体", Font.PLAIN, 12));   
  120.   
  121.         // 输出图片到文件   
  122.         FileOutputStream fos_jpg = null;   
  123.         try {   
  124.             fos_jpg = new FileOutputStream("D:\\项目状态分布.jpg");   
  125.             ChartUtilities.writeChartAsJPEG(fos_jpg, 0.8f, chart, 800600,   
  126.                     null);   
  127.             fos_jpg.close();   
  128.         } catch (Exception e) {   
  129.             e.printStackTrace();   
  130.         }   
  131.     }   
  132.   
  133.     private static PieDataset getDataSet() {   
  134.         DefaultPieDataset dataset = new DefaultPieDataset();   
  135.         dataset.setValue(" 市场前期"new Double(10));   
  136.         dataset.setValue(" 立项"new Double(15));   
  137.         dataset.setValue(" 计划"new Double(10));   
  138.         dataset.setValue(" 需求与设计"new Double(10));   
  139.         dataset.setValue(" 执行控制"new Double(35));   
  140.         dataset.setValue(" 收尾"new Double(10));   
  141.         dataset.setValue(" 运维"new Double(10));   
  142.         return dataset;   
  143.     }   
  144. }  
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)
分享到:
评论

相关推荐

    jfreechart-1.5.2.jar,jfreechart|jfreechart

    jfreechart-1.5.2.jar,jfreechart|jfreechart

    Jfreechart

    Jfreechart 乱码分析和解决方法

    JFreeChart双Y轴折线图实例,可以直接运行

    JFreeChart双Y轴折线图实例,可以直接运行,实例类为LineChartDemo1.JAVA,有注释。 若想在web工程使用只需如下。 String filename = ServletUtilities.saveChartAsPNG(jfreechart, 600, 400, null, session); ...

    jfreechart1.0.1 资源插件

    jfreechart-1.0.1.jar junit.jar servlet.jar JFreeChart目前是最好的java图形解决方案,基本能够解决目前的图形方面的需求,主要包括如下几个方面: 饼图, 柱状图 ,线图,点图,时间变化图,甘特图, 股票行情图,混和...

    JFreeChart 折线、柱状组合图

    public JFreeChart createChart(String Ytitle, String title, CategoryDataset lineData, CategoryDataset barData) { //参考附件 return chart; } //struts 部分 <!--begin 维优特例 --> ...

    JFreeChart的JAR包和API文档(包含例子)

    JFreeChart的JAR包和API文档JFreeChart的JAR包和API文档JFreeChart的JAR包和API文档JFreeChart的JAR包和API文档JFreeChart的JAR包和API文档JFreeChart的JAR包和API文档JFreeChart的JAR包和API文档JFreeChart的JAR包...

    JFreeChart使用的jar包

    JFreeChart的使用,需要导入的2个jar文件: jcommon-1.0.23.jar和jfreechart-1.0.19.jar。 可以去官网下载:http://sourceforge.net/projects/jfreechart/files/ 上传以作后续使用时备份.

    jfreechart所需jar包.zip

    jfreechart折线图所需jar包

    JFreeChart使用教程(中文).pdf

    JFreeChart是一组功能强大、灵活易用的Java绘图API,使用它可以生成多种通用性的报表,包括柱状图、饼图、曲线图、甘特图等。它能够 用在Swing和Web等中制作自定义的图表或报表,并且得到广泛的应用。本文将通过引领...

    jfreechart-1.0.9+api

    jfreechart

    jfreechart的一个小总结

    关于jfreechart总结的一个小文档,是转载的,所有各位亲,如果愿意可以看看

    JFreeChart 指南 & JFreeChart 示例

    JFreeChart 指南 JFreeChart Guide JFreeChart 示例 JFreeChart demo

    jfreechart开发jar包

    jfreechart开发jar包,真正要引用到eclipse build path的是jfreechart-1.0.14\lib目录下的gnujaxp.jar、jcommon-1.017.jar和jfreechart-1.0.14.jar三个包,我用的就是这3个jar包。。

    jfreechart 饼形图

    jfreechart百分比在饼上, 有分离的饼。 private static JFreeChart createChart(PieDataset piedataset) { JFreeChart jfreechart = ChartFactory.createPieChart("Pie Chart Demo 2", piedataset, true, true, ...

    jfreechart 折线图 应用

    package: jfreechart-1.0.9.jar jcommon-1.0.12.jar 本人也上传了,可以在本人上传资源中找到 环境: myeclipse 6.5 jdk 1.6 该程序为main 函数,定义了三个方法,分别是jfreechart三种实现折线图的类型,返回的是...

    jfreechart 绘制横向立体柱状图

    利用jfreechart绘制的横向立体柱状图,横坐标是数值,纵坐标是数据分类,需要提前引入jfreechart包文件到工程里,才不会报错

Global site tag (gtag.js) - Google Analytics