要将条形图上方加一个数值,需要使用G2的annotation.text()方法来实现。以下是详细的解释和代码示例:

  1. 在数据循环中添加annotation.text()方法,用于在每个柱形图上方添加数值标签。在text()方法中,需要指定位置(position)、内容(content)和样式(style)等属性。

  2. 在position属性中,可以使用具体的坐标值或根据数据字段的值来确定位置。在这里,我们可以使用item.name和item.mounth来确定位置,即柱形图的name和mounth属性值。

  3. 在content属性中,可以设置要显示的数值内容。在这里,我们可以使用item.number来获取数值数据。

  4. 在style属性中,可以设置标签的样式。在这里,我们可以设置文本对齐方式(textAlign)和文本颜色(color)。

  5. 在offsetY属性中,可以设置标签相对于柱形图的垂直偏移量,以调整标签的位置。

下面是完整的代码示例:

<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8">
    <title>分组柱状图</title>
  </head>
  <body>
    <div id="container" />
    <script src="https://gw.alipayobjects.com/os/lib/antv/g2/4.2.8/dist/g2.min.js"></script>
    <script src="https://gw.alipayobjects.com/os/antv/pkg/_antv.data-set-0.11.1/dist/data-set.js"></script>
    <script>
      const data = [
        { name: 'London', mounth: 'Jan.', number: 18.9 },
        { name: 'London', mounth: 'Feb.', number: 28.8 },
        { name: 'London', mounth: 'Mar.', number: 39.3 },
        { name: 'London', mounth: 'Apr.', number: 81.4 },
        { name: 'London', mounth: 'May', number: 47 },
        { name: 'London', mounth: 'Jun.', number: 20.3 },
        { name: 'London', mounth: 'Jul.', number: 24 },
        { name: 'London', mounth: 'Aug.', number: 35.6 },
        { name: 'Berlin', mounth: 'Jan.', number: 12.4 },
        { name: 'Berlin', mounth: 'Feb.', number: 23.2 },
        { name: 'Berlin', mounth: 'Mar.', number: 34.5 },
        { name: 'Berlin', mounth: 'Apr.', number: 99.7 },
        { name: 'Berlin', mounth: 'May', number: 52.6 },
        { name: 'Berlin', mounth: 'Jun.', number: 35.5 },
        { name: 'Berlin', mounth: 'Jul.', number: 37.4 },
        { name: 'Berlin', mounth: 'Aug.', number: 42.4 },
      ];

      const chart = new Chart({
        container: 'container',
        autoFit: true,
        height: 500,
      });

      chart.data(data);
      chart.scale('number', {
        nice: true,
      });
      chart.tooltip({
        showMarkers: false,
        shared: true,
      });
      chart.legend("mounth",{
        itemName:{
          style:{
            // color:
          }
        }
      })

      chart
        .interval()
        .position('mounth*number')
        .color('name')
        .adjust([
          {
            type: 'dodge',
            marginRatio: 0,
          },
        ]);

      chart.interaction('active-region');

      data.forEach((item) => {
        chart
          .annotation()
          .text({
            position: [item.name, item.mounth],
            content: item.number,
            style: {
              textAlign: 'center',
              color: 'red',
            },
            offsetY: -12,
          });
      });

      chart.render();
    </script>
  </body>
</html>

在上述代码中,我们在forEach循环中使用annotation.text()方法来添加数值标签。其中,position属性使用[item.name, item.mounth]来确定标签位置,content属性使用item.number来设置标签内容,style属性设置文本对齐方式为居中(textAlign: 'center'),颜色为红色(color: 'red'),offsetY属性设置标签相对于柱形图的垂直偏移量为-12,即向上移动12个像素。

运行以上代码,即可在每个柱形图上方添加对应的数值标签

!DOCTYPE html html head meta charset=UTF-8 title分组柱状图title head body div id=container script src=httpsgwalipayobjectscomo

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

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