amcharts 导出图片~~perfect

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代码   收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <pie>  
  3.     <slice title="中国" pull_out="true">27</slice>  
  4.     <slice title="美国" pull_out="false" alpha="80">16</slice>  
  5.     <slice title="德国" pull_out="false">8</slice>  
  6.     <slice title="韩国" pull_out="false">7</slice>  
  7.     <slice title="意大利" alpha="100">6</slice>  
  8. </pie>  


页面代码: 
Html代码   收藏代码
  1. <html>  
  2.     <head>  
  3.         <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />  
  4.         <title>2008年第29届北京奥运会金牌榜</title>  
  5.         <script type="text/javascript" src="ampie/swfobject.js"></script>  
  6.         <script type="text/javascript">  
  7.              function exportImage() {  
  8.                  var flashMovie = document.getElementById('ampie');  
  9.                  if (flashMovie) {  
  10.                     flashMovie.exportImage('servlet/ExportImage');   
  11.                  }  
  12.               }   
  13.         </script>  
  14.     </head>  
  15.   
  16.     <body>  
  17.         <div id="flashcontent">  
  18.             <strong>You need to upgrade your Flash Player</strong>  
  19.         </div>  
  20.         <script type="text/javascript">  
  21.             var so = new SWFObject("ampie/ampie.swf", "ampie", "400", "200", "8", "#FFFFFF");  
  22.             so.addVariable("path", "ampie/");  
  23.             so.addVariable("settings_file", escape("ampie/ampie_settings.xml"));  
  24.             so.addVariable("data_file", escape("ampie/ampie_data.xml"));  
  25.             so.addVariable("preloader_color", "#999999");  
  26.             so.write("flashcontent");  
  27.   
  28.         </script>  
  29.         <hr noshade size="1">  
  30.         <input type="button" value="导出图片" onclick="exportImage();" />  
  31.     </body>  
  32. </html>  


处理导出的Servlet: 
Java代码   收藏代码
  1. package org.lhq.ampie.demo;  
  2.   
  3. import java.awt.Graphics2D;  
  4. import java.awt.RenderingHints;  
  5. import java.awt.image.BufferedImage;  
  6. import java.io.IOException;  
  7.   
  8. import javax.imageio.ImageIO;  
  9. import javax.servlet.ServletException;  
  10. import javax.servlet.ServletOutputStream;  
  11. import javax.servlet.http.HttpServlet;  
  12. import javax.servlet.http.HttpServletRequest;  
  13. import javax.servlet.http.HttpServletResponse;  
  14.   
  15. import com.sun.image.codec.jpeg.JPEGCodec;  
  16. import com.sun.image.codec.jpeg.JPEGEncodeParam;  
  17. import com.sun.image.codec.jpeg.JPEGImageEncoder;  
  18.   
  19. /** 
  20.  * 这个Servlet处理图表页面传来的导出图片请求 
  21.  *  
  22.  * @author 廖瀚卿 
  23.  *  
  24.  */  
  25. @SuppressWarnings("serial")  
  26. public class ExportImage extends HttpServlet {  
  27.   
  28.     public void doGet(HttpServletRequest request, HttpServletResponse response)  
  29.             throws ServletException, IOException {  
  30.         this.doPost(request, response);  
  31.     }  
  32.   
  33.     public void doPost(HttpServletRequest request, HttpServletResponse response)  
  34.             throws ServletException, IOException {  
  35.         // 页面flash的宽度和高度  
  36.         int width = Integer.parseInt(request.getParameter("width"));  
  37.         int height = Integer.parseInt(request.getParameter("height"));  
  38.   
  39.         BufferedImage result = new BufferedImage(width, height,  
  40.                 BufferedImage.TYPE_INT_RGB);  
  41.         // 页面是将一个个像素作为参数传递进来的,所以如果图表越大,处理时间越长  
  42.         for (int y = 0; y < height; y++) {  
  43.             int x = 0;  
  44.             String[] row = request.getParameter("r" + y).split(",");  
  45.             for (int c = 0; c < row.length; c++) {  
  46.                 String[] pixel = row[c].split(":"); // 十六进制颜色数组  
  47.                 int repeat = pixel.length > 1 ? Integer.parseInt(pixel[1]) : 1;  
  48.                 for (int l = 0; l < repeat; l++) {  
  49.                     result.setRGB(x, y, Integer.parseInt(pixel[0], 16));  
  50.                     x++;  
  51.                 }  
  52.             }  
  53.         }  
  54.         response.setContentType("image/jpeg");  
  55.         response.addHeader("Content-Disposition",  
  56.                 "attachment; filename=\"amchart.jpg\"");  
  57.         Graphics2D g = result.createGraphics();  
  58.         // 处理图形平滑度  
  59.         g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,  
  60.                 RenderingHints.VALUE_ANTIALIAS_ON);  
  61.         g.drawImage(result, 00, width, height, null);  
  62.         g.dispose();  
  63.   
  64.         ServletOutputStream f = response.getOutputStream();  
  65.         JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(f);  
  66.         JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(result);  
  67.         param.setQuality((float) (100 / 100.0), true);// 设置图片质量,100最大,默认70  
  68.         encoder.encode(result, param);  
  69.         ImageIO.write(result, "JPEG", response.getOutputStream());// 输出图片  
  70.         f.close();  
  71.     }  
  72.   
  73. }  
  • ampie_export_images.rar (39.5 KB)
  • 描述: 源代码,包括amCharts的破解:),希望能提出宝贵的意见,相互学习
  • 下载次数: 1775
xml <!-- [xml] (xml / csv) 数据类型xml/csv--> ; <!-- 如果使用csv作为数据的话,需要使用这个属性;表示文件数据分隔符,(平常以";"和","为主) [;] (string) csv file data separator (you need it only if you are using csv file for your data) --> 1 <!-- 如果使用的是csv数据,可以设置跳过几行再显示数据,默认为0表示csv中的数据全部显示,大于n(n>0);表示前面n行都不显示[0] (Number) if you are using csv data type, you can set the number of rows which should be skipped here --> <!-- 设置系统中的字体[Arial] (font name) use device fonts, such as Arial, Times New Roman, Tahoma, Verdana... --> <!-- 设置所有文本的大小,默认为11,具体的文本的字体大小也可以在下面的设置中设置[11] (Number) text size of all texts. Every text size can be set individually in the settings below --> <!-- 同上[#000000] (hex color code) main text color. Every text color can be set individually in the settings below--> . <!-- 小数分隔符,默认为[,]注:该属性只是用来显示,而在csv数据文件中,必须使用[.] (string) decimal separator. Note, that this is for displaying data only. Decimals in data xml file must be separated with a dot --> <!-- 千位分隔符,默认为空[ ] (string) thousand separator. use "none" if you don't want to separate --> 3 <!-- 如果百分数格式的数字,后面的小数位小于该属性的值,则在小数后面加0补充。如54.2%,该属性设置为3,那么显示的效果为54.200%。[] (Number) if your value has less digits after decimal then is set here, zeroes will be added --> <!--设置科学记数法的最小值 [0.000001] If absolute value of your number is equal or less then scientific_min, this number will be form
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值