@RequestMapping("imgSecond")
public void genImageChart(HttpServletResponse resp) {
DefaultPieDataset dataset = new DefaultPieDataset();
dataset.setValue("A1", new Double(20));
dataset.setValue("B1", new Double(20));
dataset.setValue("C1", new Double(40));
dataset.setValue("D1", new Double(10));
dataset.setValue("E1", new Double(10));
dataset.setValue("F1", new Double(10));
dataset.setValue("G1", new Double(10));
dataset.setValue("H1", new Double(10));
dataset.setValue("I1", new Double(10));
dataset.setValue("J1", new Double(10));
dataset.setValue("K1", 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("A1", Color.decode("#749f83"));
pieplot.setSectionPaint("B1", Color.decode("#2f4554"));
pieplot.setSectionPaint("C1", Color.decode("#61a0a8"));
pieplot.setSectionPaint("D1", Color.decode("#d48265"));
pieplot.setSectionPaint("E1", Color.decode("#91c7ae"));
pieplot.setSectionPaint("F1", Color.decode("#c23531"));
pieplot.setSectionPaint("G1", Color.decode("#ca8622"));
pieplot.setSectionPaint("H1", Color.decode("#bda29a"));
pieplot.setSectionPaint("I1", Color.decode("#6e7074"));
pieplot.setSectionPaint("J1", Color.decode("#546570"));
pieplot.setSectionPaint("K1", 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 os = new BufferedOutputStream(resp.getOutputStream());
EncoderUtil.writeBufferedImage(image, ImageFormat.PNG, os);
System.err.println("成功");
} catch (Exception e) {
System.out.println(e);
System.err.println("创建图形时出错");
}
}
然后在jsp页面写上图片代码
<img alt="tupian" src="imgSecond">