插件窝 干货文章 如何修改ECharts环状图中间文字的颜色?

如何修改ECharts环状图中间文字的颜色?

文字 value name 颜色 229    来源:    2025-03-20

在ECharts中,环状图(Pie Chart)中间的文字通常是通过titlelabel来设置的。要修改环状图中间文字的颜色,你可以通过配置titlelabel的样式来实现。

方法1:使用 title 配置

如果你在环状图中间使用了title来显示文字,可以通过设置title.textStyle.color来修改文字颜色。

option = {
    title: {
        text: '中间文字',
        left: 'center',
        top: 'center',
        textStyle: {
            color: '#FF0000', // 修改文字颜色为红色
            fontSize: 20
        }
    },
    series: [
        {
            type: 'pie',
            data: [
                {value: 335, name: 'A'},
                {value: 310, name: 'B'},
                {value: 234, name: 'C'},
                {value: 135, name: 'D'},
                {value: 1548, name: 'E'}
            ]
        }
    ]
};

方法2:使用 label 配置

如果你在环状图中间使用了label来显示文字,可以通过设置label.color来修改文字颜色。

option = {
    series: [
        {
            type: 'pie',
            radius: ['40%', '70%'],
            label: {
                show: true,
                position: 'center',
                formatter: '中间文字',
                color: '#00FF00', // 修改文字颜色为绿色
                fontSize: 20
            },
            data: [
                {value: 335, name: 'A'},
                {value: 310, name: 'B'},
                {value: 234, name: 'C'},
                {value: 135, name: 'D'},
                {value: 1548, name: 'E'}
            ]
        }
    ]
};

方法3:使用 graphic 配置

如果你需要更复杂的布局或样式,可以使用graphic来绘制自定义的文本。

option = {
    graphic: [
        {
            type: 'text',
            left: 'center',
            top: 'center',
            style: {
                text: '中间文字',
                fill: '#0000FF', // 修改文字颜色为蓝色
                fontSize: 20
            }
        }
    ],
    series: [
        {
            type: 'pie',
            data: [
                {value: 335, name: 'A'},
                {value: 310, name: 'B'},
                {value: 234, name: 'C'},
                {value: 135, name: 'D'},
                {value: 1548, name: 'E'}
            ]
        }
    ]
};

总结

  • 使用title配置时,通过title.textStyle.color修改颜色。
  • 使用label配置时,通过label.color修改颜色。
  • 使用graphic配置时,通过graphic.style.fill修改颜色。

根据你的需求选择合适的方法来修改环状图中间文字的颜色。