How to display values ​​over nvd3 multiparameter graph?

I want the actual value of each bar to be displayed at the top as it is shown here

I try this on a multi-line chart .

Unable to find the link anywhere.

+5
source share
3 answers

Duplicate How to Display Values ​​in a Stacked Multi-bar chart - nvd3 Charts

There is a fix that you can implement at https://gist.github.com/topicus/217444acb4204f364e46

EDIT: copied the code if the github link is removed:

// 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 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');
  });
});
+4
source
+2

I'm not sure you tried so thickly, but the example in here is pretty simple.

.showValues(true) pretty much doing the trick.

Hope this helps.

+1
source

All Articles