Search code examples
d3.jsnvd3.js

How to create a tooltip with custom value


i use NVD3 for a scatter chart but when hovering for the tooltip i want the label instead of the key.

this is my json:

long_data = [ 
  {
    key: 'PC1',
    color: '#00cc00',
    values: [
      { 
        "label" : "Lichtpuntje" ,
        "x" : 11.16,
        "y" : -0.99,
        "size":1000,
        "color": '#FFCCOO'
      } , 
      { ....

this is the js

nv.addGraph(function() {

chart = nv.models.scatterChart()
            .showDistX(true)
            .showDistY(true)
            .useVoronoi(true)
            .color(d3.scale.category10().range())
            .transitionDuration(300)
       ...
      chart.xAxis.tickFormat(d3.format('.02f'));
      chart.yAxis.tickFormat(d3.format('.02f'));
      chart.tooltipContent(function(i) { return labelArray[i];  });

      d3.select('#test1 svg')
          .datum(long_data)
          .call(chart);
      ...

how i can i get the tooltip to have the label value? or how can i have i as index parameter?


Solution

  • ok, not a clean solution, but works:

    chart.tooltipContent(function(key, y, e, graph) { 
          labelIndex=arrayContains(resultYVal, e);
          return resultLabel[labelIndex];
    });
    
    function arrayContains(a, obj) {
       var i = a.length;
       while (i--) {
          if (a[i] == obj) {
              return i;
          }
       }
       return false;
    }