vue2中echarts的使用

本文介绍了在Vue项目中如何通过npm和yarn安装ECharts库,以及如何导入并实现基本的图表展示,包括数据配置和tooltip功能。

一、安装

npm安装

 npm i echarts--save

yarn安装

yarn add echarts

二、引入echarts

import * as echarts from 'echarts'

三、使用echarts

<template>
  <div id="chart" style="width: 50vw; height: 400px;"></div>
</template>

<script>
import * as echarts from 'echarts'

export default {
  data() {
    return {

    }
  },
  mounted() {
    this.initChart()
  },
  methods: {
    initChart() {
      const chart = echarts.init(document.getElementById('chart'))

      const option = {
       grid: { //占比大小
                    left: '5%',
                    right: '5%',
                    bottom: '10%',
                    containLabel: true
               },
        title: {
          text: 'ECharts 示例'
        },
        tooltip: {
          trigger: 'axis',
          formatter: function (params) {
            console.log(params[0].seriesName);
            return '2022年数据' + '<br/>' + params[0].seriesName + ':' + params[0].data + '<br>'
              + params[1].seriesName + ':' + params[1].data + '<br>'
            // + params[2].seriesName + ':' + params[2].data
          }

        },
        legend: {
          data: ['销量']
        },
        xAxis: {
          type: 'category',
          data: ['衬衫', '羊毛衫', '雪纺衫', '裤子']
        },
        yAxis: {
          type: 'value'
          min: 0,
          name: '单位 W',
          splitNumber: 5, // 设置为 5 表示显示五行刻度线
        },
        series: [
          {
            name: '销量',
            type: 'bar',
            data: [5, 2, 3, 4],
            barWidth:'30' //设置柱子的width

          },
          {
            name: 'app',
            type: 'bar',
            data: [8, 2, 3, 4],
            itemStyle: {
              color: 'blue'
            }
          },

        ]
      }

      chart.setOption(option)
    }
  }
}
</script>

Vue2 项目中集成和使用 ECharts 图表库,可以按照以下方式操作: ### 安装 ECharts 首先,需要在项目中安装 ECharts。可以通过 npm 或 yarn 来完成安装: ```bash npm install echarts --save ``` 或者使用 yarn: ```bash yarn add echarts ``` ### 全局引入 ECharts 可以在 `main.js` 文件中全局引入 ECharts,并将其挂载到 Vue 的原型链上,这样可以在所有组件中通过 `this.$echarts` 来访问: ```javascript import Vue from 'vue' import App from './App.vue' import * as echarts from 'echarts' Vue.config.productionTip = false // 将 ECharts 挂载到 Vue 实例上 Vue.prototype.$echarts = echarts new Vue({ render: h => h(App) }).$mount('#app') ``` ### 局部引入 ECharts 如果只需要在某个特定的组件中使用 ECharts,则可以在该组件中局部引入: ```javascript import { defineComponent } from 'vue' import * as echarts from 'echarts' export default defineComponent({ name: 'EChartsComponent', mounted() { this.initChart() }, methods: { initChart() { const chartDom = document.getElementById('chart') const myChart = echarts.init(chartDom) const option = { tooltip: { trigger: 'axis' }, 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' } ] } myChart.setOption(option) } } }) ``` ### 使用 ECharts 在 HTML 模板中定义一个用于显示图表的容器 `<div>`,并确保为其设置宽度和高度,否则图表可能无法正确显示: ```html <template> <div id="chart" style="width: 600px;height:400px;"></div> </template> ``` 此外,还可以参考 ECharts 官方提供的示例来获取更多图表类型和样式的信息:[Examples - Apache ECharts](https://echarts.apache.org/examples/zh/index.html) [^3]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值