weather-line-chart.vue 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. <template>
  2. <div class="chart" :id="id"></div>
  3. </template>
  4. <script>
  5. import util from "@/helper/util.js";
  6. import partten from "@/helper/partten.js";
  7. import * as echarts from "echarts";
  8. export default {
  9. name: "weather-line-chart",
  10. componentName: "weather-line-char",
  11. props: {
  12. width: {
  13. type: String,
  14. default: "100%",
  15. },
  16. height: {
  17. type: String,
  18. default: "270px",
  19. },
  20. list: {
  21. type: Array,
  22. default: [],
  23. },
  24. },
  25. data() {
  26. return {
  27. id: "",
  28. chart: null,
  29. };
  30. },
  31. methods: {
  32. initChart() {
  33. let that = this;
  34. const chart = echarts.init(this.$el);
  35. const xData = [];
  36. const fsData = [];
  37. this.list.forEach((element) => {
  38. xData.push(new Date(element.time).formatDate("hh时"));
  39. fsData.push(element.fs + 5);
  40. });
  41. let option = {
  42. grid: {
  43. top: 50,
  44. bottom: 50,
  45. left: 20,
  46. },
  47. tooltip: {
  48. trigger: "axis",
  49. formatter: function (params) {
  50. const data = params[0]["data"];
  51. new Date(data.time).formatDate("yyyy-MM-dd hh时");
  52. return [
  53. new Date(data.time).formatDate("yyyy-MM-dd hh时"),
  54. "天气:" + data.tqmc + "",
  55. "风速:" + data.fs + " m/s",
  56. "风向:" + data.fx + " 度",
  57. "气温:" + data.wd + " ℃",
  58. "大气压强:" + data.dqyl + " pa",
  59. "湿度:" + data.sd + " %",
  60. ].join("<br>");
  61. },
  62. },
  63. xAxis: {
  64. type: "category",
  65. data: xData,
  66. },
  67. yAxis: [
  68. {
  69. axisLine: { show: false },
  70. axisTick: { show: false },
  71. axisLabel: { show: false },
  72. splitLine: { show: false },
  73. },
  74. {
  75. type: "value",
  76. // max: 25,
  77. axisLine: { show: false },
  78. axisTick: { show: false },
  79. axisLabel: { show: false },
  80. splitLine: { show: false },
  81. },
  82. ],
  83. series: [
  84. {
  85. type: "custom",
  86. renderItem: function (param, api) {
  87. const spiltSize = 2;
  88. if (param.dataIndex % spiltSize == 0 && that.list) {
  89. const xx = param.coordSys.width / ((that.list.length / spiltSize) | 1);
  90. const x = param.coordSys.x + xx * (param.dataIndex / spiltSize) * 1.05;
  91. const y = param.coordSys.y - 30;
  92. const image = require("@assets/icon/svg/weather/" + that.list[param.dataIndex]["tqtp"] + ".png");
  93. return {
  94. type: "group",
  95. children: [
  96. {
  97. type: "image",
  98. style: {
  99. image: image,
  100. x: -5,
  101. y: 0,
  102. width: 30,
  103. height: 30,
  104. },
  105. position: [x, y],
  106. },
  107. ],
  108. };
  109. }
  110. },
  111. data: this.list,
  112. yAxisIndex: 0,
  113. z: 11,
  114. },
  115. {
  116. type: "custom",
  117. renderItem: function (param, api) {
  118. const spiltSize = 2;
  119. if (param.dataIndex % spiltSize == 0 && that.list) {
  120. const xx = param.coordSys.width / ((that.list.length / spiltSize) | 1);
  121. const x = param.coordSys.x + 8 + xx * (param.dataIndex / spiltSize) * 1.05;
  122. const y = param.coordSys.y;
  123. return {
  124. type: "path",
  125. shape: {
  126. pathData: "M31 16l-15-15v9h-26v12h26v9z",
  127. x: -15 / 2,
  128. y: -15 / 2,
  129. width: 15,
  130. height: 15,
  131. },
  132. rotation: (Math.PI / 180) * (that.list[param.dataIndex]["fx"] + 90),
  133. // rotation: (Math.PI / 180) * 45,
  134. position: [x, 200],
  135. style: api.style({
  136. stroke: "#555",
  137. lineWidth: 1,
  138. }),
  139. };
  140. }
  141. },
  142. data: that.list,
  143. z: 10,
  144. },
  145. {
  146. type: "line",
  147. lineStyle: { normal: { color: "#303f6e" } },
  148. data: fsData,
  149. z: 1,
  150. },
  151. ],
  152. };
  153. chart.clear();
  154. chart.setOption(option);
  155. this.resize = function () {
  156. chart.resize();
  157. };
  158. window.addEventListener("resize", this.resize);
  159. },
  160. },
  161. created() {
  162. this.id = "pie-chart-" + util.newGUID();
  163. },
  164. mounted() {
  165. this.$nextTick(() => {
  166. setTimeout(() => {
  167. this.$el.style.width = this.width;
  168. this.$el.style.height = this.height;
  169. this.initChart();
  170. }, 500);
  171. });
  172. },
  173. updated() {
  174. this.$nextTick(() => {
  175. setTimeout(() => {
  176. this.$el.style.width = this.width;
  177. this.$el.style.height = this.height;
  178. this.initChart();
  179. }, 1000);
  180. });
  181. },
  182. unmounted() {
  183. window.removeEventListener("resize", this.resize);
  184. },
  185. };
  186. </script>
  187. <style lang="less">
  188. .chart {
  189. width: 100%;
  190. height: 100%;
  191. display: inline-block;
  192. }
  193. </style>