How to show the text of D3 node on hover?





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







0















I am very new to D3 and I am trying to replicate the behavior for text like is done with the circle elements here with the mouseover,mouseout behavior. Basically, to show text when hovered and hidden when not. Do I need to create a node var as I have with circle and text or is it possible with the current implementation:






var nodes = {};

// Compute the distinct nodes from the links.
links.forEach(function(link) {
link.source = nodes[link.source] || (nodes[link.source] = {name: link.source, value: link.type});
link.target = nodes[link.target] || (nodes[link.target] = {name: link.target, value: link.type});
});

var width = 1000,
height = 900;

var force = d3.layout.force()
.nodes(d3.values(nodes))
.links(links)
.size([width, height])
.linkDistance(90)
.charge(-125)
.on("tick", tick)
.start();

var svg = d3.select("body").append("svg")
.attr("width", width)
.attr("height", height);

// Per-type markers, as they don't inherit styles.
svg.append("defs").selectAll("marker")
.data(["suit", "licensing", "resolved"])
.enter().append("marker")
.attr("id", function(d) { return d; })
.attr("viewBox", "0 -5 10 10")
.attr("refX", 15)
.attr("refY", -1.5)
.attr("markerWidth", 6)
.attr("markerHeight", 6)
.attr("orient", "auto")
.append("path")
.attr("d", "M0,-5L10,0L0,5");

var path = svg.append("g").selectAll("path")
.data(force.links())
.enter().append("path")
.attr("class", function(d) { return "link " + d.type; })
.attr("marker-end", function(d) { return "url(#" + d.type + ")"; })
.style("stroke", function(d){

if (d.type >= 0.4) {
return '#000066'
}

if (d.type >= 0.25) {
return '#ccccff'
}

if (d.type >= 0.01) {
return 'e8f4f8'
}
else {
return '#FFFFFF'
}
});

var circle = svg.append("g").selectAll("circle")
.data(force.nodes())
.enter().append("circle")
.attr("r", 6)
.on("dblclick", dblclick)
.on('mouseover', function(d){
var nodeSelection = d3.select(this).style({opacity:'0.5'});
nodeSelection.select("text").style({opacity:'1.0'});
})
.on('mouseout', function(d){
var nodeSelection = d3.select(this).style({opacity:'0.0'});
nodeSelection.select("text").style({opacity:'0.0'});
})
.call(force.drag);

var text = svg.append("g").selectAll("text")
.data(force.nodes())
.enter().append("text")
.attr("x", 8)
.attr("y", ".31em")
.text(function(d){
if (d.value >= 0.35){
return d.name
}
});

// Use elliptical arc path segments to doubly-encode directionality.
function tick() {
path.attr("d", linkArc);
circle.attr("transform", transform);
text.attr("transform", transform);
}

function linkArc(d) {
var dx = d.target.x - d.source.x,
dy = d.target.y - d.source.y,
dr = Math.sqrt(dx * dx + dy * dy);
return "M" + d.source.x + "," + d.source.y + "A" + dr + "," + dr + " 0 0,1 " + d.target.x + "," + d.target.y;
}


function transform(d) {
return "translate(" + d.x + "," + d.y + ")";
}

var drag = force.drag()
.on("dragstart", dragstart);

function dblclick(d) {
d3.select(this).classed("fixed", d.fixed = false);
}

function dragstart(d) {
d3.select(this).classed("fixed", d.fixed = true);
}












