实现天气预报走势图

news2024/9/28 7:22:53

实现效果:

 这里我用的天气接口是网上开源的,可以自己找一下。

 稍微简单封装了一下axiso以及接口

封装的axios:

// import { useUserStore } from '@/stores/user'
import axios from 'axios'
import router from '@/router'
import { ElMessage } from 'element-plus'

const baseURL = 'https://v0.yiketianqi.com'

const instance = axios.create({
    baseURL,
    timeout: 100000
})

// instance.interceptors.request.use(
//     (config) => {
//         const userStore = useUserStore()
//         if (userStore.token) {
//             config.headers.Authorization = userStore.token
//         }
//         return config
//     },
//     (err) => Promise.reject(err)
// )

instance.interceptors.response.use(
    (res) => {
        if (res.data.code === 0 || res.status == 200) {
            return res
        }
        ElMessage({ message: res.data.message || '服务异常', type: 'error' })
        return Promise.reject(res.data)
    },
    (err) => {
        ElMessage({ message: err.response.data.message || '服务异常', type: 'error' })
        console.log(err)
        if (err.response?.status === 401) {
            router.push('/login')
        }
        return Promise.reject(err)
    }
)

export default instance
export { baseURL }

封装的接口:

import instance from '../../utils/requestWeather'

export function getWeather() {
    return instance({
        url: '/api?unescape=1&version=v9&appid=75878732&appsecret=kcKOD1Qz',
        method: 'GET',
    })
}

具体展示页面:

<template>
  <div>城市:{{ weatherData.city }}</div>
  <div class="container">
    <div id="main" style="width: 100%; height: 500px"></div>
  </div>
</template>

<script setup>
import * as echarts from 'echarts'
import { onMounted, reactive, ref } from 'vue'
import { getWeather } from '../../api/weather/weather'

