kage23
kage23

Reputation: 401

nvd3 piechart.js - How to edit the tooltip?

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?

Upvotes: 40

Views: 39708

Answers (9)

Ab Pati
Ab Pati

Reputation: 27

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

Upvotes: -2

pCyril
pCyril

Reputation: 134

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;
      }
  },
  ...
  }
};

Upvotes: 8

Doctor
Doctor

Reputation: 7956

var chart = nv.models.multiBarChart();

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

This worked for me...

Upvotes: 1

LostInBrittany
LostInBrittany

Reputation: 1162

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.

Upvotes: 42

Jorge Leitao
Jorge Leitao

Reputation: 20103

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.

Upvotes: 5

user2612936
user2612936

Reputation: 151

Customized tooltip can not exist with "useInteractiveGuideline".

Upvotes: 15

jbizzle
jbizzle

Reputation: 29

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)

Upvotes: 2

Aleck Landgraf
Aleck Landgraf

Reputation: 1555

my_chart = nv.models.multiBarChart()
  .tooltip(function(key, x, y, e, graph) {
    return '<h3>' + key + '</h3>' +
           '<p>' +  y + ' on ' + x + '</p>';
  });

Upvotes: 4

user1847371
user1847371

Reputation: 579

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

Upvotes: 55

Related Questions