小试JFreeChart柱状图和饼状图

本文介绍如何使用JFreeChart绘制柱状图与饼状图,并提供了完整的示例代码。通过导入必要的jar包并利用ChartFactory创建图表,展示了不同小组的人数分布情况。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

 闲来无事,就学学报表技术,目前找到一款开源JFreeChart,遂写下最常用的柱状图与饼状图以作记录。

 

导入jar包:

 

1、jfreechart-1.0.14.jar

2、jcommon-1.0.17.jar

 

其他不解释 直接看代码

 

package jfreechart;

import java.awt.Font;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartFrame;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PiePlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.data.general.DefaultPieDataset;

public class TestJfreeChart {

	public static void main(String[] args) throws Exception {
		//显示柱状图
		showBarChart();
		
		//显示饼状图
		showPieChart();
	}

	private static void showBarChart() throws IOException,
			FileNotFoundException {
		CategoryDataset dataset = getDataSet2();
		
		JFreeChart barChart = ChartFactory.createBarChart3D("柱状图", "分组", "人数",  
				dataset,PlotOrientation.VERTICAL, true, true, true);
		
		CategoryPlot  plot = (CategoryPlot) barChart.getPlot();
		CategoryAxis domainAxis = plot.getDomainAxis();
		ValueAxis rangeAxis = plot.getRangeAxis();
		
		Font font = new Font("微软雅黑", Font.BOLD, 12);
		
		barChart.getTitle().setFont(font );
		barChart.getLegend().setItemFont(font);
		
		domainAxis.setLabelFont(font);
		domainAxis.setTickLabelFont(font);
		rangeAxis.setLabelFont(font);
		rangeAxis.setTickLabelFont(font);
		
		ChartUtilities.writeChartAsJPEG(new FileOutputStream("C:/bar.jpg"),
				barChart, 1000, 700);
		
		ChartFrame frame = new ChartFrame("柱状图", barChart);
		
		frame.pack();
		frame.setVisible(true);
	}

	private static CategoryDataset getDataSet2() {
		DefaultCategoryDataset dataset = new DefaultCategoryDataset();

		dataset.addValue(78, "", "C++组");
		dataset.addValue(120, "", "JAVA组");
		dataset.addValue(65, "", "JS组");
		dataset.addValue(20, "", "数据库组");
		dataset.addValue(5, "", "需求组");

		return dataset;
	}

	private static void showPieChart() throws Exception {
		DefaultPieDataset dataset = getDataSet();
		
		JFreeChart pieChart = ChartFactory.createPieChart("人员分布图", dataset, true, true, true);

		PiePlot plot = (PiePlot) pieChart.getPlot();
		
		Font font = new Font("微软雅黑", Font.BOLD, 12);
		plot.setLabelFont(font);
		pieChart.getLegend().setItemFont(font);
		pieChart.getTitle().setFont(font);
		
		ChartUtilities.writeChartAsJPEG(new FileOutputStream("C:/pie.jpg"), 
				pieChart, 1000, 700);
		
		ChartFrame frame =  new ChartFrame("测试", pieChart);
		
		frame.pack();
		
		frame.setVisible(true);
	}

	private static DefaultPieDataset getDataSet() {
		DefaultPieDataset dataset = new DefaultPieDataset();
		
		dataset.setValue("C++组", 78);
		dataset.setValue("JAVA组", 120);
		dataset.setValue("JS组", 65);
		dataset.setValue("数据库组", 20);
		dataset.setValue("需求组", 5);
		return dataset;
	}
}

 

 

生成的效果图:

 

 

算了,不展示图片了。真心受不了iteye的上传附件速度,简直是坑爹………………

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值