package cn.bdqn.test;
import java.awt.Color;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import javax.imageio.ImageIO;
import org.jfree.chart.ChartColor;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.encoders.EncoderUtil;
import org.jfree.chart.encoders.ImageFormat;
import org.jfree.chart.plot.PiePlot;
import org.jfree.data.general.DefaultPieDataset;
public class ChartPie {
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);
BufferedImage image = chart.createBufferedImage(400, 300);
OutputStream out = new FileOutputStream("e:/PieChart2.png");
EncoderUtil.writeBufferedImage(image, ImageFormat.PNG, out);
System.err.println("成功");
} catch (Exception e) {
System.out.println(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("没有可供使用的数据!");
}
public static void saveAsFile(JFreeChart chart, String outputPath,
int weight, int height)throws Exception {
FileOutputStream out = null;
File outFile = new File(outputPath);
if (!outFile.getParentFile().exists()) {
outFile.getParentFile().mkdirs();
}
out = new FileOutputStream(outputPath);
// 保存为PNG
//ChartUtilities.writeChartAsPNG(out, chart, weight, height);
// 保存为JPEG
// ChartUtilities.writeChartAsJPEG(out, chart, weight, height);
out.flush();
if (out != null) {
try {
out.close();
} catch (IOException e) {
// do nothing
}
}
}
}