使用echarts画图表

项目需要在网页上画图表,在网上找了一下,发现百度出品的echarts非常好用。echarts把看似复杂的图表进行了分解,使用结构化的模型来完成图表的配置。画图表就跟写CSS一样,实际上echarts使用的很多属性名字都和CSS是一样的。

echarts有非常详细的文档,官网还提供了很多个例子,很容易学会。

echarts是支持IE8的,虽然IE8不支持canvas,但是echarts画出的图表在IE8显示,目前没有发现有问题。

想学echarts,看官网的文档就可以了。以下是个人学习之后整理的helloworld小例子,以及学习过程中碰到的一些小问题。

1 画一个最粗糙的图表

官方推荐使用模块化单文件引入,我不知道模块化,所以采用了标签式单文件引入。

[html]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. <!DOCTYPE html>  
  2. <html>  
  3. <head>  
  4. <meta charset="UTF-8">  
  5. <title>echarts测试</title>  
  6. </head>  
  7. <body>  
  8.     <!-- 第二步:为ECharts准备一个具备大小(宽高)的Dom -->  
  9.     <div id="main"  
  10.         style="height: 400px; width: 800px; border: 1px dotted black"></div>  
  11.           
  12.     <!-- 第一步:标签式单文件引入。引入之后可以直接使用echarts对象。 -->  
  13.     <script src="../js/echarts-plain-original.js"></script>  
  14.       
  15.     <!-- 注意顺序,使用echarts对象要在引入echarts文件之后 -->  
  16.     <script type="text/javascript">  
  17.         // 初始化一个图表实例  
  18.         var myChart = echarts.init(document.getElementById('main'));  
  19.   
  20.         // echarts把复杂的图表结构化,图表的基本结构包括以下部分:标题,x轴,y轴,数值序列。  
  21.         var option = {  
  22.             // 标题  
  23.             title : {  
  24.                 text : '销量和进货量'  
  25.             },  
  26.             // x轴  
  27.             xAxis : [ {  
  28.                 type : 'category',  
  29.                 data : [ "衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋", "袜子" ]  
  30.             } ],  
  31.             // y轴  
  32.             yAxis : [ {  
  33.                 type : 'value',  
  34.                 axisLabel : {  
  35.                     formatter : '{value}件'  
  36.                 }  
  37.             } ],  
  38.             // 数值序列  
  39.             series : [ {  
  40.                 name : '销量',  
  41.                 type : 'line',  
  42.                 data : [ 5, 20, 40, 10, 10, 20 ],  
  43.             } ]  
  44.         };  
  45.   
  46.         // 为图表实例加载数据  
  47.         myChart.setOption(option);  
  48.     </script>  
  49. </body>  
  50. </html>  

得到的图表


2 为图表添加各种样式

[html]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. <!DOCTYPE html>  
  2. <html>  
  3. <head>  
  4. <meta charset="UTF-8">  
  5. <title>echarts测试</title>  
  6. </head>  
  7. <body>  
  8.     <!-- 第二步:为ECharts准备一个具备大小(宽高)的Dom -->  
  9.     <div id="main"  
  10.         style="height: 400px; width: 800px; border: 1px dotted black"></div>  
  11.   
  12.     <!-- 第一步:标签式单文件引入。引入之后可以直接使用echarts对象。 -->  
  13.     <script src="../js/echarts-plain-original.js"></script>  
  14.   
  15.     <!-- 注意顺序,使用echarts对象要在引入echarts文件之后 -->  
  16.     <script type="text/javascript">  
  17.         // 初始化一个图表实例  
  18.         var myChart = echarts.init(document.getElementById('main'));  
  19.   
  20.         // echarts把复杂的图表结构化,图表的基本结构包括以下部分:标题,x轴,y轴,数值序列。  
  21.         var option = {  
  22.             // 标题  
  23.             title : {  
  24.                 text : '销量和进货量',  
  25.                 x : 'center',  
  26.                 y : 'top',  
  27.                 textStyle : {  
  28.                     fontSize : 26,  
  29.                     fontFamily : "微软雅黑",  
  30.                 }  
  31.             },  
  32.             //   
  33.             grid : {  
  34.                 borderWidth : 0  
  35.             },  
  36.             tooltip : {  
  37.                 trigger : 'axis',  
  38.                 axisPointer : {  
  39.                     type : 'line',  
  40.                     lineStyle : {  
  41.                         color : '#0f0',  
  42.                         width : 2,  
  43.                         type : 'solid'  
  44.                     }  
  45.                 }  
  46.             },  
  47.             // x轴  
  48.             xAxis : [ {  
  49.                 type : 'category',  
  50.                 data : [ "衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋", "袜子" ],  
  51.                 axisTick : {  
  52.                     show : false  
  53.                 },  
  54.                 splitLine : {  
  55.                     lineStyle : {  
  56.                         type : 'dotted'  
  57.                     }  
  58.                 }  
  59.             } ],  
  60.             // y轴  
  61.             yAxis : [ {  
  62.                 type : 'value',  
  63.                 axisLabel : {  
  64.                     formatter : '{value}件'  
  65.                 },  
  66.                 splitLine : {  
  67.                     lineStyle : {  
  68.                         type : 'dotted'  
  69.                     }  
  70.                 }  
  71.             } ],  
  72.             // 数值序列  
  73.             series : [ {  
  74.                 name : '销量',  
  75.                 type : 'line',  
  76.                 data : [ 5, 20, 40, 10, 10, 20 ],  
  77.                 itemStyle : {  
  78.                     normal : {  
  79.                         color : '#f00',  
  80.                         lineStyle : {  
  81.                             width : 2  
  82.                         },  
  83.                         label : {  
  84.                             show : true,  
  85.                             position : 'right',  
  86.                             textStyle : {  
  87.                                 fontStyle : 'bolder',  
  88.                                 fontSize : 15  
  89.                             }  
  90.                         }  
  91.                     }  
  92.   
  93.                 },  
  94.                 symbol : 'emptyCircle',  
  95.                 symbolSize : 4,  
  96.             } ]  
  97.         };  
  98.   
  99.         // 为图表实例加载数据  
  100.         myChart.setOption(option);  
  101.     </script>  
  102. </body>  
  103. </html>  

