当前位置:   article > 正文

echats实现天气预报+折线(转载)_echarts天气预报

echarts天气预报

实现方法有两种:1.echarts 2.canvas绘制折线。

此处介绍echarts绘制方法:

  1. var option = {
  2. grid: {
  3. show: true,
  4. backgroundColor: 'transparent',
  5. opacity: 0.3,
  6. borderWidth: '0',
  7. top: '180',
  8. bottom: '0'
  9. },
  10. tooltip: {
  11. trigger: 'axis'
  12. },
  13. legend: {
  14. show: false
  15. },
  16. xAxis: [
  17. // 日期
  18. {
  19. type: 'category',
  20. boundaryGap: false,
  21. position: 'top',
  22. offset: 130,
  23. zlevel: 100,
  24. axisLine: {
  25. show: false
  26. },
  27. axisTick: {
  28. show: false
  29. },
  30. axisLabel: {
  31. interval: 0,
  32. formatter: [
  33. '{a|{value}}'
  34. ].join('\n'),
  35. rich: {
  36. a: {
  37. // color: 'white',
  38. fontSize: 18
  39. }
  40. }
  41. },
  42. nameTextStyle: {
  43. },
  44. data: ["25日","26日","27日","28日","29日","30日","31日"]
  45. },
  46. // 星期
  47. {
  48. type: 'category',
  49. boundaryGap: false,
  50. position: 'top',
  51. offset: 110,
  52. zlevel: 100,
  53. axisLine: {
  54. show: false
  55. },
  56. axisTick: {
  57. show: false
  58. },
  59. axisLabel: {
  60. interval: 0,
  61. formatter: [
  62. '{a|{value}}'
  63. ].join('\n'),
  64. rich: {
  65. a: {
  66. // color: 'white',
  67. fontSize: 14
  68. }
  69. }
  70. },
  71. nameTextStyle: {
  72. fontWeight: 'bold',
  73. fontSize: 19
  74. },
  75. data: ["周一","周二","周三","周四","周五","周六","周日"]
  76. },
  77. // 天气图标
  78. {
  79. type: 'category',
  80. boundaryGap: false,
  81. position: 'top',
  82. offset: 20,
  83. zlevel: 100,
  84. axisLine: {
  85. show: false
  86. },
  87. axisTick: {
  88. show: false
  89. },
  90. axisLabel: {
  91. interval: 0,
  92. formatter: function(value, index) {
  93. return '{' + index + '| }\n{b|' + value + '}'
  94. },
  95. rich: {
  96. 0: {
  97. backgroundColor: {
  98. // image: require('@/assets/weather_icon/' + this.weatherIconDic[this.weatherdata.weather[0]] + '.png')
  99. image: 'https://d.scggqx.com/forecast/img/小雨.png'
  100. },
  101. height: 40,
  102. width: 40
  103. },
  104. 1: {
  105. backgroundColor: {
  106. // image: require('@/assets/weather_icon/' + this.weatherIconDic[this.weatherdata.weather[1]] + '.png')
  107. image: 'https://d.scggqx.com/forecast/img/小雨.png'
  108. },
  109. height: 40,
  110. width: 40
  111. },
  112. 2: {
  113. backgroundColor: {
  114. // image: require('@/assets/weather_icon/' + this.weatherIconDic[this.weatherdata.weather[2]] + '.png')
  115. image: 'https://d.scggqx.com/forecast/img/阴.png'
  116. },
  117. height: 40,
  118. width: 40
  119. },
  120. 3: {
  121. backgroundColor: {
  122. // image: require('@/assets/weather_icon/' + this.weatherIconDic[this.weatherdata.weather[3]] + '.png')
  123. image: 'https://d.scggqx.com/forecast/img/小雨.png'
  124. },
  125. height: 40,
  126. width: 40
  127. },
  128. 4: {
  129. backgroundColor: {
  130. // image: require('@/assets/weather_icon/' + this.weatherIconDic[this.weatherdata.weather[4]] + '.png')
  131. image: 'https://d.scggqx.com/forecast/img/多云.png'
  132. },
  133. height: 40,
  134. width: 40
  135. },
  136. 5: {
  137. backgroundColor: {
  138. // image: require('@/assets/weather_icon/' + this.weatherIconDic[this.weatherdata.weather[5]] + '.png')
  139. image: 'https://d.scggqx.com/forecast/img/小雨.png'
  140. },
  141. height: 40,
  142. width: 40
  143. },
  144. 6: {
  145. backgroundColor: {
  146. // image: require('@/assets/weather_icon/' + this.weatherIconDic[this.weatherdata.weather[6]] + '.png')
  147. image: 'https://d.scggqx.com/forecast/img/小雨.png'
  148. },
  149. height: 40,
  150. width: 40
  151. },
  152. b: {
  153. // color: 'white',
  154. fontSize: 12,
  155. lineHeight: 30,
  156. height: 20
  157. }
  158. }
  159. },
  160. nameTextStyle: {
  161. fontWeight: 'bold',
  162. fontSize: 19
  163. },
  164. // data: this.weatherdata.weather
  165. data: ["小雨","小雨","阴","小雨","多云","小雨","小雨"]
  166. }
  167. ],
  168. yAxis: {
  169. type: 'value',
  170. show: false,
  171. axisLabel: {
  172. formatter: '{value} °C',
  173. color: 'white'
  174. }
  175. },
  176. series: [
  177. {
  178. name: '最高气温',
  179. type: 'line',
  180. data: ["16.3","16.2","17.6","14.2","17.6","15.7","14.3"],
  181. symbol: 'emptyCircle',
  182. symbolSize: 10,
  183. showSymbol: true,
  184. smooth: true,
  185. itemStyle: {
  186. normal: {
  187. color: '#C95843'
  188. }
  189. },
  190. label: {
  191. show: true,
  192. position: 'top',
  193. // color: 'white',
  194. formatter: '{c} °C'
  195. },
  196. lineStyle: {
  197. width: 1,
  198. // color: 'white'
  199. },
  200. areaStyle: {
  201. opacity: 1,
  202. color: 'transparent'
  203. }
  204. },
  205. {
  206. name: '最低气温',
  207. type: 'line',
  208. data: ["13.4","12.8","13.5","12.5","12.4","13.2","13"],
  209. symbol: 'emptyCircle',
  210. symbolSize: 10,
  211. showSymbol: true,
  212. smooth: true,
  213. itemStyle: {
  214. normal: {
  215. color: 'blue'
  216. }
  217. },
  218. label: {
  219. show: true,
  220. position: 'bottom',
  221. // color: 'white',
  222. formatter: '{c} °C'
  223. },
  224. lineStyle: {
  225. width: 1,
  226. // color: 'white'
  227. },
  228. areaStyle: {
  229. opacity: 1,
  230. color: 'transparent'
  231. }
  232. }
  233. ]
  234. }

