今天在学习ECharts时,想要在ECharts图表的原生态Demo中抠出漏斗图,却不知如何下手,经过一番研究,特总结如下:
首先我们需要这样做
1、拷贝出两个js文件:esl.js 和echarts.js ;
2、准备一个html页面进行图表容器配备和模块化加载js和创建图表
1)、引入esl.js文件
<script src="../js/echarts/esl.js" type="text/javascript"></script>
之所以要引入esl.js 文件,因为它内部封装了很多模块化加载js文件和创建图表的回调函数方法。
2)、准备图表的装载容器
<div id="funnel_a" style="height: 400px; width: 800px; border: 1px solid #ccc; padding: 10px;"></div>
3)、采用esl.js文件内的方法模块化加载漏斗图所需的库且在回调函数内创建漏斗图。
// Step:3 conifg ECharts's path, link to echarts.js from current page. // Step:3 为模块加载器配置echarts的路径,从当前页面链接到echarts.js,定义所需图表路径 require.config({ /* paths: { echarts: '../js/echarts', //echarts.js所在的路径 'echarts/chart/funnel': '../js/echarts' } */ packages:[{ name:'echarts', location:'../js/echarts', main:'echarts' },{ name:'zrender', location:'../js/zrender',//zrender与echarts在同一级目录 main:'zrender' } ] }); // Step:4 require echarts and use it in the callback. // Step:4 动态加载echarts然后在回调函数中开始使用,注意保持按需加载结构定义图表路径 require( [ 'echarts', 'echarts/chart/bar', 'echarts/chart/map', 'echarts/chart/funnel' ], function (ec) {//渲染ECharts图表 ,可单独写出来作为回调函数 // --- 中国地图 ---------------------------------------------- var myChart1 = ec.init(document.getElementById('funnel_a')); //加载图表 option = { title : { text: '漏斗图', subtext: '纯属虚构' }, tooltip : { trigger: 'item', formatter: "{a} <br/>{b} : {c}%" }, toolbox: { show : true, feature : { mark : {show: true}, dataView : {show: true, readOnly: false}, restore : {show: true}, saveAsImage : {show: true} } }, legend: { data : ['展现','点击','访问','咨询','订单'] }, calculable : true, series : [ { name:'漏斗图', type:'funnel', width: '40%', data:[ {value:60, name:'访问'}, {value:40, name:'咨询'}, {value:20, name:'订单'}, {value:80, name:'点击'}, {value:100, name:'展现'} ] }, { name:'金字塔', type:'funnel', x : '50%', sort : 'ascending', itemStyle: { normal: { // color: 各异, label: { position: 'left' } } }, data:[ {value:60, name:'访问'}, {value:40, name:'咨询'}, {value:20, name:'订单'}, {value:80, name:'点击'}, {value:100, name:'展现'} ] } ] }; myChart1.setOption(option); } );
type一定要是funnel,且require.config内必须要加载funnel相关的库,否则漏斗图将展示不出来的。
这样完美的漏斗图就此展现出来了,上张美图吧!
注意:如此图没有出现,则可能引入的包有问题,需要检查,再者zrender包在某种情况下最好引入,为避免不必要的麻烦也是必不可少的。
完整示例代码:
<!DOCTYPE html> <html> <head> <title>ECharts-如何快速构建漏斗图示例-STEP DAY</title> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="this is my page"> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <!--<link rel="stylesheet" type="text/css" href="./styles.css">--> <script src="../js/echarts/esl.js" type="text/javascript"></script> </head> <body> <div id="funnel_a" style="height: 400px; width: 800px; border: 1px solid #ccc; padding: 10px;"> </div> <script type="text/javascript"> // Step:3 conifg ECharts's path, link to echarts.js from current page. // Step:3 为模块加载器配置echarts的路径,从当前页面链接到echarts.js,定义所需图表路径 require.config({ /* paths: { echarts: '../js/echarts', //echarts.js所在的路径 'echarts/chart/funnel': '../js/echarts' } */ packages:[{ name:'echarts', location:'../js/echarts', main:'echarts' },{ name:'zrender', location:'../js/zrender',//zrender与echarts在同一级目录 main:'zrender' } ] }); // Step:4 require echarts and use it in the callback. // Step:4 动态加载echarts然后在回调函数中开始使用,注意保持按需加载结构定义图表路径 require( [ 'echarts', 'echarts/chart/bar', 'echarts/chart/map', 'echarts/chart/funnel' ], function (ec) {//渲染ECharts图表 ,可单独写出来作为回调函数 var myChart1 = ec.init(document.getElementById('funnel_a')); //加载图表 option = { title : { text: '漏斗图', subtext: '纯属虚构' }, tooltip : { trigger: 'item', formatter: "{a} <br/>{b} : {c}%" }, toolbox: { show : true, feature : { mark : {show: true}, dataView : {show: true, readOnly: false}, restore : {show: true}, saveAsImage : {show: true} } }, legend: { data : ['展现','点击','访问','咨询','订单'] }, calculable : true, series : [ { name:'漏斗图', type:'funnel', width: '40%', data:[ {value:60, name:'访问'}, {value:40, name:'咨询'}, {value:20, name:'订单'}, {value:80, name:'点击'}, {value:100, name:'展现'} ] }, { name:'金字塔', type:'funnel', x : '50%', sort : 'ascending', itemStyle: { normal: { // color: 各异, label: { position: 'left' } } }, data:[ {value:60, name:'访问'}, {value:40, name:'咨询'}, {value:20, name:'订单'}, {value:80, name:'点击'}, {value:100, name:'展现'} ] } ] }; myChart1.setOption(option); } ); </script> </body> </html>
所有评论(0)