You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何移除Chart.js堆叠柱状图中各分段的数值标签?

问题描述

堆叠柱状图示例

我已成功使用Chart.js创建了堆叠柱状图,目前唯一的问题是每个堆叠分段上都带有显示具体数值的标签。请问有人知道在Chart.js中通过什么配置可以移除这些标签吗?

我已查阅官方文档并搜索了其他网站,但始终找不到这些数值标签的官方称谓。

我的配置代码如下:

DefaultBar:{
    Type: 'bar',
    data:{
        datasets:[],
        labels:[]
    },
    options:{
        responsive:true,
        plugins:{
            title:{
                display:true,
                text: 'Discontinue Reasons',
                font:{
                    size: 20,
                    weight: 'bold'
                }
            },
            subtitle:{
                display:true,
                text: 'Additional Subtitles'
            },
            legend:{
                display: true,
                position: 'right'
            },
            tooltip:{
                enabled: true
            }
        },
        scales: {
            y:{
                title:{
                    display:true, 
                    text: 'Count',
                    font:{
                        size: 18
                    }
                },
                stacked:true,
                border:{
                    width: 2,
                }
            },
            x:{
                stacked: true,
                title:{
                    display:true,
                    text: 'Year',
                    font:{
                        size: 18
                    }
                },
                border:{
                    width:2,
                }
            }
        }
    }
}
解决方案

这些堆叠分段上的数值标签官方叫数据标签(data labels),一般是通过chartjs-plugin-datalabels第三方插件实现的(Chart.js核心本身不自带此功能)。要移除它们,只需在配置的plugins节点下添加禁用数据标签的配置:

修改你的options.plugins部分,新增以下内容:

datalabels: {
    display: false
}

完整修改后的配置代码:

DefaultBar:{
    Type: 'bar',
    data:{
        datasets:[],
        labels:[]
    },
    options:{
        responsive:true,
        plugins:{
            title:{
                display:true,
                text: 'Discontinue Reasons',
                font:{
                    size: 20,
                    weight: 'bold'
                }
            },
            subtitle:{
                display:true,
                text: 'Additional Subtitles'
            },
            legend:{
                display: true,
                position: 'right'
            },
            tooltip:{
                enabled: true
            },
            // 新增禁用数据标签的配置
            datalabels: {
                display: false
            }
        },
        scales: {
            y:{
                title:{
                    display:true, 
                    text: 'Count',
                    font:{
                        size: 18
                    }
                },
                stacked:true,
                border:{
                    width: 2,
                }
            },
            x:{
                stacked: true,
                title:{
                    display:true,
                    text: 'Year',
                    font:{
                        size: 18
                    }
                },
                border:{
                    width:2,
                }
            }
        }
    }
}

如果初始化图表时单独注册过该插件,直接去掉插件注册代码也能达到同样效果。

内容的提问来源于stack exchange,提问作者Gabriel Wilkins

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.14 00:54:50