加了样式之后的结果如下,我想说我很佩服做美工的同学(我自己画的图表真不好看)



3 为图表添加事件

[html]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. // 事件的参数中包括:数据在序列中的下标dataIndex,数据的值value,x轴上的名称name  
  2. function eConsole(param) {  
  3.     if (typeof param.seriesIndex == 'undefined') {  
  4.         return;  
  5.     }  
  6.     if (param.type == 'click') {  
  7.         var mes = param.name + ':' + param.value;  
  8.         document.getElementById('info').innerHTML = mes;  
  9.     }  
  10. }  
  11. myChart.on(echarts.config.EVENT.CLICK, eConsole);  
效果就是点击图表之后,可以在下面显示出被点击的类目和对应的数量

4 最后,做了一个小练习,使用jQuery来从服务端请求数据

这里,客户端的option一开始只有样式,没有数据。从服务端得到数据之后,把option补全,然后调用setOption方法就OK了。

[html]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. <!DOCTYPE html>  
  2. <html>  
  3. <head>  
  4. <meta charset="UTF-8">  
  5. <script type="text/javascript" src="../js/jquery-1.11.1.js"></script>  
  6. <title>通过ajax为echarts更新数据</title>  
  7. </head>  
  8. <body>  
  9.     <!-- 第二步:为ECharts准备一个具备大小(宽高)的Dom -->  
  10.     <div id="main"  
  11.         style="height: 400px; width: 800px; border: 1px dotted black"></div>  
  12.   
  13.     <span id="info"></span>  
  14.   
  15.     <!-- 第一步:标签式单文件引入。引入之后可以直接使用echarts对象。 -->  
  16.     <script src="../js/echarts-plain-original.js"></script>  
  17.   
  18.     <!-- 注意顺序,使用echarts对象要在引入echarts文件之后 -->  
  19.     <script type="text/javascript">  
  20.         $(document).ready(function() {  
  21.             // 初始化一个图表实例  
  22.             var myChart = echarts.init(document.getElementById('main'));  
  23.   
  24.             // 使用jquery发送post请求,第四个参数指明如何解析服务端返回的数据。  
  25.             // 服务端返回的必须是标准格式的json,也就是双引号括起来的键值对  
  26.             $.post("../data", {  
  27.                 name : "testdata"  
  28.             }, function(data) {  
  29.                 option.title.text = data.title;  
  30.                 option.xAxis[0].data = data.category;  
  31.                 option.series[0].data = data.value;  
  32.                 // 取得数据后显示到图表中  
  33.                 myChart.setOption(option);  
  34.                 myChart.on(echarts.config.EVENT.CLICK, eConsole);  
  35.             }, 'json');  
  36.   
  37.             // echarts把复杂的图表结构化,图表的基本结构包括以下部分:标题,x轴,y轴,数值序列。  
  38.             var option = {  
  39.                 // 标题  
  40.                 title : {  
  41.                     x : 'center',  
  42.                     y : 'top',  
  43.                     textStyle : {  
  44.                         fontSize : 26,  
  45.                         fontFamily : "微软雅黑",  
  46.                     }  
  47.                 },  
  48.                 //   
  49.                 grid : {  
  50.                     borderWidth : 0  
  51.                 },  
  52.                 tooltip : {  
  53.                     trigger : 'axis',  
  54.                     axisPointer : {  
  55.                         type : 'line',  
  56.                         lineStyle : {  
  57.                             color : '#0f0',  
  58.                             width : 2,  
  59.                             type : 'solid'  
  60.                         }  
  61.                     }  
  62.                 },  
  63.                 // x轴  
  64.                 xAxis : [ {  
  65.                     type : 'category',  
  66.                     axisTick : {  
  67.                         show : false  
  68.                     },  
  69.                     splitLine : {  
  70.                         lineStyle : {  
  71.                             type : 'dotted'  
  72.                         }  
  73.                     }  
  74.                 } ],  
  75.                 // y轴  
  76.                 yAxis : [ {  
  77.                     type : 'value',  
  78.                     axisLabel : {  
  79.                         formatter : '{value}件'  
  80.                     },  
  81.                     splitLine : {  
  82.                         lineStyle : {  
  83.                             type : 'dotted'  
  84.                         }  
  85.                     }  
  86.                 } ],  
  87.                 // 数值序列  
  88.                 series : [ {  
  89.                     name : '销量',  
  90.                     type : 'line',  
  91.                     itemStyle : {  
  92.                         normal : {  
  93.                             color : '#f00',  
  94.                             lineStyle : {  
  95.                                 width : 2  
  96.                             },  
  97.                             label : {  
  98.                                 show : true,  
  99.                                 position : 'right',  
  100.                                 textStyle : {  
  101.                                     fontStyle : 'bolder',  
  102.                                     fontSize : 15  
  103.                                 }  
  104.                             }  
  105.                         }  
  106.                     },  
  107.                     symbol : 'emptyCircle',  
  108.                     symbolSize : 4,  
  109.                 } ]  
  110.             };  
  111.   
  112.             // 事件的参数中包括:数据在序列中的下标dataIndex,数据的值value,x轴上的名称name  
  113.             function eConsole(param) {  
  114.                 if (typeof param.seriesIndex == 'undefined') {  
  115.                     return;  
  116.                 }  
  117.                 if (param.type == 'click') {  
  118.                     var mes = param.name + ':' + param.value;  
  119.                     document.getElementById('info').innerHTML = mes;  
  120.                 }  
  121.             }  
  122.   
  123.         });  
  124.     </script>  
  125. </body>  
  126. </html>  
