Javascript NVD3.js yAxis和工具提示不同的感知

Javascript NVD3.js yAxis和工具提示不同的感知,javascript,d3.js,nvd3.js,Javascript,D3.js,Nvd3.js,我正在使用NVD3.js显示一个多折线图 我希望yAxis显示2个十进制数字 编辑答案 但是在工具提示中,我想显示12位小数 这可能吗?您可以通过图表对象的.tooltipContent设置调用的函数,以格式化工具提示的内容。默认函数定义如下 tooltip = function(key, x, y, e, graph) { return '<h3>' + key + '</h3>' + '<p>' + y + ' at ' +

我正在使用NVD3.js显示一个多折线图

我希望yAxis显示2个十进制数字

编辑答案

但是在工具提示中,我想显示12位小数


这可能吗?

您可以通过图表对象的.tooltipContent设置调用的函数,以格式化工具提示的内容。默认函数定义如下

tooltip = function(key, x, y, e, graph) {
    return '<h3>' + key + '</h3>' +
           '<p>' +  y + ' at ' + x + '</p>'
}

这意味着轴的勾号格式将影响它,因此为了获得更高的精度,您需要直接获取值-线可以通过chart.lines访问。

在nvd3源中,它们具有类似于线形图的线条:

 interactiveLayer.dispatch.on('elementMousemove', function(e) {
  ...
      var xValue = xAxis.tickFormat()(chart.x()(singlePoint,pointIndex));
      interactiveLayer.tooltip
              .position({left: pointXLocation + margin.left, top: e.mouseY + margin.top})
              ...
              .valueFormatter(function(d,i) {
                 return yAxis.tickFormat()(d);
              })
              .data(
                  {
                    value: xValue,
                    series: allData
                  }
              )();
...
}
因此,如果您想要覆盖tooltip.contentGenerator之类的内容,那么您将被它提供给您的数据所困扰。我认为这样做的一个好方法是为工具提示x格式化程序或其他东西设置一个setter,然后它将在axis格式化程序上使用它。或者简单地发送原始数据以及值和序列

我的用例是在图表上显示7天的时间刻度,我只勾选每天的开始。不过,在工具提示中,我想给出该数据的更详细视图

下面是我的拉取请求,具体到指南工具提示


调用:chart.tooltipXAxisFormatterd3.time.formattoolTipFormat;将设置它。

我确实尝试过编辑该函数,但它对工具提示没有影响。您是否使用源存储库中最新版本的NVD3?你用的是哪种图表类型?是的,我昨天拉的,我用的是折线图模型。chart=nv.models.linechart此时应该可以工作。你能给我们看一下完整的代码吗?@Larskothoff-我假设一旦数据使用.tickFormatd3.format',.2f'格式化到小数点后2位,图表就会在整个图表中使用应用的数据格式,即使数据显示在工具提示中。那么,是否可以使用y在工具提示中显示12位小数
yAxis.tickFormat()(lines.y()(e.point, e.pointIndex))
 interactiveLayer.dispatch.on('elementMousemove', function(e) {
  ...
      var xValue = xAxis.tickFormat()(chart.x()(singlePoint,pointIndex));
      interactiveLayer.tooltip
              .position({left: pointXLocation + margin.left, top: e.mouseY + margin.top})
              ...
              .valueFormatter(function(d,i) {
                 return yAxis.tickFormat()(d);
              })
              .data(
                  {
                    value: xValue,
                    series: allData
                  }
              )();
...
}