javascript 在discreteBarChart nvd3.js 中自定义带有数据的工具提示内容

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22713400/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 23:38:46  来源:igfitidea点击:

custom a tooltipContent of tooltips with datum in discreteBarChart nvd3.js

javascriptd3.jstooltipnvd3.js

提问by Fernando Montoya

How I can custom a tooltipContent of tooltips using data loaded into "datum" in discreteBarChart nvd3.js?, for example, with the following data Jason, I want to see data3, data4, Data5 in tooltips

如何使用加载到“datum”中的数据在discreteBarChart nvd3.js中自定义工具提示的tooltipContent?例如,使用以下数据Jason,我想在工具提示中看到data3、data4、Data5

JsonData = [ 
             {
               key: "Serie1",
               values: [
                         {'Data1':  1, 
                          'Data2':  2, 
                          'Data3':  3,
                          'Data4':  4,
                          'Data5':  5
                         }
                       ]
             }
           ];

回答by Fernando Montoya

This is how to do it:

这是如何做到的:

nv.addGraph(function() {  
   var chart = nv.models.discreteBarChart()
      .x(function(d) { return d.Data1 })
      .y(function(d) { return d.Data2 })
      .tooltips(true)
      .tooltipContent(function(key, y, e, graph) {
       var data =graph.series.values[y-1];
       return  '<p> Text1: ' +  data.Data3 + '</p>'
             + '<p> Text2: ' +  data.Data4 + '</p>'
             + '<p> Text3: ' +  data.Data5 + '</p>'
       });

   d3.select('#chart svg')
      .datum(JsonData)
      .call(chart);

   nv.utils.windowResize(chart.update);

   return chart;
});

回答by xJREB

I came up with something like this, since the graph object has a value attribute:

我想出了这样的东西,因为图形对象有一个 value 属性:

chart.tooltipContent(function (key, date, e, graph) {
     var value = graph.value;
     return  "<p><strong>" + date + "</strong><br>" + value + "</p>";
});

No need for accessing the series-array I guess.

我猜不需要访问系列数组。