论坛首页 Java企业应用论坛

amCharts导出图片JAVA实现

浏览 12451 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2008-08-17   最后修改:2009-05-14
amCharts 是基于Flash的一套图表解决方法,界面比JFreeChart华丽很多,而且可以与用户交互,动画效果非常漂亮.官网:http://www.amcharts.com
About

AmCharts is a set of Flash charts for your websites and Web-based products. AmCharts can extract data from simple CSV or XML files, or they can read dynamic data generated with PHP, .NET, Java, Ruby on Rails, Perl, ColdFusion, and many other programming languages.
可见她很强大!

官网截图:


曲线图


柱状图


饼图

[img]http://yourgame.iteye.com/upload/picture/pic/20039/0fba1a10-9105-35bf-99c0-8ab1460e5ba6.gif [/img]
泡沫图


股票图


华丽的界面,非常的诱惑人,可是这个产品是要收费的,不过网上可以找到破解版本.
在官方的Demo很简单,大家可以下载来学习.不过我发现Demo中只有PHP,ASP.NET的导出图片示例,而没有JAVA导出图片的示例,我仿照ASP.NET的实现方法,做了一个JAVA导出的Demo!写得不怎么严谨,望大家多多指点


先看截图:

3D饼图


支持两种导出方式(设置很简单)


弹出保存对话框

[img]http://yourgame.iteye.com/upload/picture/pic/20047/472b70c3-06b8-3361-98ae-103b1111be89.gif [/img]
导出后的图片

图表的数据源:
<?xml version="1.0" encoding="UTF-8"?>
<pie>
	<slice title="中国" pull_out="true">27</slice>
	<slice title="美国" pull_out="false" alpha="80">16</slice>
	<slice title="德国" pull_out="false">8</slice>
	<slice title="韩国" pull_out="false">7</slice>
	<slice title="意大利" alpha="100">6</slice>
</pie>


页面代码:
<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
		<title>2008年第29届北京奥运会金牌榜</title>
		<script type="text/javascript" src="ampie/swfobject.js"></script>
		<script type="text/javascript">
			 function exportImage() {
			 	 var flashMovie = document.getElementById('ampie');
		         if (flashMovie) {
		            flashMovie.exportImage('servlet/ExportImage'); 
		         }
		      } 
		</script>
	</head>

	<body>
		<div id="flashcontent">
			<strong>You need to upgrade your Flash Player</strong>
		</div>
		<script type="text/javascript">
			var so = new SWFObject("ampie/ampie.swf", "ampie", "400", "200", "8", "#FFFFFF");
			so.addVariable("path", "ampie/");
			so.addVariable("settings_file", escape("ampie/ampie_settings.xml"));
			so.addVariable("data_file", escape("ampie/ampie_data.xml"));
			so.addVariable("preloader_color", "#999999");
			so.write("flashcontent");

		</script>
		<hr noshade size="1">
		<input type="button" value="导出图片" onclick="exportImage();" />
	</body>
</html>


处理导出的Servlet:
package org.lhq.ampie.demo;

import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGEncodeParam;
import com.sun.image.codec.jpeg.JPEGImageEncoder;

/**
 * 这个Servlet处理图表页面传来的导出图片请求
 * 
 * @author 廖瀚卿
 * 
 */