上面的代码,最难的部分就是天气图标的设置,由于axisLabel的formatter方法中的value值没法在富文本中使用,所以这里在formatter方法中将value的下标设置成了富文本中的css名,然后在设置天气图标时使用下标获取需要显示的图标名称。

  1. // axisLabel的formatter方法
  2. formatter: function(value, index) {
  3. return '{' + index + '| }\n{b|' + value + '}'
  4. }
  5. // axisLabel的rich方法
  6. rich: {
  7. index: {
  8. backgroundColor: {
  9. image: require('@/assets/weather_icon/' + this.weatherIconDic[this.weatherdata.weather[index]] + '.png')
  10. },
  11. height: 40,
  12. width: 40
  13. }
  14. }

1、this.weatherIconDic是我本地定义的一个天气图标的数据字典。(如:{ ‘晴': ‘a00', ‘多云': ‘a01', ‘阴': ‘a02', ‘阵雨': ‘a03', ‘雷阵雨': ‘a04', ‘冰雹': ‘a05', ‘雨夹雪': ‘a06', ‘小雨': ‘a07', ‘中雨': ‘a08', ‘大雨': ‘a09', ‘暴雨': ‘a10', ‘大暴雨': ‘a11', ‘特大暴雨': ‘a12', ‘阵雪': ‘a13', ‘小雪': ‘a14', ‘中雪': ‘a15', ‘大雪': ‘a16', ‘暴雪': ‘a17', ‘雾': ‘a18', ‘冻雨': ‘a19', ‘沙尘暴': ‘a20', ‘小到中雨': ‘a21', ‘中雨-大雨': ‘a22', ‘大雨-暴雨': ‘a23', ‘暴雨-大暴雨': ‘a24', ‘大暴雨-特大暴雨': ‘a25', ‘小雪-中雪': ‘a26', ‘中雪-大雪': ‘a27', ‘大雪-暴雪': ‘a28', ‘浮尘': ‘a29', ‘扬沙': ‘a30', ‘强沙尘暴': ‘a31' })

2、this.weatherdata.weather是后端传回来的天气类型。(如:[“小雨”,“小雨”,“阴”,“小雨”,“多云”,“小雨”,“小雨”])

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