You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

186 lines
4.4 KiB

  1. //地图容器
  2. var chart = echarts.init(document.getElementById('main'));
  3. //34个省、市、自治区的名字拼音映射数组
  4. var provinces = {
  5. //23个省
  6. "台湾": "taiwan",
  7. "河北": "hebei",
  8. "山西": "shanxi",
  9. "辽宁": "liaoning",
  10. "吉林": "jilin",
  11. "黑龙江": "heilongjiang",
  12. "江苏": "jiangsu",
  13. "浙江": "zhejiang",
  14. "安徽": "anhui",
  15. "福建": "fujian",
  16. "江西": "jiangxi",
  17. "山东": "shandong",
  18. "河南": "henan",
  19. "湖北": "hubei",
  20. "湖南": "hunan",
  21. "广东": "guangdong",
  22. "海南": "hainan",
  23. "四川": "sichuan",
  24. "贵州": "guizhou",
  25. "云南": "yunnan",
  26. "陕西": "shanxi1",
  27. "甘肃": "gansu",
  28. "青海": "qinghai",
  29. //5个自治区
  30. "新疆": "xinjiang",
  31. "广西": "guangxi",
  32. "内蒙古": "neimenggu",
  33. "宁夏": "ningxia",
  34. "西藏": "xizang",
  35. //4个直辖市
  36. "北京": "beijing",
  37. "天津": "tianjin",
  38. "上海": "shanghai",
  39. "重庆": "chongqing",
  40. //2个特别行政区
  41. "香港": "xianggang",
  42. "澳门": "aomen"
  43. };
  44. //直辖市和特别行政区-只有二级地图,没有三级地图
  45. var special = ["北京","天津","上海","重庆","香港","澳门"];
  46. var mapdata = [];
  47. //绘制全国地图
  48. $.getJSON('static/map/china.json', function(data){
  49. d = [];
  50. for( var i=0;i<data.features.length;i++ ){
  51. d.push({
  52. name:data.features[i].properties.name
  53. })
  54. }
  55. mapdata = d;
  56. //注册地图
  57. echarts.registerMap('china', data);
  58. //绘制地图
  59. renderMap('china',d);
  60. });
  61. //地图点击事件
  62. chart.on('click', function (params) {
  63. if( params.name in provinces ){
  64. //如果点击的是34个省、市、自治区,绘制选中地区的二级地图
  65. $.getJSON('static/map/province/'+ provinces[params.name] +'.json', function(data){
  66. echarts.registerMap( params.name, data);
  67. var d = [];
  68. for( var i=0;i<data.features.length;i++ ){
  69. d.push({
  70. name:data.features[i].properties.name
  71. })
  72. }
  73. renderMap(params.name,d);
  74. });
  75. }else if( params.seriesName in provinces ){
  76. //如果是【直辖市/特别行政区】只有二级下钻
  77. if( special.indexOf( params.seriesName ) >=0 ){
  78. renderMap('china',mapdata);
  79. }else{
  80. //显示县级地图
  81. $.getJSON('static/map/city/'+ cityMap[params.name] +'.json', function(data){
  82. echarts.registerMap( params.name, data);
  83. var d = [];
  84. for( var i=0;i<data.features.length;i++ ){
  85. d.push({
  86. name:data.features[i].properties.name
  87. })
  88. }
  89. renderMap(params.name,d);
  90. });
  91. }
  92. }else{
  93. renderMap('china',mapdata);
  94. }
  95. });
  96. //初始化绘制全国地图配置
  97. var option = {
  98. backgroundColor: '#000',
  99. title : {
  100. text: 'Echarts3 中国地图下钻至县级',
  101. subtext: '三级下钻',
  102. link:'http://www.ldsun.com',
  103. left: 'center',
  104. textStyle:{
  105. color: '#fff',
  106. fontSize:16,
  107. fontWeight:'normal',
  108. fontFamily:"Microsoft YaHei"
  109. },
  110. subtextStyle:{
  111. color: '#ccc',
  112. fontSize:13,
  113. fontWeight:'normal',
  114. fontFamily:"Microsoft YaHei"
  115. }
  116. },
  117. tooltip: {
  118. trigger: 'item',
  119. formatter: '{b}'
  120. },
  121. toolbox: {
  122. show: true,
  123. orient: 'vertical',
  124. left: 'right',
  125. top: 'center',
  126. feature: {
  127. dataView: {readOnly: false},
  128. restore: {},
  129. saveAsImage: {}
  130. },
  131. iconStyle:{
  132. normal:{
  133. color:'#fff'
  134. }
  135. }
  136. },
  137. animationDuration:1000,
  138. animationEasing:'cubicOut',
  139. animationDurationUpdate:1000
  140. };
  141. function renderMap(map,data){
  142. option.title.subtext = map;
  143. option.series = [
  144. {
  145. name: map,
  146. type: 'map',
  147. mapType: map,
  148. roam: false,
  149. nameMap:{
  150. 'china':'中国'
  151. },
  152. label: {
  153. normal:{
  154. show:true,
  155. textStyle:{
  156. color:'#999',
  157. fontSize:13
  158. }
  159. },
  160. emphasis: {
  161. show: true,
  162. textStyle:{
  163. color:'#fff',
  164. fontSize:13
  165. }
  166. }
  167. },
  168. itemStyle: {
  169. normal: {
  170. areaColor: '#323c48',
  171. borderColor: 'dodgerblue'
  172. },
  173. emphasis: {
  174. areaColor: 'darkorange'
  175. }
  176. },
  177. data:data
  178. }
  179. ];
  180. //渲染地图
  181. chart.setOption(option);
  182. }