读书人

jfreechar 图生验方法

发布时间: 2012-11-13 10:00:51 作者: rapoo

jfreechar 图生成方法

package com.giyo.util;

import java.awt.Color;
import java.awt.Font;
import java.awt.GradientPaint;
import java.io.File;
import java.io.FileOutputStream;

import javax.servlet.http.HttpServletRequest;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.CategoryAxis;

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.plot.CategoryPlot;
import org.jfree.chart.plot.PiePlot;
import org.jfree.chart.plot.PiePlot3D;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.BarRenderer;
import org.jfree.chart.title.TextTitle;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.general.PieDataset;
import org.jfree.ui.TextAnchor;


/**
?* <span>自动生成图表</span>
?* <span>现导出为图片</span>
?* @author?Zs
?* @version 1.0
?*/
public class Chart {?
?
?/**
? * <span>生成3D直方图</span>
? * 垂直的
? * @param title
? * ???图表名称
? * @param xtitle
? * ???X轴名称
? * @param ytitle
? * ???Y轴名称
? * @param isVisible
? * ???是否显示图例(true-显示)
? * ???简单柱状图必须为false
? * @param width
? * ???图片宽度
? * @param height
? * ???图片高度?
? * @param imgType
? * ???输出图片的类型(现支持jpg,gif)?
? *@param request

? *????HttpServletRequest?可以去掉?

? */
?public static String vBar3DChart(HttpServletRequest request, String title,String xtitle,String ytitle,boolean isVisible,CategoryDataset data,int width,int height,String imgType){??
??
??JFreeChart chart = ChartFactory.createBarChart3D(title,
????xtitle,
????ytitle,
????data,
????PlotOrientation.VERTICAL,
????isVisible,
????false,
????false
????);
??
??chart.setBackgroundPaint(Color.gray);
??chart.setBackgroundPaint(Color.WHITE);
??chart.setBorderVisible(false);
??chart.setBackgroundImageAlpha(1.0f);
??Font font = new Font("黑体",Font.CENTER_BASELINE,20);
??TextTitle text = new TextTitle(title);
??text.setFont(font);
??chart.setTitle(text);
??
??CategoryPlot xyplot = chart.getCategoryPlot();
??CategoryAxis xaxis = xyplot.getDomainAxis();
??xaxis.setTickLabelFont(new Font("Serif",Font.CENTER_BASELINE,13));
??xaxis.setLabelFont(new Font("Serif",Font.CENTER_BASELINE,20));
??
??ValueAxis yaxis = xyplot.getRangeAxis();
??yaxis.setTickLabelFont(new Font("Serif",Font.CENTER_BASELINE,13));
??yaxis.setLabelFont(new Font("Serif",Font.CENTER_BASELINE,15));
???????? //设置bar显示value 如果加入则自动变为2D图

?? CategoryPlot plot=chart.getCategoryPlot();
?? BarRenderer renderer1=new BarRenderer();
?? renderer1.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator());
???renderer1.setBaseItemLabelsVisible(true);
???temLabelPosition itemlabelposition = new ItemLabelPosition(ItemLabelAnchor.OUTSIDE12,TextAnchor.BASELINE_CENTER,TextAnchor.CENTER,0.0D);
?????? renderer1.setBasePositiveItemLabelPosition(itemlabelposition);
??????
??????
??????? //让上面对柱子的设置生效
??????? plot.setRenderer(renderer1);
????????
??String filePath = request.getSession().getServletContext().getRealPath("\\");
??String?? temp="word\\";??
??????? String?? path=filePath+temp;??

//????????? System.getProperty("user.dir");
??String imgName = ExportImage(chart,width,height,path,imgType);
??
??if(new File(path+File.separator+imgName).exists()){
???return path+File.separator+imgName;
??}else{
???return null;
??}
??
?}
?/**
? * <span>生成3D直方图</span>
? * 水平的
? * @param title
? * ???图表名称
? * @param xtitle
? * ???X轴名称
? * @param ytitle
? * ???Y轴名称
? * @param isVisible
? * ???是否显示图例(true-显示)
? * ???简单柱状图必须为false
? * @param width
? * ???图片宽度
? * @param height
? * ???图片高度?
? * @param imgType
? * ???输出图片的类型(现支持jpg,gif)

? *@param request

? *????HttpServletRequest?可以去掉
? */
?public static String hBar3DChart(HttpServletRequest request, String title,String xtitle,String ytitle,boolean isVisible,CategoryDataset data,int width,int height,String imgType){??
??JFreeChart chart = ChartFactory.createBarChart3D(title,
????xtitle,
????ytitle,
????data,
????PlotOrientation.HORIZONTAL,
????isVisible,
????false,
????false
????);
??chart.setBackgroundPaint(Color.WHITE);
??chart.setBackgroundPaint(Color.WHITE);
??chart.setBorderVisible(false);
??chart.setBackgroundImageAlpha(1.0f);
??Font font = new Font("黑体",Font.CENTER_BASELINE,20);
??TextTitle text = new TextTitle(title);
??text.setFont(font);
??chart.setTitle(text);
??CategoryPlot xyplot = chart.getCategoryPlot();
??CategoryAxis xaxis = xyplot.getDomainAxis();
??xaxis.setTickLabelFont(new Font("Serif",Font.CENTER_BASELINE,13));
??xaxis.setLabelFont(new Font("Serif",Font.CENTER_BASELINE,18));
??ValueAxis yaxis = xyplot.getRangeAxis();
??yaxis.setTickLabelFont(new Font("Serif",Font.CENTER_BASELINE,13));
??yaxis.setLabelFont(new Font("Serif",Font.CENTER_BASELINE,18));
??
??//设置bar显示value 如果生成变为2D图

????? CategoryPlot plot=chart.getCategoryPlot();
?????? BarRenderer renderer1=new BarRenderer();
?????? renderer1.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator());
?????? renderer1.setBaseItemLabelsVisible(true);
?????? ItemLabelPosition itemlabelposition = new ItemLabelPosition(ItemLabelAnchor.OUTSIDE3, TextAnchor.CENTER_LEFT, TextAnchor.BASELINE_RIGHT, 0.0D);
?????? renderer1.setBasePositiveItemLabelPosition(itemlabelposition);
?????? ItemLabelPosition itemlabelposition1 = new ItemLabelPosition(ItemLabelAnchor.INSIDE9, TextAnchor.CENTER_LEFT, TextAnchor.CENTER, 5D);
?????? renderer1.setPositiveItemLabelPositionFallback(itemlabelposition1);

