vue3引入echarts正确姿势

echarts文档地址:

echarts官网地址

echarts配置手册

echarts 模板地址

1、安装

(1)安装echarts包
npm install echarts --save
或者
cnpm install echarts --save

(2)安装vue echarts工具包 

npm install echarts vue-echarts
或者
cnpm install echarts vue-echarts

2、挂载

全局引入的挂载方式

main.js文件中

 

import ECharts from 'vue-echarts' 
import "echarts";               
// 挂载ECharts   
app.component('ECharts',ECharts)  

 3. 自定义组件


<template>
  <div>
    <e-charts id="main" class="chart" :option="props.option" />
  </div>
</template>

<script setup lang="ts">
  import * as echarts from 'echarts';
  /** 接受父组件传来的值  */
  const props = defineProps({
    option: {
      type: Object,
      require: true
    }
  });

  onMounted(() => {
    var chartDom = document.getElementById('main');
    var myChart = echarts.init(chartDom);
    props.option && myChart.setOption(props.option);
  });
</script>

<style scoped>
  .chart {
    width: 600px;
    height: 400px;
  }
</style>

项目中使用

引入组件

 import CommonChart from "@/components/Echarts/commonChart.vue";

 传入option相关数据

const dataPost = [
    { value: 1048, name: '后厨清洁工' },
    { value: 735, name: '西式餐饮服务员帮工' },
    { value: 580, name: '宴席服务帮工' },
    { value: 484, name: '宴会服务帮工' },
    { value: 300, name: '礼宾员-门童零工' },
    { value: 300, name: '中餐饮大厅服务帮工' },
    { value: 300, name: '厨房帮工' },
    { value: 300, name: '洗衣房帮工' },
  ]
  const optionPost = {
    title: {
      text: '岗位来源分类',
      bottom: '0',
      left: 'center',
    },
    tooltip: {
      trigger: 'item'
    },
    legend: { // 对图形的解释部分
      orient: 'vertical',
      right: 10,
      y: 'center',
      icon: 'circle',
      formatter: (name : any) => {
        let total = 0
        let target
        for (let i = 0; i < dataPost.length; i++) {
          total += dataPost[i].value
          if (dataPost[i].name === name) {
            target = dataPost[i].value
          }
        }
        const arr = [
          '{a|' + name + '}',
          '{b|' + target + '}',
          '{c|' + ((target / total) * 100).toFixed(2) + '%}'
        ]
        return arr.join('  ')
      },
      textStyle: {
        padding: [10, 0, 0, 0],
        rich: {
          a: {
            fontSize: 15,
            width: 135
          },
          b: {
            fontSize: 15,
            width: 50,
          },
          c: {
            fontSize: 15,
            color: '#c1c1c1'
          },
        }
      }
    },
    series: [
      {
        name: '岗位来源分类',
        type: 'pie',
        radius: ['40%', '70%'],
        center: ['25%', '50%'],
        avoidLabelOverlap: false,
        label: {
          show: false,
          position: 'center'
        },
        emphasis: {
          label: {
            show: true,
            fontSize: '20',
            fontWeight: 'bold'
          }
        },
        labelLine: {
          show: false
        },
        data: dataPost
      }
    ]

 效果如下:

  • 12
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值