@SuppressWarnings("serial")
public class ExportImage extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		this.doPost(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// 页面flash的宽度和高度
		int width = Integer.parseInt(request.getParameter("width"));
		int height = Integer.parseInt(request.getParameter("height"));

		BufferedImage result = new BufferedImage(width, height,
				BufferedImage.TYPE_INT_RGB);
		// 页面是将一个个像素作为参数传递进来的,所以如果图表越大,处理时间越长
		for (int y = 0; y < height; y++) {
			int x = 0;
			String[] row = request.getParameter("r" + y).split(",");
			for (int c = 0; c < row.length; c++) {
				String[] pixel = row[c].split(":"); // 十六进制颜色数组
				int repeat = pixel.length > 1 ? Integer.parseInt(pixel[1]) : 1;
				for (int l = 0; l < repeat; l++) {
					result.setRGB(x, y, Integer.parseInt(pixel[0], 16));
					x++;
				}
			}
		}
		response.setContentType("image/jpeg");
		response.addHeader("Content-Disposition",
				"attachment; filename=\"amchart.jpg\"");
		Graphics2D g = result.createGraphics();
		// 处理图形平滑度
		g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
				RenderingHints.VALUE_ANTIALIAS_ON);
		g.drawImage(result, 0, 0, width, height, null);
		g.dispose();

		ServletOutputStream f = response.getOutputStream();
		JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(f);
		JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(result);
		param.setQuality((float) (100 / 100.0), true);// 设置图片质量,100最大,默认70
		encoder.encode(result, param);
		ImageIO.write(result, "JPEG", response.getOutputStream());// 输出图片
		f.close();
	}

}
  • ampie_export_images.rar (39.5 KB)
  • 描述: 源代码,包括amCharts的破解:),希望能提出宝贵的意见,相互学习
  • 下载次数: 1894
   发表时间:2008-09-14  
赞个  ,以前都用 jfreechart的 ,搞得 服务器端 生成一段temp file

试试这个了
0 请登录后投票
   发表时间:2008-11-10  
这个Flash图表 元件很棒啊
并不需要破解,只要准备一个版权文字档amcharts_key.txt放进去就是正式版了

这档案也很好取得,上Google搜索一下就有

http://lethargy.org/amcharts/amcolumn/amcharts_key.txt
 
http://www.dragonsoft.net/projects/erning/browser/amcharts/dotnet/trunk/amCharts/Static/amcharts_key.txt?rev=88

0 请登录后投票
   发表时间:2008-11-12  
amcharts_key.txt
flash2u 写道

这个Flash图表 元件很棒啊 并不需要破解,只要准备一个版权文字档amcharts_key.txt放进去就是正式版了 这档案也很好取得,上Google搜索一下就有 http://lethargy.org/amcharts/amcolumn/amcharts_key.txt &nbsp; http://www.dragonsoft.net/projects/erning/browser/amcharts/dotnet/trunk/amCharts/Static/amcharts_key.txt?rev=88

下载包中已经包含了该文件。
0 请登录后投票
   发表时间:2009-01-12  
yourgame 写道
amCharts 是基于Flash的一套图表解决方法,界面比JFreeChart华丽很多,而且可以与用户交互,动画效果非常漂亮.官网:http://www.amcharts.com
About

AmCharts is a set of Flash charts for your websites and Web-based products. AmCharts can extract data from simple CSV or XML files, or they can read dynamic data generated with PHP, .NET, Java, Ruby on Rails, Perl, ColdFusion, and many other programming languages.
可见她很强大!

官网截图:

[img]http://yourgame.iteye.com/upload/picture/pic/20045/4987df21-a373-37f9-9963-e18f7e65b6a9.gif [/img]
曲线图

[img]http://yourgame.iteye.com/upload/picture/pic/20043/4ca7b27a-2595-3783-a42f-31479877ce81.gif [/img]
柱状图

[img]http://yourgame.iteye.com/upload/picture/pic/20041/31db4626-e2fe-33e7-b42c-866aa7dcc15c.gif [/img]
饼图

[img]http://yourgame.iteye.com/upload/picture/pic/20039/0fba1a10-9105-35bf-99c0-8ab1460e5ba6.gif [/img]
泡沫图

[img]http://yourgame.iteye.com/upload/picture/pic/20037/c42d3157-f296-322b-9693-0caf332bb06f.gif [/img]
股票图


华丽的界面,非常的诱惑人,可是这个产品是要收费的,不过网上可以找到破解版本.
在官方的Demo很简单,大家可以下载来学习.不过我发现Demo中只有PHP,ASP.NET的导出图片示例,而没有JAVA导出图片的示例,我仿照ASP.NET的实现方法,做了一个JAVA导出的Demo!写得不怎么严谨,望大家多多指点


先看截图:
[img]http://yourgame.iteye.com/upload/picture/pic/20053/da2c9334-0a98-3fff-b7c3-ac8d8b26f9fa.gif [/img]
3D饼图

