java 画饼状图

本文介绍了一个使用Java和JFreeChart库绘制饼状图的例子。通过具体代码展示了如何设置数据集、调整颜色和保存图表为PNG图片。此外,还介绍了如何优化图表样式以提高视觉效果。

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

jar包:

效果:

代码:

import java.awt.Color;
import java.io.File;


import org.jfree.chart.ChartColor;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartFrame;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PiePlot;
import org.jfree.data.general.DefaultPieDataset;
import org.jfree.ui.RefineryUtilities;


public class ChartPie1 {


	public static void main(String[] args) {

		DefaultPieDataset dataset = new DefaultPieDataset();
		dataset.setValue("A", new Double(20));
		dataset.setValue("B", new Double(20));
		dataset.setValue("C", new Double(40));
		dataset.setValue("D", new Double(10));
		dataset.setValue("E", new Double(10));
		dataset.setValue("F", new Double(10));
		dataset.setValue("G", new Double(10));
		dataset.setValue("H", new Double(10));
		dataset.setValue("I", new Double(10));
		dataset.setValue("J", new Double(10));
		dataset.setValue("K", new Double(10));
		
		JFreeChart chart = ChartFactory.createPieChart("Mobile Sales", // chart
				dataset, // data
				true, // include legend
				true, false);
		setChart(chart);
		PiePlot pieplot = (PiePlot) chart.getPlot();    
		pieplot.setSectionPaint("A", Color.decode("#749f83"));    
		pieplot.setSectionPaint("B", Color.decode("#2f4554"));    
	    pieplot.setSectionPaint("C", Color.decode("#61a0a8"));    
	    pieplot.setSectionPaint("D", Color.decode("#d48265"));    
	    pieplot.setSectionPaint("E", Color.decode("#91c7ae"));    
	    pieplot.setSectionPaint("F", Color.decode("#c23531"));    
	    pieplot.setSectionPaint("G", Color.decode("#ca8622"));    
	    pieplot.setSectionPaint("H", Color.decode("#bda29a"));    
	    pieplot.setSectionPaint("I", Color.decode("#6e7074"));    
	    pieplot.setSectionPaint("J", Color.decode("#546570"));    
	    pieplot.setSectionPaint("K", Color.decode("#c4ccd3"));    
		
	    try {
//			// 创建图形显示面板
//			ChartFrame cf = new ChartFrame("柱状图", chart);
//			// cf.pack();
//			// // 设置图片大小
//			cf.setSize(width, height);
//			// // 设置图形可见
//			cf.setVisible(true);

			// 保存图片到指定文件夹
			ChartUtilities.saveChartAsPNG(new File("d:\\PieChart2.png"), chart, 1500, 800);
			System.err.println("成功");
		} catch (Exception e) {
			System.err.println("创建图形时出错");
		}
	}


	public static void setChart(JFreeChart chart) {
		chart.setTextAntiAlias(true);
		
		PiePlot pieplot = (PiePlot) chart.getPlot();
		// 设置图表背景颜色
		pieplot.setBackgroundPaint(ChartColor.WHITE);


		pieplot.setLabelBackgroundPaint(null);// 标签背景颜色


		pieplot.setLabelOutlinePaint(null);// 标签边框颜色


		pieplot.setLabelShadowPaint(null);// 标签阴影颜色


		pieplot.setOutlinePaint(null); // 设置绘图面板外边的填充颜色


		pieplot.setShadowPaint(null); // 设置绘图面板阴影的填充颜色
		
		pieplot.setSectionOutlinesVisible(false);        
		pieplot.setNoDataMessage("没有可供使用的数据!");        


	}
}


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值