当前位置:   article > 正文

Echart在Openlayers的应用

openresty建站 echart图表

概述

echart,一个由百度前端发起的canvas国产类库(官网:http://echarts.baidu.com/index.html)。echart其实是在canvas类库zrender的基础上做的主题图库,优点有数据驱动,图例丰富,功能强大,支持数据拖拽重计算,数据区域漫游等功能,不论是从使用便捷程度、美观程度等方面都是不错的选择,在本文讲述如何在Openlayers中结合echart实现地图统计图的展示。


效果



实现代码

1、piechart

  1. <!DOCTYPE html>
  2. <html>
  3. <head lang="en">
  4. <meta charset="UTF-8">
  5. <title>openlayers map</title>
  6. <link rel="stylesheet" href="../../../plugin/OpenLayers-2.13.1/theme/default/style.css" type="text/css">
  7. <style>
  8. html, body, #map{
  9. padding:0;
  10. margin:0;
  11. height:100%;
  12. width:100%;
  13. overflow: hidden;
  14. }
  15. .tool{
  16. position: absolute;
  17. top:10pt;
  18. right: 10pt;
  19. padding: 5px;
  20. background: #fff;
  21. border: 1px solid #ff5500;
  22. z-index: 1000;
  23. }
  24. </style>
  25. <script src="../../../plugin/OpenLayers-2.13.1/OpenLayers.js"></script>
  26. <script src="../../../plugin/jquery/jquery-1.8.3.js"></script>
  27. <script src="../../../plugin/echart/build/dist/echarts.js"></script>
  28. <script>
  29. require.config({
  30. paths: {
  31. echarts: '../../../plugin/echart/build/dist'
  32. }
  33. });
  34. var data = [{name:"乌鲁木齐",x:87.5758285931,y:43.7822116460,data:[
  35. {
  36. name: '男',
  37. value: 40.0
  38. },{
  39. name: '女',
  40. value: 60.0
  41. }
  42. ]},
  43. {name:"拉萨",x:91.1629975040,y:29.7104204643,data:[
  44. {
  45. name: '男',
  46. value: 45.0
  47. },{
  48. name: '女',
  49. value: 55.0
  50. }
  51. ]},
  52. {name:"北京",x:116.4575803581078,y:40.04054437977018,data:[
  53. {
  54. name: '男',
  55. value: 35.0
  56. },{
  57. name: '女',
  58. value: 65.0
  59. }
  60. ]},
  61. {name:"兰州",x:103.584297498,y:36.1190864503,data:[
  62. {
  63. name: '男',
  64. value: 44.0
  65. },{
  66. name: '女',
  67. value: 56.0
  68. }
  69. ]}];
  70. var map;
  71. var tiled;
  72. OpenLayers.IMAGE_RELOAD_ATTEMPTS = 5;
  73. OpenLayers.DOTS_PER_INCH = 25.4 / 0.28;
  74. $(window).load(function() {
  75. var format = 'image/png';
  76. var bounds = new OpenLayers.Bounds(
  77. 73.45100463562233, 18.16324718764174,
  78. 134.97679764650596, 53.531943152223576
  79. );
  80. var options = {
  81. controls: [],
  82. maxExtent: bounds,
  83. maxResolution: 0.2403351289487642,
  84. projection: "EPSG:4326",
  85. units: 'degrees'
  86. };
  87. map = new OpenLayers.Map('map', options);
  88. var url = "http://localhost:8088/geoserver/lzugis/wms";
  89. tiled = new OpenLayers.Layer.WMS(
  90. "Geoserver layers - Tiled",
  91. url,
  92. {
  93. "LAYERS": 'lzugis:province',
  94. "STYLES": '',
  95. format: format
  96. },
  97. {
  98. buffer: 0,
  99. displayOutsideMaxExtent: true,
  100. isBaseLayer: true,
  101. yx : {'EPSG:4326' : true}
  102. }
  103. );
  104. map.addLayers([tiled]);
  105. map.addControl(new OpenLayers.Control.Zoom());
  106. map.addControl(new OpenLayers.Control.Navigation());
  107. map.zoomToExtent(bounds);
  108. $("#addchart").on("click",function(){
  109. addMapChart();
  110. map.events.register("zoomend", map, function(){
  111. addMapChart();
  112. });
  113. });
  114. });
  115. function addMapChart(){
  116. $(".olPopup").remove();
  117. var zoom = map.getZoom();
  118. for(var i=0;i<data.length;i++){
  119. var d = data[i];
  120. var size=30+(zoom-1)*10;
  121. var domid = "chart"+i;
  122. var content = "<div class='mapChart' id='"+domid+"' ></div>";
  123. var popup = new OpenLayers.Popup(domid,
  124. new OpenLayers.LonLat(d.x,d.y),
  125. new OpenLayers.Size(size,size),
  126. content,
  127. false);
  128. popup.setBackgroundColor("transparent");
  129. popup.setBorder("0px #0066ff solid");
  130. popup.keepInMap = false;
  131. map.addPopup(popup,false);
  132. addChart(domid,d);
  133. }
  134. var pops = $(map.div).find(".olPopup");
  135. for(var i=0;i<pops.length;i++){
  136. var pop = $(pops[i]);
  137. var top = pop.position().top,
  138. left = pop.position().left;
  139. pop.css("top",(top-size/2)+"px").css("left",(left-size/2)+"px");
  140. }
  141. }
  142. function addChart(domid,data){
  143. require(
  144. [
  145. 'echarts',
  146. 'echarts/chart/pie'
  147. ],
  148. function (ec) {
  149. // 基于准备好的dom,初始化echarts图表
  150. var myChart = ec.init(document.getElementById(domid));
  151. var option = {
  152. // renderAsImage:true,
  153. animation:true,
  154. tooltip : {
  155. trigger: 'item',
  156. formatter: "{b}:{c}"
  157. },
  158. series : [
  159. {
  160. type:'pie',
  161. radius : '100%',
  162. center: ['50%', '50%'],
  163. itemStyle:{
  164. normal: {
  165. labelLine:{show: false}
  166. },
  167. },
  168. data:data.data,
  169. line:false
  170. }
  171. ]
  172. };
  173. // 为echarts对象加载数据
  174. myChart.setOption(option);
  175. }
  176. );
  177. }
  178. </script>
  179. </head>
  180. <body>
  181. <div id="map">
  182. <div class="tool">
  183. <button id="addchart">添加统计图</button>
  184. </div>
  185. </div>
  186. </body>
  187. </html>

