JFreeChart 画饼状图 颜色 字体 样式的设置

一个画饼状图的方法:
 
/**
     * 画饼状图
     * @param dataset DefaultPieDataset
     * @return JFreeChart
     */

     public static JFreeChart getPieChart(DefaultPieDataset dataset,
                                                                             String title) {
             //创建JFreeChart,都使用ChartFactory来创建JFreeChart,很标准的工厂设计模式
            JFreeChart chart = ChartFactory.createPieChart(title,
                    dataset,
                     true,
                     false,
                     false);

             //设定图片标题
            chart.setTitle( new TextTitle(title, new Font( "隶书", Font.BOLD, 16)));
             //设定背景
            chart.setBackgroundPaint(Color.white);
             //饼图使用一个PiePlot
            PiePlot pie = (PiePlot) chart.getPlot();
             //设定百分比显示格式
            pie.setLabelGenerator( new StandardPieItemLabelGenerator( "{0}={1}({2})",
                    NumberFormat.getNumberInstance(),
                     new DecimalFormat( "0.00%")));
             //指定 section 轮廓线的颜色
            pie.setBaseSectionOutlinePaint( new Color(0xF7, 0x79, 0xED));
             //指定 section 轮廓线的厚度
            pie.setSectionOutlineStroke( new BasicStroke(0));
             //指定 section 的色彩
            pie.setSectionPaint(0, new Color(0xF7, 0x79, 0xED));
            pie.setNoDataMessage( "没有数据!");
            pie.setNoDataMessagePaint(Color.blue);
            pie.setCircular( true);
            pie.setLabelGap(0.01D); //间距
            pie.setBackgroundPaint(Color.white);
            pie.setLabelFont( new Font( "黑体", Font.TRUETYPE_FONT, 12));
             //设定背景透明度(0-1.0之间)
            pie.setBackgroundAlpha(0.6f);
             //设定前景透明度(0-1.0之间)
            pie.setForegroundAlpha(0.8f);
            BarRenderer3D renderer = new BarRenderer3D();
            renderer.setWallPaint(Color.lightGray);
             return chart;
    }
 
开始生成饼状图:
JFreeChart chart = CategoryChart.getPieChart(dataset, "饼状图");
    FileOutputStream jpg = null;
     try {
      jpg = new FileOutputStream(jpgname);
      ChartUtilities.writeChartAsPNG(jpg, chart, 500, 400);
//      ChartUtilities.writeChartAsJPEG(jpg, 100, chart, 500, 400, null);
其中 ChartUtilities.writeChartAsJPEG(jpg, 100, chart, 500, 400, null);中 第二个参数指的是生成饼状图的品质,也就是图片的质量,数值越高颜色应该越清楚。
 
 
修改饼状图 指定 颜色和元素相对应:
 
比如,有一个问题,选择ABCD四个答案的比例分别是:10%,30%,20%,40%。希望饼图中A的颜色是red,B的颜色是green,C的颜色是yellow,D的颜色是blue。
可以这么做:
1.准备好dataset,这一步就不讲了,pieChart有其要求的dataset格式。
2.书写一个创建pieChart的方法:
 
public static JFreeChart createPieChart(DefaultPieDataset dataset, String mytitle) {        
                        JFreeChart chart = ChartFactory.createPieChart(mytitle,dataset, true, true, false);        
                        PiePlot plot = (PiePlot) chart.getPlot();        
                        plot.setSectionOutlinesVisible( false);        
                        plot.setNoDataMessage( "没有可供使用的数据!");        
                        plot.setSectionPaint( "A", Color.RED);        
                        plot.setSectionPaint( "B", Color.GREEN);        
                        plot.setSectionPaint( "C", Color.YELLOW);        
                        plot.setSectionPaint( "D", Color.BLUE);        
                         //就是这个地方,实现了对各个key对应饼图区域的颜色设置        
    
//                    PiePlot plot = (PiePlot) chart.getPlot();        
                        plot.setLabelFont( new Font( "SansSerif", Font.PLAIN, 12));        
                        plot.setCircular( false);        
                        plot.setLabelGap(0.02);        
//do more things here        
                         return chart;        
}    
注意到plot.setSectionPaint("A", Color.RED);这个方法了吧,该方法就是设置颜色的关键语句。如果没有此方法,则JFreeChart在其创建过程中,会调用默认的颜色来给各个区域进行颜色设置。
 
需要注意的是
plot.setSectionPaint( "A" , Color.RED);此类方法只有1.0.6版本支持,1.0.6以前的版本中
setSectionPaint方法的第一个参数是整形!如:plot.setSectionPaint(0, Color.RED);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值