[img]http://yourgame.iteye.com/upload/picture/pic/20051/0b118ae6-c0bd-3c6f-adde-ef940ac5e65f.gif [/img]
支持两种导出方式(设置很简单)

[img]http://yourgame.iteye.com/upload/picture/pic/20049/f47b6428-4d73-383d-a39a-82e3d3dea790.gif [/img]
弹出保存对话框

[img]http://yourgame.iteye.com/upload/picture/pic/20047/472b70c3-06b8-3361-98ae-103b1111be89.gif [/img]
导出后的图片


图表的数据源:
<?xml version="1.0" encoding="UTF-8"?>
<pie>
	<slice title="中国" pull_out="true">27</slice>
	<slice title="美国" pull_out="false" alpha="80">16</slice>
	<slice title="德国" pull_out="false">8</slice>
	<slice title="韩国" pull_out="false">7</slice>
	<slice title="意大利" alpha="100">6</slice>
</pie>


页面代码:
<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
		<title>2008年第29届北京奥运会金牌榜</title>
		<script type="text/javascript" src="ampie/swfobject.js"></script>
		<script type="text/javascript">
			 function exportImage() {
			 	 var flashMovie = document.getElementById('ampie');
		         if (flashMovie) {
		            flashMovie.exportImage('servlet/ExportImage'); 
		         }
		      } 
		</script>
	</head>

	<body>
		<div id="flashcontent">
			<strong>You need to upgrade your Flash Player</strong>
		</div>
		<script type="text/javascript">
			var so = new SWFObject("ampie/ampie.swf", "ampie", "400", "200", "8", "#FFFFFF");
			so.addVariable("path", "ampie/");
			so.addVariable("settings_file", escape("ampie/ampie_settings.xml"));
			so.addVariable("data_file", escape("ampie/ampie_data.xml"));
			so.addVariable("preloader_color", "#999999");
			so.write("flashcontent");

		</script>
		<hr noshade size="1">
		<input type="button" value="导出图片" onclick="exportImage();" />
	</body>
</html>


处理导出的Servlet:
package org.lhq.ampie.demo;

import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGEncodeParam;
import com.sun.image.codec.jpeg.JPEGImageEncoder;

/**
 * 这个Servlet处理图表页面传来的导出图片请求
 * 
 * @author 廖瀚卿
 * 
 */
@SuppressWarnings("serial")
public class ExportImage extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		this.doPost(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// 页面flash的宽度和高度
		int width = Integer.parseInt(request.getParameter("width"));
		int height = Integer.parseInt(request.getParameter("height"));

		BufferedImage result = new BufferedImage(width, height,
				BufferedImage.TYPE_INT_RGB);
		// 页面是将一个个像素作为参数传递进来的,所以如果图表越大,处理时间越长
		for (int y = 0; y < height; y++) {
			int x = 0;
			String[] row = request.getParameter("r" + y).split(",");
			for (int c = 0; c < row.length; c++) {
				String[] pixel = row[c].split(":"); // 十六进制颜色数组
				int repeat = pixel.length > 1 ? Integer.parseInt(pixel[1]) : 1;
				for (int l = 0; l < repeat; l++) {
					result.setRGB(x, y, Integer.parseInt(pixel[0], 16));
					x++;
				}
			}
		}
		response.setContentType("image/jpeg");
		response.addHeader("Content-Disposition",
				"attachment; filename=\"amchart.jpg\"");
		Graphics2D g = result.createGraphics();
		// 处理图形平滑度
		g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
				RenderingHints.VALUE_ANTIALIAS_ON);
		g.drawImage(result, 0, 0, width, height, null);
		g.dispose();

		ServletOutputStream f = response.getOutputStream();
		JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(f);
		JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(result);
		param.setQuality((float) (100 / 100.0), true);// 设置图片质量,100最大,默认70
		encoder.encode(result, param);
		ImageIO.write(result, "JPEG", response.getOutputStream());// 输出图片
		f.close();
	}

}

0 请登录后投票
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics