vue3中封装echarts

子组件

<template>
  <div :id="uid" :style="myStyle"></div>
</template>
<script setup>
import { onMounted, onBeforeMount, ref, defineProps, onBeforeUnmount, onUnmounted } from 'vue';
import * as echarts from 'echarts';
// 因为是封装的组件,会多次调用,id不能重复,要在初始化之前写,不然会报错dom为定义
let uid = ref('');
onBeforeMount(() => {
  uid.value = `echarts-uid-${parseInt((Math.random() * 1000000).toString())}`;
});

onMounted(() => {
  let myChart = echarts.init(document.getElementById(uid.value));
  // 在template中可以直接取props中的值,但是在script中不行,因为script是在挂载之前执行的
  myChart.setOption(props.myOption, {
    notMerge: true, //不和之前的option合并
  });

  // 监听页面的大小
  window.addEventListener('resize', () => {
    setTimeout(() => {
      myChart?.resize({
        animation: {
          duration: 300,
        },
      });
    }, 300);
  });
});

const props = defineProps({
  myStyle: {
    type: Object,
    default: () => ({
      width: '100%',
      height: '100%',
    }),
  },
  myOption: {
    type: Object,
    default: () => ({}),
    required: true,
  },
});
</script>

父组件

<template>
  <div class="card">
    <current-echarts :myOption="chartLineData" :myStyle="{ width: '273px', height: '330px' }"></current-echarts>
  </div>
</template>
<script>
import CurrentEcharts from '../../../components/CurrentEcharts.vue';
import { onMounted, reactive, toRefs, getCurrentInstance, onBeforeUnmount, onUnmounted } from 'vue';

export default {
  name: 'ehartLine',
  components: {
    CurrentEcharts
  },
  props: {

  },
  setup(props) {
    const chartLineData = {
      series: [
        {
          type: 'pie',
          radius: ['50%', '70%'],
          avoidLabelOverlap: false,
          label: {
            show: false,
            position: 'center'
          },
          labelLine: {
            show: false
          },
          emphasis: {
            label: {
              show: true,
              fontSize: '30',
              fontWeight: 'bold'
            }
          },
          data: [
            { value: 335, name: 'A' },
            { value: 310, name: 'B' },
            { value: 234, name: 'C' },
            { value: 135, name: 'D' },
            { value: 1548, name: 'E' }
          ]
        }
      ]
    }
    onMounted(() => {
    })
    onUnmounted(() => {

    })
    return {
      chartLineData
    };
  },
};
</script>


<style lang="scss" scoped>
.card {
  background: #FFFFFF;
  opacity: 1;
  border: 1px solid #E7EBF2;
  border-radius: 5px;

  .title {
    font-size: 16px;
    font-family: PingFang SC-Semibold, PingFang SC;
    font-weight: 600;
    color: #34383E;
    padding: 20px 25px 18px 0;
    border-bottom: 1px solid #EEF1F5;
  }
}
</style>
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue Echarts 是一个基于 ECharts 封装Vue.js 组件,可以很方便地在 Vue.js 项目使用 ECharts。下面是一个简单的柱状图封装示例: 1. 安装 EChartsVue Echarts: ``` npm install echarts vue-echarts ``` 2. 创建一个 BarChart.vue 组件: ```html <template> <div ref="chart" style="width:100%;height:100%;"></div> </template> <script> import ECharts from 'vue-echarts'; export default { name: 'BarChart', components: { 'v-chart': ECharts, }, props: { data: { type: Array, required: true, }, xName: { type: String, default: '', }, yName: { type: String, default: '', }, title: { type: String, default: '', }, }, mounted() { this.drawChart(); }, watch: { data() { this.drawChart(); }, }, methods: { drawChart() { const chart = this.$refs.chart; const myChart = this.$refs.chart.echarts.init(chart); myChart.setOption({ title: { text: this.title, }, tooltip: { trigger: 'axis', }, legend: {}, xAxis: { type: 'category', data: this.data.map(item => item.name), name: this.xName, }, yAxis: { type: 'value', name: this.yName, }, series: [ { name: this.yName, type: 'bar', data: this.data.map(item => item.value), }, ], }); }, }, }; </script> ``` 3. 在需要使用柱状图的组件使用: ```html <template> <div> <bar-chart :data="chartData" x-name="X轴名称" y-name="Y轴名称" title="图表标题" /> </div> </template> <script> import BarChart from './BarChart.vue'; export default { name: 'MyComponent', components: { 'bar-chart': BarChart, }, data() { return { chartData: [ { name: '项目1', value: 100 }, { name: '项目2', value: 200 }, { name: '项目3', value: 300 }, ], }; }, }; </script> ``` 这样就可以在页面显示一个简单的柱状图了。可以根据需要在 BarChart.vue 组件添加更多的配置项,以满足不同的需求。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值