vue3中怎么引入Echarts

Echarts是什么?

Echarts 是一个流行的图表和数据可视化库

官方网站:Apache ECharts

vue3中引入Echarts,步骤如下:

1. 安装 Echarts 和 vue-echarts

npm install echarts vue-echarts

2. 导入 Echarts 和 vue-echarts

import * as echarts from 'echarts'
import { useECharts } from 'vue-echarts'

3. 注册为全局组件

app.component('v-chart', useECharts(echarts))

4. 使用组件并初始化图表

<v-chart :options="options" />
data() {
  return {
    options: {
      xAxis: {
        type: 'category',
        data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
      },
      yAxis: {
        type: 'value'
      },
      series: [{
        data: [820, 932, 901, 934, 1290, 1330, 1320],
        type: 'line'
      }]
    }
  }
}

5. 完整示例

<div id="app">
  <v-chart :options="options" />
  <button @click="update">Update</button>
</div>
import { createApp } from 'vue'
import * as echarts from 'echarts'
import { useECharts } from 'vue-echarts'

const app = createApp({
  methods: {
    update() {
      this.options.series[0].data = [...this.options.series[0].data, 100]
    }
  },
  data() {
    return {
      options: { /*  */ }
    }
  } 
})

app.component('v-chart', useECharts(echarts))

const vm = app.mount('#app') 
setInterval(() => {
  vm.update() 
}, 1000)

这会在页面上渲染一个 Echarts 图表,每秒数据更新一次。

最后,总结一下,在vue3中引入Echarts的主要步骤是:

1. 安装 Echarts 和 vue-echarts
2. 导入 Echarts 和 vue-echarts
3. 注册 Echarts 组件
4. 在模板中使用组件并传入初始化 options
5. 通过更新 options 刷新图表

猜你喜欢

转载自blog.csdn.net/qwe0415/article/details/130350418