Jfreechart创建环形图

10 篇文章 0 订阅
import java.awt.Color;
import java.awt.Font;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartFrame;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.StandardChartTheme;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.axis.NumberTickUnit;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.labels.ItemLabelAnchor;
import org.jfree.chart.labels.ItemLabelPosition;
import org.jfree.chart.labels.StandardCategoryItemLabelGenerator;
import org.jfree.chart.labels.StandardPieSectionLabelGenerator;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.IntervalMarker;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.RingPlot;
import org.jfree.chart.title.LegendTitle;
import org.jfree.chart.title.TextTitle;
import org.jfree.data.general.DefaultPieDataset;
import org.jfree.data.general.PieDataset;
import org.jfree.ui.Layer;
import org.jfree.ui.LengthAdjustmentType;
import org.jfree.ui.TextAnchor;

public class RingChart {
 public RingChart(){
  this.createChart();
 }

 public void createChart() {

       StandardChartTheme mChartTheme = new StandardChartTheme("CN");
      mChartTheme.setLargeFont(new Font("黑体", Font.BOLD, 14)); // Y柱标签字体(浓度(单位:ppm))
        mChartTheme.setExtraLargeFont(new Font("宋体", Font.PLAIN, 20)); //标题 (二氧化碳浓度趋势分析图)
         //应用主题样式
         ChartFactory.setChartTheme(mChartTheme);
          //定义图标对象
          JFreeChart chart = ChartFactory.createRingChart ("环形图",// 报表题目,字符串类型
                                 this.createDataset(), // 获得数据集
                                 true, // 显示图例
                                 false, // 不用生成工具
                                 false // 不用生成URL地址
                                 );
             //图表
              RingPlot ringplot=(RingPlot) chart.getPlot();
               ringplot.setLabelFont(new Font("宋体", Font.BOLD, 14));
              //用来显示标注的注解,{0}:{1}用来设置显示的格式
               ringplot.setLabelGenerator(new StandardPieSectionLabelGenerator("{0}:{1}"));
               ringplot.setBackgroundPaint(Color.gray);//设置背景色
               //设置简单标签
                ringplot.setSimpleLabels(true);
                 //标题
                TextTitle texttitle=chart.getTitle();
                 texttitle.setFont(new Font("宋体", Font.BOLD, 30));
                 //图示
                LegendTitle legendtitle =chart.getLegend();
                legendtitle.setItemFont(new Font("宋体", Font.BOLD, 14));
                ChartFrame mChartFrame = new ChartFrame("环形图", chart);
                mChartFrame.pack();
               mChartFrame.setVisible(true);
     }

     // 获得数据集 (这里的数据是为了测试我随便写的一个自动生成数据的例子)
     public PieDataset createDataset() {
            DefaultPieDataset dataSet = new DefaultPieDataset();
          //使用循环向数据集合中添加数据
            int i,j;            
            String []a={"Java","VC","C++","数据结构","计算机网络"};            
            int []b={1964,820,825,960,1000};            
            for(i=0,j=0;i<a.length&&j<b.length;i++,j++){              
             dataSet.setValue(a[i],b[j]);
            }        
            return dataSet;
     }

 public static void main(String[] args) {

  RingChart RC=new RingChart();
 }
}
运行结果图:

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值