`
jimmy.shine
  • 浏览: 389730 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

jFreechart三种图的测试代码

阅读更多

Jfreechart的最新版本,对于中文默认是不支持的,因为默认的font为英文的,所以需要单独设置。以前一直诟病于jfreechart的生成的图形颜色不好看,图片不清晰,现在新版本已经好了很多了,至于图形的颜色,可以自定义。

 

三种常见的图代码。

 

/**
 * copyRight vaalhaai.com
 */
package com.vaalhaai.framework.report.chart;

import java.awt.Color;
import java.awt.Font;
import java.awt.FontFormatException;
import java.io.IOException;
import java.text.NumberFormat;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartFrame;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.CategoryAxis3D;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.axis.NumberAxis3D;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.labels.ItemLabelAnchor;
import org.jfree.chart.labels.ItemLabelPosition;
import org.jfree.chart.labels.StandardCategoryItemLabelGenerator;
import org.jfree.chart.labels.StandardPieSectionLabelGenerator;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PiePlot3D;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.BarRenderer;
import org.jfree.chart.renderer.category.LineAndShapeRenderer;
import org.jfree.chart.title.LegendTitle;
import org.jfree.chart.title.TextTitle;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.data.general.DefaultPieDataset;
import org.jfree.ui.TextAnchor;

/**
 * @author Jimmy.Shine 2010-5-9
 * 
 */
public class ChartTest {

	/**
	 * @param args
	 * @throws IOException
	 * @throws FontFormatException
	 */
	public static void main(String[] args) throws FontFormatException, IOException {
	//	pie3D();
	//	bar3D();
		line();
	}
	
	public static void pie3D(){

		DefaultPieDataset dataset = new DefaultPieDataset();
		dataset.setValue("中国电信", 30);
		dataset.setValue("中国移动", 60);
		dataset.setValue("中国联通", 10);
		dataset.setValue("固定电话", 40);
		dataset.setValue("其它", 20);

		JFreeChart chart = ChartFactory.createPieChart3D("通信分布饼图", dataset, true, true, false);
		TextTitle title = chart.getTitle();
		title.setFont(new Font("黑体", Font.BOLD, 18));
		LegendTitle legendTitle = chart.getLegend();
		legendTitle.setItemFont(new Font("微软雅黑", Font.BOLD, 12));
		PiePlot3D plot = (PiePlot3D) chart.getPlot();
		plot.setLabelFont(new Font("微软雅黑", Font.PLAIN, 12));
		plot.setLabelBackgroundPaint(new Color(255, 255, 255));
		plot.setBackgroundPaint(new Color(255, 255, 255));
		plot.setOutlineVisible(true);
		plot.setBackgroundAlpha(0.75F);
		plot.setForegroundAlpha(0.80F);
		plot.setStartAngle(90);
		//int r = 61, g = 89, b = 171;
		int r = 20, g = 20, b = 20;
		int rStep = 30;
		int gStep = 30;
		int bStep = 30;
		plot.setSectionPaint("中国电信", new Color(r, g, b));
		r = (r + rStep > 255 ? r : r + rStep);
		g = (g + gStep > 255 ? g : g + gStep);
		b = (b + bStep > 255 ? b : b + bStep);
		plot.setSectionPaint("中国移动", new Color(r, g, b));
		r = (r + rStep > 255 ? r : r + rStep);
		g = (g + gStep > 255 ? g : g + gStep);
		b = (b + bStep > 255 ? b : b + bStep);
		plot.setSectionPaint("中国联通", new Color(r, g, b));
		r = (r + rStep > 255 ? r : r + rStep);
		g = (g + gStep > 255 ? g : g + gStep);
		b = (b + bStep > 255 ? b : b + bStep);
		plot.setSectionPaint("固定电话", new Color(r, g, b));
		r = (r + rStep > 255 ? r : r + rStep);
		g = (g + gStep > 255 ? g : g + gStep);
		b = (b + bStep > 255 ? b : b + bStep);
		plot.setSectionPaint("其它", new Color(r, g, b));

		plot.setDarkerSides(true);
		plot.setShadowPaint(new Color(0, 0, 255));

		plot.setLabelGenerator(new StandardPieSectionLabelGenerator("{0}({2})"));
		plot.setLegendLabelGenerator(new StandardPieSectionLabelGenerator("{0}({1}/{3}={2})"));
		// plot.setExplodePercent("中国电信", 0.3F);
		// plot.setExplodePercent("中国移动", 0.3F);
		// plot.setExplodePercent("中国联通", 0.3F);
		// plot.setExplodePercent("固定电话", 0.3F);
		
		ChartFrame frame = new ChartFrame("通信 ", chart);
		frame.pack();
		frame.setVisible(true);
	}
	
	public static void bar3D(){
		DefaultCategoryDataset dataset = new DefaultCategoryDataset();
		dataset.addValue(30, "移动通信", "中国电信");
		dataset.addValue(60, "移动通信", "中国移动");
		dataset.addValue(10, "移动通信", "中国联通");
		dataset.addValue(40, "固定通信", "中国电信");
		dataset.addValue(-10, "固定通信", "中国移动");
		dataset.addValue(20, "固定通信", "中国联通");
		

		JFreeChart chart = ChartFactory.createBarChart3D("通信分布柱状图", "类别", "值", dataset,PlotOrientation.VERTICAL, true, true, false);
		TextTitle title = chart.getTitle();
		title.setFont(new Font("黑体", Font.BOLD, 18));
		LegendTitle legendTitle = chart.getLegend();
		legendTitle.setItemFont(new Font("微软雅黑", Font.BOLD, 12));
		CategoryPlot plot = (CategoryPlot) chart.getPlot();
        plot.setBackgroundPaint(new Color(255, 255, 255));
		plot.setOutlineVisible(true);
		plot.setBackgroundAlpha(0.75F);
		plot.setForegroundAlpha(0.80F);
		plot.setRangeGridlinesVisible(true);
		plot.setRangeGridlinePaint(Color.GRAY);
		
		CategoryAxis3D xAxis = (CategoryAxis3D) plot.getDomainAxis();
		xAxis.setLabelFont(new Font("微软雅黑", Font.PLAIN, 12));
		xAxis.setTickLabelFont(new Font("微软雅黑", Font.PLAIN, 12));
		
		ValueAxis yAxis = plot.getRangeAxis();
		yAxis.setLabelFont(new Font("微软雅黑", Font.PLAIN, 12));
		yAxis.setTickLabelFont(new Font("微软雅黑", Font.PLAIN, 12));
		
		BarRenderer render = (BarRenderer) plot.getRenderer();
		render.setBaseItemLabelFont(new Font("微软雅黑", Font.PLAIN, 12));
		render.setBaseLegendTextFont(new Font("微软雅黑", Font.BOLD, 12));
		render.setMaximumBarWidth(0.1F);
		render.setItemMargin(0.0F);
		render.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator("{3}",NumberFormat.getInstance()));
		render.setBaseItemLabelsVisible(true);
		render.setItemLabelAnchorOffset(8);
		render.setBasePositiveItemLabelPosition(new ItemLabelPosition(ItemLabelAnchor.OUTSIDE12,TextAnchor.BASELINE_LEFT));
		render.setBaseNegativeItemLabelPosition(new ItemLabelPosition(ItemLabelAnchor.OUTSIDE12,TextAnchor.BASELINE_LEFT));
/*
		ValueMarker marker = new ValueMarker(0.70, new Color(200, 200, 255), 
                new BasicStroke(1.0f), new Color(200, 200, 255), 
                new BasicStroke(1.0f), 1.0f);
		plot.addRangeMarker(marker);
*/	
  /*      GradientPaint gp0 = new GradientPaint(
                0.0f, 0.0f, Color.blue, 
                0.0f, 0.0f, new Color(0, 0, 64)
            );
            GradientPaint gp1 = new GradientPaint(
                0.0f, 0.0f, Color.green, 
                0.0f, 0.0f, new Color(0, 64, 0)
            );
            GradientPaint gp2 = new GradientPaint(
                0.0f, 0.0f, Color.red, 
                0.0f, 0.0f, new Color(64, 0, 0)
            );
            render.setSeriesPaint(0, gp0);
            render.setSeriesPaint(1, gp1);
            render.setSeriesPaint(2, gp2);
*/
		int r = 20, g = 20, b = 20;
		int rStep = 120;
		int gStep = 120;
		int bStep = 120;
		render.setSeriesPaint(0, new Color(r,g,b));
		r = (r + rStep > 255 ? r : r + rStep);
		g = (g + gStep > 255 ? g : g + gStep);
		b = (b + bStep > 255 ? b : b + bStep);		
		render.setSeriesPaint(1, new Color(r,g,b));


		
		ChartFrame frame = new ChartFrame("通信 ", chart);
		frame.pack();
		frame.setVisible(true);
	}

	public static void line(){
		DefaultCategoryDataset dataset = new DefaultCategoryDataset();
		dataset.addValue(30, "通信", "中国电信");
		dataset.addValue(60, "通信", "中国移动");
		dataset.addValue(10, "通信", "中国联通");
		dataset.addValue(40, "通信", "中国电信固话");
		dataset.addValue(10, "通信", "其它固话");
		dataset.addValue(20, "通信", "中国联通固话");

		dataset.addValue(30, "3G", "中国电信");
		dataset.addValue(50, "3G", "中国移动");
		dataset.addValue(20, "3G", "中国联通");
		dataset.addValue(50, "3G", "中国电信固话");
		dataset.addValue(30, "3G", "其它固话");
		dataset.addValue(20, "3G", "中国联通固话");

		JFreeChart chart = ChartFactory.createLineChart("通信分布线图", "类别", "值", dataset,PlotOrientation.VERTICAL, true, true, false);
		TextTitle title = chart.getTitle();
		title.setFont(new Font("黑体", Font.BOLD, 18));
		LegendTitle legendTitle = chart.getLegend();
		legendTitle.setItemFont(new Font("微软雅黑", Font.BOLD, 12));

		CategoryPlot plot = (CategoryPlot) chart.getPlot();
        plot.setBackgroundPaint(new Color(255, 255, 255));
		plot.setOutlineVisible(true);
		plot.setBackgroundAlpha(0.75F);
		plot.setForegroundAlpha(0.80F);
		plot.setRangeGridlinesVisible(true);
		plot.setRangeGridlinePaint(Color.GRAY);
		
		CategoryAxis xAxis = (CategoryAxis) plot.getDomainAxis();
		xAxis.setLabelFont(new Font("微软雅黑", Font.PLAIN, 12));
		xAxis.setTickLabelFont(new Font("微软雅黑", Font.PLAIN, 12));
		
		NumberAxis yAxis = (NumberAxis)plot.getRangeAxis();
		yAxis.setLabelFont(new Font("微软雅黑", Font.PLAIN, 12));
		yAxis.setTickLabelFont(new Font("微软雅黑", Font.PLAIN, 12));
		yAxis.setStandardTickUnits(NumberAxis3D.createIntegerTickUnits());
		
		LineAndShapeRenderer render = (LineAndShapeRenderer) plot.getRenderer();
		render.setBaseItemLabelFont(new Font("微软雅黑", Font.PLAIN, 12));
		render.setBaseLegendTextFont(new Font("微软雅黑", Font.BOLD, 12));
		render.setItemMargin(0.0F);
		render.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator("{2}",NumberFormat.getInstance()));
		render.setBaseItemLabelsVisible(true);
		render.setBaseLinesVisible(true);
		render.setBaseSeriesVisibleInLegend(true);
		render.setBaseSeriesVisible(true);
		render.setBaseShapesVisible(true);
		render.setItemLabelAnchorOffset(2);
		render.setBasePositiveItemLabelPosition(new ItemLabelPosition(ItemLabelAnchor.OUTSIDE2,TextAnchor.BASELINE_LEFT));
		render.setBaseNegativeItemLabelPosition(new ItemLabelPosition(ItemLabelAnchor.OUTSIDE2,TextAnchor.BASELINE_LEFT));
		
		int r = 61, g = 89, b = 171;
		int rStep = 120;
		int gStep = 120;
		int bStep = 120;
		render.setSeriesPaint(0, new Color(r,g,b));
		r = (r + rStep > 255 ? r : r + rStep);
		g = (g + gStep > 255 ? g : g + gStep);
		b = (b + bStep > 255 ? b : b + bStep);		
		render.setSeriesPaint(1, new Color(r,g,b));		
		
		ChartFrame frame = new ChartFrame("通信 ", chart);
		frame.pack();
		frame.setVisible(true);
	}
}

 效果见附件

  • 大小: 27.7 KB
  • 大小: 15.6 KB
  • 大小: 25 KB
分享到:
评论

相关推荐

    JFreeChart生成柱形图完整测试代码

    JFreeChart生成柱形图完整测试代码 放在普通项目里运行main方法就能生成柱形图 不需要添加任何代码 可根据自己需要修改参数

    JFreeChart 测试代码

    JFreeChart 的测试代码,各种报表演示

    jfreechart源代码【附详细中文注释、注释到每个变量和方法】

    jfreechart源代码【附详细中文注释、注释到每个变量和方法】 用起来很方便,个人觉得不错

    jfreechart-1.0.19-Demo-Source 源代码

    jfreechart-1.0.19-Demo-Source 源代码,亲自整理测试,绝对可用

    jfreechart绘制体温单demo

    代码中包绘制总体的工具类TwdChartUtil,及自定义的浮点型类型的线条、坐标点的基类,以及绘制过程在需要的一些工具类,使用时可以执行复制到自己的代码中。主类中包含测试数据,可以直接运行查看显示效果,需要显示...

    JAVA上百实例源码以及开源项目源代码

    util实现Java图片水印添加功能,有添加图片水印和文字水印,可以设置水印位置,透明度、设置对线段锯齿状边缘处理、水印图片的路径,水印一般格式是gif,png,这种图片可以设置透明度、水印旋转等,可以参考代码加以...

    Java Web入门经典第二章源代码

    第三篇为“高级应用篇”,主要包括在互联网中操作文件资源、利用JFreeChart实现动态图表、在JSP中操作XML、完美体验Ajax技术、MVC架构实现者——Struts 2框架、数据库持久化利器——Hibernate技术、简化企业开发瑰宝...

    JAVA上百实例源码以及开源项目

    util实现Java图片水印添加功能,有添加图片水印和文字水印,可以设置水印位置,透明度、设置对线段锯齿状边缘处理、水印图片的路径,水印一般格式是gif,png,这种图片可以设置透明度、水印旋转等,可以参考代码加以...

    seng637-assignment-2-patrickjykwan

    该实验室本身分为三个部分:熟悉,单元测试生成和测试代码开发。 每个部分的内容如下所示: 熟悉 在本部分中,学生将使用Eclipse创建一个JUnit测试项目,并熟悉JUnit和JavaDoc等测试工具。 JUnit是一个流行的Java...

    JSP开发技术大全 源码

    软件工程师典藏•JSP开发技术大全》是一本JSP综合开发参考手册,书中几乎囊括了使用JSP...光盘提供了书中示例和典型应用实例的全部源代码,所有源代码都经过精心调试,在Windows 2003下测试通过,保证能够正常运行。

    FC-USB-Tester-Data-Logger-App:Java桌面应用程序可绘制图形并保存从USB测试仪记录的数据

    Java桌面应用程序可以绘制图形并保存从USB测试仪记录的数据。 USBTester.jar需要4个库 jcommon- //www.jfree.org/jcommon/ jfreechart- //www.jfree.org/jfreechart/ RXTXcomm- //rxtx.qbang.org/wiki/index....

    淘特大型门户网站CMS企业版

    2、支持多浏览器,所有代码均在多种浏览器通过测试 3、可视化文本在线编辑,文章添加可保存远程图片. 4、文章、栏目列表、栏目首页全部生成静态 5、支持自定义标签管理,可以灵活定制多种显示效果。 6、文章、栏目...

    基于Java的学生成绩管理系统,Spring+Jquery UI+Ajax,优秀毕业设计源代码+答辩PPT,新手必看!

    JFreeChart——成绩分析 iText——成绩打印PDF格式 Ajax+Json——表单验证 系统登录用户: 学生用户(学号+密码+验证码) 教师用户(教师编号+密码+验证码) 管理员用户(姓名+密码+验证码) 学生登录用户: 成绩...

    Ztree动态绑定和报表

    动态异步绑定Ztree,用的mysql数据库,jdbc访问。数据库中新建表dept加入id,pid,...该项目中还包括用jFreeChart生成统计报表的一些代码。项目中包含完整jar包,只需根据自己需要配置数据库和自己添加测试数据就能用。

    java开源包8

    Spring4GWT GWT Spring 使得在 Spring 框架下构造 GWT 应用变得很简单,提供一个易于理解...可以将网络图导出为 GIF, JPEG, PNG, PPM, ARP and PNML (XML based)文件格式。使用了优秀的JHotDraw 5.2 框架。 activemq...

    java开源包10

    Spring4GWT GWT Spring 使得在 Spring 框架下构造 GWT 应用变得很简单,提供一个易于理解...可以将网络图导出为 GIF, JPEG, PNG, PPM, ARP and PNML (XML based)文件格式。使用了优秀的JHotDraw 5.2 框架。 activemq...

    JSP开发技术大全 JSP

    光盘提供了书中示例和典型应用实例的全部源代码,所有源代码都经过精心调试,在Windows 2003下测试通过,保证能够正常运行。 本书内容精练、重点突出、实例丰富,是各级程序开发人员必备的参考书,也非常适合大中专...

    jsp 开发技术大全

    光盘提供了书中示例和典型应用实例的全部源代码,所有源代码都经过精心调试,在Windows 2003下测试通过,保证能够正常运行。  本书内容精练、重点突出、实例丰富,是各级程序开发人员必备的参考书,也非常适合大...

    基于jbpm与activiti的工作流平台技术架构介绍

    系统采用多层的系统架构进行功能开发,有利于代码功能责任分开,同时有利于不同开发人员的分工及合作,也有利于代码的单元测试。系统总体结构如下图所示: 【图二】BPMX3多层架构 •数据访问层Dao: 负责与数据库...

Global site tag (gtag.js) - Google Analytics