echarts 环形图默认中心文字标题,内容颜色随标签颜色变化,并在环形图内外都显示标签formatter

废话不多说,上效果图:

完整代码见文章末尾 

一、环形图

       中心文字是通过title完成的,通过left和top调整了位置并且使用了textStyle调整了文字样式

触发鼠标悬停时显示的是formatter。

var data = [
  {
    name: '峰',
    value: 4694.0
  },
  {
    name: '平',
    value: 5365.0
  },
  {
    name: '谷',
    value: 6036.0
  }
];
var keys = Object.keys(data);
var option = {
  title: {
    text: '标题内容',
    left: 'center',
    top: '45%',
    textStyle: {
      fontSize: 30,
      lineHeight: 48
    }
  },
  tooltip: {
    trigger: 'item'
  },
  legend: {
    bottom: '1%',
    left: 'center',
    icon: 'circle'
  },
  color: ['#2eacff', '#fd6067', '#f9be0e', '#ef6567', '#f9c956', '#3BA272'],
  series: [
    {
      name: '',
      type: 'pie',
      radius: ['55%', '70%'],
      hoverOffset: 20,
      label: {
        normal: {
          show: false,
          position: 'center'
        },
        emphasis: {
          show: true,
          formatter: function (params) {
            return `{a|${params.name}}\n` + `{b|${params.value}}`;
          },
          //中间文字样式
          rich: {
            a: {
              color: function (params) {},
              fontSize: 30,
              lineHeight: 48
            },
            b: {
              color: function (params) {},
              align: 'center',
              fontSize: 30,
              lineHeight: 48
            }
          }
        },
      },
      data: data
    }
  ]
};
二、 中心内容改变
if (option && typeof option === 'object') {
  myChart.setOption(option);
  //隐藏标题
  myChart.on('highlight', function (e) {
    myChart.setOption({
      title: {
        show: false
      }
    });
  });
  //显示标题
  myChart.on('downplay', function (e) {
    myChart.setOption({
      title: {
        show: true
      }
    });
  });
  myChart.on('mouseover', function (e) {
    // 取消所有高亮
    myChart.dispatchAction({
      type: 'downplay',
      seriesIndex: 0,
      dataIndex: keys
    });
    // 当前高亮
    myChart.dispatchAction({
      type: 'highlight',
      seriesIndex: 0,
      dataIndex: e.dataIndex
    });
    myChart.setOption({
      title: {
        show: false
      }
    });
  });
  myChart.on('mouseout', function (e) {
    // 取消所有高亮
    myChart.dispatchAction({
      type: 'downplay',
      seriesIndex: 0,
      dataIndex: keys
    });
    myChart.setOption({
      title: {
        show: true
      }
    });
  });
}
三、显示外部引导线注释

      这里是通过echarts图中series:[ ] 是数组的特性完成的,用第二个图的formatter覆盖上去完成

 series: [
    {
      name: '',
      type: 'pie',
      radius: ['55%', '70%'],
      hoverOffset: 20,
      label: {
        normal: {
          show: false,
          position: 'center'
        },
        emphasis: {
          show: true,
          formatter: function (params) {
            return `{a|${params.name}}\n` + `{b|${params.value}}`;
          },
          //中间文字样式
          rich: {
            a: {
              color: function (params) {},
              fontSize: 30,
              lineHeight: 48
            },
            b: {
              color: function (params) {},
              align: 'center',
              fontSize: 30,
              lineHeight: 48
            }
          }
        },
      },
      data: data
    },
    {
      type: 'pie',
      radius: ['55%', '70%'],
      avoidLabelOverlap: true,
      label: {
        show: true,
        position: 'outside',
        formatter: '{b}  {c}.0 {d}%',
        textStyle: {
          color: 'gray'
        }
      },
      data: data
    }
  ]
四、完整代码
var data = [
  {
    name: '峰',
    value: 4694.0
  },
  {
    name: '平',
    value: 5365.0
  },
  {
    name: '谷',
    value: 6036.0
  }
];
var keys = Object.keys(data);
var option = {
  title: {
    text: '标题内容',
    left: 'center',
    top: '45%',
    textStyle: {
      fontSize: 30,
      lineHeight: 48
    }
  },
  tooltip: {
    trigger: 'item'
  },
  legend: {
    bottom: '1%',
    left: 'center',
    icon: 'circle'
  },
  color: ['#2eacff', '#fd6067', '#f9be0e', '#ef6567', '#f9c956', '#3BA272'],
  series: [
    {
      name: '',
      type: 'pie',
      radius: ['55%', '70%'],
      hoverOffset: 20,
      label: {
        normal: {
          show: false,
          position: 'center'
        },
        emphasis: {
          show: true,
          formatter: function (params) {
            return `{a|${params.name}}\n` + `{b|${params.value}}`;
          },
          //中间文字样式
          rich: {
            a: {
              color: function (params) {},
              fontSize: 30,
              lineHeight: 48
            },
            b: {
              color: function (params) {},
              align: 'center',
              fontSize: 30,
              lineHeight: 48
            }
          }
        },
      },
      data: data
    },
    {
      type: 'pie',
      radius: ['55%', '70%'],
      avoidLabelOverlap: true,
      label: {
        show: true,
        position: 'outside',
        formatter: '{b}  {c}.0 {d}%',
        textStyle: {
          color: 'gray'
        }
      },
      data: data
    }
  ]
};
if (option && typeof option === 'object') {
  myChart.setOption(option);
  myChart.on('highlight', function (e) {
    myChart.setOption({
      title: {
        show: false
      }
    });
  });
  myChart.on('downplay', function (e) {
    myChart.setOption({
      title: {
        show: true
      }
    });
  });
  myChart.on('mouseover', function (e) {
    // 取消所有高亮
    myChart.dispatchAction({
      type: 'downplay',
      seriesIndex: 0,
      dataIndex: keys
    });
    // 当前高亮
    myChart.dispatchAction({
      type: 'highlight',
      seriesIndex: 0,
      dataIndex: e.dataIndex
    });
    myChart.setOption({
      title: {
        show: false
      }
    });
  });
  myChart.on('mouseout', function (e) {
    // 取消所有高亮
    myChart.dispatchAction({
      type: 'downplay',
      seriesIndex: 0,
      dataIndex: keys
    });
    myChart.setOption({
      title: {
        show: true
      }
    });
  });
}

    本文章为免费阅读,若有对您帮助,请多多点赞!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值