nvd3 piechart.js - How to edit the tooltip?

kage23 picture kage23 · Sep 14, 2012 · Viewed 38.4k times · Source

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?

Answer

user1847371 picture user1847371 · Nov 23, 2012

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