Jfreechart学习心得之一(饼形状图形的创建)

最近在做一个项目,需要用到java来画图,就在网上找了一个Jfreechaet这个api,简单的学习了一下,也找到了一个很不错的博客,就借鉴一下。
原文地址:原文地址
1.饼形状图形的创建的例子

package com.langhua.Pie;  
  
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;  
  
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("中文",Color.blue);  
        piePlot.setSectionPaint("Category 2",Color.red);  
        piePlot.setSectionPaint("Category 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("Category 1"0.10);  
        piePlot.setExplodePercent("Category 2"0.10);  
        piePlot.setExplodePercent("Category 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 ``````````
Efreechart.getLegend().setItemFont(font);  
    //Pie图的字体  
    piePl

PiePlot3D图象的创建和PiePlot基本一样,就是创建的时候的区别,还有3D不支持爆炸模式

JFreeChart jfreechart = ChartFactory.createPieChart3D("Simple Pie Chart", dataset, true, tru`
false);  
        PiePlot3D piePlot = (PiePlot3D) jfreechart.getPlot();  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值