const draw = () => {
  var chartDom = document.getElementById('main')
  var myChart = echarts.init(chartDom)
  myChart.clear()
  myChart.setOption(option)
  // echarts表格自适应!!
  setTimeout(function () {
    window.onresize = function () {
      myChart.resize()
    }
  }, 200)
}
onMounted(() => {
  getweatherData()
})
const weatherData = ref({})
const weatherDate = reactive([])
const airLevel = reactive([])
const weatherDay = reactive([])
const tem1 = reactive([]) //最高温度
const tem2 = reactive([]) //最低温度
const getweatherData = async () => {
  try {
    const res = await getWeather()
    weatherData.value = res.data
    console.log('日期', weatherData.value.data)
    weatherData.value.data.forEach((item) => {
      weatherDate.push(item.date)
      airLevel.push(item.air_level)
      weatherDay.push(item.day)
      tem1.push(item.tem1)
      tem2.push(item.tem2)
    })
    console.log('数据', weatherData.value.data)
    draw()
  } catch (error) {
    console.log('error:', error)
  }
}
var option = reactive({
  grid: {
    show: true,
    backgroundColor: 'transparent',
    opacity: 0.3,
    borderWidth: '0',
    top: '180',
    bottom: '0'
  },
  tooltip: {
    trigger: 'axis'
  },
  legend: {
    show: false
  },
  xAxis: [
    // 日期
    {
      type: 'category',
      boundaryGap: false,
      position: 'top',
      offset: 130,
      zlevel: 100,
      axisLine: {
        show: false
      },
      axisTick: {
        show: false
      },
      axisLabel: {
        interval: 0,
        formatter: ['{a|{value}}'].join('\n'),
        rich: {
          a: {
            // color: 'white',
            fontSize: 18
          }
        }
      },
      nameTextStyle: {},
      data: weatherDate
    },
    // 星期
    {
      type: 'category',
      boundaryGap: false,
      position: 'top',
      offset: 110,
      zlevel: 100,
      axisLine: {
        show: false
      },
      axisTick: {
        show: false
      },
      axisLabel: {
        interval: 0,
        formatter: ['{a|{value}}'].join('\n'),
        rich: {
          a: {
            // color: 'white',
            fontSize: 14
          }
        }
      },
      nameTextStyle: {
        fontWeight: 'bold',
        fontSize: 19
      },
      data: weatherDay
    },
    // 天气图标,这边我是写死的
    {
      type: 'category',
      boundaryGap: false,
      position: 'top',
      offset: 20,
      zlevel: 100,
      axisLine: {
        show: false
      },
      axisTick: {
        show: false
      },
      axisLabel: {
        interval: 0,
        formatter: function (value, index) {
          return '{' + index + '| }\n{b|' + value + '}'
        },
        rich: {
          0: {
            backgroundColor: {
              // image: require('@/assets/weather_icon/' + this.weatherIconDic[this.weatherData.weather[0]] + '.png')
              image: 'https://d.scggqx.com/forecast/img/小雨.png'
            },
            height: 40,
            width: 40
          },
          1: {
            backgroundColor: {
              // image: require('@/assets/weather_icon/' + this.weatherIconDic[this.weatherData.weather[1]] + '.png')
              image: 'https://d.scggqx.com/forecast/img/小雨.png'
            },
            height: 40,
            width: 40
          },
          2: {
            backgroundColor: {
              // image: require('@/assets/weather_icon/' + this.weatherIconDic[this.weatherData.weather[2]] + '.png')
              image: 'https://d.scggqx.com/forecast/img/阴.png'
            },
            height: 40,
            width: 40
          },
          3: {
            backgroundColor: {
              // image: require('@/assets/weather_icon/' + this.weatherIconDic[this.weatherData.weather[3]] + '.png')
              image: 'https://d.scggqx.com/forecast/img/小雨.png'
            },
            height: 40,
            width: 40
          },
          4: {
            backgroundColor: {
              // image: require('@/assets/weather_icon/' + this.weatherIconDic[this.weatherData.weather[4]] + '.png')
              image: 'https://d.scggqx.com/forecast/img/多云.png'
            },
            height: 40,
            width: 40
          },
          5: {
            backgroundColor: {
              // image: require('@/assets/weather_icon/' + this.weatherIconDic[this.weatherData.weather[5]] + '.png')
              image: 'https://d.scggqx.com/forecast/img/小雨.png'
            },
            height: 40,
            width: 40
          },
          6: {
            backgroundColor: {
              // image: require('@/assets/weather_icon/' + this.weatherIconDic[this.weatherData.weather[6]] + '.png')
              image: 'https://d.scggqx.com/forecast/img/小雨.png'
            },
            height: 40,
            width: 40
          },
          b: {
            // color: 'white',
            fontSize: 12,
            lineHeight: 30,
            height: 20
          }
        }
      },
      nameTextStyle: {
        fontWeight: 'bold',
        fontSize: 19
      },
      // data: this.weatherData.weather
      data: ['小雨', '小雨', '阴', '小雨', '多云', '小雨', '小雨']
    },
    // 天气描述,这边我也是写死的
    {
      type: 'category',
      boundaryGap: false,
      position: 'top',
      offset: -200,
      zlevel: -200,
      axisLine: {
        show: false
      },
      axisTick: {
        show: false
      },
      axisLabel: {
        interval: 0,
        formatter: ['{a|{value}}'].join('\n'),
        rich: {
          a: {
            // color: 'white',
            fontSize: 14
          }
        }
      },
      nameTextStyle: {
        fontWeight: 'bold',
        fontSize: 19
      },
      data: airLevel
    }
  ],
  yAxis: {
    type: 'value',
    show: false,
    axisLabel: {
      formatter: '{value} °C',
      color: 'white'
    }
  },
  series: [
    {
      name: '最高气温',
      type: 'line',
      data: tem1,
      symbol: 'emptyCircle',
      symbolSize: 10,
      showSymbol: true,
      smooth: true,
      itemStyle: {
        normal: {
          color: '#C95843'
        }
      },
      label: {
        show: true,
        position: 'top',
        // color: 'white',
        formatter: '{c} °C'
      },
      lineStyle: {
        width: 1
        // color: 'white'
      },
      areaStyle: {
        opacity: 1,
        color: 'transparent'
      }
    },
    {
      name: '最低气温',
      type: 'line',
      data: tem2,
      symbol: 'emptyCircle',
      symbolSize: 10,
      showSymbol: true,
      smooth: true,
      itemStyle: {
        normal: {
          color: 'blue'
        }
      },
      label: {
        show: true,
        position: 'bottom',
        // color: 'white',
        formatter: '{c} °C'
      },
      lineStyle: {
        width: 1
        // color: 'white'
      },
      areaStyle: {
        opacity: 1,
        color: 'transparent'
      }
    }
  ]
})
</script>

