EchartWeather.vue 10.4 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 18
</template>

<script>
export default {
yma16's avatar
yma16 已提交
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
    name: 'EchartWeather',
    data () {
        return {
            msg: '贵阳天气',
            baseurl: '/api/',
            // baseurl: 'http://yongma16.xyz/',
            // baseurl: 'api/article/weather/',
            tabPosition: 'left',
            weather_title: [],
            weather_high: [],
            weather_low: [],
            weather_discribe: [],
            weather_windem: [],
            weather_windwl: [],
            tableData: []
        }
yma16's avatar
yma16 已提交
35
    },
yma16's avatar
yma16 已提交
36 37
    mounted () {
        this.initWeatherLine()
Y
yma16 已提交
38
    },
yma16's avatar
yma16 已提交
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
    methods: {
        initWeatherLine () {
            let that = this
            async function waitGetWeatherData () {
                await that.getWeatherData()
                await that.weatherInint()
            }
            waitGetWeatherData().then((o) => {
                console.log(o)
            }) // 同步
        },
        getWeatherData () {
            let that = this
            return new Promise((resolve) => {
                setTimeout(function () {
yma16's avatar
yma16 已提交
54
                    that.$axios
yma16's avatar
yma16 已提交
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 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
                        .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')
                        })
                        .catch((r) => {
                            console.log('r', r)
                        })
                }, 1000)
                resolve('获取后端天气成功的promise')
            })
        },
        weatherInint () {
            // 基于准备好的dom,初始化echarts实例
            const echarts = this.$echarts
            if (!echarts) return
            const 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)
                    let dom = document.getElementById('echart_weather')
                    // console.log('dom', dom)
                    let myChart = echarts.init(dom)
                    // 绘制图表
                    let option = {
                        title: {
                            text: that.msg,
                            subtext: '气温°C'
yma16's avatar
yma16 已提交
126
                        },
yma16's avatar
yma16 已提交
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 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
                        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'
                                                },
                                                type: 'max',
                                                name: '最高点'
                                            }
                                        ]
                                    ]
                                }
                            }
                        ]
                    }
                    myChart.setOption(option) // 画折线图
                    window.onresize = function () {
                        myChart.resize()
                    }
                }, 3000)
                resolve('绘制折线图Promise')
            })
            // dom = null; //销毁
        }
    }
}
yma16's avatar
yma16 已提交
238 239 240 241
</script>

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

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

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

#customers {
Y
yma16 已提交
263 264 265 266 267 268 269
  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 已提交
270 271
}
</style>