饼状图通过controller输出到jsp页面

@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">

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值