??????
??????? //让上面对柱子的设置生效
??????? plot.setRenderer(renderer1);
???????? String filePath = request.getSession().getServletContext().getRealPath("\\");
??String?? temp="word\\";??
??????? String?? path=filePath+temp;??

//????????? System.getProperty("user.dir");
??String imgName = ExportImage(chart,width,height,path,imgType);
??
??if(new File(path+File.separator+imgName).exists()){
???return path+File.separator+imgName;
??}else{
???return null;
??}
??
?}
?
?/**
? * <span>生成2D直方图</span>
? *
? * @param title
? * ???图表名称
? * @param xtitle
? * ???X轴名称
? * @param ytitle
? * ???Y轴名称
? * @param isVisible
? * ???是否显示图例(true-显示)
? * ???简单柱状图必须为false
? * @param width
? * ???图片宽度
? * @param height
? * ???图片高度
? * @param imgType
? * ???输出图片的类型(现支持jpg,gif)?
? */
?
?public static String vBar2DChart(String title,String xtitle,String ytitle,boolean isVisible,CategoryDataset data,int width,int height,String imgType){??
??JFreeChart chart = ChartFactory.createBarChart(title,
????xtitle,
????ytitle,
????data,
????PlotOrientation.VERTICAL,
????isVisible,
????false,
????false
????);
??chart.setBackgroundPaint(Color.WHITE);
??chart.setBackgroundPaint(Color.WHITE);
??chart.setBorderVisible(false);
??chart.setBackgroundImageAlpha(1.0f);
??Font font = new Font("黑体",Font.CENTER_BASELINE,20);
??TextTitle text = new TextTitle(title);
??text.setFont(font);
??chart.setTitle(text);
??CategoryPlot xyplot = chart.getCategoryPlot();
??CategoryAxis xaxis = xyplot.getDomainAxis();
??xaxis.setTickLabelFont(new Font("Serif",Font.CENTER_BASELINE,13));
??xaxis.setLabelFont(new Font("Serif",Font.CENTER_BASELINE,18));
??ValueAxis yaxis = xyplot.getRangeAxis();
??yaxis.setTickLabelFont(new Font("Serif",Font.CENTER_BASELINE,13));
??yaxis.setLabelFont(new Font("Serif",Font.CENTER_BASELINE,18));
??String filePath = System.getProperty("user.dir");
??String imgName = ExportImage(chart,width,height,filePath,imgType);
??
??if(new File(filePath+File.separator+imgName).exists()){
???return filePath+File.separator+imgName;
??}else{
???return null;
??}
??
?}
?/**
? * <span>生成2D直方图</span>
? *
? * @param title
? * ???图表名称
? * @param xtitle
? * ???X轴名称
? * @param ytitle
? * ???Y轴名称
? * @param isVisible
? * ???是否显示图例(true-显示)
? * ???简单柱状图必须为false
? * @param width
? * ???图片宽度
? * @param height
? * ???图片高度
? * @param imgType
? * ???输出图片的类型(现支持jpg,gif)?
? */
?
?public static String vBar2DChart(HttpServletRequest request,String title,String xtitle,String ytitle,boolean isVisible,CategoryDataset data,int width,int height,String imgType){??
??JFreeChart chart = ChartFactory.createBarChart(title,
????xtitle,
????ytitle,
????data,
????PlotOrientation.VERTICAL,
????isVisible,
????false,
????true
????);
??chart.setBackgroundPaint(Color.WHITE);
??chart.setBackgroundPaint(Color.WHITE);
??chart.setBorderVisible(false);
??chart.setBackgroundImageAlpha(1.0f);
??Font font = new Font("黑体",Font.CENTER_BASELINE,20);
??TextTitle text = new TextTitle(title);
??text.setFont(font);
??chart.setTitle(text);
??CategoryPlot xyplot = chart.getCategoryPlot();
??CategoryAxis xaxis = xyplot.getDomainAxis();
??xaxis.setTickLabelFont(new Font("Serif",Font.CENTER_BASELINE,13));
??xaxis.setLabelFont(new Font("Serif",Font.CENTER_BASELINE,18));
??ValueAxis yaxis = xyplot.getRangeAxis();
??yaxis.setTickLabelFont(new Font("Serif",Font.CENTER_BASELINE,13));
??yaxis.setLabelFont(new Font("Serif",Font.CENTER_BASELINE,18));
??String filePath = System.getProperty("user.dir");
??String imgName = ExportImage(chart,width,height,filePath,imgType);
??
??if(new File(filePath+File.separator+imgName).exists()){
???return filePath+File.separator+imgName;
??}else{
???return null;
??}
??
?}
?
?/**
? * <span>生成3D饼图</span>
? *
? * @param title
? * ???图表名称
? * @param isVisible
? * ???是否显示图例(true-显示)
? * @param width
? * ???图片宽度
? * @param height
? * ???图片高度
? * @param imgType
? * ???输出图片的类型(现支持jpg,gif)?
? */
?
?public static String pie3DChart(String title,boolean isVisible,PieDataset data,int width,int height,String imgType){??
??JFreeChart chart = ChartFactory.createPieChart3D(title,
????data,
????isVisible,
????false,
????false
????);
??PiePlot3D plot = (PiePlot3D) chart.getPlot();
??plot.setCircular(false);
??plot.setForegroundAlpha(1.0f);
??plot.setLabelFont(new Font("Serif",Font.CENTER_BASELINE,13));
??
??chart.setBackgroundPaint(Color.WHITE);
??chart.setBackgroundPaint(Color.WHITE);
??chart.setBorderVisible(false);
??chart.setBackgroundImageAlpha(1.0f);??
??Font font = new Font("黑体",Font.CENTER_BASELINE,20);
??TextTitle text = new TextTitle(title);
??text.setFont(font);
??chart.setTitle(text);
??
??String filePath = System.getProperty("user.dir");
??String imgName = ExportImage(chart,width,height,filePath,imgType);
??
??if(new File(filePath+File.separator+imgName).exists()){
???return filePath+File.separator+imgName;
??}else{
???return null;
??}
??
?}
?
?/**
? * <span>生成2D饼图</span>
? *
? * @param title
? * ???图表名称
? * @param isVisible
? * ???是否显示图例(true-显示)
? * @param width
? * ???图片宽度
? * @param height
? * ???图片高度
? * @param imgType
? * ???输出图片的类型(现支持jpg,gif)
? */
?
?
?
?public static String pie2DChart(String title,boolean isVisible,PieDataset data,int width,int height,String imgType){??
??JFreeChart chart = ChartFactory.createPieChart(title,
????data,
????isVisible,
????false,
????false
????);
??PiePlot plot = (PiePlot) chart.getPlot();
??plot.setCircular(false);
??plot.setForegroundAlpha(1.0f);
??plot.setLabelFont(new Font("Serif",Font.CENTER_BASELINE,14));
??chart.setBackgroundPaint(Color.WHITE);
??chart.setBackgroundPaint(Color.WHITE);
??chart.setBorderVisible(false);
??chart.setBackgroundImageAlpha(1.0f);??
??Font font = new Font("黑体",Font.CENTER_BASELINE,20);
??TextTitle text = new TextTitle(title);
??text.setFont(font);
??chart.setTitle(text);
??String filePath = System.getProperty("user.dir");
??String imgName = ExportImage(chart,width,height,filePath,imgType);
??
??if(new File(filePath+File.separator+imgName).exists()){
???return filePath+File.separator+imgName;
??}else{
???return null;
??}
??
?}
?
?/**
? * <span>生成3D条形图</span>
? *
? * @param title
? * ???图表名称
? * @param xtitle
? * ???X轴名称
? * @param ytitle
? * ???Y轴名称
? * @param isVisible
? * ???是否显示图例(true-显示)
? * ???简单柱状图必须为false
? * @param width
? * ???图片宽度
? * @param height
? * ???图片高度?
? * @param imgType
? * ???输出图片的类型(现支持jpg,gif)
? */
?
?public static String hBar3DChart(String title,String xtitle,String ytitle,boolean isVisible,CategoryDataset data,int width,int height,String imgType){??
??JFreeChart chart = ChartFactory.createBarChart3D(title,
????xtitle,
????ytitle,
????data,
????PlotOrientation.HORIZONTAL,
????isVisible,
????false,
????false
????);
??chart.setBackgroundPaint(Color.WHITE);
??chart.setBackgroundPaint(Color.WHITE);
??chart.setBorderVisible(false);
??chart.setBackgroundImageAlpha(1.0f);
??Font font = new Font("黑体",Font.CENTER_BASELINE,20);
??TextTitle text = new TextTitle(title);
??text.setFont(font);
??chart.setTitle(text);
??CategoryPlot xyplot = chart.getCategoryPlot();
??CategoryAxis xaxis = xyplot.getDomainAxis();
??xaxis.setTickLabelFont(new Font("Serif",Font.CENTER_BASELINE,13));
??xaxis.setLabelFont(new Font("Serif",Font.CENTER_BASELINE,18));
??ValueAxis yaxis = xyplot.getRangeAxis();
??yaxis.setTickLabelFont(new Font("Serif",Font.CENTER_BASELINE,13));
??yaxis.setLabelFont(new Font("Serif",Font.CENTER_BASELINE,18));
??String filePath = System.getProperty("user.dir");
??String imgName = ExportImage(chart,width,height,filePath,imgType);
??
??if(new File(filePath+File.separator+imgName).exists()){
???return filePath+File.separator+imgName;
??}else{
???return null;
??}
??
?}
?
?/**
? * <span>生成2D条形图</span>
? *
? * @param title
? * ???图表名称
? * @param xtitle
? * ???X轴名称
? * @param ytitle
? * ???Y轴名称
? * @param isVisible
? * ???是否显示图例(true-显示)
? * ???简单柱状图必须为false
? * @param width
? * ???图片宽度
? * @param height
? * ???图片高度
? * @param imgType
? * ???输出图片的类型(现支持jpg,gif)?
? */
?
?public static String hBar2DChart(String title,String xtitle,String ytitle,boolean isVisible,CategoryDataset data,int width,int height,String imgType){??
??JFreeChart chart = ChartFactory.createBarChart(title,
????xtitle,
????ytitle,
????data,
????PlotOrientation.HORIZONTAL,
????isVisible,
????false,
????false
????);
??chart.setBackgroundPaint(Color.WHITE);
??chart.setBackgroundPaint(Color.WHITE);
??chart.setBorderVisible(false);
??chart.setBackgroundImageAlpha(1.0f);
??Font font = new Font("黑体",Font.CENTER_BASELINE,20);
??TextTitle text = new TextTitle(title);
??text.setFont(font);
??chart.setTitle(text);
??CategoryPlot xyplot = chart.getCategoryPlot();
??CategoryAxis xaxis = xyplot.getDomainAxis();
??xaxis.setTickLabelFont(new Font("Serif",Font.CENTER_BASELINE,13));
??xaxis.setLabelFont(new Font("Serif",Font.CENTER_BASELINE,18));
??ValueAxis yaxis = xyplot.getRangeAxis();
??yaxis.setTickLabelFont(new Font("Serif",Font.CENTER_BASELINE,13));
??yaxis.setLabelFont(new Font("Serif",Font.CENTER_BASELINE,18));
??String filePath = System.getProperty("user.dir");
??String imgName = ExportImage(chart,width,height,filePath,imgType);
??
??if(new File(filePath+File.separator+imgName).exists()){
???return filePath+File.separator+imgName;
??}else{
???return null;
??}
??
?}
?
?/**
? * <span>输出图片</span>
? *
? * @param chart
? * ???输入的图表
? * @param width
? * ???输出的图片的宽度
? * @param height
? * ???输出的图片的高度
? * @param filePath
? * ???输出的图片的绝对路径
? * @param imgType
? * ???输出图片的类型(现支持jpg,gif)
? * @return
? */
?
?public static String ExportImage(JFreeChart chart,int width,int height,String filePath,String imgType){
??FileOutputStream fosImg = null;
??String imgName = new Long(System.currentTimeMillis()).toString();
??try{???
???fosImg = new FileOutputStream(filePath+File.separator+imgName+"."+imgType);???
???ChartUtilities.writeChartAsJPEG(fosImg,1.0f,chart,width,height,null);
??}catch(Exception e){
???try{
????fosImg.close();
???}catch(Exception e1){}
???e.printStackTrace();
??}finally{
???try{
????fosImg.close();
???}catch(Exception e){}
???
??}
??return imgName+"."+imgType;
?}
}

