条形图还可以配上实际的数值,我们通过文本元素添加数据值。

svg.selectAll("text")
    .data(dataset)
    .enter()
    .append("text")
    .text(function(d){
        return d;
    })

通过 x 和 y 值来定位文本元素。

               .attr("text-anchor", "middle")
               .attr("x", function(d, i) {
                       return i * (w / dataset.length) + (w / dataset.length - barPadding) / 2;
               })
               .attr("y", function(d) {
                       return h - (d * 4) + 14;
               })
               .attr("font-family", "sans-serif")
               .attr("font-size", "11px")
               .attr("fill", "white");

 

结果如图所示:

D3.js 加标签

 

相关文章:

  • 2022-12-23
  • 2021-11-23
  • 2021-11-13
  • 2022-12-23
  • 2021-11-02
  • 2022-12-23
猜你喜欢
  • 2021-06-09
  • 2021-04-08
  • 2022-12-23
  • 2022-12-23
  • 2021-11-23
  • 2021-12-19
  • 2021-11-23
相关资源
相似解决方案