Jfreechart创建环形图

来源:互联网 发布:太阳镜知乎 编辑:程序博客网 时间:2024/05/16 17:21
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