------------------------------------------------------解释-------------------------------------------------------

JFreeChart目前是最好的java图形解决方案,基本能够解决目前的图形方面的需求,主要包括如下几个方面:
????pie?charts?(2D?and?3D):饼图(平面和立体)
????bar?charts?(regular?and?stacked,?with?an?optional?3D?effect):柱状图
????line?and?area?charts:曲线图
????scatter?plots?and?bubble?charts
????time?series,?high/low/open/close?charts?and?candle?stick?charts:时序图
????combination?charts:复合图
????Pareto?charts
????Gantt?charts:甘特图
????wind?plots,?meter?charts?and?symbol?charts
????wafer?map?charts
?????? ??(态图表,饼图(二维和三维)?,?柱状图?(水平,垂直),线图,点图,时间变化图,甘特图,?股票行情图,混和图,?温度计图,?刻度图等常用商用图表)
??????? ? 图形可以导出成PNG和JPEG格式,同时还可以与PDF和EXCEL关联

????????? 研究jfreechart源码发现源码的主要由两个大的包组成: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:用于生成图象的帮助提示,不同类型图表对应不同类型的工具提示类

?

1?柱状图?

????????? org.jfree.chart.ChartFactory这个工厂类有createBarChart、createStackedBarChart、createBarChart3D、createStackedBarChart3D,这几个工厂方法创建不同类型的柱状图,比较重要的是?PlotOrientation.VERTICAL?让平行柱垂直显示,而?PlotOrientation.HORIZONTAL?则让平行柱水平显示。对柱状图影响较大的几个类包括:org.jfree.chart.axis.CategoryAxis、org.jfree.chart.axis.ValueAxis、org.jfree.chart.renderer.BarRenderer、org.jfree.chart.renderer.?BarRenderer3D。?