share|improve this question





























    0















    I am very new to D3 and I am trying to replicate the behavior for text like is done with the circle elements here with the mouseover,mouseout behavior. Basically, to show text when hovered and hidden when not. Do I need to create a node var as I have with circle and text or is it possible with the current implementation:






    var nodes = {};

    // Compute the distinct nodes from the links.
    links.forEach(function(link) {
    link.source = nodes[link.source] || (nodes[link.source] = {name: link.source, value: link.type});
    link.target = nodes[link.target] || (nodes[link.target] = {name: link.target, value: link.type});
    });

    var width = 1000,
    height = 900;

    var force = d3.layout.force()
    .nodes(d3.values(nodes))
    .links(links)
    .size([width, height])
    .linkDistance(90)
    .charge(-125)
    .on("tick", tick)
    .start();

    var svg = d3.select("body").append("svg")
    .attr("width", width)
    .attr("height", height);

    // Per-type markers, as they don't inherit styles.
    svg.append("defs").selectAll("marker")
    .data(["suit", "licensing", "resolved"])
    .enter().append("marker")
    .attr("id", function(d) { return d; })
    .attr("viewBox", "0 -5 10 10")
    .attr("refX", 15)
    .attr("refY", -1.5)
    .attr("markerWidth", 6)
    .attr("markerHeight", 6)
    .attr("orient", "auto")
    .append("path")
    .attr("d", "M0,-5L10,0L0,5");

    var path = svg.append("g").selectAll("path")
    .data(force.links())
    .enter().append("path")
    .attr("class", function(d) { return "link " + d.type; })
    .attr("marker-end", function(d) { return "url(#" + d.type + ")"; })
    .style("stroke", function(d){

    if (d.type >= 0.4) {
    return '#000066'
    }

    if (d.type >= 0.25) {
    return '#ccccff'
    }

    if (d.type >= 0.01) {
    return 'e8f4f8'
    }
    else {
    return '#FFFFFF'
    }
    });

    var circle = svg.append("g").selectAll("circle")
    .data(force.nodes())
    .enter().append("circle")
    .attr("r", 6)
    .on("dblclick", dblclick)
    .on('mouseover', function(d){
    var nodeSelection = d3.select(this).style({opacity:'0.5'});
    nodeSelection.select("text").style({opacity:'1.0'});
    })
    .on('mouseout', function(d){
    var nodeSelection = d3.select(this).style({opacity:'0.0'});
    nodeSelection.select("text").style({opacity:'0.0'});
    })
    .call(force.drag);

    var text = svg.append("g").selectAll("text")
    .data(force.nodes())
    .enter().append("text")
    .attr("x", 8)
    .attr("y", ".31em")
    .text(function(d){
    if (d.value >= 0.35){
    return d.name
    }
    });

    // Use elliptical arc path segments to doubly-encode directionality.
    function tick() {
    path.attr("d", linkArc);
    circle.attr("transform", transform);
    text.attr("transform", transform);
    }

    function linkArc(d) {
    var dx = d.target.x - d.source.x,
    dy = d.target.y - d.source.y,
    dr = Math.sqrt(dx * dx + dy * dy);
    return "M" + d.source.x + "," + d.source.y + "A" + dr + "," + dr + " 0 0,1 " + d.target.x + "," + d.target.y;
    }


    function transform(d) {
    return "translate(" + d.x + "," + d.y + ")";
    }

    var drag = force.drag()
    .on("dragstart", dragstart);

    function dblclick(d) {
    d3.select(this).classed("fixed", d.fixed = false);
    }

    function dragstart(d) {
    d3.select(this).classed("fixed", d.fixed = true);
    }












    share|improve this question

























      0












      0








      0








      I am very new to D3 and I am trying to replicate the behavior for text like is done with the circle elements here with the mouseover,mouseout behavior. Basically, to show text when hovered and hidden when not. Do I need to create a node var as I have with circle and text or is it possible with the current implementation:






      var nodes = {};

      // Compute the distinct nodes from the links.
      links.forEach(function(link) {
      link.source = nodes[link.source] || (nodes[link.source] = {name: link.source, value: link.type});
      link.target = nodes[link.target] || (nodes[link.target] = {name: link.target, value: link.type});
      });

      var width = 1000,
      height = 900;

      var force = d3.layout.force()
      .nodes(d3.values(nodes))
      .links(links)
      .size([width, height])
      .linkDistance(90)
      .charge(-125)
      .on("tick", tick)
      .start();

      var svg = d3.select("body").append("svg")
      .attr("width", width)
      .attr("height", height);

      // Per-type markers, as they don't inherit styles.
      svg.append("defs").selectAll("marker")
      .data(["suit", "licensing", "resolved"])
      .enter().append("marker")
      .attr("id", function(d) { return d; })
      .attr("viewBox", "0 -5 10 10")
      .attr("refX", 15)
      .attr("refY", -1.5)
      .attr("markerWidth", 6)
      .attr("markerHeight", 6)
      .attr("orient", "auto")
      .append("path")
      .attr("d", "M0,-5L10,0L0,5");

      var path = svg.append("g").selectAll("path")
      .data(force.links())
      .enter().append("path")
      .attr("class", function(d) { return "link " + d.type; })
      .attr("marker-end", function(d) { return "url(#" + d.type + ")"; })
      .style("stroke", function(d){

      if (d.type >= 0.4) {
      return '#000066'
      }

      if (d.type >= 0.25) {
      return '#ccccff'
      }

      if (d.type >= 0.01) {
      return 'e8f4f8'
      }
      else {
      return '#FFFFFF'
      }
      });

      var circle = svg.append("g").selectAll("circle")
      .data(force.nodes())
      .enter().append("circle")
      .attr("r", 6)
      .on("dblclick", dblclick)
      .on('mouseover', function(d){
      var nodeSelection = d3.select(this).style({opacity:'0.5'});
      nodeSelection.select("text").style({opacity:'1.0'});
      })
      .on('mouseout', function(d){
      var nodeSelection = d3.select(this).style({opacity:'0.0'});
      nodeSelection.select("text").style({opacity:'0.0'});
      })
      .call(force.drag);

      var text = svg.append("g").selectAll("text")
      .data(force.nodes())
      .enter().append("text")
      .attr("x", 8)
      .attr("y", ".31em")
      .text(function(d){
      if (d.value >= 0.35){
      return d.name
      }
      });

      // Use elliptical arc path segments to doubly-encode directionality.
      function tick() {
      path.attr("d", linkArc);
      circle.attr("transform", transform);
      text.attr("transform", transform);
      }

      function linkArc(d) {
      var dx = d.target.x - d.source.x,
      dy = d.target.y - d.source.y,
      dr = Math.sqrt(dx * dx + dy * dy);
      return "M" + d.source.x + "," + d.source.y + "A" + dr + "," + dr + " 0 0,1 " + d.target.x + "," + d.target.y;
      }


      function transform(d) {
      return "translate(" + d.x + "," + d.y + ")";
      }

      var drag = force.drag()
      .on("dragstart", dragstart);

      function dblclick(d) {
      d3.select(this).classed("fixed", d.fixed = false);
      }

      function dragstart(d) {
      d3.select(this).classed("fixed", d.fixed = true);
      }












      share|improve this question














      I am very new to D3 and I am trying to replicate the behavior for text like is done with the circle elements here with the mouseover,mouseout behavior. Basically, to show text when hovered and hidden when not. Do I need to create a node var as I have with circle and text or is it possible with the current implementation:






      var nodes = {};

      // Compute the distinct nodes from the links.
      links.forEach(function(link) {
      link.source = nodes[link.source] || (nodes[link.source] = {name: link.source, value: link.type});
      link.target = nodes[link.target] || (nodes[link.target] = {name: link.target, value: link.type});
      });

      var width = 1000,
      height = 900;

      var force = d3.layout.force()
      .nodes(d3.values(nodes))
      .links(links)
      .size([width, height])
      .linkDistance(90)
      .charge(-125)
      .on("tick", tick)
      .start();

      var svg = d3.select("body").append("svg")
      .attr("width", width)
      .attr("height", height);

      // Per-type markers, as they don't inherit styles.
      svg.append("defs").selectAll("marker")
      .data(["suit", "licensing", "resolved"])
      .enter().append("marker")
      .attr("id", function(d) { return d; })
      .attr("viewBox", "0 -5 10 10")
      .attr("refX", 15)
      .attr("refY", -1.5)
      .attr("markerWidth", 6)
      .attr("markerHeight", 6)
      .attr("orient", "auto")
      .append("path")
      .attr("d", "M0,-5L10,0L0,5");

      var path = svg.append("g").selectAll("path")
      .data(force.links())
      .enter().append("path")
      .attr("class", function(d) { return "link " + d.type; })
      .attr("marker-end", function(d) { return "url(#" + d.type + ")"; })
      .style("stroke", function(d){

      if (d.type >= 0.4) {
      return '#000066'
      }

      if (d.type >= 0.25) {
      return '#ccccff'
      }

      if (d.type >= 0.01) {
      return 'e8f4f8'
      }
      else {
      return '#FFFFFF'
      }
      });

      var circle = svg.append("g").selectAll("circle")
      .data(force.nodes())
      .enter().append("circle")
      .attr("r", 6)
      .on("dblclick", dblclick)
      .on('mouseover', function(d){
      var nodeSelection = d3.select(this).style({opacity:'0.5'});
      nodeSelection.select("text").style({opacity:'1.0'});
      })
      .on('mouseout', function(d){
      var nodeSelection = d3.select(this).style({opacity:'0.0'});
      nodeSelection.select("text").style({opacity:'0.0'});
      })
      .call(force.drag);

      var text = svg.append("g").selectAll("text")
      .data(force.nodes())
      .enter().append("text")
      .attr("x", 8)
      .attr("y", ".31em")
      .text(function(d){
      if (d.value >= 0.35){
      return d.name
      }
      });

      // Use elliptical arc path segments to doubly-encode directionality.
      function tick() {
      path.attr("d", linkArc);
      circle.attr("transform", transform);
      text.attr("transform", transform);
      }

      function linkArc(d) {
      var dx = d.target.x - d.source.x,
      dy = d.target.y - d.source.y,
      dr = Math.sqrt(dx * dx + dy * dy);
      return "M" + d.source.x + "," + d.source.y + "A" + dr + "," + dr + " 0 0,1 " + d.target.x + "," + d.target.y;
      }


      function transform(d) {
      return "translate(" + d.x + "," + d.y + ")";
      }

      var drag = force.drag()
      .on("dragstart", dragstart);

      function dblclick(d) {
      d3.select(this).classed("fixed", d.fixed = false);
      }

      function dragstart(d) {
      d3.select(this).classed("fixed", d.fixed = true);
      }








      var nodes = {};

      // Compute the distinct nodes from the links.
      links.forEach(function(link) {
      link.source = nodes[link.source] || (nodes[link.source] = {name: link.source, value: link.type});
      link.target = nodes[link.target] || (nodes[link.target] = {name: link.target, value: link.type});
      });

      var width = 1000,
      height = 900;

      var force = d3.layout.force()
      .nodes(d3.values(nodes))
      .links(links)
      .size([width, height])
      .linkDistance(90)
      .charge(-125)
      .on("tick", tick)
      .start();

      var svg = d3.select("body").append("svg")
      .attr("width", width)
      .attr("height", height);

      // Per-type markers, as they don't inherit styles.
      svg.append("defs").selectAll("marker")
      .data(["suit", "licensing", "resolved"])
      .enter().append("marker")
      .attr("id", function(d) { return d; })
      .attr("viewBox", "0 -5 10 10")
      .attr("refX", 15)
      .attr("refY", -1.5)
      .attr("markerWidth", 6)
      .attr("markerHeight", 6)
      .attr("orient", "auto")
      .append("path")
      .attr("d", "M0,-5L10,0L0,5");

      var path = svg.append("g").selectAll("path")
      .data(force.links())
      .enter().append("path")
      .attr("class", function(d) { return "link " + d.type; })
      .attr("marker-end", function(d) { return "url(#" + d.type + ")"; })
      .style("stroke", function(d){

      if (d.type >= 0.4) {
      return '#000066'
      }

      if (d.type >= 0.25) {
      return '#ccccff'
      }

      if (d.type >= 0.01) {
      return 'e8f4f8'
      }
      else {
      return '#FFFFFF'
      }
      });

      var circle = svg.append("g").selectAll("circle")
      .data(force.nodes())
      .enter().append("circle")
      .attr("r", 6)
      .on("dblclick", dblclick)
      .on('mouseover', function(d){
      var nodeSelection = d3.select(this).style({opacity:'0.5'});
      nodeSelection.select("text").style({opacity:'1.0'});
      })
      .on('mouseout', function(d){
      var nodeSelection = d3.select(this).style({opacity:'0.0'});
      nodeSelection.select("text").style({opacity:'0.0'});
      })
      .call(force.drag);

      var text = svg.append("g").selectAll("text")
      .data(force.nodes())
      .enter().append("text")
      .attr("x", 8)
      .attr("y", ".31em")
      .text(function(d){
      if (d.value >= 0.35){
      return d.name
      }
      });

      // Use elliptical arc path segments to doubly-encode directionality.
      function tick() {
      path.attr("d", linkArc);
      circle.attr("transform", transform);
      text.attr("transform", transform);
      }

      function linkArc(d) {
      var dx = d.target.x - d.source.x,
      dy = d.target.y - d.source.y,
      dr = Math.sqrt(dx * dx + dy * dy);
      return "M" + d.source.x + "," + d.source.y + "A" + dr + "," + dr + " 0 0,1 " + d.target.x + "," + d.target.y;
      }


      function transform(d) {
      return "translate(" + d.x + "," + d.y + ")";
      }

      var drag = force.drag()
      .on("dragstart", dragstart);

      function dblclick(d) {
      d3.select(this).classed("fixed", d.fixed = false);
      }

      function dragstart(d) {
      d3.select(this).classed("fixed", d.fixed = true);
      }





      var nodes = {};

      // Compute the distinct nodes from the links.
      links.forEach(function(link) {
      link.source = nodes[link.source] || (nodes[link.source] = {name: link.source, value: link.type});
      link.target = nodes[link.target] || (nodes[link.target] = {name: link.target, value: link.type});
      });

      var width = 1000,
      height = 900;

      var force = d3.layout.force()
      .nodes(d3.values(nodes))
      .links(links)
      .size([width, height])
      .linkDistance(90)
      .charge(-125)
      .on("tick", tick)
      .start();

      var svg = d3.select("body").append("svg")
      .attr("width", width)
      .attr("height", height);

      // Per-type markers, as they don't inherit styles.
      svg.append("defs").selectAll("marker")
      .data(["suit", "licensing", "resolved"])
      .enter().append("marker")
      .attr("id", function(d) { return d; })
      .attr("viewBox", "0 -5 10 10")
      .attr("refX", 15)
      .attr("refY", -1.5)
      .attr("markerWidth", 6)
      .attr("markerHeight", 6)
      .attr("orient", "auto")
      .append("path")
      .attr("d", "M0,-5L10,0L0,5");

      var path = svg.append("g").selectAll("path")
      .data(force.links())
      .enter().append("path")
      .attr("class", function(d) { return "link " + d.type; })
      .attr("marker-end", function(d) { return "url(#" + d.type + ")"; })
      .style("stroke", function(d){

      if (d.type >= 0.4) {
      return '#000066'
      }

      if (d.type >= 0.25) {
      return '#ccccff'
      }

      if (d.type >= 0.01) {
      return 'e8f4f8'
      }
      else {
      return '#FFFFFF'
      }
      });

      var circle = svg.append("g").selectAll("circle")
      .data(force.nodes())
      .enter().append("circle")
      .attr("r", 6)
      .on("dblclick", dblclick)
      .on('mouseover', function(d){
      var nodeSelection = d3.select(this).style({opacity:'0.5'});
      nodeSelection.select("text").style({opacity:'1.0'});
      })
      .on('mouseout', function(d){
      var nodeSelection = d3.select(this).style({opacity:'0.0'});
      nodeSelection.select("text").style({opacity:'0.0'});
      })
      .call(force.drag);

      var text = svg.append("g").selectAll("text")
      .data(force.nodes())
      .enter().append("text")
      .attr("x", 8)
      .attr("y", ".31em")
      .text(function(d){
      if (d.value >= 0.35){
      return d.name
      }
      });

      // Use elliptical arc path segments to doubly-encode directionality.
      function tick() {
      path.attr("d", linkArc);
      circle.attr("transform", transform);
      text.attr("transform", transform);
      }

      function linkArc(d) {
      var dx = d.target.x - d.source.x,
      dy = d.target.y - d.source.y,
      dr = Math.sqrt(dx * dx + dy * dy);
      return "M" + d.source.x + "," + d.source.y + "A" + dr + "," + dr + " 0 0,1 " + d.target.x + "," + d.target.y;
      }


      function transform(d) {
      return "translate(" + d.x + "," + d.y + ")";
      }

      var drag = force.drag()
      .on("dragstart", dragstart);

      function dblclick(d) {
      d3.select(this).classed("fixed", d.fixed = false);
      }

      function dragstart(d) {
      d3.select(this).classed("fixed", d.fixed = true);
      }






      javascript html d3.js data-visualization






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 26 '18 at 13:40









      this_is_davidthis_is_david

      615




      615
























          1 Answer
          1






          active

          oldest

          votes


















          0














          Add opacity attribute to your text that defaults to 0. It appears you already have text to show and hide it on mouseover/mouseout. I posted a different way to do it as well if your method is not working for you.



          var text = svg.append("g").selectAll("text")
          .data(force.nodes())
          .enter().append("text")
          .attr("x", 8)
          .attr("y", ".31em")
          .attr("opacity", 1)
          .text(function(d){
          if (d.value >= 0.35){
          return d.name
          }
          });



          function mouseout(d) { text.style("opacity", 0); }
          function mouseover(d) { text.style("opacity", 1);





          share|improve this answer
























            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53482378%2fhow-to-show-the-text-of-d3-node-on-hover%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0














            Add opacity attribute to your text that defaults to 0. It appears you already have text to show and hide it on mouseover/mouseout. I posted a different way to do it as well if your method is not working for you.



            var text = svg.append("g").selectAll("text")
            .data(force.nodes())
            .enter().append("text")
            .attr("x", 8)
            .attr("y", ".31em")
            .attr("opacity", 1)
            .text(function(d){
            if (d.value >= 0.35){
            return d.name
            }
            });



            function mouseout(d) { text.style("opacity", 0); }
            function mouseover(d) { text.style("opacity", 1);





            share|improve this answer




























              0














              Add opacity attribute to your text that defaults to 0. It appears you already have text to show and hide it on mouseover/mouseout. I posted a different way to do it as well if your method is not working for you.



              var text = svg.append("g").selectAll("text")
              .data(force.nodes())
              .enter().append("text")
              .attr("x", 8)
              .attr("y", ".31em")
              .attr("opacity", 1)
              .text(function(d){
              if (d.value >= 0.35){
              return d.name
              }
              });



              function mouseout(d) { text.style("opacity", 0); }
              function mouseover(d) { text.style("opacity", 1);





              share|improve this answer


























                0












                0








                0







                Add opacity attribute to your text that defaults to 0. It appears you already have text to show and hide it on mouseover/mouseout. I posted a different way to do it as well if your method is not working for you.



                var text = svg.append("g").selectAll("text")
                .data(force.nodes())
                .enter().append("text")
                .attr("x", 8)
                .attr("y", ".31em")
                .attr("opacity", 1)
                .text(function(d){
                if (d.value >= 0.35){
                return d.name
                }
                });



                function mouseout(d) { text.style("opacity", 0); }
                function mouseover(d) { text.style("opacity", 1);





                share|improve this answer













                Add opacity attribute to your text that defaults to 0. It appears you already have text to show and hide it on mouseover/mouseout. I posted a different way to do it as well if your method is not working for you.



                var text = svg.append("g").selectAll("text")
                .data(force.nodes())
                .enter().append("text")
                .attr("x", 8)
                .attr("y", ".31em")
                .attr("opacity", 1)
                .text(function(d){
                if (d.value >= 0.35){
                return d.name
                }
                });



                function mouseout(d) { text.style("opacity", 0); }
                function mouseover(d) { text.style("opacity", 1);






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 26 '18 at 14:25









                A ZibudaA Zibuda

                388




                388
































                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53482378%2fhow-to-show-the-text-of-d3-node-on-hover%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Tonle Sap (See)

                    I get strange results when I access the Sqlitedatabase with Unity C# via XAMPP

                    Guatemaltekische Davis-Cup-Mannschaft