服务端的servlet如下,这种小东西还写的不熟练:获取文件路径,设置UTF-8编码,关闭流等操作还不够轻车熟路

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. protected void doPost(HttpServletRequest request,  
  2.         HttpServletResponse response)  
  3.     throws ServletException, IOException  
  4. {  
  5.     response.setCharacterEncoding("UTF-8");  
  6.     String name = request.getParameter("name");  
  7.     String fullPath = this.getServletContext().getRealPath("/") + "data"  
  8.             + File.separator + name;  
  9.     PrintWriter out = null;  
  10.     BufferedReader br = null;  
  11.     try  
  12.     {  
  13.         out = response.getWriter();  
  14.         br = new BufferedReader(new FileReader(fullPath));  
  15.         String str = null;  
  16.         while ((str = br.readLine()) != null)  
  17.         {  
  18.             out.print(str);  
  19.         }  
  20.         out.flush();  
  21.     }  
  22.     finally  
  23.     {  
  24.         if (br != null)  
  25.         {  
  26.             br.close();  
  27.         }  
  28.         // 即使自己不关闭,tomcat等容器也会关闭  
  29.         if (out != null)  
  30.         {  
  31.             out.close();  
  32.         }  
  33.     }  
  34. }  
服务端返回的数据一定要是标准格式的json。标准是指用双引号,不用单引号。另外,最后一个键值对后面不要有逗号,IE8中不允许JavaScript代码中的json采用这种不规范的写法,FireFox容许。

[javascript]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. {  
  2.  "title" : "销量和进货量",  
  3.  "category" : [ "衬衫""羊毛衫""雪纺衫""裤子""高跟鞋""袜子" ],  
  4.  "value" : [ 5, 20, 40, 10, 10, 20 ]  
  5. }  
如果在图表已经加载过数据之后,想要换一组数据,可以用getOption方法得到一份option的深拷贝,然后把其中的数据改掉,再调用setOption方法就可以:
[html]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. var newOption = myChart.getOption(); // 深拷贝  
  2. newOption.xAxis[0].data = newData.category;  
  3. newOption.series[0].data = newData.value;  
  4. myChart.setOption(newOption,true); // 第二个参数表示不和原先的option合并  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值