ofey
ofey

Reputation: 3347

d3 v4 tooltip passing data d issue

I am trying to get text to display on mouseover of name on an x-axis. This stackoverflow post almost gets there, except that i am unable to pass data to the function called on mouseover.

// Add the X Axis
  svg.append("g")
    .attr("class", "axis")
    .attr("transform", "translate(0," + height + ")")
    .call(d3.axisBottom(x))
    .selectAll("text")
    .style("text-anchor", "end")
    .style("font", "7px times")
    .attr("dx", "-.8em")
    .attr("dy", ".15em")
    .attr("transform", "rotate(-65)")
    .on("mouseover", function(d) {
      return tooltip.style("visibility", "visible")
    })
    .on("mouseout", function(d) {
      return tooltip.style("visibility", "hidden");
    });

  var tooltip = d3.select("#info")
    .append("div")
    .style("position", "absolute")
    .style("z-index", "10")
    // .style("visibility", "hidden")
    .text("a simple tooltip" + d.name);

When I remove d.name it works. I am unable to pass d to tooltip.

Perhaps I need to use .data(data).enter() which i have tried but maybe I am not using it correctly.

Thanks,

https://d3js.org/d3.v4.min.js is the only library I am using.

Upvotes: 0

Views: 1135

Answers (1)

user3432422
user3432422

Reputation: 1369

Can you just set the text in the mouseover callback, where you have access to d?

// Add the X Axis
svg.append("g")
    .attr("class", "axis")
    .attr("transform", "translate(0," + height + ")")
    .call(d3.axisBottom(x))
    .selectAll("text")
    .style("text-anchor", "end")
    .style("font", "7px times")
    .attr("dx", "-.8em")
    .attr("dy", ".15em")
    .attr("transform", "rotate(-65)")
    .on("mouseover", function(d) {
        return tooltip
            .style("visibility", "visible")
            .text("a simple tooltip" + d.name);
    })
    .on("mouseout", function(d) {
        return tooltip.style("visibility", "hidden");
    });

var tooltip = d3.select("#info")
    .append("div")
    .style("position", "absolute")
    .style("z-index", "10")
    // .style("visibility", "hidden")
    // .text("a simple tooltip" + d.name);

Upvotes: 2

Related Questions