快速基于echarts的大数据可视化

[Author]: kwu 

快速基于echarts的大数据可视化,echarts纯粹的js实现的图表工具,快速开发的步骤如下:

1、引入echarts的依赖js库

[javascript]  view plain copy
  1. <script type="text/javascript" src="js/esl/esl.js"></script>  
  2. <script type="text/javascript" src="js/echarts.js"></script>  
  3. <script type="text/javascript" src="js/jquery.js"></script>  

2、设置展示的div

[html]  view plain copy
  1. <!-- 为ECharts准备一个具备大小(宽高)的Dom -->  
  2. <div id="main" style="height: 300px"></div>  

3、绘图的JS

[javascript]  view plain copy
  1. var myChart;  
  2. var option;  
  3.   
  4. // 画图  
  5. function drawCharts(echartsHomePath) {  
  6.     // 路径配置  
  7.     require.config({  
  8.         paths : {  
  9.             echarts : echartsHomePath +'js'  
  10.         }  
  11.     })  
  12.       
  13.     // 使用  
  14.     require([ 'echarts''echarts/chart/bar''echarts/chart/line' ], function(  
  15.             ec) {  
  16.         myChart = ec.init(document.getElementById('main'));  
  17.           
  18.           
  19.         //官网复制option 开始  
  20.           
  21.           
  22.         option = {  
  23.                 title : {  
  24.                     text: '某地区蒸发量和降水量',  
  25.                     subtext: '纯属虚构'  
  26.                 },  
  27.                 tooltip : {  
  28.                     trigger: 'axis'  
  29.                 },  
  30.                 legend: {  
  31.                     data:['蒸发量']  
  32.                 },  
  33.                 toolbox: {  
  34.                     show : true,  
  35.                     feature : {  
  36.                         mark : {show: true},  
  37.                         dataView : {show: true, readOnly: false},  
  38.                         magicType : {show: true, type: ['line''bar']},  
  39.                         restore : {show: true},  
  40.                         saveAsImage : {show: true}  
  41.                     }  
  42.                 },  
  43.                 calculable : true,  
  44.                 xAxis : [  
  45.                     {  
  46.                         type : 'category',  
  47.                         data : ['1月','2月','3月','4月','5月','6月','7月','8月','9月','10月','11月','12月']  
  48.                     }  
  49.                 ],  
  50.                 yAxis : [  
  51.                     {  
  52.                         type : 'value'  
  53.                     }  
  54.                 ],  
  55.                 series : [  
  56.                     {  
  57.                         name:'蒸发量',  
  58.                         type:'bar',  
  59.                         data:[2.0, 4.9, 7.0, 23.2, 25.6, 76.7, 135.6, 162.2, 32.6, 20.0, 6.4, 3.3],  
  60.                         markPoint : {  
  61.                             data : [  
  62.                                 {type : 'max', name: '最大值'},  
  63.                                 {type : 'min', name: '最小值'}  
  64.                             ]  
  65.                         },  
  66.                         markLine : {  
  67.                             data : [  
  68.                                 {type : 'average', name: '平均值'}  
  69.                             ]  
  70.                         }  
  71.                     }  
  72.                 ]  
  73.             };  
  74.                                   
  75.           
  76.         //官网复制option 结束  
  77.         myInterval(restPath);  
  78.     });  
  79. }  
  80.   
  81.   
  82. //填充数据  
  83. function setResult(result, option, myChart) {  
  84.     if (result) {  
  85.         option.title.text = "每日apputrack趋势图";  
  86.         option.title.subtext = "apputrack";  
  87.         option.legend.data[0] = "apputrack";  
  88.         option.xAxis[0].data = result.day;  
  89.         option.series[0].name = "apputrack";  
  90.         option.series[0].data = result.cnt;  
  91.         myChart.setOption(option);  
  92.     }  
  93. }  

4、ajax获取restful数据

[javascript]  view plain copy
  1. //ajax获取数据  
  2. function myInterval(restPath) {  
  3.     $.ajax({  
  4.         type : 'get',// jquey是不支持post方式跨域的  
  5.         async : false,  
  6.         url : baseUrl +restPath,  // 跨域请求的URL  
  7.         dataType : 'jsonp',  
  8.         jsonp : "callback",// 服务端用于接收callback调用的function名的参数  
  9.         success : function(result) {  
  10.             setResult(result, option, myChart);  
  11.         },  
  12.         error : function() {  
  13.             alert('fail');  
  14.         }  
  15.     });  
  16. }  

5、定时调度及参数设置

[javascript]  view plain copy
  1. //ajax获取数据  
  2. function myInterval(restPath) {  
  3.     $.ajax({  
  4.         type : 'get',// jquey是不支持post方式跨域的  
  5.         async : false,  
  6.         url : baseUrl +restPath,  // 跨域请求的URL  
  7.         dataType : 'jsonp',  
  8.         jsonp : "callback",// 服务端用于接收callback调用的function名的参数  
  9.         success : function(result) {  
  10.             setResult(result, option, myChart);  
  11.         },  
  12.         error : function() {  
  13.             alert('fail');  
  14.         }  
  15.     });  
  16. }  

展示效果图:

快速基于echarts的大数据可视化_第1张图片


你可能感兴趣的:(大数据,ECharts,可视化)