【问题标题】:NVD3 - tickFormat for currency issuesNVD3 - 货币问题的tickFormat
【发布时间】:2014-10-13 07:16:54
【问题描述】:

现在我正试图弄清楚如何让我的 y 轴看起来更适合货币。您可以注意到下图有两个问题:

  1. 顶部和底部的数字包含难看的小数位
  2. “G”应该是“B”,代表十亿

我能做些什么来清理这种东西。这是我当前的 y 轴代码:

var commasFormatter = d3.format("1s");

chart.yAxis
  .tickFormat(function(d) { return "$" + commasFormatter(d); })
  .axisLabelDistance(10)
  .axisLabel('Profits')        
  ;

【问题讨论】:

    标签: javascript d3.js nvd3.js


    【解决方案1】:

    环顾四周后,我找到了解决方案。

    为格式编写了一个变量,使用 .3s 将数字减少到 3 并附加一个后缀 M 以表示百万。

    var formatAxis = d3.format('.3s');
    

    然后在.tickFormat 上添加一个函数,将G 替换为B

    .tickFormat(function(val) { return formatAxis(val).replace('G', 'B'); })
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-01-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-31
      • 1970-01-01
      相关资源
      最近更新 更多