JFreeChart

基本步骤:      

(1)建立Dataset。将你所想要显示的数据都放到这个库中。

(2)建立JFreeChart对象。将你的dataset填入到这个对象中。

(3)设置各种JFreeChart的属性和效果。通过它提供的各种方法和接口设置相关的属性。

(4)生成图表,然后按照个人的需求进行执行

注意:最新的版本会有中文无法显示的bug,设置标签的属性后就可以解决问题



条形统计图的基本创建方法



//创建一个条形容统计图
public class JFreeBarChartTest {
	public static void main(String[] args){
		
		
		// 创建数据集对象
		DefaultCategoryDataset dataset = new DefaultCategoryDataset();
		dataset.addValue(20, "小明", "语文");
		dataset.addValue(60, "小丽", "语文");
		dataset.addValue(50, "小明", "数学");
		dataset.addValue(50, "小丽", "数学");
		
		
		// 创建JFreeChart对象
		JFreeChart barChart =  ChartFactory.createBarChart3D(
				"条形统计图",//标题
				"姓名",     //x轴标签
				"分数" ,    //y轴标签
				dataset,    //数据集
				PlotOrientation.VERTICAL,//设置Y轴的方向垂直
				true,       //显示图例
				true,       //不生成工具
				true        //不生成连接
				);
		
		//设置字体属性
		CategoryPlot barPlot = barChart.getCategoryPlot();
		barChart.getLegend().setItemFont(new Font("宋体", Font.PLAIN, 12)); //底部标签
       
		CategoryAxis domainAxis = barPlot.getDomainAxis(); //x轴标签设置
		 domainAxis.setTickLabelFont(new Font("sans-serif", Font.PLAIN, 11));  
	     domainAxis.setLabelFont(new Font("宋体", Font.PLAIN, 12)); 
	        
        ValueAxis rAxis = barPlot.getRangeAxis();        //y轴标签设置
        rAxis.setTickLabelFont(new Font("sans-serif", Font.PLAIN, 12));  
        rAxis.setLabelFont(new Font("宋体", Font.PLAIN, 12));  
		
        TextTitle textTitle = barChart.getTitle();  //标签设置
        textTitle.setFont(new Font("宋体", Font.PLAIN, 20));  
        
        
        
       
        
		
		//以swing的形式输出图表
		ChartFrame barChartFrameframe = new ChartFrame("barChartFrame",barChart);
		barChartFrameframe.pack();
		barChartFrameframe.setVisible(true);
	}

	

}


运行结果:



饼图的基本创建方法

public class JfreePieChartTest {
	public static void main(String[] args) {
		// 创建数据集对象
		DefaultPieDataset dataset = new DefaultPieDataset();
		dataset.setValue("男生", 120);
		dataset.setValue("女生", 200);

		// 创建JFreeChart对象
		JFreeChart pieChart = ChartFactory.createPieChart("饼图", // 标题
				dataset, // 数据集
				true, true, true);

		// 设置字体属性
		PiePlot pieplot = (PiePlot) pieChart.getPlot(); // 通过JFreeChart对象获得plot

		TextTitle textTitle = pieChart.getTitle(); // 标题设置
		textTitle.setFont(new Font("宋体", Font.PLAIN, 20));

		pieChart.getLegend().setItemFont(new Font("宋体", Font.PLAIN, 12));//底部标签字体

		StandardPieSectionLabelGenerator sp = new StandardPieSectionLabelGenerator("{2}");//{1}表示显示数值, {2}表示显示百分比
		pieplot.setLabelGenerator(sp);// 设置百分比

		// 以swing的形式输出图表
		ChartFrame pieChartFrame = new ChartFrame("peiFrame", pieChart);
		pieChartFrame.setVisible(true);
		pieChartFrame.pack();

	}

}

运行结果:


  • 5
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值