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

使用JFreeChart创建3D饼图

阅读更多
package com.cs.jfreechart;

import java.awt.Color;
import java.awt.Font;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PiePlot;
import org.jfree.chart.title.LegendTitle;
import org.jfree.chart.title.TextTitle;
import org.jfree.data.general.DefaultPieDataset;

public class Pie3DChartDemo {

	/**
	 * @param args
	 * @throws IOException 
	 */
	public static void main(String[] args) throws IOException {
		//生成饼图
		JFreeChart chart = ChartFactory.createPieChart3D(
				"图书销售统计表",     //图表标题
				getDateSet(),      //数据
				true,             //是否显示图例
				false,            //是否显示工具提示
				false             //是否生成URL
		);
		//设置标题及标题字体
		chart.setTitle(new TextTitle("图书销售统计图",new Font("黑体",Font.ITALIC,22)));
		//建一个图例
		LegendTitle legendTitle = chart.getLegend(0);
		//设置图例字体
		legendTitle.setItemFont(new Font("宋体",Font.BOLD,14));
		//获取饼图plot对象
		PiePlot plot = (PiePlot) chart.getPlot();
		//设置plot字体
		plot.setLabelFont(new Font("宋体",Font.BOLD,18));
		//根据key指定各个数据饼图的颜色
		plot.setSectionPaint("JAVA教程", Color.RED);
		plot.setSectionPaint("c++教程", Color.BLUE);
		plot.setSectionPaint("C#教程", Color.GREEN);
		plot.setSectionPaint("VC++教程", Color.ORANGE);
		//设置背景透明度(0~1)
		plot.setBackgroundAlpha(0.9f);
		//设置前景色透明度(0~1)
		plot.setForegroundAlpha(0.5f);
		//输出文件
		FileOutputStream fos = new FileOutputStream("book.jpg");
		//用ChartUtilities工具输出
		ChartUtilities.writeChartAsJPEG(fos, chart, 800, 600);
		fos.close();
	}
	
	private static DefaultPieDataset getDateSet() {
		//提供生成饼图的数据
		DefaultPieDataset dataset = new DefaultPieDataset();
		dataset.setValue("JAVA教程", 47);
		dataset.setValue("c++教程", 23);
		dataset.setValue("C#教程", 20);
		dataset.setValue("VC++教程", 10);
		return dataset;
	}

}

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics