vue3引入echarts图表

1、安装echarts依赖包
npm install echarts --save
npm install echarts vue-echarts
2、全局引入echarts
import { createApp } from 'vue'
import App from './App.vue'

import ECharts from 'vue-echarts' // 引入ECharts
import 'echarts' // 全局引入echarts

const app = createApp(App)

app.component('ECharts',ECharts)
app.mount('#app')
3、在界面直接就可以使用
    <template>
     <div>
      <e-charts class="chart" :option="option" />
     </div>
   </template>

<script setup>
import { ref, computed } from 'vue'

const option = computed(() => {
  return {
    title: {
      text: 'Stacked Line'
    },
    tooltip: {
      trigger: 'axis'
    },
    legend: {
      data: ['Email', 'Union Ads', 'Video Ads', 'Direct', 'Search Engine']
    },
    grid: {
      left: '3%',
      right: '4%',
      bottom: '3%',
      containLabel: true
    },
    toolbox: {
      feature: {
        saveAsImage: {}
      }
    },
    xAxis: {
      type: 'category',
      boundaryGap: false,
      data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
    },
    yAxis: {
      type: 'value'
    },
    series: [
      {
        name: 'Email',
        type: 'line',
        stack: 'Total',
        data: [120, 132, 101, 134, 90, 230, 210]
      },
      {
        name: 'Union Ads',
        type: 'line',
        stack: 'Total',
        data: [220, 182, 191, 234, 290, 330, 310]
      },
      {
        name: 'Video Ads',
        type: 'line',
        stack: 'Total',
        data: [150, 232, 201, 154, 190, 330, 410]
      },
      {
        name: 'Direct',
        type: 'line',
        stack: 'Total',
        data: [320, 332, 301, 334, 390, 330, 320]
      },
      {
        name: 'Search Engine',
        type: 'line',
        stack: 'Total',
        data: [820, 932, 901, 934, 1290, 1330, 1320]
      }
    ]
  }
})


</script>
<style lang="scss" scoped>

.chart {
  height: 400px;
}
</style>
4、效果图

  • 8
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
您可以按照以下步骤在Vue项目中引入ECharts: 1. 首先,通过npm安装ECharts依赖包。打开终端并运行以下命令: ``` npm install echarts --save ``` 2. 在需要使用ECharts的组件中,引入ECharts库。在组件的JavaScript文件中添加以下代码: ```javascript import echarts from 'echarts' ``` 3. 如果您使用的是Vue CLI创建的项目,可以在main.js文件中全局注册echarts: ```javascript import echarts from 'echarts' Vue.prototype.$echarts = echarts ``` 这样,您就可以在整个项目中通过`this.$echarts`来访问ECharts库。 4. 在组件的模板中,使用`<div>`标签创建一个容器来显示图表,并设置其样式和尺寸。例如: ```html <div id="chart-container" style="width: 400px; height: 300px;"></div> ``` 5. 在组件的JavaScript文件中,使用`this.$echarts.init`方法初始化图表,并传入容器的DOM对象和可选的主题名称。例如: ```javascript mounted() { const chartContainer = document.getElementById('chart-container') const chart = this.$echarts.init(chartContainer) // 在chart变量上配置和渲染图表数据 } ``` 6. 配置和渲染图表数据。您可以使用ECharts提供的API来配置和渲染各种类型的图表。例如: ```javascript const option = { // 图表配置项和数据 } chart.setOption(option) ``` 通过上述步骤,您就可以在Vue项目中成功引入和使用ECharts图表库了。请注意,具体的图表配置和数据根据您的需求进行相应的调整。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值