EchartWeather.vue 10.1 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>
yma16's avatar
yma16 已提交
18
import * as echarts from 'echarts/index'
yma16's avatar
yma16 已提交
19
export default {
yma16's avatar
yma16 已提交
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
    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 已提交
36
    },
yma16's avatar
yma16 已提交
37 38
    mounted () {
        this.initWeatherLine()
Y
yma16 已提交
39
    },
yma16's avatar
yma16 已提交
40 41 42 43 44 45 46
    methods: {
        initWeatherLine () {
            let that = this
            async function waitGetWeatherData () {
                await that.getWeatherData()
                await that.weatherInint()
            }
47
            waitGetWeatherData()
yma16's avatar
yma16 已提交
48 49 50 51 52
        },
        getWeatherData () {
            let that = this
            return new Promise((resolve) => {
                setTimeout(function () {
yma16's avatar
yma16 已提交
53
                    that.$axios
yma16's avatar
yma16 已提交
54 55
                        .get(that.baseurl + 'article/weather/')
                        .then((res) => {
56 57 58
                            let resData = res.data
                            let weather = resData.weather
                            that.msg = weather.location
yma16's avatar
yma16 已提交
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
                            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)
                            }
                        })
                        .catch((r) => {
99
                            throw Error(r)
yma16's avatar
yma16 已提交
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
                        })
                }, 1000)
                resolve('获取后端天气成功的promise')
            })
        },
        weatherInint () {
            // 基于准备好的dom,初始化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])
                    let dom = document.getElementById('echart_weather')
                    let myChart = echarts.init(dom)
                    // 绘制图表
                    let option = {
                        title: {
                            text: that.msg,
                            subtext: '气温°C'
yma16's avatar
yma16 已提交
121
                        },
yma16's avatar
yma16 已提交
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 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232
                        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 已提交
233 234 235 236
</script>

<style scoped>
.weather {
Y
yma16 已提交
237 238 239
  position: relative;
  width: 100%;
  height: 100%;
yma16's avatar
yma16 已提交
240 241 242
}

#echart_weather {
Y
yma16 已提交
243 244 245
  position: relative;
  width: 100%;
  height: 300px;
yma16's avatar
yma16 已提交
246 247 248
}

.weatherDiscribe {
Y
yma16 已提交
249 250 251 252 253 254
  position: relative;
  margin-top: 5px;
  width: 100%;
  height: auto;
  left: 50%;
  transform: translateX(-50%);
yma16's avatar
yma16 已提交
255 256 257
}

#customers {
Y
yma16 已提交
258 259 260 261 262 263 264
  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 已提交
265 266
}
</style>