2、gaugechart

  1. <!DOCTYPE html>
  2. <html>
  3. <head lang="en">
  4. <meta charset="UTF-8">
  5. <title>openlayers map</title>
  6. <link rel="stylesheet" href="../../../plugin/OpenLayers-2.13.1/theme/default/style.css" type="text/css">
  7. <style>
  8. html, body, #map{
  9. padding:0;
  10. margin:0;
  11. height:100%;
  12. width:100%;
  13. overflow: hidden;
  14. }
  15. .tool{
  16. position: absolute;
  17. top:10pt;
  18. right: 10pt;
  19. padding: 5px;
  20. background: #fff;
  21. border: 1px solid #ff5500;
  22. z-index: 1000;
  23. }
  24. </style>
  25. <script src="../../../plugin/OpenLayers-2.13.1/OpenLayers.js"></script>
  26. <script src="../../../plugin/jquery/jquery-1.8.3.js"></script>
  27. <script src="../../../plugin/echart/build/dist/echarts.js"></script>
  28. <script>
  29. require.config({
  30. paths: {
  31. echarts: '../../../plugin/echart/build/dist'
  32. }
  33. });
  34. var data = [{name:"乌鲁木齐",x:87.5758285931,y:43.7822116460,value:22},
  35. {name:"拉萨",x:91.1629975040,y:29.7104204643,value:-10},
  36. {name:"北京",x:116.4575803581078,y:40.04054437977018,value:32},
  37. {name:"兰州",x:103.584297498,y:36.1190864503,value:25}];
  38. var map;
  39. var tiled;
  40. OpenLayers.IMAGE_RELOAD_ATTEMPTS = 5;
  41. OpenLayers.DOTS_PER_INCH = 25.4 / 0.28;
  42. $(window).load(function() {
  43. var format = 'image/png';
  44. var bounds = new OpenLayers.Bounds(
  45. 73.45100463562233, 18.16324718764174,
  46. 134.97679764650596, 53.531943152223576
  47. );
  48. var options = {
  49. controls: [],
  50. maxExtent: bounds,
  51. maxResolution: 0.2403351289487642,
  52. projection: "EPSG:4326",
  53. units: 'degrees'
  54. };
  55. map = new OpenLayers.Map('map', options);
  56. var url = "http://localhost:8088/geoserver/lzugis/wms";
  57. tiled = new OpenLayers.Layer.WMS(
  58. "Geoserver layers - Tiled",
  59. url,
  60. {
  61. "LAYERS": 'lzugis:province',
  62. "STYLES": '',
  63. format: format
  64. },
  65. {
  66. buffer: 0,
  67. displayOutsideMaxExtent: true,
  68. isBaseLayer: true,
  69. yx : {'EPSG:4326' : true}
  70. }
  71. );
  72. map.addLayers([tiled]);
  73. map.addControl(new OpenLayers.Control.Zoom());
  74. map.addControl(new OpenLayers.Control.Navigation());
  75. map.zoomToExtent(bounds);
  76. $("#addchart").on("click",function(){
  77. addMapChart();
  78. map.events.register("zoomend", map, function(){
  79. addMapChart();
  80. });
  81. });
  82. });
  83. function addMapChart(){
  84. $(".olPopup").remove();
  85. var zoom = map.getZoom();
  86. for(var i=0;i<data.length;i++){
  87. var d = data[i];
  88. var size=60+(zoom-1)*10;
  89. var domid = "chart"+i;
  90. var content = "<div class='mapChart' id='"+domid+"' ></div>";
  91. var popup = new OpenLayers.Popup(domid,
  92. new OpenLayers.LonLat(d.x,d.y),
  93. new OpenLayers.Size(size,size),
  94. content,
  95. false);
  96. popup.setBackgroundColor("transparent");
  97. popup.setBorder("0px #0066ff solid");
  98. popup.keepInMap = false;
  99. map.addPopup(popup,false);
  100. addChart(domid,d);
  101. }
  102. var pops = $(map.div).find(".olPopup");
  103. for(var i=0;i<pops.length;i++){
  104. var pop = $(pops[i]);
  105. var top = pop.position().top,
  106. left = pop.position().left;
  107. pop.css("top",(top-size/2)+"px").css("left",(left-size/2)+"px");
  108. }
  109. }
  110. function addChart(domid,data){
  111. require(
  112. [
  113. 'echarts',
  114. 'echarts/chart/gauge'
  115. ],
  116. function (ec) {
  117. // 基于准备好的dom,初始化echarts图表
  118. var myChart = ec.init(document.getElementById(domid));
  119. var option = {
  120. tooltip : {
  121. formatter: "{c}℃"
  122. },
  123. series : [
  124. {
  125. type:'gauge',
  126. radius:'80%',
  127. min:-20,
  128. max:40,
  129. axisLine: { // 坐标轴线
  130. lineStyle: { // 属性lineStyle控制线条样式
  131. color: [[0.33, '#48b'],[0.8, '#228b22'],[1, '#ff4500']],
  132. width: 4
  133. }
  134. },
  135. axisTick: { // 坐标轴小标记
  136. splitNumber: 2, // 每份split细分多少段
  137. length :12, // 属性length控制线长
  138. lineStyle: { // 属性lineStyle控制线条样式
  139. color: 'auto'
  140. }
  141. },
  142. axisLabel: {
  143. show:false,
  144. interval:'auto',
  145. textStyle: {
  146. color: 'auto',
  147. fontFamily:'微软雅黑',
  148. fontSize: '10'
  149. }
  150. },
  151. splitLine: {
  152. show: true,
  153. length :16,
  154. lineStyle: { // 属性lineStyle控制线条样式
  155. color: 'auto'
  156. }
  157. },
  158. detail : {
  159. formatter:'{value}℃',
  160. textStyle: { // 其余属性默认使用全局文本样式,详见TEXTSTYLE
  161. color: 'auto',
  162. fontWeight: 'bolder',
  163. fontSize: '15'
  164. }
  165. },
  166. data:[{value: data.value}]
  167. }
  168. ]
  169. };
  170. // 为echarts对象加载数据
  171. myChart.setOption(option);
  172. }
  173. );
  174. }
  175. </script>
  176. </head>
  177. <body>
  178. <div id="map">
  179. <div class="tool">
  180. <button id="addchart">添加统计图</button>
  181. </div>
  182. </div>
  183. </body>
  184. </html>

传播GIS知识 | 交流GIS经验 | 分享GIS价值 | 专注GIS发展

技术博客

http://blog.csdn.net/gisshixisheng

在线教程

http://edu.csdn.net/course/detail/799
Github

https://github.com/lzugis/

联系方式

q       q:1004740957

e-mail:niujp08@qq.com

公众号:lzugis15

Q Q 群:452117357(webgis)
             337469080(Android)




转载于:https://www.cnblogs.com/lzugis/p/6539776.html

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家自动化/article/detail/728246
推荐阅读
相关标签
  

闽ICP备14008679号