在 AntV 柱状图中,在柱子中间和上方一起显示文字需要使用 label 配置项。具体实现步骤如下:

  1. 在图表中添加 label 配置项,设置 label 的样式和位置。如下所示:
const chart = new Chart({
  container: 'container',
  autoFit: true,
  height: 500,
});

chart.data(data);

chart.scale({
  sales: {
    max: 5000,
    min: 0,
  },
});

chart.axis('city', {
  label: {
    offset: 12, // 设置label距离坐标轴的偏移量
    style: {
      fontSize: 14,
    },
  },
});

chart.axis('sales', {
  label: {
    style: {
      fontSize: 14,
    },
  },
});

chart.coordinate().transpose();

chart.interval().position('city*sales').color('category')
  .label('sales', {
    style: {
      fill: '#fff',
    },
    position: 'middle', // 在柱子中间显示label
    offset: 0, // 设置偏移量
  })
  .label('sales', {
    style: {
      fill: '#000',
    },
    position: 'top', // 在柱子顶部显示label
  });

chart.render();
  1. 在 interval 上添加两次 label,分别设置位置为 middle 和 top,来实现在柱子中间和上方一起显示文字的效果。

通过上述代码实现,在 AntV 中柱状图中,在柱子中间和上方一起显示文字。

AntV 柱状图:如何在柱子中间和上方同时显示文字

原文地址: https://www.cveoy.top/t/topic/mQuC 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录