vue3 antd modal 弹框中echarts显示
注意:echarts初始化要在dom生成后,不然会报错,我这里是监听弹框显示的时候初始化echarts
1. <div ref="main" style="width: 700px; height: 600px"></div>
2. const main = ref(null) // 使用ref创建虚拟DOM引用,使用时用main.value
3. watch(EnvironChartVisible,(newValue,oldValue)=>{
if(newValue === true){
init() //初始化echarts函数
}
})
eg:
<template>
<div>
<div ref="connectModal"></div>
<a-modal
v-model:visible="EnvironChartVisible"
:maskClosable="false"
title="Environmental Chart"
okText="Submit"
cancelText="Cancel"
@cancel="handleCancel"
@ok="handleOk"
width="60%"
:getContainer="() => $refs.connectModal"
>
<div ref="main" style="width: 700px; height: 600px"></div>
</a-modal>
</div>
</template>
<script lang="ts" setup>
import { defineProps, defineEmits, toRefs, reactive, ref,onMounted,nextTick,watch } from 'vue'
import * as echarts from "echarts";
const emit = defineEmits(['update:EnvironChartVisible'])
const props = defineProps({
EnvironChartVisible: Boolean
})
const {EnvironChartVisible}=toRefs(props)
const main = ref(null) // 使用ref创建虚拟DOM引用,使用时用main.value
watch(EnvironChartVisible,(newValue,oldValue)=>{
if(newValue === true){
init()
}
})
function init() {
// 基于准备好的dom,初始化echarts实例
nextTick(() => {
// 此dom为echarts图标展示dom
let myChart =echarts.init(main.value!);
// 指定图表的配置项和数据
let option = {
tooltip: {
trigger: 'axis',
axisPointer: { type: 'cross' }
},
legend: {},
xAxis: [
{
type: 'category',
axisTick: {
alignWithLabel: true
},
data: [
'1月',
'2月',
'3月',
'4月',
'5月',
'6月',
'7月',
'8月',
'9月',
'10月',
'11月',
'12月'
]
}
],
yAxis: [
{
type: 'value',
name: 'Humidity',
min: 0,
max: 100,
interval:2.5,
position: 'right',
axisLabel: {
show:true,
formatter: '{value}',
}
},
{
type: 'value',
name: 'Temperature',
min: -300,
max: 0,
interval:10,
position: 'left',
axisLabel: {
formatter: '{value}',
}
}
],
series: [
{
name: 'Humidity',
type: 'line',
yAxisIndex: 0,
data: [6, 32, 70, 86, 68.7, 100.7, 125.6, 112.2, 78.7, 48.8, 36.0, 19.3]
},
{
name: 'Temperature',
type: 'line',
smooth: true,
yAxisIndex: 1,
data: [
6.0,
10.2,
10.3,
11.5,
10.3,
13.2,
14.3,
16.4,
18.0,
16.5,
12.0,
5.2
]
}
]
};
// 使用刚指定的配置项和数据显示图表。
myChart.setOption(option);
});
}
</script>
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
· .NET周刊【3月第1期 2025-03-02】