您的位置:首页 > 其它

用jFreeChart制作一个饼状的图形统计图

2014-09-12 19:16 141 查看
package com.test.chart;

import java.awt.Color;
import java.awt.Font;
import java.io.FileOutputStream;
import java.text.DecimalFormat;
import java.text.NumberFormat;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.labels.StandardPieSectionLabelGenerator;
import org.jfree.chart.plot.PiePlot3D;
import org.jfree.data.general.DefaultPieDataset;
import org.jfree.util.Rotation;

public class PieChartDemo {

private static DefaultPieDataset getDataSet() {
DefaultPieDataset dataset = new DefaultPieDataset();
dataset.setValue("剩余容量",0.5);
dataset.setValue("已用容量",1.5);
return dataset;
}

public static void main(String[] args) throws Exception {

Font font = new Font("Microsoft YaHei",Font.BOLD,12);

DefaultPieDataset data = getDataSet();
JFreeChart chart = ChartFactory.createPieChart3D(null, data, false, false, false );

//获得3D的水晶饼图对象
PiePlot3D pieplot3d = (PiePlot3D) chart.getPlot();

//获取图像的对象,设置字体。不设置字体中文乱码
pieplot3d.setLabelFont(font);

//设置开始角度
pieplot3d.setStartAngle(70D);
//设置方向为”顺时针方向“
pieplot3d.setDirection(Rotation.CLOCKWISE);
//设置透明度,0.5F为半透明,1为不透明,0为全透明
pieplot3d.setForegroundAlpha(0.6F);
pieplot3d.setNoDataMessage("无数据显示");

//给标签的显示加上百分比格式
StandardPieSectionLabelGenerator generator = new StandardPieSectionLabelGenerator(("{0}{2}"), NumberFormat.getNumberInstance(),new DecimalFormat("0.00%"));
pieplot3d.setLabelGenerator(generator);

//标签与图形之间的距离
//pieplot3d.setLabelLinkMargin(-0.05);
//设置连线的颜色
//pieplot3d.setLabelLinkPaint(new Color(0, 180, 205));
pieplot3d.setSimpleLabels(true);

//为分区设置指定的颜色
pieplot3d.setSectionPaint("剩余容量", new Color(125, 125, 125));
pieplot3d.setSectionPaint("已用容量", new Color(0, 180, 205));

//设置绘图面板外边以及阴影的填充颜色
pieplot3d.setOutlinePaint(Color.RED);
pieplot3d.setShadowPaint(Color.BLUE);

//设置整个面板的背景色
pieplot3d.setBackgroundPaint(new Color(238, 241, 242));
//pieplot3d.setBackgroundPaint(null);

//设置标签背景颜色、边框颜色、阴影颜色和文字颜色
pieplot3d.setLabelBackgroundPaint(null);
pieplot3d.setLabelOutlinePaint(null);
pieplot3d.setLabelShadowPaint(null);
pieplot3d.setLabelPaint(new Color(120, 120, 120));

//重新设置图例的字体,保证汉字显示
//LegendTitle legend = chart.getLegend();
//legend.setItemFont(font);

FileOutputStream fos_jpg = null;
try {
fos_jpg = new FileOutputStream("D:\\fruit.jpg");
ChartUtilities.writeChartAsJPEG(fos_jpg, chart, 490, 300, null);
} finally {
try {
fos_jpg.close();
} catch(Exception e) {}
}
}
}

效果图如下:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: