如何在nvd3多参数图上显示值

How to display values on top of nvd3 multi par graph?

本文关键字:显示 参数 nvd3      更新时间:2023-09-26

我希望每个栏的实际值按照这里的方式显示在顶部

我正在多条形图上尝试。

找不到参考

如何在堆叠多柱图中显示值- nvd3图形

有一个修复你可以自己实现https://gist.github.com/topicus/217444acb4204f364e46

编辑:如果github链接被删除,复制代码:

// You need to apply this once all the animations are already finished. Otherwise labels will be placed wrongly.
d3.selectAll('.nv-multibar .nv-group').each(function(group){
  var g = d3.select(this);
  // Remove previous labels if there is any
  g.selectAll('text').remove();
  g.selectAll('.nv-bar').each(function(bar){
    var b = d3.select(this);
    var barWidth = b.attr('width');
    var barHeight = b.attr('height');
    g.append('text')
      // Transforms shift the origin point then the x and y of the bar
      // is altered by this transform. In order to align the labels
      // we need to apply this transform to those.
      .attr('transform', b.attr('transform'))
      .text(function(){
        // Two decimals format
        return parseFloat(bar.y).toFixed(2);
      })
      .attr('y', function(){
        // Center label vertically
        var height = this.getBBox().height;
        return parseFloat(b.attr('y')) - 10; // 10 is the label's magin from the bar
      })
      .attr('x', function(){
        // Center label horizontally
        var width = this.getBBox().width;
        return parseFloat(b.attr('x')) + (parseFloat(barWidth) / 2) - (width / 2);
      })
      .attr('class', 'bar-values');
  });
});

显然这还不存在。有一个问题(https://github.com/novus/nvd3/issues/150)已经关闭,因为这(显然)很难实现。

我不知道你已经尝试了这么多,但在这里的例子是相当直接的。

.showValues(true)几乎做到了这一点。

希望能有所帮助。