echarts-圆环图

来源:互联网 发布:进入大数据时代 编辑:程序博客网 时间:2024/05/22 13:40

1、问题背景

     利用echarts制作一个圆环图,图的容器动态生成,图的数据源利用随机数动态变化,模拟后台获取数据


2、实现源码

[html] view plain copy print?
  1. <!DOCTYPE html>  
  2. <html>  
  3.     <head>  
  4.         <meta charset="UTF-8">  
  5.         <title>echarts-圆环图</title>  
  6.         <link rel="shortcut icon" href="../js/echarts-2.2.7/doc/asset/ico/favicon.png">  
  7.         <script type="text/javascript" src="../js/echarts-2.2.7/doc/asset/js/jquery.min.js" ></script>  
  8.         <script type="text/javascript" src="../js/echarts-2.2.7/doc/example/www2/js/echarts-all.js" ></script>  
  9.         <style>  
  10.             body,html{  
  11.                 width: 99%;  
  12.                 height: 99%;  
  13.                 font-family: "微软雅黑";  
  14.                 font-size: 12px;  
  15.             }  
  16.             #pie{  
  17.                 width: 100%;  
  18.                 height: 80%;  
  19.             }  
  20.         </style>  
  21.         <script>  
  22.             $(document).ready(function(){  
  23.                 randomData();  
  24.             });  
  25.               
  26.             //产生随机数  
  27.             function randomData()  
  28.             {  
  29.                 var first = (Math.random()*1000+1000).toFixed(2);  
  30.                 var second = (Math.random()*1000+1000).toFixed(2);  
  31.                 var third = (Math.random()*1000+1000).toFixed(2);  
  32.                 var fourth = (Math.random()*1000+1000).toFixed(2);  
  33.                 var chartId = Math.floor(Math.random()*1000+10000);  
  34.                 var pieName = ['第一季度','第二季度','第三季度','第四季度'];  
  35.                 var pieValue = new Array();  
  36.                 pieValue.push(first);  
  37.                 pieValue.push(second);  
  38.                 pieValue.push(third);  
  39.                 pieValue.push(fourth);  
  40.                 $("#bodyDiv").empty().append("<div id='pie"+chartId+"' style='width:100%;height:100%;'></div>");  
  41.                   
  42.                 buildChart(pieName,pieValue,chartId);  
  43.             }  
  44.               
  45.             //生成圆环图  
  46.             function buildChart(pieName,pieValue,chartId)  
  47.             {  
  48.                 var pieData = new Array();  
  49.                 for(var i=0;i<pieName.length;i++)  
  50.                 {  
  51.                     pieData.push(eval('(' + ("{'value':"+pieValue[i]+",'name':'"+pieName[i]+"'}") + ')'));  
  52.                 }  
  53.                 var pieChart = document.getElementById("pie"+chartId);  
  54.                 var echart = echarts.init(pieChart);  
  55.                 var option =  {  
  56.                     title : {  
  57.                         text: '一年四季收入比例',  
  58.                         x:"center"  
  59.                     },  
  60.                     tooltip : {  
  61.                         trigger: 'item',  
  62.                         formatter: "{a} <br/>{b} : {c} ({d}%)"  
  63.                     },  
  64.                     legend: {  
  65.                         orient: 'horizontal',  
  66.                         x:"center",  
  67.                         y:"bottom",  
  68.                         data: pieName  
  69.                     },  
  70.                     series : [  
  71.                         {  
  72.                             name: '季度',  
  73.                             type: 'pie',  
  74.                             radius : ['50%','70%'],  
  75.                             center: ['50%', '50%'],  
  76.                             data:pieData,  
  77.                             itemStyle: {  
  78.                                 emphasis: {  
  79.                                     shadowBlur: 10,  
  80.                                     shadowOffsetX: 0,  
  81.                                     shadowColor: 'rgba(0, 0, 0, 0.5)'  
  82.                                 }  
  83.                             }  
  84.                         }  
  85.                     ]  
  86.                 };  
  87.                   
  88.                 echart.setOption(option);  
  89.             }  
  90.               
  91.             //window.setInterval(randomData(),1000);  
  92.         </script>  
  93.     </head>  
  94.     <body id="bodyDiv">  
  95.           
  96.     </body>  
  97. </html>  

3、实现结果
原创粉丝点击