How to override a function in NVD3 library?

535 views Asked by At

My goal is to override the function used by NVD3 for the content of a tooltip. I found the function that provides this behaviour: contentGenerator.

Below here the code:

var contentGenerator = function(d) {
        if (content != null) {
            return content;
        }

        if (d == null) {
            return '';
        }

        var table = d3.select(document.createElement("table"));
        var theadEnter = table.selectAll("thead")
            .data([d])
            .enter().append("thead");

        theadEnter.append("tr")
            .append("td")
            .attr("colspan",3)
            .append("strong")
            .classed("x-value",true)
            .html(headerFormatter(d.value));


        var tbodyEnter = table.selectAll("tbody")
            .data([d])
            .enter().append("tbody");

        var trowEnter = tbodyEnter.selectAll("tr")
                .data(function(p) { return p.series})
                .enter()
                .append("tr")
                .classed("highlight", function(p) { return p.highlight});

        trowEnter.append("td")
            .classed("legend-color-guide",true)
            .append("div")
            .style("background-color", function(p) { return p.color});

        trowEnter.append("td")
            .classed("key",true)
            .html(function(p) {return p.key});

        trowEnter.append("td")
            .classed("value",true)
            .html(function(p,i) { return valueFormatter(p.value,i) });


        trowEnter.selectAll("td").each(function(p) {
            if (p.highlight) {
                var opacityScale = d3.scale.linear().domain([0,1]).range(["#fff",p.color]);
                var opacity = 0.6;
                d3.select(this)
                    .style("border-bottom-color", opacityScale(opacity))
                    .style("border-top-color", opacityScale(opacity))
                ;
            }
        });
         var html = table.node().outerHTML;
        if (d.footer == undefined)
            html += "<div class='footer'>" + d.footer + "</div>";
        return html;

    };

Now, I'd like to put inside my chart section the function to override this method.

  var chart = nv.models.lineChart()
            .x(function(d) { return d.x; })
            .y(function(d) { return d.y; })
            .useInteractiveGuideline(false)
           .tooltipContent(function (key, x, y, e, graph) {
               return contentGenerator
           });

How Can I achieve this goal?

I wanted to customise: 1) In the header add a simple text 2) Add a footer

0

There are 0 answers