java使用jfreechar绘制饼型统计图

2025-09-26 03:59:04

1、使用eclipse,新建一个java项目,项目的名称javachar。

java使用jfreechar绘制饼型统计图

2、在项目中增加jfreechar类库,没有jfreechar下载,http://www.jfree.org/jfreechart/download/。

java使用jfreechar绘制饼型统计图

3、新建一个javachar.java类,带有main方法的主类。

public class javachar {

public static void main(String[] args) {

// TODO Auto-generated method stub

}

}

java使用jfreechar绘制饼型统计图

4、在main方法中,定义一个窗口。

public static void main(String[] args) {

// TODO Auto-generated method stub

JFrame jf=new JFrame();

jf.setSize(600,500);

jf.setLocationRelativeTo(null);

jf.setVisible(true);

}

java使用jfreechar绘制饼型统计图

5、在类中定义统计图的数据。

public static PieDataset shuju( ) 

{

     DefaultPieDataset dataset = new DefaultPieDataset( );

     dataset.setValue( "安阳一中" , new Double( 50 ) );  

     dataset.setValue( "安阳二中" , new Double( 100 ) );   

     dataset.setValue( "安阳三中" , new Double( 150 ) );    

     dataset.setValue( "安阳六中" , new Double( 80 ) );  

     return dataset;         

}

java使用jfreechar绘制饼型统计图

6、定义饼型图:

public static JFreeChart createChart( PieDataset dataset )

{

StandardChartTheme standardChartTheme = new StandardChartTheme("CN");

standardChartTheme.setExtraLargeFont(new Font("宋书", Font.BOLD, 25));

standardChartTheme.setRegularFont(new Font("宋书", Font.PLAIN, 15));

standardChartTheme.setLargeFont(new Font("宋书", Font.PLAIN, 15));

ChartFactory.setChartTheme(standardChartTheme);

   JFreeChart chart = ChartFactory.createPieChart("中学招生人数",dataset, true, true,  false);

   return chart;

}

java使用jfreechar绘制饼型统计图

7、在main方法中,把统计图增加到窗口中。

jf.add(new ChartPanel(createChart(shuju())));

java使用jfreechar绘制饼型统计图

8、运行项目,结果如下图。

java使用jfreechar绘制饼型统计图

声明:本网站引用、摘录或转载内容仅供网站访问者交流或参考,不代表本站立场,如存在版权或非法内容,请联系站长删除,联系邮箱:site.kefu@qq.com。
猜你喜欢