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

3

6

I want the actual value of each bar displayed on top in the way it's shown here

I am trying this on multi bar chart.

Can't find reference anywhere.

Presumptive answered 26/4, 2013 at 11:26 Comment(0)
H
5

Duplicate of How to display values in Stacked Multi-bar chart - nvd3 Graphs

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

EDIT: Copied the code if the github link gets 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'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');
  });
});
Hershelhershell answered 21/10, 2015 at 8:12 Comment(0)
M
2

Apparently this doesn't exist yet. There is an issue (https://github.com/novus/nvd3/issues/150) that was closed because this is (apparently) hard to implement.

Mesmerize answered 17/3, 2014 at 9:34 Comment(0)
T
-1

I am not sure what you have tried so fat, but the example in here is pretty straight forward.

.showValues(true) pretty much does the trick.

Hope it helps.

Thaxter answered 26/4, 2013 at 11:41 Comment(3)
thanks bro.. but i was trying to get this working on a multi bar chart.. i have now edited the question to mention this explicitly.Presumptive
@Presumptive - did you try adding that line into the multi bar chart as well? what have you done so far?Thaxter
yep, i did try that.. didn't work... i have the multi bar chart working on production.. just wanted to display the values on top... didn't find any reference anywhere... so haven't tried anything(other than what you suggested)Presumptive

© 2022 - 2024 — McMap. All rights reserved.