我如何格式化d3图中显示的值 [英] how do i format the value shown in a d3 graph

查看:101
本文介绍了我如何格式化d3图中显示的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试更改nvd3离散条形图中条形图顶部显示的值的格式.我相信我做错了,我不知道.

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.

这是该图的js小提琴 http://jsfiddle.net/looneydoodle/PdpRq/

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

这是代码

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

弄清楚怎么做,尽管这可能是一个不好的方法. 这是更新的小提琴: http://jsfiddle.net/looneydoodle/PdpRq/2/

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/

推荐答案

好像离散BarChart具有valueFormat()方法.您可以在232行的 nvd3源中看到定义的内容./a>

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

这是jsFiddle

这篇关于我如何格式化d3图中显示的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