Vue3使用ECharts 绘制天气预报折线图

Apache ECharts

安装

 npm i -S vue-echarts echarts

引入

 import { defineProps, onMounted, ref } from "vue";
 import * as echarts from 'echarts';

使用

 <template>
   <div id="mychart" ref="chartRef"></div>
 </template>
 onMounted(() => {
   chartInstance = echarts.init(chartRef.value);
 ​
   // console.log("chart子组件接收: ", props.weatherData);
 ​
   // y轴范围
   const max = props.weatherData.reduce((maxItem, item) => item.max > maxItem.max ? item : maxItem, props.weatherData[0]).max;
   const min = props.weatherData.reduce((minItem, item) => item.min < minItem.min ? item : minItem, props.weatherData[0]).min;
 ​
   // 配置项
   const options = {
     // 设置内边距
     grid: {
       x: 0,
       y: 0,
       x2: 0,
       y2: 0,
     },
     xAxis: {
       type: 'category',
       data: props.weatherData.map((item) => item.dayWeek),
       // 取消刻度线
       axisTick: {
         show: false,
       },
       // 取消x轴
       axisLine: {
         show: false,
       },
       // 取消坐标值
       axisLabel: {
         show: false,
       },
     },
     yAxis: {
       show: false,
       type: 'value',
       // 设置y轴范围
       min: max + 1,
       max: min - 2,
     },
     series: [
       {
         data: props.weatherData.map((item) => item.max),
         type: 'line',
         showSymbol: true,
         // smooth: true, // 曲线
         // 显示温度
         label: {
           show: true,
           position: 'bottom',
           formatter: '{c} °C',
         },
       },
       {
         data: props.weatherData.map((item) => item.min),
         type: 'line',
         showSymbol: true,
         // smooth: true,
         label: {
           show: true,
           position: 'bottom',
           formatter: '{c} °C',
         },
       },
     ],
   };
 ​
   // 设置图表配置项
   chartInstance.setOption(options);
 });

子组件配置

 const props = defineProps({
   weatherData: {
     type: Array,
     default: () => [],
   },
 });
 ​
 const chartRef = ref(null);
 let chartInstance = null;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值