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

大兔子大兔子 提交于 2019-11-30 23:25:22

问题


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

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

回答1:


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;
});



回答2:


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

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.



来源:https://stackoverflow.com/questions/22713400/custom-a-tooltipcontent-of-tooltips-with-datum-in-discretebarchart-nvd3-js

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!