<style lang="scss" scoped>
.container {
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
}
</style>

至此,天气预报走势图就完成了,日期,天气描述图片,星期,空气质量这些展示就是通过设置多个x轴控制offset,zlevel属性来实现的。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/838097.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

P14 电路定理——巧妙-灵性-智慧

1、替代定理 图示表示&#xff1a; 叠加定理和齐性定理只能用于线性电路&#xff0c;但是替代定理无论线不线性都可以用。 常见的&#xff1a;线性电路将某复杂支路等效成电压源或电流源之后&#xff0c;就可以使用叠加原理了。 引入两个相互抵消的电压源&#xff0c;拿其中一…

【数字IC基础】低功耗设计

低功耗技术 功耗构成静态功耗(漏电功耗)动态功耗翻转功耗(Switch Power)短路功耗(Internal Power) 不同类型的标准单元的功耗 低功耗设计方法降低芯片工作电压多阈值工艺方法电源门控&#xff08;Power Gating&#xff09;多电压域(Multi-Voltage Domain)体偏置门控时钟一个简单…

AWS Amplify 部署node版本18报错修复

Amplify env&#xff1a;Amazon Linux:2 Build Error : Specified Node 18 but GLIBC_2.27 or GLIBC_2.28 not found on build 一、原因 报错原因是因为默认情况下&#xff0c;AWS Amplify 使用 Amazon Linux:2 作为其构建镜像&#xff0c;并自带 GLIBC 2.26。不过&#xff0c;…

【M波段2D双树(希尔伯特)小波多分量图像去噪】基于定向M波段双树(希尔伯特)小波对多分量/彩色图像进行降噪研究(Matlab代码实现)

&#x1f4a5;&#x1f4a5;&#x1f49e;&#x1f49e;欢迎来到本博客❤️❤️&#x1f4a5;&#x1f4a5; &#x1f3c6;博主优势&#xff1a;&#x1f31e;&#x1f31e;&#x1f31e;博客内容尽量做到思维缜密&#xff0c;逻辑清晰&#xff0c;为了方便读者。 ⛳️座右铭&a…

使用 OpenCV 和 Python 卡通化图像-附源码

介绍 在本文中,我们将构建一个有趣的应用程序,它将卡通化提供给它的图像。为了构建这个卡通化器应用程序,我们将使用 python 和 OpenCV。这是机器学习令人兴奋的应用之一。在构建此应用程序时,我们还将了解如何使用 easygui、Tkinter 等库。在这里,您必须选择图像,然后应…

二叉树的遍历(先序遍历,中序遍历,后序遍历)递归与非递归算法

目录 一、先序遍历题目链接1.递归2.非递归 二、中序遍历题目链接1.递归2.非递归 三、后序遍历题目链接1.递归2.非递归 一、先序遍历 先序遍历&#xff1a;先遍历一颗树的根节点&#xff0c;后遍历左子树&#xff0c;最后遍历右子树 先序遍历序列&#xff1a; 1 -> 2 -> 4…

20.4 HTML 表单

1. form表单 <form>标签: 用于创建一个表单, 通过表单, 用户可以向网站提交数据. 表单可以包含文本输入字段, 复选框, 单选按钮, 下拉列表, 提交按钮等等. 当用户提交表单时, 表单数据会发送到服务器进行处理.action属性: 应指向一个能够处理表单数据的服务器端脚本或UR…

vue使用拖拽功能实现仓库存放货物的需求

效果&#xff1a; 代码 <template><div><div class"bigTitle">xxxxxxxxxxxxxx仓库拖拽系统</div><div class"container2 flex-j-space-between"><div class"product-list"><div class"leftTree h…

Python入门【​try和except结构、常见异常、with上下文管理 、traceback模块和生成异常日志、自定义异常类】(十八)

