如何在nvd3多图表上显示值?

azi*_*azi 5 javascript d3.js nvd3.js

我希望每个条形图的实际值以这里显示的方式显示在顶部

我在多条形图上尝试这个.

在任何地方都找不到参考.

Bjö*_*son 5

如何在堆叠多条形图中显示值的重复- nvd3 Graphs

您可以在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');
  });
});
Run Code Online (Sandbox Code Playgroud)


sha*_*r90 -1

我不确定你尝试过什么,但这里的例子非常简单。

.showValues(true)几乎可以达到目的。

希望能帮助到你。