引入echarts环形图及显示后端数据

实现效果:

1.下载echarts 并引用

复制代码
npm install echarts --save 或 pnpm install -S echarts

项目中引用:

在项目main.ts中 import * as echarts from "echarts"; //引入echarts

3.页面中使用

<div id="main" class="chart" :option="option"></div>//定义一个有宽高的盒子

复制代码
//图表
const echartInit = async () => {
  var chartDom = document.getElementById('main');
  var myChart = echarts.init(chartDom);
  var option;
  let datas = []
  let numAll = ('0')
//调用后台接口
  const res = await getType();
  if (res && res.code == 200) {
    datas = res.data

  }
  //总计
   datas.forEach((item) => {
     let value = parseInt(item.value)
     numAll = numAll+ value - 0
  })
  // 指定图表的配置项和数据
  option = {
    // title: {
    //   text: '诉求类型分布',
    //   left: 'center',
    //   textStyle: {
    //   color: '#fff',
    //   fontWeight: 'normal',
    //   fontSize: 18
    // }
    // },
    graphic: [
          {
            //环形图中间添加文字
            type: "text", //通过不同top值可以设置上下显示
            left: "45%",
            top: "47%",
            style: {
              text: `总计\n\n${numAll}条`,
              textAlign: "center",
              fill: "#fff", //文字的颜色
              fontSize: 12,
              lineHeight: 10,
            },
          },
        ],
    tooltip: {
      trigger: 'item'
    },
    legend: {
      // top: '5%',
      // orient: 'vertical',
      left: 'center',
      bottom: -5,
      icon: 'circle',
      textStyle: {//图例文字的样式
        color: '#fff',
        fontSize:12,
      }

    },
    color:['#33ff99','#6699ff','#be99FF','#FFFF66'],//修改圆形图颜色
    series: [
      {
        name: '诉求类型',
        type: 'pie',
        radius: ['35%', '50%'],
        // center:['30%', '50%'],
        avoidLabelOverlap: true,
        minAngle: 20,//最小的扇区角度(0 ~ 360),用于防止某个值过小导致扇区太小影响交互
        itemStyle: {
          borderRadius: 10,
          borderColor: '#fff',
          borderWidth: 2
        },
        label: {
          show: false,
          normal: {
                   show: true,
                   lineHeight:10,
                   // position: 'inside',
                  formatter: '{b}{d}%\n\n{c}条', //自定义显示格式(b:name, c:value, d:百分比)
                  fontSize: 10   // 字体大小
                  },
          // position: 'center'
        },
        labelLine: {
         length: 20,
         length2: 50,
         maxSurfaceAngle: 80
        },
       emphasis:{
          shadowBlur: 10,
          hadowOffsetX: 0,
          shadowColor: 'rgba(0, 0, 0, 0.5)'
          },
        emphasis: {
          label: {
            show: true,
            fontSize: 12,
            fontWeight: 'bold'
          }
        },
        itemStyle: {
             borderColor: '#fff',
             borderWidth: -1
             },
        data: datas
      }
    ]
  };
  // 使用刚指定的配置项和数据显示图表。
  option && myChart.setOption(option);

}

可根据自己需求配置相关样式

相关推荐
抹茶冰淇淋1 小时前
降级系统后,2019年的Mac电脑重获新生
前端
雪碧聊技术2 小时前
前端VUE3项目部署到linux服务器(CentOS 7)
前端·linux部署vue3项目
酒尘&8 小时前
JS数组不止Array!索引集合类全面解析
开发语言·前端·javascript·学习·js
学历真的很重要8 小时前
VsCode+Roo Code+Gemini 2.5 Pro+Gemini Balance AI辅助编程环境搭建(理论上通过多个Api Key负载均衡达到无限免费Gemini 2.5 Pro)
前端·人工智能·vscode·后端·语言模型·负载均衡·ai编程
用户47949283569159 小时前
"讲讲原型链" —— 面试官最爱问的 JavaScript 基础
前端·javascript·面试
用户47949283569159 小时前
2025 年 TC39 都在忙什么?Import Bytes、Iterator Chunking 来了
前端·javascript·面试
大怪v10 小时前
【Virtual World 04】我们的目标,无限宇宙!!
前端·javascript·代码规范
狂炫冰美式11 小时前
不谈技术,搞点文化 🧀 —— 从复活一句明代残诗破局产品迭代
前端·人工智能·后端
xw511 小时前
npm几个实用命令
前端·npm
!win !12 小时前
npm几个实用命令
前端·npm