- 转自 : http://hua04104.iteye.com/blog/704094
- 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;
- }
- }
使用JFreeChart创建饼图
最新推荐文章于 2025-02-23 00:15:00 发布