具体实现步骤:?

1)创建用于图形生成所要的数据集对象。?

CategoryDataset?dataset?=?DatasetUtilities.createCategoryDataset(rowKeys,?columnKeys,?data);?

其中:rowKeys表示X轴数据,columnKeys表示Y轴数据,data表示填充柱状图所要的实际数据(来自于数据库)。?

2)创建图形对象。?

JFreeChart?chart?=?ChartFactory.createBarChart3D("标题",?null,null,dataset,PlotOrientation.VERTICAL,,true,false,false);?

createBarChart3D方法是ChartFactory工厂类里的一个方法,用于3D柱状图的生成,该类继承自JFreeChart。其中的八个参数分别代表:图形的标题、X轴标题、Y轴标题、dataset就是CategoryDataset类的实例对象、显示标题、启用热键、启用超键接。?

3)设置图形显示的属性。?

a?)?ValueAxis类,设置柱到图上下边的距离。实现方法是:?

ValueAxis?rangeAxis?=?plot.getRangeAxis();?

设置最高的一个柱与图片顶端的距离:?

rangeAxis.setUpperMargin(0.15)?

设置最低的一个柱与图片底端的距离:?

rangeAxis.setLowerMargin(0.15)?

b)org.jfree.chart.renderer.BarRenderer3D类,设置图形上显示的数值。实现方法如下:?

