[Echarts] The realization of Pie Special Label (pie chart)! ! Come take a look! !

Implementation of Pie Special Label (Pie Chart)

Look at the effect first

Insert picture description here

File Directory

Insert picture description here

Get Echarts

Insert picture description here

Introduce Echarts

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <!-- 引入 ECharts 文件 -->
    <script src="../incubator-echarts-5.0.0-alpha.2/dist/echarts.min.js"></script>
</head>
</html>

Draw a chart

Before drawing, we need to prepare a DOM container with height and width for ECharts

<body style="background: black;">
    <!-- 为 ECharts 准备一个具备大小(宽高)的 DOM -->
    <div id="main" style="width: 1710px; height: 670px"></div>
</body>

Then you can initialize an echarts instance through the echarts.init method and generate a Pie Special Label (pie chart) through the setOption method

 <script type="text/javascript">
   // 基于准备好的dom, 初始化echarts实例
   var myChart = echarts.init(document.getElementById('main'));
   // 使用刚指定的配置项和数据显示图表
   myChart.setOption(option)
 </script>

The complete code is as follows

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>租房数据</title>
  <script src="../incubator-echarts-5.0.0-alpha.2/dist/echarts.min.js"></script>
</head>
<body style="background: black;">
  <!-- 为 ECharts 准备一个具备大小(宽高)的 DOM -->
  <div id="main" style="width: 1710px; height: 670px"></div>
  <script type="text/javascript">
    // 基于准备好的dom, 初始化echarts实例
    var myChart = echarts.init(document.getElementById('main'));
    
    // 数据源
    const dataList = [
      {
     
     name: '附近租房', value: 67},
      {
     
     name: '当地租房', value: 33}
    ]

    // 自定义富文本样式
    var rich = {
     
     
      otherCenter: {
     
     
        borderColor: '#6482ff',
        width: 0,
        height: 40,
        align: 'center',
        borderWidth: 1
      },
      otherCenter1: {
     
     
        borderColor: '#6482ff',
        width: '100%',
        height: 0,
        borderWidth: 1
      },
      otherCenterNumColor: {
     
     
        color: '#6482ff',
        fontSize: '28',
        align: 'left',
        lineHeight: 45
      },
      otherCenterTextColor: {
     
     
        color: '#c2cbf2',
        fontSize: '24',
        align: 'left'
      },
      center: {
     
     
        borderColor: '#25eaff',
        width: 0,
        align: 'center',
        borderWidth: 1,
        height: 40
      },
      center1: {
     
     
        borderColor: '#25eaff',
        width:'100%',
        borderWidth: 1,
        height: 0
      },
      centerNumColor: {
     
     
        color: '#25eaff',
        fontSize: '24',
        align: 'left',
        lineHeight: 45
      },
      centerTextColor: {
     
     
        color: '#c2cbf2',
        fontSize: '24',
        align: 'left'
      }
    }

    var option = {
     
     
      title: {
     
     
        text: '租房数据',
        show: true,
        textStyle: {
     
     
          color: '#fff',
          fontSize: '36',
          fontFamily: 'Microsoft YaHei',
          fontWeight: 400
        },
        top: '42',
        left: '40'
      },
      tooltip: {
     
     
        trigger: 'item',
        formatter: '{a} <br/>{b} : {c} ({d}%)'
      },
      series: [
        {
     
      
          type: 'pie',
          data: dataList.map((item, index) => {
     
     
            return {
     
     
              ...(item || {
     
     }),
              label: {
     
     
                normal: {
     
     
                  show: true,
                  rich: rich,
                  padding: [0, -53],
                  height: 221,
                  formatter: (params) => {
     
     
                    if(index === 0) {
     
     
                      return (
                        `{otherCenterTextColor|附近租房}` +
                        '\n' + 
                        `{otherCenterNumColor|${
       
       Math.round(params.value)}%}` +
                        '\n{otherCenter1|}\n{otherCenter|}\n'
                      )
                    }
                    return (
                      `{centerTextColor|当地租房}` + 
                      '\n' + 
                      `{centerNumColor|${
       
       Math.round(params.value)}%}` + 
                      '\n{center1|}\n{center|}\n'
                    )
                  }
                }
              }
            }
          }),
          labelLine: {
     
     
            normal: {
     
     
              show: true,
              length: 100,
              length2: 100
            }
          }
        },
        {
     
      
          type: 'pie',
          radius: [0, '60%'],
          data: dataList.map((item, index) => {
     
     
            if(index === 1) {
     
     
              return {
     
     
                ...(item || {
     
     }),
                itemStyle: {
     
     
                  color: 'rgba(0,0,0,0)'
                }
              }
            }
            return item;
          })
        }
      ]
    }
 

    // 使用刚指定的配置项和数据显示图表
    myChart.setOption(option)
  </script>
</body>
</html>

Guess you like

Origin blog.csdn.net/weixin_43352901/article/details/108514897