Javascript 如何更改离散条形图中整数的十进制值

Javascript 如何更改离散条形图中整数的十进制值,javascript,nvd3.js,Javascript,Nvd3.js,我正在使用离散条形图(NVD3库)在我的站点中显示图表。 显示图形时一切都很好,但我的问题是如何将十进制值更改为整数。 显示在y轴上的值显示整数值。 尽管我使用哪个数组来显示具有整数值的值。下面是我使用的js代码 nv.addGraph(function() { var chart = nv.models.discreteBarChart() .x(function(d) { return d.label }) .y(function(d) { retur

我正在使用离散条形图(NVD3库)在我的站点中显示图表。 显示图形时一切都很好,但我的问题是如何将十进制值更改为整数。 显示在y轴上的值显示整数值。 尽管我使用哪个数组来显示具有整数值的值。下面是我使用的js代码

     nv.addGraph(function() {  
     var chart = nv.models.discreteBarChart()
    .x(function(d) { return d.label })
    .y(function(d) { return d.value })
    .staggerLabels(true)
    .tooltips(false)
    .showValues(true)
    .transitionDuration(250);
    chart.xAxis.axisLabel("<?php echo $configureGrid['x_axis']['legend']; ?>");
    chart.yAxis.axisLabel("<?php echo $configureGrid['y_axis']['legend']; ?>");
    d3.select('#issue_by_time svg')
    .datum(historicalBarChart)
    .call(chart);
    nv.utils.windowResize(chart.update);
    return chart;
    });
    });
nv.addGraph(函数(){
var chart=nv.models.discreteBarChart()
.x(函数(d){返回d.label})
.y(函数(d){返回d.value})
.错误标签(正确)
.工具提示(错误)
.showValues(真)
.过渡期(250);
chart.xAxis.axisLabel(“”);
chart.yAxis.axisLabel(“”);
d3.选择(“#按时间发布svg”)
.基准面(历史条形图)
.电话(图表);
nv.utils.windowResize(图表更新);
收益表;
});
});

您不能在NVD3中直接执行此操作,但通过手动选择
文本
元素并更改文本,您可以。生成图表后运行的以下代码将处理该问题

d3.selectAll(".nv-y.nv-axis")
  .selectAll("text[text-anchor=end]")
  .text(function() { return Math.round(d3.select(this).text()); });
用这个,

chart.yAxis.tickFormat(d3.format(',f'));
将y轴的小数值转换为整数的步骤

chart.xAxis.tickFormat(d3.format(',f'));
将x轴的小数值转换为整数的步骤

chart.valueFormat(d3.format('d'));
要将小数值转换为整数,需要通过图形输入chart.valueFormat(d3.format('d'); 这显示了数据表中的精确值 (可以是整数或浮点)

要在showValue选项上显示确切的值,您可以使用
.valueFormat(d3.format('.0'))

进一步回答@moaz-ateeq,在这方面

yAxis: {
  tickFormat: function(d){ return d3.format(',f')(d) }
}

我们可以直接在Lars kotthoff处这样做。看我的答案。