nvd3 piechart.js - How to edit the tooltip?
Asked Answered
G

9

40

I'm using nvd3's piechart.js component to generate a piechart on my site. The provided .js file includes several var's, as follows:

var margin = {top: 30, right: 20, bottom: 20, left: 20}
    , width = null
    , height = null
    , showLegend = true
    , color = nv.utils.defaultColor()
    , tooltips = true
    , tooltip = function(key, y, e, graph) {
        return '<h3>' + key + '</h3>' +
               '<p>' +  y + '</p>'
      }
    , noData = "No Data Available."
    , dispatch = d3.dispatch('tooltipShow', 'tooltipHide')
;

In my in-line js, I've been able to override some of those variables, like this (overriding showLegend and margin):

var chart = nv.models.pieChart()
    .x(function(d) { return d.label })
    .y(function(d) { return d.value })
    .showLabels(false)
    .showLegend(false)
    .margin({top: 10, right: 0, bottom: 0, left: 0})
    .donut(true);

I've tried overwriting the tooltip in the same way:

.tooltip(function(key, y, e, graph) { return 'Some String' })

but when I do that, my piechart does not display at all. Why can't I overwrite the tooltip here? Is there another way I can do so? I'd really rather not have to edit piechart.js itself at all; I need to keep that file generic for use in multiple widgets.

And while we're at it, is there some way I can make the entire tooltip into a clickable link?

Golding answered 14/9, 2012 at 0:8 Comment(1)
I'm not nvd3 expert, just started playing with it. I had the same problem as you, what you're looking for is : .tooltipContent(function(key, y, e, graph) { return 'Some String' }) And while we're at it, is there some way I can make the entire tooltip into a clickable link? You can return any HTML for the tooltip, but as it appear/disappears on hover, you'll have a hard time clicking on it.Emilemile
I
55

Just override in this way it will work definitely

function tooltipContent(key, y, e, graph) {
            return '<h3>' + key + '</h3>' +'<p>' + y + '</p>' ;
        }

Or

tooltipContent(function(key, y, e, graph) { return 'Some String' })
Indraft answered 23/11, 2012 at 11:25 Comment(3)
+1 for the alternative solution. I have been using .tooltip(..) for a lot of NVD3 charts, and just had to use .tooltipContent(..) for the lineChart.jsMansfield
can you please tell me, how I can modify this method for NVD3's linechart coz tooltipcontent is not working in that case?Disrelish
@MitakshGupta tooltipContent is deprecated, use chart.tooltip.contentGenerator insteadPomerania
S
42

I have just got the same problem, with nvd3 1.8.1, and this is the solution I've found.

Without the option useInteractiveGuideline you could simply declare your tooltip generating function in chart.tooltip.contentGenerator(function (d){ YOUR_FUNCTION_HERE}):

The argument d is given by nvd3 when calling the tooltip, and it has three properties :

  • value: the x-axis value for the cursor position
  • index: the index in chart's datum corresponding to the the cursor position
  • series: an array containing, for each item in the chart :
    • key: the legend key for that item
    • value: the y-axis value for that item at the cursor position
    • color: the legend color for that item

So here you have an example:

chart.tooltip.contentGenerator(function (d) {
          var html = "<h2>"+d.value+"</h2> <ul>";

          d.series.forEach(function(elem){
            html += "<li><h3 style='color:"+elem.color+"'>"
                    +elem.key+"</h3> : <b>"+elem.value+"</b></li>";
          })
          html += "</ul>"
          return html;
        })

Important note

When the option useInteractiveGuideline is used, the chart.tooltip object isn't used to generate the tooltip, you must instead use the chart.interactiveLayer.tooltip, i.e.:

this.chart.interactiveLayer.tooltip.contentGenerator(function (d) { ... })

I hope the answer is useful for you, even if late.

Sophistry answered 10/9, 2015 at 10:29 Comment(2)
great answer, very useful.. :-)Funky
holy hell that was frustrating trying to figure out why chart.tooltip wasn't workingHoneysweet
F
15

Customized tooltip can not exist with "useInteractiveGuideline".

Firth answered 11/3, 2014 at 6:22 Comment(1)
It's now possible to have custom content with interactive guidelines as of version 1.8.1 (github.com/novus/nvd3/tree/v1.8.1-alpha).Crumple
A
8

If you happen to use the Angular NVD3 wrapper, the way to set the custom message is through chart options, simply:

$scope.options = {
  chart: {
  ...
  tooltip: {
      contentGenerator: function(d) {
          return d.series[0].key + ' ' + d.series[0].value;
      }
  },
  ...
  }
};
Azoth answered 23/6, 2016 at 7:41 Comment(1)
you're a savior!Pentlandite
C
5

To add to previous answers, sometimes you want to use the data of the series and not only of x and y. For instance when

data = {'values': [{'month': 1, 'count': 2}, ...], 'key': 'test' }

For those situations, use

.tooltip(function(key, x, y, e, graph) {
         var d = e.series.values[e.pointIndex];
         return '<h3>' + e.series.key + '</h3><p>' + d.month.toString() + ...;
 });

e.series is the particular series the mouse is hovering, e.pointIndex is the index on the values of the series. Here e.series.key == key, but I used to empathise what is e.series.

Carboloy answered 26/5, 2014 at 6:16 Comment(1)
Would you take a moment to look at my question here #64051527 What you say is very close to what I want to achieve, and your data structure is exactly what I have, but my function(d) has only one argument - d, and I am unable to reach to the custom attributes in values array via the d argument.Anxious
D
4
my_chart = nv.models.multiBarChart()
  .tooltip(function(key, x, y, e, graph) {
    return '<h3>' + key + '</h3>' +
           '<p>' +  y + ' on ' + x + '</p>';
  });
Dorella answered 8/3, 2013 at 3:52 Comment(3)
Just voted this from -1 to 0. Why was it downvoted? This was helpful.Pampas
it wasn't me, but I'd guess it's because the answer is incorrect - the question was about piecharts, and tooltip doesn't work for pie, it has to be tooltipContent and there shouldn't be an x parameterCollect
While the question may have been directed at pie charts I was thankful this was added in. My search was more general.Brand
K
2

I think you're missing the 'x' parameter in your tooltip function. The format of the function call is:

function(key, x, y, e, graph)
Ketron answered 17/9, 2013 at 19:26 Comment(1)
this is incorrect - there's no x parameter in pie charts; the problem is that it should be .tooltipContent as in user1847371's answerCollect
C
1
var chart = nv.models.multiBarChart();

chart.tooltip.contentGenerator(function (obj) {
                return JSON.stringify("<b>HOHO</b>")
            })

This worked for me...

Cimbalom answered 23/5, 2016 at 15:20 Comment(0)
L
-2

chart:{
interactive:true,
tooltips: true,
tooltip: {
  contentGenerator: function (d) {
    return '<h3>PUT YOUR DATA HERE E.g. d.data.name</h3>'
  }
}
Thank You and Best Regards Abhay Patidar
Librettist answered 14/9, 2012 at 0:8 Comment(1)
Please edit your answer, or you could be down-voted. As it is now, it's just a suggestion with no context. Please see here for How to Write a Good Answer on StackOverflow.Perrine

© 2022 - 2024 — McMap. All rights reserved.