使用v-for画Echarts环形图图表

 html部分,echarts图示根据id在找到要绘画的位置,这里v-for循环出来两个id就可以画两个echarts图。

<div v-for="(item,i) in xxxx" :key="i">
  <div :id="`chart_${i}`"></div>
</div>

 js部分,绘画echarts图的是写在一个方法里面的。

loadCharts() {
  const V = this;
  V.xxxx.forEach((item,i) => {
  const chart = V.$echarts.init(document.getElementById(`chart_${i}`));
  chart2.setOption({
    title: {
      text: item.percer,
      left: "center",
      top: "40%",
      textStyle: {
        color: item.color,
        fontSize: 14,
        align: "center"
      }
    },
    graphic:{
      type:"text",
      left:"center",
      top:"55%",
      style:{
        text: item.name,
        textAlign: "center",
        fill:item.color,
        fontSize:12,
        fontWeight:1000
      }
    },
    tooltip: {
      show: false,
      trigger: 'item'
    },
    color:[item.color,'#2a2a2a'],

    series: [
      {
        name: '访问来源',
        type: 'pie',
        radius: ['55%', '70%'],
        center: ['50%', '55%'],
        avoidLabelOverlap: false,
        hoverAnimation: false,
        label: {
          show: false,
          position: 'center'
        },
        labelLine: {
          show: false
        },
        data: [
          {value: item.value, name: item.name},
          {value: sum - item.value, name:''},
        ],
      }
    ]
  });
  window.addEventListener('resize', function () {
    chart.resize();
  });
})
}

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
要在 Vue3 中 Echarts 环形图表,可以按照以下步骤进行: 1. 安装 Echarts 和 Vue-Echarts: ``` npm install echarts vue-echarts@5.0.0-beta.5 ``` 2. 在需要使用的组件中引入 Echarts 和 Vue-Echarts: ```javascript import * as echarts from 'echarts' import { use } from 'echarts/core' import { CanvasRenderer } from 'echarts/renderers' import { PieChart } from 'echarts/charts' import { LegendComponent, TooltipComponent } from 'echarts/components' import VueECharts from 'vue-echarts' use([CanvasRenderer, PieChart, LegendComponent, TooltipComponent]) export default { components: { VueECharts } } ``` 3. 在组件的模板中使用 Vue-Echarts 组件,并传入需要渲染的数据和 Echarts 实例: ```vue <template> <div> <vue-echarts :options="options" :auto-resize="true" :renderer="'canvas'" :chart="chart"></vue-echarts> </div> </template> <script> export default { data() { return { chart: null, options: { title: { text: '环形示例' }, tooltip: { trigger: 'item', formatter: '{a} <br/>{b}: {c} ({d}%)' }, legend: { orient: 'vertical', left: 10, data: ['数据1', '数据2', '数据3', '数据4', '数据5'] }, series: [ { name: '访问来源', type: 'pie', radius: ['50%', '70%'], avoidLabelOverlap: false, label: { show: false, position: 'center' }, emphasis: { label: { show: true, fontSize: '30', fontWeight: 'bold' } }, labelLine: { show: false }, data: [ { value: 335, name: '数据1' }, { value: 310, name: '数据2' }, { value: 234, name: '数据3' }, { value: 135, name: '数据4' }, { value: 1548, name: '数据5' } ] } ] } } }, mounted() { this.chart = echarts.init(this.$refs.chart) } } </script> ``` 这样就可以在 Vue3 中出一个简单的 Echarts 环形图表了。注意,在组件 mounted 钩子函数中,需要使用 Echarts.init 方法初始化 Echarts 实例。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值