highcharts饼图添加单击事件

  1. highcharts饼图添加单击事件  
  2.   
  3.   
  4. 因为项目需要,要做一个投票系统,用饼图展示用户选择了哪些选项,同时点击每个饼图,显示相应的用户。  
  5.   
  6.    
  7.   
  8. 系统采用了net+jquery+highcharts开发。  
  9.   
  10.    
  11.   
  12. 前端代码如下:  
  13.   
  14.    
  15.   
  16. <html >  
  17.   
  18. <head >  
  19.   
  20.     <title>无标题页</title>  
  21.   
  22. <script type="text/javascript" src="js/jquery.min.js"></script>  
  23.   
  24. <script type="text/javascript"  src="js/highcharts.js"></script>  
  25.   
  26.    
  27.   
  28. <script type="text/javascript">  
  29.   
  30.    
  31.   
  32. $(function () {  
  33.   
  34.     var chart;  
  35.   
  36.     $(document).ready(function() {  
  37.   
  38.       
  39.   
  40.     // Radialize the colors  
  41.   
  42. Highcharts.getOptions().colors = Highcharts.map(Highcharts.getOptions().colors, function(color) {  
  43.   
  44.     return {  
  45.   
  46.         radialGradient: { cx: 0.5, cy: 0.3, r: 0.7 },  
  47.   
  48.         stops: [  
  49.   
  50.             [0, color],  
  51.   
  52.             [1, Highcharts.Color(color).brighten(-0.3).get('rgb')] // darken  
  53.   
  54.         ]  
  55.   
  56.     };  
  57.   
  58. });  
  59.   
  60.    
  61.   
  62. // Build the chart  
  63.   
  64.         chart = new Highcharts.Chart({  
  65.   
  66.             chart: {  
  67.   
  68.                 renderTo: 'container',  
  69.   
  70.                 plotBackgroundColor: null,  
  71.   
  72.                 plotBorderWidth: null,  
  73.   
  74.                 plotShadow: false  
  75.   
  76.             },  
  77.   
  78.             title: {  
  79.   
  80.                 text: '你幸福吗?' //选择题的标题  
  81.   
  82.             },  
  83.   
  84.             tooltip: {  
  85.   
  86.             pointFormat: '{point.txt}: <b>{point.percentage}%</b>',  //鼠标放在每个饼图上显示内容。显示 “选项:百分比”  
  87.   
  88.             percentageDecimals: 1  
  89.   
  90.             },  
  91.   
  92.             plotOptions: {  
  93.   
  94.                 pie: {  
  95.   
  96.                     allowPointSelect: true,  
  97.   
  98.                     cursor: 'pointer',  
  99.   
  100.                     dataLabels: {  
  101.   
  102.                         enabled: true,  
  103.   
  104.                         color: '#000000',  
  105.   
  106.                         connectorColor: '#000000',  
  107.   
  108.                         formatter: function() {  
  109.   
  110.                             return '<b>'+ this.point.name +'</b>: '+ this.point.num +' 人'; //在饼图上直接显示 “A:多少人”  
  111.   
  112.                         }  
  113.   
  114.                     },  
  115.   
  116.                     events:{  
  117.   
  118.                         click: function(e) {  
  119.   
  120.  window.open('123.aspx?type='+e.point.type+'&id='+e.point.tid); //单击每个饼图打开一个页面,同时传递参数,单击了哪个饼图,题                                                                //目id是多少。  
  121.   
  122.                         }  
  123.   
  124.                     },  
  125.   
  126.                     showInLegend: false  
  127.   
  128.                       
  129.   
  130.                 }  
  131.   
  132.             },  
  133.   
  134.             series: [{  
  135.   
  136.                 type: 'pie',  
  137.   
  138.                 name: '',              //name,type,data是内置参数。不能自己定义一个参数。  
  139.   
  140.                 data: [                //整个data数组,可以从后台数据库读取之后,用string变量形成一个字符串,然后输出到这个位置。即<% =str %>,也可以用ajax实现。  
  141.   
  142.                     {  
  143.   
  144.                         name: 'A',    //name,y,sliced,selected是highchart的内置参数,下面的txt,type,tid,num是自己定义的。  
  145.   
  146.                         y: 10.0,  
  147.   
  148. txt: '幸福',  //这些是自己定义的,使用时,用this.point.txt或者e.point.txt来用。  
  149.   
  150. type:'A',  
  151.   
  152. tid: '123',  
  153.   
  154. num:20,  
  155.   
  156.                         sliced: true,   //true代表选中。可以将正确答案默认选中。  
  157.   
  158.                         selected: true  
  159.   
  160.                     },  
  161.   
  162. {  
  163.   
  164.                         name: 'B',  
  165.   
  166.                         y: 20.0,  
  167.   
  168. txt: '不幸福',  
  169.   
  170. tid: '123',  
  171.   
  172. type:'B',  
  173.   
  174. num:40  
  175.   
  176.                     },  
  177.   
  178. {  
  179.   
  180.                         name: 'C',  
  181.   
  182.                         y: 30.0,  
  183.   
  184. txt: '被幸福',  
  185.   
  186. tid: '123',  
  187.   
  188. type:'C',  
  189.   
  190. num:60  
  191.   
  192.                     },  
  193.   
  194. {  
  195.   
  196.                         name: 'D',  
  197.   
  198.                         y: 30.0,  
  199.   
  200. txt: '我姓曾',  
  201.   
  202. tid: '123',  
  203.   
  204. type:'D',  
  205.   
  206. num:60  
  207.   
  208.                     },  
  209.   
  210. {  
  211.   
  212.                         name: '未选择',  
  213.   
  214.                         y: 10.0,  
  215.   
  216. txt: '未选择',  
  217.   
  218. tid: '123',  
  219.   
  220. type:'nochoose',  
  221.   
  222. num:20  
  223.   
  224.                     }  
  225.   
  226.                 ]  
  227.   
  228.             }]  
  229.   
  230.         });  
  231.   
  232.     });  
  233.   
  234.       
  235.   
  236. });    
  237.   
  238. </script>  
  239.   
  240. <head>  
  241.   
  242. </head>  
  243.   
  244. <body>  
  245.   
  246.     <form id="form1" >  
  247.   
  248.     <div>      
  249.   
  250. <div id="container" style="width: 500px; height: 400px; margin: 0 auto"></div>  
  251.   
  252.     </div>  
  253.   
  254.     </form>  
  255.   
  256. </body>  
  257.   
  258. </html> 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值