how do i format the value shown in a d3 graph
Asked Answered
B

1

6

I am trying to change the formatting on the value shown on top of the bar graph in a nvd3 discrete bar graph. I believe I am doing something wrong, I have no idea.

Here is the js fiddle for the graph http://jsfiddle.net/looneydoodle/PdpRq/

Here is the code

var basicformat = d3.format(',f');
d3graph.selectAll('g.nv-bar text').each(function(d,i){
  this.text(basicformat(this.value));
});

EDIT:

Figured out how to do it, although it might be a bad way to do it. Here is the updated fiddle: http://jsfiddle.net/looneydoodle/PdpRq/2/

Bonus answered 12/3, 2013 at 19:46 Comment(0)
P
15

Looks like the discreteBarChart has a valueFormat() method. You can sorta see it defined in the nvd3 source on line 232.

So, to set a format other than the default that you're seeing, all you need is:

chart.valueFormat(d3.format('f'))// Or whatever format you'd like

Here's the jsFiddle

Phytogenesis answered 12/3, 2013 at 20:51 Comment(3)
thanks @meetamit! any idea how i can add another x axis so i can show percent values for each bar?Bonus
@Bonus NP. Not sure exactly what's the right way to do it, or that using another x axis is the best approach. But here's one way I can think of: jsfiddle.net/mrUSj/1. Works ok if the chart is not too narrow.Phytogenesis
I think I have to add another graph below it, i dont seem to be able to add another x axis.Bonus

© 2022 - 2024 — McMap. All rights reserved.