使用JFreeChart创建饼图


package com.cs.jfreechart;

import java.awt.Color;
import java.awt.Font;
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 PieChartDemo {

/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
//生成饼图
JFreeChart chart = ChartFactory.createPieChart(
"图书销售统计表", //图表标题
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();
//根据key指定各个数据饼图的颜色
plot.setSectionPaint("JAVA教程", Color.RED);
plot.setSectionPaint("c++教程", Color.BLUE);
plot.setSectionPaint("C#教程", Color.GREEN);
plot.setSectionPaint("VC++教程", Color.ORANGE);
//设置plot字体
plot.setLabelFont(new Font("宋体",Font.BOLD,18));
//设置背景透明度(0~1)
plot.setBackgroundAlpha(0.9f);
//输出文件
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;
}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值