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

Chart.js移除图表顶部标签及调整标签至右侧的方法咨询

Chart.js 图例配置解决方案

你提到的「数据说明标签」是Chart.js默认生成的图例(legend),两个需求都可以通过修改图表配置快速实现:

1. 完全移除图例

在图表配置的options字段中添加图例隐藏配置即可:

options: {
  legend: {
    display: false
  }
}

2. 将图例放置在图表右侧

如果需要保留图例仅调整位置,修改legend的position属性为right即可:

options: {
  legend: {
    position: 'right'
  }
}

完整配置示例

合并到你现有代码的最终效果如下,两个方案按需二选一即可:

{
  type: 'bar',
  data: {
    labels: data.map(x => moment(x.date).format("MMM Do")),
    datasets: [{
      data: data.map(x => x.premium),
      backgroundColor: '#ffec87',
      borderColor: "#ffec87",
      borderWidth: 2,
      borderStyle: 'dotted'
    }]
  },
  // 新增配置段
  options: {
    // 方案1:完全隐藏图例启用该配置
    legend: {
      display: false
    }
    // 方案2:将图例放在右侧启用该配置,和方案1互斥
    // legend: {
    //   position: 'right'
    // }
  }
}

注:如果你使用的是Chart.js 3.x及以上版本,上述配置需要调整路径为options.plugins.legend,示例:

options: {
  plugins: {
    legend: {
      display: false // 或position: 'right'
    }
  }
}

内容的提问来源于stack exchange,提问作者the wind

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.27 19:15:06