EchartWeather.vue 7.7 KB
Newer Older
yma16's avatar
yma16 已提交
1
<template>
Y
yma16 已提交
2 3
  <div class="weather">
    <div id="echart_weather"></div>
yma16's avatar
yma16 已提交
4
    <div class="weatherDiscribe">
Y
yma16 已提交
5 6 7 8 9 10 11 12
      <!-- 表格 -->
      <el-table :data="tableData" border style="width: 100%">
        <el-table-column prop="date" label="日期"> </el-table-column>
        <el-table-column prop="discrible" label="天气"> </el-table-column>
        <el-table-column prop="temperature" label="温度"> </el-table-column>
        <el-table-column prop="windem" label="风向"> </el-table-column>
        <el-table-column prop="windwl" label="风级"> </el-table-column>
      </el-table>
yma16's avatar
yma16 已提交
13
    </div>
Y
yma16 已提交
14
  </div>
yma16's avatar
yma16 已提交
15 16 17
</template>

<script>
Y
yma16 已提交
18 19
import axios from "axios";
import * as echarts from "echarts";
yma16's avatar
yma16 已提交
20
export default {
Y
yma16 已提交
21 22 23 24
  name: "EchartWeather",
  data() {
    return {
      msg: "贵阳天气",
yma16's avatar
yma16 已提交
25
      baseurl: "/api/",
Y
yma16 已提交
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
      // baseurl: 'http://yongma16.xyz/',
      // baseurl: 'api/article/weather/',
      tabPosition: "left",
      weather_title: [],
      weather_high: [],
      weather_low: [],
      weather_discribe: [],
      weather_windem: [],
      weather_windwl: [],
      tableData: [],
    };
  },
  mounted() {
    this.initWeatherLine();
  },
  methods: {
    initWeatherLine() {
      let that = this;
      async function waitGetWeatherData() {
        await that.getWeatherData();
        await that.weatherInint();
      }
      waitGetWeatherData().then((o) => {
        console.log(o);
      }); // 同步
yma16's avatar
yma16 已提交
51
    },
Y
yma16 已提交
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
    getWeatherData() {
      let that = this;
      return new Promise((resolve) => {
        setTimeout(function () {
          axios
            .get(that.baseurl + "article/weather/")
            .then((res) => {
              console.log("天气接口返回", res);
              let resdata = res.data;
              let weather = resdata.weather;
              let title = weather.title;
              let high = weather.high;
              let low = weather.low;
              let discribe = weather.discribe;
              let windem = weather.windem;
              let windwl = weather.windwl;
              that.tableData = [];
              title.map((item) => {
                that.weather_title.push(item);
              });
              high.map((o) => {
                let temp = parseInt(o);
                that.weather_high.push(temp);
              });
              low.map((o) => {
                let temp = parseInt(o);
                that.weather_low.push(temp);
              });
              that.weather_windem = Array.from(...[windem]);
              that.weather_windwl = Array.from(...[windwl]);
              discribe.map((item) => {
                that.weather_discribe.push(item);
              });
              let weatherLength = title.length;
              for (let loc = 0; loc < weatherLength; ++loc) {
                let discribeText = {
                  date: "",
                  discrible: "",
                  temperature: "",
                };
                discribeText.date = that.weather_title[loc];
                discribeText.discrible = that.weather_discribe[loc];
                discribeText.temperature =
                  that.weather_low[loc] + "/" + that.weather_high[loc] + "°C";
                discribeText.windem = that.weather_windem[loc];
                discribeText.windwl = that.weather_windwl[loc];
                that.tableData.push(discribeText);
              }
              console.log(that.tableData, "that.tableData");
yma16's avatar
yma16 已提交
101
            })
Y
yma16 已提交
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217
            .catch((r) => {
              console.log("r", r);
            });
        }, 1000);
        resolve("获取后端天气成功的promise");
      });
    },
    weatherInint() {
      // 基于准备好的dom,初始化echarts实例
      console.log("echarts!");
      var that = this; // 传递
      return new Promise((resolve) => {
        setTimeout(function () {
          let highData = Array.from(...[that.weather_high]);
          let lowData = Array.from(...[that.weather_low]);
          let titleData = Array.from(...[that.weather_title]);
          console.log("绘制折线图数据", titleData, highData, lowData);
          var dom = document.getElementById("echart_weather");
          // console.log('dom', dom)
          var myChart = echarts.init(dom);
          // 绘制图表
          var option = {
            title: {
              text: that.msg,
              subtext: "气温°C",
            },
            tooltip: {
              trigger: "axis",
            },
            // legend: {
            //     data: ['最高', '最低']
            // },
            toolbox: {
              show: true,
              feature: {
                dataZoom: {
                  yAxisIndex: "none",
                },
                dataView: {
                  readOnly: false,
                },
                magicType: {
                  type: ["bar", "line"],
                },
                restore: {},
                saveAsImage: {},
              },
            },
            xAxis: {
              type: "category",
              boundaryGap: true,
              data: titleData,
            },
            yAxis: {
              type: "value",
              axisLabel: {
                formatter: "{value}",
              },
            },
            series: [
              {
                name: "Highest",
                type: "bar",
                data: highData,
                markPoint: {
                  data: [
                    {
                      type: "max",
                      name: "Max",
                    },
                    {
                      type: "min",
                      name: "Min",
                    },
                  ],
                },
                markLine: {
                  data: [
                    {
                      type: "average",
                      name: "avg",
                    },
                  ],
                },
              },
              {
                name: "Lowest",
                type: "line",
                data: lowData,
                markPoint: {
                  data: [
                    {
                      name: "最低",
                      value: -2,
                      xAxis: 1,
                      yAxis: -1.5,
                    },
                  ],
                },
                markLine: {
                  data: [
                    {
                      type: "average",
                      name: "Avg",
                    },
                    [
                      {
                        symbol: "circle",
                        x: "75%",
                        yAxis: "max",
                      },
                      {
                        symbol: "circle",
                        label: {
                          position: "start",
                          formatter: "Max",
yma16's avatar
yma16 已提交
218
                        },
Y
yma16 已提交
219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
                        type: "max",
                        name: "最高点",
                      },
                    ],
                  ],
                },
              },
            ],
          };
          myChart.setOption(option); // 画折线图
          window.onresize = function () {
            myChart.resize();
          };
        }, 3000);
        resolve("绘制折线图Promise");
      });
      // dom = null; //销毁
yma16's avatar
yma16 已提交
236
    },
Y
yma16 已提交
237 238
  },
};
yma16's avatar
yma16 已提交
239 240 241 242
</script>

<style scoped>
.weather {
Y
yma16 已提交
243 244 245
  position: relative;
  width: 100%;
  height: 100%;
yma16's avatar
yma16 已提交
246 247 248
}

#echart_weather {
Y
yma16 已提交
249 250 251
  position: relative;
  width: 100%;
  height: 300px;
yma16's avatar
yma16 已提交
252 253 254
}

.weatherDiscribe {
Y
yma16 已提交
255 256 257 258 259 260
  position: relative;
  margin-top: 5px;
  width: 100%;
  height: auto;
  left: 50%;
  transform: translateX(-50%);
yma16's avatar
yma16 已提交
261 262 263
}

#customers {
Y
yma16 已提交
264 265 266 267 268 269 270
  position: relative;
  font-family: Arial, Helvetica, sans-serif;
  border-collapse: collapse;
  width: 100%;
  left: 50%;
  top: 50%;
  transform: translateX(-50%, -50%);
yma16's avatar
yma16 已提交
271 272
}
</style>