BarRenderer3D?renderer?=?new?BarRenderer3D();?

renderer.setBaseOutlinePaint(Color.BLACK);?

设置?Wall?的颜色:?

renderer.setWallPaint(Color.gray);?

设置每个柱的颜色:?

renderer.setSeriesPaint(0,?new?Color(0,?0,?255));?

renderer.setSeriesPaint(1,?new?Color(0,?100,?255));?

renderer.setSeriesPaint(2,?Color.GREEN);?

设置每个柱的?Outline?颜色
renderer.setSeriesOutlinePaint(0,?Color.BLACK);?

renderer.setSeriesOutlinePaint(1,?Color.BLACK);?

renderer.setSeriesOutlinePaint(2,?Color.BLACK);?

设置每个地区所包含的平行柱之间的距离
renderer.setItemMargin(0.1);?

显示每个柱的数值,并修改该数值的字体属性?

renderer.setItemLabelGenerator(new?StandardCategoryItemLabelGenerator());?

renderer.setItemLabelFont(new?Font("黑体",Font.PLAIN,12));?

renderer.setItemLabelsVisible(true);?

为图形加入超连接?

renderer.setItemURLGenerator(new?StandardCategoryURLGenerator());?

renderer.setToolTipGenerator(new?StandardCategoryToolTipGenerator());?

