how do i format the value shown in a d3 graph

只愿长相守 提交于 2019-12-07 03:05:42

问题


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/


回答1:


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



来源:https://stackoverflow.com/questions/15370713/how-do-i-format-the-value-shown-in-a-d3-graph

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!