jfreechart学习笔记

package com.yong;

import java.awt.BasicStroke;
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.plot.PiePlot;
import org.jfree.chart.title.TextTitle;
import org.jfree.data.general.DefaultPieDataset;

/**
 * 
 * @author 赵永恩
 *
 */
public class Pie {
	public static void main(String[] args) {
		//数据
		DefaultPieDataset dataset = new DefaultPieDataset();
		dataset.setValue("中文", 43.2);
		dataset.setValue("Category 2", 27.9);
		dataset.setValue("Category 3", 79.5);
		dataset.setValue("Category 4", null);
		dataset.setValue("Category 5", 0);
		dataset.setValue("Category 6", -50.0f);
		//创建 Jfreechart对象  
		JFreeChart jfreechart = ChartFactory.createPieChart("Simple Pie Chart", dataset, true, true, false);
		PiePlot piePlot = (PiePlot) jfreechart.getPlot();
		//指定分类饼的颜色
		piePlot.setSectionPaint(33,Color.blue);
		piePlot.setSectionPaint(2,Color.red);
		piePlot.setSectionPaint(3,Color.yellow);
		
		//设置Pie的边框是否可见
		piePlot.setSectionOutlinesVisible(true);
		// 指定图片的透明度(0.0-1.0)
		piePlot.setForegroundAlpha(1.0f);
		//设置边框的颜色
		piePlot.setBaseSectionOutlinePaint(Color.green);
		//设置边框的粗细,new BasicStroke(2.0f)
		piePlot.setBaseSectionOutlineStroke(new BasicStroke(1));
		//设置空值,0值,负值是否显示出来,如果显示的话就是false
		piePlot.setIgnoreNullValues(true);
		piePlot.setIgnoreZeroValues(true);
		
		//设置上面的样式,0表示KEY,1表示VALUE,2表示百分之几,DecimalFormat用来显示百分比的格式
		piePlot.setLabelGenerator(new StandardPieSectionLabelGenerator("{0}=>{1}({2})",NumberFormat.getNumberInstance(),new DecimalFormat("0.00%")));
		
		//设置下面方框的样式,0表示KEY,1表示VALUE,2表示百分之几,DecimalFormat用来显示百分比的格式
		piePlot.setLegendLabelGenerator(new StandardPieSectionLabelGenerator("{0}=>{1}({2})",NumberFormat.getNumberInstance(),	new DecimalFormat("0.00%")));
		
		//爆炸模式,使Pie的一块分离出去,不支持3D
		piePlot.setExplodePercent(1, 0.10);
		piePlot.setExplodePercent(2, 0.10);
		piePlot.setExplodePercent(3, 0.10);
		
		//定义字体格式
		Font font = new Font("微软雅黑", Font.CENTER_BASELINE, 12);
		//定义图片标题
		TextTitle title = new TextTitle("Pie状图标题嘛");
		//设置标题的格式
		title.setFont(font);
		//把标题设置到图片里面
		jfreechart.setTitle(title);
		
		//设置字体,非常关键不然会出现乱码的,下方的字体
		jfreechart.getLegend().setItemFont(font);
		//Pie图的字体
		piePlot.setLabelFont(font);
		
		//创建输出流
		FileOutputStream fos_jpg = null;
		try {			
			fos_jpg = new FileOutputStream("c:\\Pie.jpg");
			//用工具把图象写到硬盘,支持两种格式,JPG,PNG,还支持MAP
			ChartUtilities.writeChartAsJPEG(fos_jpg,0.99f,jfreechart,640,480,null);
			fos_jpg.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}



package com.yong;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.io.FileOutputStream;
import java.io.UnsupportedEncodingException;
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.plot.PiePlot3D;
import org.jfree.chart.title.TextTitle;
import org.jfree.data.general.DefaultPieDataset;


/**
 * 
 * @author 赵永恩 
 *
 */
public class Pie3D {

	public static void main(String[] args) throws UnsupportedEncodingException {
		DefaultPieDataset dataset = new DefaultPieDataset();
		dataset.setValue("中文123", 43.2);
		dataset.setValue("Category 2", 27.9);
		dataset.setValue("Category 3", 27.9);
		dataset.setValue("Category 4", null);
		dataset.setValue("Category 5", 0);
		dataset.setValue("Category 6", -50.0f);
		
		JFreeChart jfreechart = ChartFactory.createPieChart3D("Simple Pie Chart", dataset, true, true, false);
		PiePlot3D piePlot = (PiePlot3D) jfreechart.getPlot();
		//指定分类饼的颜色
		//piePlot.setSectionPaint("Category 1",Color.blue);
		//piePlot.setSectionPaint("Category 2",Color.red);
		//piePlot.setSectionPaint("Category 3",Color.yellow);
		
		//设置Pie的边框是否可见
		piePlot.setSectionOutlinesVisible(true);
		
		//设置边框的颜色
		piePlot.setBaseSectionOutlinePaint(Color.green);
		//设置边框的粗细,new BasicStroke(2.0f)
		piePlot.setBaseSectionOutlineStroke(new BasicStroke(0));
		//设置空值,0值,负值是否显示出来,如果显示的话就是false
		piePlot.setIgnoreNullValues(true);
		piePlot.setIgnoreZeroValues(true);
		
		//设置上面的样式,0表示KEY,1表示VALUE,2表示百分之几,DecimalFormat用来显示百分比的格式
		piePlot.setLabelGenerator(new StandardPieSectionLabelGenerator("{0}=>{1}({2})",NumberFormat.getNumberInstance(),new DecimalFormat("0.00%")));
		
		//设置下面方框的样式,0表示KEY,1表示VALUE,2表示百分之几,DecimalFormat用来显示百分比的格式
		piePlot.setLegendLabelGenerator(new StandardPieSectionLabelGenerator("{0}=>{1}({2})",NumberFormat.getNumberInstance(),	new DecimalFormat("0.00%")));
		
		//爆炸模式,使Pie的一块分离出去,不支持3D
		piePlot.setExplodePercent(11, 0.10);
		piePlot.setExplodePercent(2, 0.10);
		piePlot.setExplodePercent(3, 0.10);
		
		//定义字体格式
		Font font = new Font("微软雅黑", Font.CENTER_BASELINE, 12);
		//定义图片标题
		TextTitle title = new TextTitle("Pie状图");
		//设置标题的格式
		title.setFont(font);
		//把标题设置到图片里面
		jfreechart.setTitle(title);
		
		//设置字体,非常关键不然会出现乱码的,下方的字体
		jfreechart.getLegend().setItemFont(font);
		//Pie图的字体
		piePlot.setLabelFont(font);
		
		FileOutputStream fos_jpg = null;
		try {
			fos_jpg = new FileOutputStream("c:\\Pie3D.jpg");
			ChartUtilities.writeChartAsJPEG(fos_jpg, 0.99f, jfreechart, 640, 480,null);
			fos_jpg.close();
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

}



package com.yong;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.io.FileOutputStream;
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.CategoryLabelPositions;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.BarRenderer;
import org.jfree.chart.title.TextTitle;
import org.jfree.data.category.DefaultCategoryDataset;

/**
 * 
 * @author 赵永恩
 *
 */
public class Bar {
	private static final long serialVersionUID = 1L;

	public static void main(String[] args) {
		//创建数据
		DefaultCategoryDataset dataset = new DefaultCategoryDataset();
		//数据初始化
		dataset.addValue(1.0, "北京", "苹果");
		dataset.addValue(7.0, "北京", "香蕉");
		dataset.addValue(-3.0, "北京", "桔子");
		dataset.addValue(2.0, "上海", "苹果");
		dataset.addValue(3.0, "上海", "香蕉");
		dataset.addValue(2.0, "上海", "桔子");
		//创建 JFreeChart 对象
		JFreeChart chart = ChartFactory.createBarChart("Bar Chart 例子","水果(X)", "价格(Y)", dataset,PlotOrientation.VERTICAL,true,true,false);
		//配置JFreeChart对象相关信息,如:字体大小,颜色,防止乱码
		Bar.configFont(chart);
		//输出
		FileOutputStream fos_jpg = null;
		try {
			fos_jpg = new FileOutputStream("c:\\Bar11.jpg");
			ChartUtilities.writeChartAsJPEG(fos_jpg,0.99f,chart,800,600,	null);
			fos_jpg.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	private static void configFont(JFreeChart chart) {
		//设置边框宽度
		chart.setBorderStroke(new BasicStroke(1));
		//设置边框是否可见
		chart.setBorderVisible(true);
		//设置边框着色
		chart.setBorderPaint(Color.cyan);		
		//设置背景颜色
		//chart.setBackgroundPaint(Color.YELLOW);		
		// 配置字体
		Font xfont = new Font("宋体", Font.CENTER_BASELINE, 20);// X轴
		Font yfont = new Font("宋体", Font.CENTER_BASELINE, 20);// Y轴
		Font kfont = new Font("宋体", Font.CENTER_BASELINE, 18);// 底部
		Font titleFont = new Font("微软雅黑", Font.CENTER_BASELINE, 25); // 图片标题
		CategoryPlot plot = chart.getCategoryPlot();// 图形的绘制结构对象
		//数据轴网格线条颜色
		plot.setRangeGridlinePaint(Color.BLUE);
		//数据轴网格线条笔触
		plot.setRangeGridlineStroke(new BasicStroke(1.0f));
		// 图片标题
		chart.setTitle(new TextTitle(chart.getTitle().getText(),titleFont));

		// 底部字体样式,防止乱码
		chart.getLegend().setItemFont(kfont);

		//X轴
		CategoryAxis domainAxis = plot.getDomainAxis();
		//设置X轴标题字体
		domainAxis.setLabelFont(xfont);
		//设置X轴字体
		domainAxis.setTickLabelFont(xfont);
		//设置字体颜色
		domainAxis.setTickLabelPaint(Color.BLUE);
		//横轴上的label斜显示
		domainAxis.setCategoryLabelPositions(CategoryLabelPositions.UP_45); 
		//domainAxis.setCategoryLabelPositions(CategoryLabelPositions.DOWN_45);
		//分类轴边距,同种类型之间的距离
		//这是分类之间的距离,和BAR与BAR之间的距离有差别
		//domainAxis.setCategoryMargin(0.2f);
		//分类轴下(左)边距,就是离左边的距离
		domainAxis.setLowerMargin(0.1);
		//分类轴下(右)边距,就是离最右边的距离
		domainAxis.setUpperMargin(0.1);
		
		//Y 轴
		ValueAxis rangeAxis = plot.getRangeAxis();
		//设置Y轴标题字体
		rangeAxis.setLabelFont(yfont);
		//设置Y轴字体
		rangeAxis.setTickLabelFont(yfont);
		// 字体颜色
		//rangeAxis.setLabelPaint(Color.RED); //包不同就这个属性 yong
		//设置Bar的颜色
		BarRenderer renderer = (BarRenderer) plot.getRenderer();
		
		renderer.setSeriesPaint(0, Color.gray);
		renderer.setSeriesPaint(1, Color.orange);
		// 每个BAR之间的间隔
		renderer.setItemMargin(0.0f);
		//每个BAR的最大宽度
		//renderer.setMaximumBarWidth(0.5f);
	}
}



package com.yong;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.io.FileOutputStream;
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.NumberAxis;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.LineAndShapeRenderer;
import org.jfree.chart.title.TextTitle;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.ui.HorizontalAlignment;
import org.jfree.ui.RectangleEdge;

/**
 * 
 * @author 赵永恩
 *
 */
public class Line {

	public static void main(String[] args) {
		//数据
		DefaultCategoryDataset dataset = new DefaultCategoryDataset();
		dataset.addValue(212, "Classes类", "JDK 1.0版本");
		dataset.addValue(504, "Classes类", "JDK 1.1版本");
		dataset.addValue(1520, "Classes类", "SDK 1.2版本");
		dataset.addValue(1842, "Classes类", "SDK 1.3版本");
		dataset.addValue(2991, "Classes类", "SDK 1.4版本");
		JFreeChart chart = ChartFactory.createLineChart(	"Java Standard Class Library","Release版本","Class数量",dataset,PlotOrientation.VERTICAL,false,	true,false	);
		//配置chart
		Line.configFont(chart);
		//设置标题
		TextTitle t = new TextTitle("Number of Classes By Release");
		t.setFont(new Font("Comic Sans MS", Font.PLAIN, 15));
		chart.addSubtitle(t);
		//设置标题,并把标题放到最下面的右边
		TextTitle source = new TextTitle("Source: Java In A Nutshell (4th Edition) by David Flanagan (O’Reilly)");
		source.setFont(new Font("Comic Sans MS", Font.PLAIN, 15));
		//放到底部
		source.setPosition(RectangleEdge.BOTTOM);
		//放到右边
		source.setHorizontalAlignment(HorizontalAlignment.RIGHT);
		//添加
		chart.addSubtitle(source);
		
		//设置背影色
		chart.setBackgroundPaint(Color.white);
		
		CategoryPlot plot = (CategoryPlot)chart.getPlot();
		
		//设置网格线
		plot.setRangeGridlinePaint(Color.white);
		// customise the range axis...(这个我不知道是啥子)		
		NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
		rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
		//设置线与线之间的连结点的样式,比如说是否空心,颜色等
		LineAndShapeRenderer renderer = (LineAndShapeRenderer) plot	.getRenderer();
		renderer.setShapesVisible(true);
		renderer.setDrawOutlines(true);
		renderer.setUseFillPaint(true);
		renderer.setFillPaint(Color.yellow);
		
		FileOutputStream fos_jpg = null;
		try {
			fos_jpg = new FileOutputStream("c:\\Line.jpg");
			ChartUtilities.writeChartAsJPEG(fos_jpg,0.99f,chart,800,600,	null);
			fos_jpg.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
         	/**
	 * 解决乱码和样式等问题
	 * @param chart
	 */
	private static void configFont(JFreeChart chart) {
		// 配置字体
		Font xfont = new Font("宋体", Font.CENTER_BASELINE, 12);// X轴
		Font yfont = new Font("宋体", Font.CENTER_BASELINE, 20);// Y轴
		Font kfont = new Font("宋体", Font.CENTER_BASELINE, 18);// 底部
		Font titleFont = new Font("微软雅黑", Font.CENTER_BASELINE, 25); // 图片标题
		CategoryPlot plot = chart.getCategoryPlot();// 图形的绘制结构对象
		//数据轴网格线条颜色
		plot.setRangeGridlinePaint(Color.BLUE);
		//数据轴网格线条笔触
		plot.setRangeGridlineStroke(new BasicStroke(1.0f));
		// 图片标题
		chart.setTitle(new TextTitle(chart.getTitle().getText(),titleFont));

		//X轴
		CategoryAxis domainAxis = plot.getDomainAxis();
		//设置X轴标题字体
		domainAxis.setLabelFont(xfont);
		//设置X轴字体
		domainAxis.setTickLabelFont(xfont);
		//设置字体颜色
		domainAxis.setTickLabelPaint(Color.BLUE);
		//横轴上的label斜显示
		//domainAxis.setCategoryLabelPositions(CategoryLabelPositions.UP_45); 
		//domainAxis.setCategoryLabelPositions(CategoryLabelPositions.DOWN_45);
		//分类轴边距,同种类型之间的距离,比如说Row 1之间的
		//domainAxis.setCategoryMargin(0.2f);
		//分类轴下(左)边距,就是离左边的距离
		domainAxis.setLowerMargin(0.1);
		//分类轴下(右)边距,就是离最右边的距离
		domainAxis.setUpperMargin(0.1);
		
		//Y 轴
		ValueAxis rangeAxis = plot.getRangeAxis();
		//设置Y轴标题字体
		rangeAxis.setLabelFont(yfont);
		//设置Y轴字体
		rangeAxis.setTickLabelFont(yfont);
		// 字体颜色
		rangeAxis.setLabelPaint(Color.RED); 
	}
}




package com.yong;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.io.FileOutputStream;
import java.text.SimpleDateFormat;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.DateAxis;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYItemRenderer;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.chart.title.TextTitle;
import org.jfree.data.time.Month;
import org.jfree.data.time.TimeSeries;
import org.jfree.data.time.TimeSeriesCollection;
import org.jfree.ui.RectangleInsets;

/**
 * 
 * @author 赵永恩
 *
 */
public class TimeSeriesTest {

	public static void main(String[] args) {
		//初始化数据
		TimeSeries s1 = new TimeSeries("L&G European Index Trust中文", Month.class);
		s1.add(new Month(2, 2001), 181.8);
		s1.add(new Month(3, 2001), 167.3);
		s1.add(new Month(4, 2001), 153.8);
		s1.add(new Month(5, 2001), 167.6);
		s1.add(new Month(6, 2001), 158.8);
		s1.add(new Month(7, 2001), 148.3);
		s1.add(new Month(8, 2001), 153.9);
		s1.add(new Month(9, 2001), 142.7);
		s1.add(new Month(10, 2001), 123.2);
		s1.add(new Month(11, 2001), 131.8);
		s1.add(new Month(12, 2001), 139.6);
		s1.add(new Month(1, 2002), 142.9);
		s1.add(new Month(2, 2002), 138.7);
		s1.add(new Month(3, 2002), 137.3);
		s1.add(new Month(4, 2002), 143.9);
		s1.add(new Month(5, 2002), 139.8);
		s1.add(new Month(6, 2002), 137.0);
		s1.add(new Month(7, 2002), 132.8);
		TimeSeries s2 = new TimeSeries("L&G UK Index Trust中文", Month.class);
		s2.add(new Month(2, 2001), 129.6);
		s2.add(new Month(3, 2001), 123.2);
		s2.add(new Month(4, 2001), 117.2);
		s2.add(new Month(5, 2001), 124.1);
		s2.add(new Month(6, 2001), 122.6);
		s2.add(new Month(7, 2001), 119.2);
		s2.add(new Month(8, 2001), 116.5);
		s2.add(new Month(9, 2001), 112.7);
		s2.add(new Month(10, 2001), 101.5);
		s2.add(new Month(11, 2001), 106.1);
		s2.add(new Month(12, 2001), 110.3);
		s2.add(new Month(1, 2002), 111.7);
		s2.add(new Month(2, 2002), 111.0);
		s2.add(new Month(3, 2002), 109.6);
		s2.add(new Month(4, 2002), 113.2);
		s2.add(new Month(5, 2002), 111.6);
		s2.add(new Month(6, 2002), 108.8);
		s2.add(new Month(7, 2002), 101.6);
		TimeSeriesCollection dataset = new TimeSeriesCollection();
		dataset.addSeries(s1);
		dataset.addSeries(s2);
		//创建 相应的对象
		JFreeChart chart = ChartFactory.createTimeSeriesChart("日期","Date时间","Price价格",dataset,true,	true,	false	);
		//配置字体等格式
		TimeSeriesTest.configFont(chart);
		//设置背影色,由于这个例是COPY过来的,并没有放到TimeSeriesTest.configFont(chart);这个方法里面
		chart.setBackgroundPaint(Color.white);
		//获得相当对象
		XYPlot plot = (XYPlot) chart.getPlot();
		//背影色
		plot.setBackgroundPaint(Color.lightGray);
		//X轴网格线和Y轴网格线颜色
		plot.setDomainGridlinePaint(Color.white);
		plot.setRangeGridlinePaint(Color.white);
		//以下还不清楚
		plot.setAxisOffset(new RectangleInsets(5.0, 5.0, 5.0, 5.0));
		plot.setDomainCrosshairVisible(true);
		plot.setRangeCrosshairVisible(true);
		//以下也不清楚
		XYItemRenderer r = plot.getRenderer();
		if (r instanceof XYLineAndShapeRenderer) {
			XYLineAndShapeRenderer renderer = (XYLineAndShapeRenderer) r;
			renderer.setBaseShapesVisible(true);
			renderer.setBaseShapesFilled(true);
		}
		//设置X坐标上面的格式
		DateAxis axis = (DateAxis) plot.getDomainAxis();
		axis.setDateFormatOverride(new SimpleDateFormat("MMM-yyyy"));
		
		FileOutputStream fos_jpg = null;
		try {
			fos_jpg = new FileOutputStream("c:\\TimeSeries.jpg");
			ChartUtilities.writeChartAsJPEG(fos_jpg,0.99f,chart,800,600,	null);
			fos_jpg.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/**
	 * 配置字体等信息
	 * @param chart
	 */
	private static void configFont(JFreeChart chart) {
		// 配置字体
		Font xfont = new Font("宋体", Font.CENTER_BASELINE, 12);// X轴
		Font yfont = new Font("宋体", Font.CENTER_BASELINE, 20);// Y轴
		Font kfont = new Font("宋体", Font.CENTER_BASELINE, 18);// 底部
		Font titleFont = new Font("微软雅黑", Font.CENTER_BASELINE, 25); // 图片标题
		XYPlot plot = (XYPlot) chart.getPlot();// 图形的绘制结构对象
		//数据轴网格线条颜色
		plot.setRangeGridlinePaint(Color.BLUE);
		//数据轴网格线条笔触
		plot.setRangeGridlineStroke(new BasicStroke(1.0f));
		// 图片标题
		chart.setTitle(new TextTitle(chart.getTitle().getText(),titleFont));
		chart.getLegend().setItemFont(kfont);

		//X轴
		ValueAxis domainAxis = plot.getDomainAxis();
		//设置X轴标题字体
		domainAxis.setLabelFont(xfont);
		//设置X轴字体
		domainAxis.setTickLabelFont(xfont);
		//设置字体颜色
		domainAxis.setTickLabelPaint(Color.BLUE);
		//横轴上的label斜显示
		//domainAxis.setCategoryLabelPositions(CategoryLabelPositions.UP_45); 
		//domainAxis.setCategoryLabelPositions(CategoryLabelPositions.DOWN_45);
		//分类轴边距,同种类型之间的距离,比如说Row 1之间的
		//domainAxis.setCategoryMargin(0.2f);
		//分类轴下(左)边距,就是离左边的距离
		domainAxis.setLowerMargin(0.1);
		//分类轴下(右)边距,就是离最右边的距离
		domainAxis.setUpperMargin(0.1);
		
		//Y 轴
		ValueAxis rangeAxis = plot.getRangeAxis();
		//设置Y轴标题字体
		rangeAxis.setLabelFont(yfont);
		//设置Y轴字体
		rangeAxis.setTickLabelFont(yfont);
		// 字体颜色
		rangeAxis.setLabelPaint(Color.RED); 

	}
}




JFreeChart jfreechart = null;
			
			//获得标题title.getText()
			TextTitle title = jfreechart.getTitle();
			
			//设置标题
			jfreechart.setTitle("A Chart Title");
			
			//设置标题的位置
			jfreechart.getTitle().setPosition(RectangleEdge.BOTTOM);
			
			//当然了还可以设置副标题
			TextTitle subtitle1 = new TextTitle("A Subtitle");
			jfreechart.addSubtitle(subtitle1);
			
			//获得副标题,可以有很多副标题,这是获得第0个
			Title subtitle = jfreechart.getSubtitle(0);
			
			//设置背景颜色
			jfreechart.setBackgroundPaint(Color.blue);
			
			//当然还可以设置多种背影颜色
			//表示在某个坐标之后是什么颜色
			Paint p = new GradientPaint(0, 0, Color.white, 1000, 0, Color.green);		
			jfreechart.setBackgroundPaint(p);
			
			//设置背景图片LOG
			jfreechart.setBackgroundImage(JFreeChart.INFO.getLogo());
			
			//设置背景图片的位置,这个没有试过
			jfreechart.setBackgroundImageAlignment(Align.TOP_LEFT);
			
			//Plot Attributes(共有的)
			//Plot(饼状图,)
			Plot plot1 = jfreechart.getPlot();
			
			//Plot(柱状图,)
			CategoryPlot plot2 = jfreechart.getCategoryPlot();
			
			//Plot(XY坐标图)
			XYPlot plot3 = jfreechart.getXYPlot();
			
			//设置背影颜色(是图象的)(其它形状同理)
			plot1.setBackgroundPaint(Color.white);
			//设置背景颜色
			plot1.setBackgroundImage(JFreeChart.INFO.getLogo());
			
			//Axis Attributes(X和Y上面的,轴对象,分为X轴Y轴)
			// get an axis reference...
			CategoryPlot plot = jfreechart.getCategoryPlot();
			
			//获得X轴对象
			CategoryAxis domainAxis = plot.getDomainAxis();
			//获得Y轴对象
			ValueAxis rangeAxis = plot.getRangeAxis();
			
			//change axis properties...改变属性
			domainAxis.setLabel("Categories");
			domainAxis.setLabelFont(new Font("宋体", Font.CENTER_BASELINE, 20));
			
			//设置X轴字体,Y轴同样
			domainAxis.setTickLabelFont(new Font("宋体", Font.CENTER_BASELINE, 20));

你可能感兴趣的:(jdk,数据结构,UI,jfreechart,UP)