2?饼图?

org.jfree.chart.plot包,包含创建饼形图的所有方法和属性。?

笔者根据业务需求创建了setURLGenerator(PieURLGenerator?generator)方法,在图片上建立连接,就是图片不同部分连接不同的资源。?

setSectionLabelType(int?type)方法:?

指定?section?标签的类型,共有?7?种类型。如果不指定,默认是?NAME_LABELS,其中类型分别是:?

PiePlot.NO_LABELS?

PiePlot.NAME_LABELS?

PiePlot.VALUE_LABELS?

PiePlot.PERCENT_LABELS?、PiePlot.NAME_AND_VALUE_LABELS、PiePlot.?NAME_AND_PERCENT_LABELS、PiePlot.VALUE_AND_PERCENT_LABELS。?

setDefaultOutlinePaint(java.awt.Paint?paint)方法,指定?section?轮廓线的颜色,如果不指定,默认值为NULL。?

setDefaultOutlineStroke(java.awt.Stroke?stroke)方法,指定?section?轮廓线的厚度。?

setRadius(double?percent)?和?setExplodePercent(int?section,?double?percent)方法,抽离?section,就是把某一section从饼形图剥离出来,需要两个方法一起使用。?

setStartAngle(double?angle)方法,设置第一个section开始位置,默认从12点钟方向开始。?

setPaint(int?section,?java.awt.Paint?paint)方法指定section的颜色。?

setDirection(int?direction)方法指定section顺序,默认是顺时针方向。顺时针:PiePlot.CLOCKWISE;逆时针:PiePlot.ANTICLOCKWISE。?

具体实现步骤:?

1)创建用于图形生成所要的数据集对象。?

首先实例化类DefaultPieDataset?dataset?=?new?DefaultPieDataset()。然后利用DefaultPieDataset类提供的setValue(value1,value2)方法,把从数据库里提取的数据存入DefaultPieDataset对象。其中value1是数据名称、value2是数据值。?

2)创建图形对象。?

首先实例化JFreeChart?chart?=?ChartFactory.createPieChart3D(title,?dataset,?true,?true,?false)createPieChart3D方法是用于饼图生成的主要方法。其中title代表图形的标题、dataset就是DefaultPieDataset对象的实例。?

3)设置图形显示的属性。?

String?filename?=?ServletUtilities.saveChartAsPNG(jFreeChart,?700,?450,?info,?session);?

ChartUtilities.writeImageMap(pw,?filename,?info);?

pw.flush()?

saveChartAsPNG方法在ServletUtilities工厂类定义完成。主要用于把图形对象JFreeChart以图片的形式保存。其中的jFreeChart就是JFreeChart对象的实例。该方法返回一个文件名。?

writeImageMap(pw,?filename,?info)方法用于把保存的图片文件以字节流的形式写入用户界面。?

其中pw是java.io包的PrintWriter类的实例对象,该对象创建一个图形输出流。Filename是输出图片的文件名。该文件名来自ServletUtilities.saveChartAsPNG方法创建。?

参数info用于图形信息的显示。?

用ChartRenderingInfo?info=new?ChartRenderingInfo(new?StandardEntityCollection())创建。?

最后输出完成图形,调用pw.flush()方法关闭IO流。

读书人网 >软件架构设计

热点推荐