vue3+ts中使用echarts
vue3 + ts 中使用ECharts
·
搭建Vite项目
使用vite创建vue-ts项目
兼容性注意
Vite 需要 Node.js 版本 >= 12.0.0。
此处使用 pnpm
构建项目,npm及yarn请查看官方文档。
pnpm create vite my-vue-app -- --template vue-ts
使用vscode打开项目,新建终端,执行 pnpm install
或 pnpm i
安装依赖。执行 pnpm run dev
或 pnpm dev
运行项目:
安装ECharts并绘制基础折线图
通过pnpm安装ECharts,当前版本为5.3.2
pnpm add echarts
引入ECharts
<script setup lang="ts">
import { onMounted } from 'vue';
import { EChartsOption, init } from 'echarts';
onMounted(() => {
// 获取dom,断言HTMLElement类型,否则会报错
const chartEle: HTMLElement = document.getElementById('chart') as HTMLElement;
const chart = init(chartEle);
const option: EChartsOption = {
xAxis: {
type: 'category',
data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
},
yAxis: {
type: 'value'
},
series: [
{
data: [150, 230, 224, 218, 135, 147, 260],
type: 'line'
}
]
};
option && chart.setOption(option);
})
</script>
<template>
<div id="chart"></div>
</template>
<style>
#app {
font-family: Avenir, Helvetica, Arial, sans-serif;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
text-align: center;
color: #2c3e50;
margin-top: 60px;
}
#chart {
width: 600px;
height: 400px;
}
</style>
完成,效果图如下:
2022-08-15更新:ref用法(推荐)
<script setup lang="ts">
import { onMounted, Ref, ref } from 'vue'
import { ECharts, EChartsOption, init } from 'echarts'
let chart: ECharts;
const chartRef: Ref<HTMLElement | null> = ref(null)
const initChart = () => {
const option: EChartsOption = {
xAxis: {
type: 'category'
},
yAxis: {
type: 'value'
},
series: [
{
type: 'line'
}
]
};
chart.setOption(option);
}
const updateChart = () => {
const option: EChartsOption = {
xAxis: {
data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
},
series: [
{
data: [150, 230, 224, 218, 135, 147, 260]
}
]
};
chart.setOption(option);
}
onMounted(() => {
chart = init(chartRef.value as HTMLElement)
initChart()
// 延时2秒后执行增量更新
setTimeout(() => {
updateChart()
}, 2000)
})
</script>
<template>
<div class="chart" ref="chartRef"></div>
</template>
<style scoped>
.chart {
width: 100%;
height: 400px;
}
</style>
2022-12-12更新:定时重载图表
<script setup lang="ts">
import { onMounted, onUnmounted, Ref, ref } from 'vue'
import { ECharts, EChartsOption, init } from 'echarts'
let chart: ECharts;
const chartRef: Ref<HTMLElement | null> = ref(null)
// 初始化图表
const initChart = () => {
const option: EChartsOption = {
xAxis: {
type: 'category'
},
yAxis: {
type: 'value'
},
series: [
{
type: 'line'
}
]
};
chart.setOption(option);
}
// 更新图表
const updateChart = () => {
const option: EChartsOption = {
xAxis: {
data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
},
series: [
{
data: [150, 230, 224, 218, 135, 147, 260]
}
]
};
chart.setOption(option);
}
// 声明定时器
let interval: number | null = null;
onMounted(() => {
chart = init(chartRef.value as HTMLElement)
initChart()
// 延时2秒后执行增量更新
setTimeout(() => {
updateChart()
}, 2000)
// 添加定时器,每隔5秒重新渲染图表
interval = setInterval(() => {
chart.clear()
initChart()
updateChart()
}, 5000)
})
// 卸载组件时清除定时器
onUnmounted(() => {
interval && clearInterval(interval)
})
</script>
<template>
<div class="chart" ref="chartRef"></div>
</template>
<style scoped>
.chart {
width: 370px;
height: 400px;
}
</style>
更多推荐
已为社区贡献1条内容
所有评论(0)