&#x1f44f;作者简介&#xff1a;大家好&#xff0c;我是爱敲代码的小王&#xff0c;CSDN博客博主,Python小白 &#x1f4d5;系列专栏&#xff1a;python入门到实战、Python爬虫开发、Python办公自动化、Python数据分析、Python前后端开发 &#x1f4e7;如果文章知识点有错误…

【C++】STL——stack和queue的模拟实现、空间适配器、deque的介绍、增删查改函数的简单实现

文章目录 1.deque的简单介绍2.模拟实现stack3.模拟实现queue 1.deque的简单介绍 deque的介绍文档 deque(双端队列)&#xff1a;是一种双开口的"连续"空间的数据结构&#xff0c;双开口的含义是&#xff1a;可以在头尾两端进行插入和删除操作&#xff0c;且时间复杂度…

实现多线程的三种方式

1. 继承Thread 类实现多线程 想要实现多线程&#xff0c;第一种方法就是通过继承Thread类实现多线程&#xff0c;有以下几步 &#xff08;1&#xff09;我们要先自定义一个类然后继承Thread类&#xff1b; &#xff08;2&#xff09;在继承Trread的类中重写 run 方法&#x…

成功了!|| Poetry安装pytorch || 整理自github项目Poetry下的issue

在使用Poetry安装pytorch的时候&#xff0c;常常会遇到各种问题&#xff1a;首先是使用add添加时&#xff0c;会说只有torch没有什么pytorch&#xff0c;很显然&#xff0c;它是直接针对包的&#xff0c;第二点是&#xff0c;如果是一台没有nvidia显卡的机器&#xff0c;由于po…

CISA《网络安全事件和漏洞响应手册》提到的SSVC是什么?

2021年11月16日&#xff0c;美国网络安全和基础设施安全局(CISA)根据行政命令EO 14028的要求发布了《网络安全事件和漏洞响应手册》。手册规定的漏洞响应过程包括识别、评估、修复、报告通知4个步骤&#xff0c;其中评估部分的第一句话提到“使用特定相关者漏洞分类法(Stakehol…

C++ 动态内存分配

在C中动态内存的分配技术可以保证程序在允许过程中按照实际需要申请适量的内存&#xff0c;使用结束后还可以释放&#xff0c;这种在程序运行过程中申请和释放的存储单元也称为堆。 申请和释放过程一般称为建立和删除。 在C程序中&#xff0c;建立和删除堆对象使用两个运算符&…

html页面input设置日期和时分秒组件方法

html <input class"form-control" type"datetime-local" step"01">效果图

Java根据坐标经纬度计算两点距离(5种方法)、校验经纬度是否在圆/多边形区域内的算法推荐

目录 前言 一、根据坐标经纬度计算两点距离&#xff08;5种方法&#xff09; 1.方法一 2.方法二 3.方法三 4.方法四 5.方法五 5.1 POM引入第三方依赖 5.2 代码 6.测试结果对比 二、校验经纬度是否在制定区域内 1.判断一个坐标是否在圆形区域内 2.判断一个坐标是否…

安防监控国标GB28181平台EasyGBS视频快照无法显示是什么原因?如何解决?

安防视频监控国标视频云服务EasyGBS支持设备/平台通过国标GB28181协议注册接入&#xff0c;并能实现视频的实时监控直播、录像、检索与回看、语音对讲、云存储、告警、平台级联等功能。平台部署简单、可拓展性强&#xff0c;支持将接入的视频流进行全终端、全平台分发&#xff…

【Leetcode刷题】模拟

本篇文章为 LeetCode 模拟模块的刷题笔记&#xff0c;仅供参考。 目录 一. 字符串Leetcode43.字符串相乘Leetcode592.分数加减运算Leetcode68.文本左右对齐 二. 矩阵Leetcode54.螺旋矩阵Leetcode885.螺旋矩阵 IIILeetcode498.对角线遍历Leetcode874.模拟行走机器人 三. 数组Lee…

Aligning Large Language Models with Human: A Survey

本文也是LLM相关的综述文章&#xff0c;针对《Aligning Large Language Models with Human: A Survey》的翻译。 对齐人类与大语言模型&#xff1a;综述 摘要1 引言2 对齐数据收集2.1 来自人类的指令2.1.1 NLP基准2.1.2 人工构造指令 2.2 来自强大LLM的指令2.2.1 自指令2.2.2 …