how to bring the selected node front in d3 js?












6















I am using the d3 v3 script to visualize the Data. I need to highlight and bring the node front in mousecenter and vice versa in mouseleave. Now I can able to high light the node by increasing height and width of the node.



Can't able to bring the node front. I've tried using CSS like opacity, z-index.



Script



<script>

// some colour variables
var tcBlack = "purple";

// rest of vars
var w = 1500,
h = 800,
maxNodeSize = 50,
x_browser = 25,
y_browser = 25,
root;

var vis;
var force = d3.layout.force();

vis = d3.select("#visfel_map").append("svg").attr("width", w).attr("height", h);


d3.json(url, function(error,json) {
if (error)
return console.warn(error);
root = json;
root.fixed = true;
root.x = w / 2;
root.y = h / 4;


// Build the path
var defs = vis.insert("svg:defs")
.data(["end"]);


defs.enter().append("svg:path")
.attr("d", "M0,-5L10,0L0,5");

update();
});

function update() {
var nodes = flatten(root),
links = d3.layout.tree().links(nodes);

force.nodes(nodes)
.links(links)
.gravity(0.05)
.charge(-2500)
.linkDistance(200)
.friction(0.5)
.linkStrength(function(l, i) {return 1; })
.size([w, h])
.on("tick", tick)
.start();

var path = vis.selectAll("path.link")
.data(links, function(d) { return d.target.id; });

path.enter().insert("svg:path")
.attr("class", "link")
// .attr("marker-end", "url(#end)")
.style("stroke", "#ff8888");


// Exit any old paths.
path.exit().remove();



// Update the nodes…
var node = vis.selectAll("g.node")
.data(nodes, function(d) { return d.id; });


// Enter any new nodes.
var nodeEnter = node.enter().append("svg:g")
.attr("class", "node")
.attr("transform", function(d) { return "translate(" + d.x + "," + d.y + ")"; })
.on("click", click)
.call(force.drag);

// Append a circle
nodeEnter.append("svg:circle")
.attr("r", function(d) { return Math.sqrt(d.size) / 10 || 4.5; })
.style("fill", "#eee");


// Append images
var images = nodeEnter.append("svg:image")
.attr("xlink:href", function(d) { return d.img;})
.attr("x", function(d) { return -25;})
.attr("y", function(d) { return -25;})
.attr("height", 65)
.attr("width", 65);

// make the image grow a little on mouse over and add the text details on click
var setEvents = images
.on( 'click', function (d) {
console.log(d.sub_category_id)
})
.on( 'mouseenter', function() {
var currNode = d3.select(this);
currNode.transition()
.attr("x", function(d) { return -60;})
.attr("y", function(d) { return -60;})
.attr("height", 300)
.attr("width", 300);

})
// set back
.on( 'mouseleave', function() {
d3.select(this)
.transition()
.attr("x", function(d) { return -25;})
.attr("y", function(d) { return -25;})
.attr("height", 65)
.attr("width", 65);


});
// Append name on roll over next to the node as well
nodeEnter.append("text")
.attr("class", "nodetext")
.attr("x", function(d) { return d.children ? 70 : 70; })
.attr("y", function(d) { return d.children ? 10 : 10; })
.style("text-anchor", function(d) { return d.children ? "end" : "end"; })
.attr("fill", tcBlack)
.text(function(d) { return d.name; });


// Exit any old nodes.
node.exit().remove();


// Re-select for update.
path = vis.selectAll("path.link");
node = vis.selectAll("g.node");

function tick() {


path.attr("d", function(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;
});
node.attr("transform", nodeTransform);
}
}


/**
* Gives the coordinates of the border for keeping the nodes inside a frame
* http://bl.ocks.org/mbostock/1129492
*/
function nodeTransform(d) {
d.x = Math.max(maxNodeSize, Math.min(w - (d.imgwidth/2 || 16), d.x));
d.y = Math.max(maxNodeSize, Math.min(h - (d.imgheight/2 || 16), d.y));
return "translate(" + d.x + "," + d




.y + ")";
}

/**
* Toggle children on click.
*/
function click(d) {
if (d.children) {
d._children = d.children;
d.children = null;
} else {
d.children = d._children;
d._children = null;
}

update();
}


/**
* Returns a list of all nodes under the root.
*/
function flatten(root) {
var nodes = ;
var i = 0;

function recurse(node) {
if (node.children)
node.children.forEach(recurse);
if (!node.id)
node.id = ++i;
nodes.push(node);
}

recurse(root);
return nodes;
}

</script>


enter image description here



JSON Data



{
"type": "map",
"tree_size": "2",
"map_id": "1",
"name": "Sounds for Speech",
"img": "manage/visfel_images/map-1516338051-sounds for speech.png",
"children": [
{
"type": "category",
"tree_size": "2",
"category_id": "1",
"name": "Vowels",
"img": "manage/visfel_images/category-1516338094-vowel sound.png",
"children": [
{
"type": "sub",
"tree_size": "2",
"sub_category_id": "1",
"name": "A",
"img": "manage/visfel_images/sub-1516338159-A.png"
},
{
"type": "sub",
"tree_size": "2",
"sub_category_id": "2",
"name": "E",
"img": "manage/visfel_images/sub-1516338189-E.png"
},
{
"type": "sub",
"tree_size": "2",
"sub_category_id": "3",
"name": "I",
"img": "manage/visfel_images/sub-1516338212-i.png"
},
{
"type": "sub",
"tree_size": "2",
"sub_category_id": "4",
"name": "O",
"img": "manage/visfel_images/sub-1516338235-O.png"
},
{
"type": "sub",
"tree_size": "2",
"sub_category_id": "5",
"name": "U",
"img": "manage/visfel_images/sub-1516338260-U.png"
}
]
},
{
"type": "category",
"tree_size": "2",
"category_id": "2",
"name": "Consonents",
"img": "manage/visfel_images/category-1516338121-consonents.png",
"children": [
{
"type": "sub",
"tree_size": "2",
"sub_category_id": "6",
"name": "B",
"img": "manage/visfel_images/sub-1516338304-B.png"
},
{
"type": "sub",
"tree_size": "2",
"sub_category_id": "7",
"name": "C",
"img": "manage/visfel_images/sub-1516338323-C.png"
},
{
"type": "sub",
"tree_size": "2",
"sub_category_id": "8",
"name": "D",
"img": "manage/visfel_images/sub-1516338342-D.png"
},
{
"type": "sub",
"tree_size": "2",
"sub_category_id": "9",
"name": "F",
"img": "manage/visfel_images/sub-1516338362-F.png"
},
{
"type": "sub",
"tree_size": "2",
"sub_category_id": "10",
"name": "G",
"img": "manage/visfel_images/sub-1516338380-G.png"
},
{
"type": "sub",
"tree_size": "2",
"sub_category_id": "11",
"name": "H",
"img": "manage/visfel_images/sub-1516338401-H.png"
},
{
"type": "sub",
"tree_size": "2",
"sub_category_id": "12",
"name": "J",
"img": "manage/visfel_images/sub-1516338427-J.png"
},
{
"type": "sub",
"tree_size": "2",
"sub_category_id": "13",
"name": "K",
"img": "manage/visfel_images/sub-1516338452-K.png"
},
{
"type": "sub",
"tree_size": "2",
"sub_category_id": "14",
"name": "L",
"img": "manage/visfel_images/sub-1516338470-L.png"
},
{
"type": "sub",
"tree_size": "2",
"sub_category_id": "15",
"name": "M",
"img": "manage/visfel_images/sub-1516338489-M.png"
},
{
"type": "sub",
"tree_size": "2",
"sub_category_id": "16",
"name": "N",
"img": "manage/visfel_images/sub-1516338508-N.png"
},
{
"type": "sub",
"tree_size": "2",
"sub_category_id": "17",
"name": "P",
"img": "manage/visfel_images/sub-1516338542-P.png"
},
{
"type": "sub",
"tree_size": "2",
"sub_category_id": "18",
"name": "Q",
"img": "manage/visfel_images/sub-1516338560-Q.png"
},
{
"type": "sub",
"tree_size": "2",
"sub_category_id": "19",
"name": "R",
"img": "manage/visfel_images/sub-1516338579-R.png"
},
{
"type": "sub",
"tree_size": "2",
"sub_category_id": "20",
"name": "S",
"img": "manage/visfel_images/sub-1516338604-S.png"
},
{
"type": "sub",
"tree_size": "2",
"sub_category_id": "21",
"name": "T",
"img": "manage/visfel_images/sub-1516338619-T.png"
},
{
"type": "sub",
"tree_size": "2",
"sub_category_id": "22",
"name": "V",
"img": "manage/visfel_images/sub-1516338635-V.png"
},
{
"type": "sub",
"tree_size": "2",
"sub_category_id": "23",
"name": "W",
"img": "manage/visfel_images/sub-1516338650-W.png"
},
{
"type": "sub",
"tree_size": "2",
"sub_category_id": "24",
"name": "X",
"img": "manage/visfel_images/sub-1516338666-X.png"
},
{
"type": "sub",
"tree_size": "2",
"sub_category_id": "25",
"name": "Y",
"img": "manage/visfel_images/sub-1516338705-Y.png"
},
{
"type": "sub",
"tree_size": "2",
"sub_category_id": "26",
"name": "Z",
"img": "manage/visfel_images/sub-1516338742-Z.png"
}
]
}
]
}


In the attached screenshot, node "M" is in focus, mouse over on that element. Node highlights by increasing the width and height, same as node should come front, overlapping nodes should go back.



By Fading the other elements also enough, or else re-arrange the node elements to fix the problem.



Awaiting Suggestions?
Thanks in Advance.










share|improve this question




















  • 1





    You can contol the positioning ("absolute", "relative" etc) or the z index property with d3. Maybe start there.

    – Ryan Morton
    Mar 6 '18 at 18:57











  • @RyanMorton Already tried. Not working.

    – Keerthivasan
    Mar 6 '18 at 19:03






  • 3





    v4 has a selection.raise() method which does just that. Unfortunately v3 does not, but selection.order might do it.

    – pmkro
    Mar 6 '18 at 19:19






  • 2





    moveToFront()? bl.ocks.org/eesur/4e0a69d57d3bfc8a82c2

    – Ryan Morton
    Mar 6 '18 at 19:19






  • 2





    Please provide the data that you're using in var url so that we can replicate the issue.

    – SilentStone
    Mar 9 '18 at 20:24
















6















I am using the d3 v3 script to visualize the Data. I need to highlight and bring the node front in mousecenter and vice versa in mouseleave. Now I can able to high light the node by increasing height and width of the node.



Can't able to bring the node front. I've tried using CSS like opacity, z-index.



Script



<script>

// some colour variables
var tcBlack = "purple";

// rest of vars
var w = 1500,
h = 800,
maxNodeSize = 50,
x_browser = 25,
y_browser = 25,
root;

var vis;
var force = d3.layout.force();

vis = d3.select("#visfel_map").append("svg").attr("width", w).attr("height", h);


d3.json(url, function(error,json) {
if (error)
return console.warn(error);
root = json;
root.fixed = true;
root.x = w / 2;
root.y = h / 4;


// Build the path
var defs = vis.insert("svg:defs")
.data(["end"]);


defs.enter().append("svg:path")
.attr("d", "M0,-5L10,0L0,5");

update();
});

function update() {
var nodes = flatten(root),
links = d3.layout.tree().links(nodes);

force.nodes(nodes)
.links(links)
.gravity(0.05)
.charge(-2500)
.linkDistance(200)
.friction(0.5)
.linkStrength(function(l, i) {return 1; })
.size([w, h])
.on("tick", tick)
.start();

var path = vis.selectAll("path.link")
.data(links, function(d) { return d.target.id; });

path.enter().insert("svg:path")
.attr("class", "link")
// .attr("marker-end", "url(#end)")
.style("stroke", "#ff8888");


// Exit any old paths.
path.exit().remove();



// Update the nodes…
var node = vis.selectAll("g.node")
.data(nodes, function(d) { return d.id; });


// Enter any new nodes.
var nodeEnter = node.enter().append("svg:g")
.attr("class", "node")
.attr("transform", function(d) { return "translate(" + d.x + "," + d.y + ")"; })
.on("click", click)
.call(force.drag);

// Append a circle
nodeEnter.append("svg:circle")
.attr("r", function(d) { return Math.sqrt(d.size) / 10 || 4.5; })
.style("fill", "#eee");


// Append images
var images = nodeEnter.append("svg:image")
.attr("xlink:href", function(d) { return d.img;})
.attr("x", function(d) { return -25;})
.attr("y", function(d) { return -25;})
.attr("height", 65)
.attr("width", 65);

// make the image grow a little on mouse over and add the text details on click
var setEvents = images
.on( 'click', function (d) {
console.log(d.sub_category_id)
})
.on( 'mouseenter', function() {
var currNode = d3.select(this);
currNode.transition()
.attr("x", function(d) { return -60;})
.attr("y", function(d) { return -60;})
.attr("height", 300)
.attr("width", 300);

})
// set back
.on( 'mouseleave', function() {
d3.select(this)
.transition()
.attr("x", function(d) { return -25;})
.attr("y", function(d) { return -25;})
.attr("height", 65)
.attr("width", 65);


});
// Append name on roll over next to the node as well
nodeEnter.append("text")
.attr("class", "nodetext")
.attr("x", function(d) { return d.children ? 70 : 70; })
.attr("y", function(d) { return d.children ? 10 : 10; })
.style("text-anchor", function(d) { return d.children ? "end" : "end"; })
.attr("fill", tcBlack)
.text(function(d) { return d.name; });


// Exit any old nodes.
node.exit().remove();


// Re-select for update.
path = vis.selectAll("path.link");
node = vis.selectAll("g.node");

function tick() {


path.attr("d", function(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;
});
node.attr("transform", nodeTransform);
}
}


/**
* Gives the coordinates of the border for keeping the nodes inside a frame
* http://bl.ocks.org/mbostock/1129492
*/
function nodeTransform(d) {
d.x = Math.max(maxNodeSize, Math.min(w - (d.imgwidth/2 || 16), d.x));
d.y = Math.max(maxNodeSize, Math.min(h - (d.imgheight/2 || 16), d.y));
return "translate(" + d.x + "," + d




.y + ")";
}

/**
* Toggle children on click.
*/
function click(d) {
if (d.children) {
d._children = d.children;
d.children = null;
} else {
d.children = d._children;
d._children = null;
}

update();
}


/**
* Returns a list of all nodes under the root.
*/
function flatten(root) {
var nodes = ;
var i = 0;

function recurse(node) {
if (node.children)
node.children.forEach(recurse);
if (!node.id)
node.id = ++i;
nodes.push(node);
}

recurse(root);
return nodes;
}

</script>


enter image description here



JSON Data



{
"type": "map",
"tree_size": "2",
"map_id": "1",
"name": "Sounds for Speech",
"img": "manage/visfel_images/map-1516338051-sounds for speech.png",
"children": [
{
"type": "category",
"tree_size": "2",
"category_id": "1",
"name": "Vowels",
"img": "manage/visfel_images/category-1516338094-vowel sound.png",
"children": [
{
"type": "sub",
"tree_size": "2",
"sub_category_id": "1",
"name": "A",
"img": "manage/visfel_images/sub-1516338159-A.png"
},
{
"type": "sub",
"tree_size": "2",
"sub_category_id": "2",
"name": "E",
"img": "manage/visfel_images/sub-1516338189-E.png"
},
{
"type": "sub",
"tree_size": "2",
"sub_category_id": "3",
"name": "I",
"img": "manage/visfel_images/sub-1516338212-i.png"
},
{
"type": "sub",
"tree_size": "2",
"sub_category_id": "4",
"name": "O",
"img": "manage/visfel_images/sub-1516338235-O.png"
},
{
"type": "sub",
"tree_size": "2",
"sub_category_id": "5",
"name": "U",
"img": "manage/visfel_images/sub-1516338260-U.png"
}
]
},
{
"type": "category",
"tree_size": "2",
"category_id": "2",
"name": "Consonents",
"img": "manage/visfel_images/category-1516338121-consonents.png",
"children": [
{
"type": "sub",
"tree_size": "2",
"sub_category_id": "6",
"name": "B",
"img": "manage/visfel_images/sub-1516338304-B.png"
},
{
"type": "sub",
"tree_size": "2",
"sub_category_id": "7",
"name": "C",
"img": "manage/visfel_images/sub-1516338323-C.png"
},
{
"type": "sub",
"tree_size": "2",
"sub_category_id": "8",
"name": "D",
"img": "manage/visfel_images/sub-1516338342-D.png"
},
{
"type": "sub",
"tree_size": "2",
"sub_category_id": "9",
"name": "F",
"img": "manage/visfel_images/sub-1516338362-F.png"
},
{
"type": "sub",
"tree_size": "2",
"sub_category_id": "10",
"name": "G",
"img": "manage/visfel_images/sub-1516338380-G.png"
},
{
"type": "sub",
"tree_size": "2",
"sub_category_id": "11",
"name": "H",
"img": "manage/visfel_images/sub-1516338401-H.png"
},
{
"type": "sub",
"tree_size": "2",
"sub_category_id": "12",
"name": "J",
"img": "manage/visfel_images/sub-1516338427-J.png"
},
{
"type": "sub",
"tree_size": "2",
"sub_category_id": "13",
"name": "K",
"img": "manage/visfel_images/sub-1516338452-K.png"
},
{
"type": "sub",
"tree_size": "2",
"sub_category_id": "14",
"name": "L",
"img": "manage/visfel_images/sub-1516338470-L.png"
},
{
"type": "sub",
"tree_size": "2",
"sub_category_id": "15",
"name": "M",
"img": "manage/visfel_images/sub-1516338489-M.png"
},
{
"type": "sub",
"tree_size": "2",
"sub_category_id": "16",
"name": "N",
"img": "manage/visfel_images/sub-1516338508-N.png"
},
{
"type": "sub",
"tree_size": "2",
"sub_category_id": "17",
"name": "P",
"img": "manage/visfel_images/sub-1516338542-P.png"
},
{
"type": "sub",
"tree_size": "2",
"sub_category_id": "18",
"name": "Q",
"img": "manage/visfel_images/sub-1516338560-Q.png"
},
{
"type": "sub",
"tree_size": "2",
"sub_category_id": "19",
"name": "R",
"img": "manage/visfel_images/sub-1516338579-R.png"
},
{
"type": "sub",
"tree_size": "2",
"sub_category_id": "20",
"name": "S",
"img": "manage/visfel_images/sub-1516338604-S.png"
},
{
"type": "sub",
"tree_size": "2",
"sub_category_id": "21",
"name": "T",
"img": "manage/visfel_images/sub-1516338619-T.png"
},
{
"type": "sub",
"tree_size": "2",
"sub_category_id": "22",
"name": "V",
"img": "manage/visfel_images/sub-1516338635-V.png"
},
{
"type": "sub",
"tree_size": "2",
"sub_category_id": "23",
"name": "W",
"img": "manage/visfel_images/sub-1516338650-W.png"
},
{
"type": "sub",
"tree_size": "2",
"sub_category_id": "24",
"name": "X",
"img": "manage/visfel_images/sub-1516338666-X.png"
},
{
"type": "sub",
"tree_size": "2",
"sub_category_id": "25",
"name": "Y",
"img": "manage/visfel_images/sub-1516338705-Y.png"
},
{
"type": "sub",
"tree_size": "2",
"sub_category_id": "26",
"name": "Z",
"img": "manage/visfel_images/sub-1516338742-Z.png"
}
]
}
]
}


In the attached screenshot, node "M" is in focus, mouse over on that element. Node highlights by increasing the width and height, same as node should come front, overlapping nodes should go back.



By Fading the other elements also enough, or else re-arrange the node elements to fix the problem.



Awaiting Suggestions?
Thanks in Advance.










share|improve this question




















  • 1





    You can contol the positioning ("absolute", "relative" etc) or the z index property with d3. Maybe start there.

    – Ryan Morton
    Mar 6 '18 at 18:57











  • @RyanMorton Already tried. Not working.

    – Keerthivasan
    Mar 6 '18 at 19:03






  • 3





    v4 has a selection.raise() method which does just that. Unfortunately v3 does not, but selection.order might do it.

    – pmkro
    Mar 6 '18 at 19:19






  • 2





    moveToFront()? bl.ocks.org/eesur/4e0a69d57d3bfc8a82c2

    – Ryan Morton
    Mar 6 '18 at 19:19






  • 2





    Please provide the data that you're using in var url so that we can replicate the issue.

    – SilentStone
    Mar 9 '18 at 20:24














6












6








6


1






I am using the d3 v3 script to visualize the Data. I need to highlight and bring the node front in mousecenter and vice versa in mouseleave. Now I can able to high light the node by increasing height and width of the node.



Can't able to bring the node front. I've tried using CSS like opacity, z-index.



Script



<script>

// some colour variables
var tcBlack = "purple";

// rest of vars
var w = 1500,
h = 800,
maxNodeSize = 50,
x_browser = 25,
y_browser = 25,
root;

var vis;
var force = d3.layout.force();

vis = d3.select("#visfel_map").append("svg").attr("width", w).attr("height", h);


d3.json(url, function(error,json) {
if (error)
return console.warn(error);
root = json;
root.fixed = true;
root.x = w / 2;
root.y = h / 4;


// Build the path
var defs = vis.insert("svg:defs")
.data(["end"]);


defs.enter().append("svg:path")
.attr("d", "M0,-5L10,0L0,5");

update();
});

function update() {
var nodes = flatten(root),
links = d3.layout.tree().links(nodes);

force.nodes(nodes)
.links(links)
.gravity(0.05)
.charge(-2500)
.linkDistance(200)
.friction(0.5)
.linkStrength(function(l, i) {return 1; })
.size([w, h])
.on("tick", tick)
.start();

var path = vis.selectAll("path.link")
.data(links, function(d) { return d.target.id; });

path.enter().insert("svg:path")
.attr("class", "link")
// .attr("marker-end", "url(#end)")
.style("stroke", "#ff8888");


// Exit any old paths.
path.exit().remove();



// Update the nodes…
var node = vis.selectAll("g.node")
.data(nodes, function(d) { return d.id; });


// Enter any new nodes.
var nodeEnter = node.enter().append("svg:g")
.attr("class", "node")
.attr("transform", function(d) { return "translate(" + d.x + "," + d.y + ")"; })
.on("click", click)
.call(force.drag);

// Append a circle
nodeEnter.append("svg:circle")
.attr("r", function(d) { return Math.sqrt(d.size) / 10 || 4.5; })
.style("fill", "#eee");


// Append images
var images = nodeEnter.append("svg:image")
.attr("xlink:href", function(d) { return d.img;})
.attr("x", function(d) { return -25;})
.attr("y", function(d) { return -25;})
.attr("height", 65)
.attr("width", 65);

// make the image grow a little on mouse over and add the text details on click
var setEvents = images
.on( 'click', function (d) {
console.log(d.sub_category_id)
})
.on( 'mouseenter', function() {
var currNode = d3.select(this);
currNode.transition()
.attr("x", function(d) { return -60;})
.attr("y", function(d) { return -60;})
.attr("height", 300)
.attr("width", 300);

})
// set back
.on( 'mouseleave', function() {
d3.select(this)
.transition()
.attr("x", function(d) { return -25;})
.attr("y", function(d) { return -25;})
.attr("height", 65)
.attr("width", 65);


});
// Append name on roll over next to the node as well
nodeEnter.append("text")
.attr("class", "nodetext")
.attr("x", function(d) { return d.children ? 70 : 70; })
.attr("y", function(d) { return d.children ? 10 : 10; })
.style("text-anchor", function(d) { return d.children ? "end" : "end"; })
.attr("fill", tcBlack)
.text(function(d) { return d.name; });


// Exit any old nodes.
node.exit().remove();


// Re-select for update.
path = vis.selectAll("path.link");
node = vis.selectAll("g.node");

function tick() {


path.attr("d", function(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;
});
node.attr("transform", nodeTransform);
}
}


/**
* Gives the coordinates of the border for keeping the nodes inside a frame
* http://bl.ocks.org/mbostock/1129492
*/
function nodeTransform(d) {
d.x = Math.max(maxNodeSize, Math.min(w - (d.imgwidth/2 || 16), d.x));
d.y = Math.max(maxNodeSize, Math.min(h - (d.imgheight/2 || 16), d.y));
return "translate(" + d.x + "," + d




.y + ")";
}

/**
* Toggle children on click.
*/
function click(d) {
if (d.children) {
d._children = d.children;
d.children = null;
} else {
d.children = d._children;
d._children = null;
}

update();
}


/**
* Returns a list of all nodes under the root.
*/
function flatten(root) {
var nodes = ;
var i = 0;

function recurse(node) {
if (node.children)
node.children.forEach(recurse);
if (!node.id)
node.id = ++i;
nodes.push(node);
}

recurse(root);
return nodes;
}

</script>


enter image description here



JSON Data



{
"type": "map",
"tree_size": "2",
"map_id": "1",
"name": "Sounds for Speech",
"img": "manage/visfel_images/map-1516338051-sounds for speech.png",
"children": [
{
"type": "category",
"tree_size": "2",
"category_id": "1",
"name": "Vowels",
"img": "manage/visfel_images/category-1516338094-vowel sound.png",
"children": [
{
"type": "sub",
"tree_size": "2",
"sub_category_id": "1",
"name": "A",
"img": "manage/visfel_images/sub-1516338159-A.png"
},
{
"type": "sub",
"tree_size": "2",
"sub_category_id": "2",
"name": "E",
"img": "manage/visfel_images/sub-1516338189-E.png"
},
{
"type": "sub",
"tree_size": "2",
"sub_category_id": "3",
"name": "I",
"img": "manage/visfel_images/sub-1516338212-i.png"
},
{
"type": "sub",
"tree_size": "2",
"sub_category_id": "4",
"name": "O",
"img": "manage/visfel_images/sub-1516338235-O.png"
},
{
"type": "sub",
"tree_size": "2",
"sub_category_id": "5",
"name": "U",
"img": "manage/visfel_images/sub-1516338260-U.png"
}
]
},
{
"type": "category",
"tree_size": "2",
"category_id": "2",
"name": "Consonents",
"img": "manage/visfel_images/category-1516338121-consonents.png",
"children": [
{
"type": "sub",
"tree_size": "2",
"sub_category_id": "6",
"name": "B",
"img": "manage/visfel_images/sub-1516338304-B.png"
},
{
"type": "sub",
"tree_size": "2",
"sub_category_id": "7",
"name": "C",
"img": "manage/visfel_images/sub-1516338323-C.png"
},
{
"type": "sub",
"tree_size": "2",
"sub_category_id": "8",
"name": "D",
"img": "manage/visfel_images/sub-1516338342-D.png"
},
{
"type": "sub",
"tree_size": "2",
"sub_category_id": "9",
"name": "F",
"img": "manage/visfel_images/sub-1516338362-F.png"
},
{
"type": "sub",
"tree_size": "2",
"sub_category_id": "10",
"name": "G",
"img": "manage/visfel_images/sub-1516338380-G.png"
},
{
"type": "sub",
"tree_size": "2",
"sub_category_id": "11",
"name": "H",
"img": "manage/visfel_images/sub-1516338401-H.png"
},
{
"type": "sub",
"tree_size": "2",
"sub_category_id": "12",
"name": "J",
"img": "manage/visfel_images/sub-1516338427-J.png"
},
{
"type": "sub",
"tree_size": "2",
"sub_category_id": "13",
"name": "K",
"img": "manage/visfel_images/sub-1516338452-K.png"
},
{
"type": "sub",
"tree_size": "2",
"sub_category_id": "14",
"name": "L",
"img": "manage/visfel_images/sub-1516338470-L.png"
},
{
"type": "sub",
"tree_size": "2",
"sub_category_id": "15",
"name": "M",
"img": "manage/visfel_images/sub-1516338489-M.png"
},
{
"type": "sub",
"tree_size": "2",
"sub_category_id": "16",
"name": "N",
"img": "manage/visfel_images/sub-1516338508-N.png"
},
{
"type": "sub",
"tree_size": "2",
"sub_category_id": "17",
"name": "P",
"img": "manage/visfel_images/sub-1516338542-P.png"
},
{
"type": "sub",
"tree_size": "2",
"sub_category_id": "18",
"name": "Q",
"img": "manage/visfel_images/sub-1516338560-Q.png"
},
{
"type": "sub",
"tree_size": "2",
"sub_category_id": "19",
"name": "R",
"img": "manage/visfel_images/sub-1516338579-R.png"
},
{
"type": "sub",
"tree_size": "2",
"sub_category_id": "20",
"name": "S",
"img": "manage/visfel_images/sub-1516338604-S.png"
},
{
"type": "sub",
"tree_size": "2",
"sub_category_id": "21",
"name": "T",
"img": "manage/visfel_images/sub-1516338619-T.png"
},
{
"type": "sub",
"tree_size": "2",
"sub_category_id": "22",
"name": "V",
"img": "manage/visfel_images/sub-1516338635-V.png"
},
{
"type": "sub",
"tree_size": "2",
"sub_category_id": "23",
"name": "W",
"img": "manage/visfel_images/sub-1516338650-W.png"
},
{
"type": "sub",
"tree_size": "2",
"sub_category_id": "24",
"name": "X",
"img": "manage/visfel_images/sub-1516338666-X.png"
},
{
"type": "sub",
"tree_size": "2",
"sub_category_id": "25",
"name": "Y",
"img": "manage/visfel_images/sub-1516338705-Y.png"
},
{
"type": "sub",
"tree_size": "2",
"sub_category_id": "26",
"name": "Z",
"img": "manage/visfel_images/sub-1516338742-Z.png"
}
]
}
]
}


In the attached screenshot, node "M" is in focus, mouse over on that element. Node highlights by increasing the width and height, same as node should come front, overlapping nodes should go back.



By Fading the other elements also enough, or else re-arrange the node elements to fix the problem.



Awaiting Suggestions?
Thanks in Advance.










share|improve this question
















I am using the d3 v3 script to visualize the Data. I need to highlight and bring the node front in mousecenter and vice versa in mouseleave. Now I can able to high light the node by increasing height and width of the node.



Can't able to bring the node front. I've tried using CSS like opacity, z-index.



Script



<script>

// some colour variables
var tcBlack = "purple";

// rest of vars
var w = 1500,
h = 800,
maxNodeSize = 50,
x_browser = 25,
y_browser = 25,
root;

var vis;
var force = d3.layout.force();

vis = d3.select("#visfel_map").append("svg").attr("width", w).attr("height", h);


d3.json(url, function(error,json) {
if (error)
return console.warn(error);
root = json;
root.fixed = true;
root.x = w / 2;
root.y = h / 4;


// Build the path
var defs = vis.insert("svg:defs")
.data(["end"]);


defs.enter().append("svg:path")
.attr("d", "M0,-5L10,0L0,5");

update();
});

function update() {
var nodes = flatten(root),
links = d3.layout.tree().links(nodes);

force.nodes(nodes)
.links(links)
.gravity(0.05)
.charge(-2500)
.linkDistance(200)
.friction(0.5)
.linkStrength(function(l, i) {return 1; })
.size([w, h])
.on("tick", tick)
.start();

var path = vis.selectAll("path.link")
.data(links, function(d) { return d.target.id; });

path.enter().insert("svg:path")
.attr("class", "link")
// .attr("marker-end", "url(#end)")
.style("stroke", "#ff8888");


// Exit any old paths.
path.exit().remove();



// Update the nodes…
var node = vis.selectAll("g.node")
.data(nodes, function(d) { return d.id; });


// Enter any new nodes.
var nodeEnter = node.enter().append("svg:g")
.attr("class", "node")
.attr("transform", function(d) { return "translate(" + d.x + "," + d.y + ")"; })
.on("click", click)
.call(force.drag);

// Append a circle
nodeEnter.append("svg:circle")
.attr("r", function(d) { return Math.sqrt(d.size) / 10 || 4.5; })
.style("fill", "#eee");


// Append images
var images = nodeEnter.append("svg:image")
.attr("xlink:href", function(d) { return d.img;})
.attr("x", function(d) { return -25;})
.attr("y", function(d) { return -25;})
.attr("height", 65)
.attr("width", 65);

// make the image grow a little on mouse over and add the text details on click
var setEvents = images
.on( 'click', function (d) {
console.log(d.sub_category_id)
})
.on( 'mouseenter', function() {
var currNode = d3.select(this);
currNode.transition()
.attr("x", function(d) { return -60;})
.attr("y", function(d) { return -60;})
.attr("height", 300)
.attr("width", 300);

})
// set back
.on( 'mouseleave', function() {
d3.select(this)
.transition()
.attr("x", function(d) { return -25;})
.attr("y", function(d) { return -25;})
.attr("height", 65)
.attr("width", 65);


});
// Append name on roll over next to the node as well
nodeEnter.append("text")
.attr("class", "nodetext")
.attr("x", function(d) { return d.children ? 70 : 70; })
.attr("y", function(d) { return d.children ? 10 : 10; })
.style("text-anchor", function(d) { return d.children ? "end" : "end"; })
.attr("fill", tcBlack)
.text(function(d) { return d.name; });


// Exit any old nodes.
node.exit().remove();


// Re-select for update.
path = vis.selectAll("path.link");
node = vis.selectAll("g.node");

function tick() {


path.attr("d", function(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;
});
node.attr("transform", nodeTransform);
}
}


/**
* Gives the coordinates of the border for keeping the nodes inside a frame
* http://bl.ocks.org/mbostock/1129492
*/
function nodeTransform(d) {
d.x = Math.max(maxNodeSize, Math.min(w - (d.imgwidth/2 || 16), d.x));
d.y = Math.max(maxNodeSize, Math.min(h - (d.imgheight/2 || 16), d.y));
return "translate(" + d.x + "," + d




.y + ")";
}

/**
* Toggle children on click.
*/
function click(d) {
if (d.children) {
d._children = d.children;
d.children = null;
} else {
d.children = d._children;
d._children = null;
}

update();
}


/**
* Returns a list of all nodes under the root.
*/
function flatten(root) {
var nodes = ;
var i = 0;

function recurse(node) {
if (node.children)
node.children.forEach(recurse);
if (!node.id)
node.id = ++i;
nodes.push(node);
}

recurse(root);
return nodes;
}

</script>


enter image description here



JSON Data



{
"type": "map",
"tree_size": "2",
"map_id": "1",
"name": "Sounds for Speech",
"img": "manage/visfel_images/map-1516338051-sounds for speech.png",
"children": [
{
"type": "category",
"tree_size": "2",
"category_id": "1",
"name": "Vowels",
"img": "manage/visfel_images/category-1516338094-vowel sound.png",
"children": [
{
"type": "sub",
"tree_size": "2",
"sub_category_id": "1",
"name": "A",
"img": "manage/visfel_images/sub-1516338159-A.png"
},
{
"type": "sub",
"tree_size": "2",
"sub_category_id": "2",
"name": "E",
"img": "manage/visfel_images/sub-1516338189-E.png"
},
{
"type": "sub",
"tree_size": "2",
"sub_category_id": "3",
"name": "I",
"img": "manage/visfel_images/sub-1516338212-i.png"
},
{
"type": "sub",
"tree_size": "2",
"sub_category_id": "4",
"name": "O",
"img": "manage/visfel_images/sub-1516338235-O.png"
},
{
"type": "sub",
"tree_size": "2",
"sub_category_id": "5",
"name": "U",
"img": "manage/visfel_images/sub-1516338260-U.png"
}
]
},
{
"type": "category",
"tree_size": "2",
"category_id": "2",
"name": "Consonents",
"img": "manage/visfel_images/category-1516338121-consonents.png",
"children": [
{
"type": "sub",
"tree_size": "2",
"sub_category_id": "6",
"name": "B",
"img": "manage/visfel_images/sub-1516338304-B.png"
},
{
"type": "sub",
"tree_size": "2",
"sub_category_id": "7",
"name": "C",
"img": "manage/visfel_images/sub-1516338323-C.png"
},
{
"type": "sub",
"tree_size": "2",
"sub_category_id": "8",
"name": "D",
"img": "manage/visfel_images/sub-1516338342-D.png"
},
{
"type": "sub",
"tree_size": "2",
"sub_category_id": "9",
"name": "F",
"img": "manage/visfel_images/sub-1516338362-F.png"
},
{
"type": "sub",
"tree_size": "2",
"sub_category_id": "10",
"name": "G",
"img": "manage/visfel_images/sub-1516338380-G.png"
},
{
"type": "sub",
"tree_size": "2",
"sub_category_id": "11",
"name": "H",
"img": "manage/visfel_images/sub-1516338401-H.png"
},
{
"type": "sub",
"tree_size": "2",
"sub_category_id": "12",
"name": "J",
"img": "manage/visfel_images/sub-1516338427-J.png"
},
{
"type": "sub",
"tree_size": "2",
"sub_category_id": "13",
"name": "K",
"img": "manage/visfel_images/sub-1516338452-K.png"
},
{
"type": "sub",
"tree_size": "2",
"sub_category_id": "14",
"name": "L",
"img": "manage/visfel_images/sub-1516338470-L.png"
},
{
"type": "sub",
"tree_size": "2",
"sub_category_id": "15",
"name": "M",
"img": "manage/visfel_images/sub-1516338489-M.png"
},
{
"type": "sub",
"tree_size": "2",
"sub_category_id": "16",
"name": "N",
"img": "manage/visfel_images/sub-1516338508-N.png"
},
{
"type": "sub",
"tree_size": "2",
"sub_category_id": "17",
"name": "P",
"img": "manage/visfel_images/sub-1516338542-P.png"
},
{
"type": "sub",
"tree_size": "2",
"sub_category_id": "18",
"name": "Q",
"img": "manage/visfel_images/sub-1516338560-Q.png"
},
{
"type": "sub",
"tree_size": "2",
"sub_category_id": "19",
"name": "R",
"img": "manage/visfel_images/sub-1516338579-R.png"
},
{
"type": "sub",
"tree_size": "2",
"sub_category_id": "20",
"name": "S",
"img": "manage/visfel_images/sub-1516338604-S.png"
},
{
"type": "sub",
"tree_size": "2",
"sub_category_id": "21",
"name": "T",
"img": "manage/visfel_images/sub-1516338619-T.png"
},
{
"type": "sub",
"tree_size": "2",
"sub_category_id": "22",
"name": "V",
"img": "manage/visfel_images/sub-1516338635-V.png"
},
{
"type": "sub",
"tree_size": "2",
"sub_category_id": "23",
"name": "W",
"img": "manage/visfel_images/sub-1516338650-W.png"
},
{
"type": "sub",
"tree_size": "2",
"sub_category_id": "24",
"name": "X",
"img": "manage/visfel_images/sub-1516338666-X.png"
},
{
"type": "sub",
"tree_size": "2",
"sub_category_id": "25",
"name": "Y",
"img": "manage/visfel_images/sub-1516338705-Y.png"
},
{
"type": "sub",
"tree_size": "2",
"sub_category_id": "26",
"name": "Z",
"img": "manage/visfel_images/sub-1516338742-Z.png"
}
]
}
]
}


In the attached screenshot, node "M" is in focus, mouse over on that element. Node highlights by increasing the width and height, same as node should come front, overlapping nodes should go back.



By Fading the other elements also enough, or else re-arrange the node elements to fix the problem.



Awaiting Suggestions?
Thanks in Advance.







javascript css html5 d3.js data-driven






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 16 '18 at 17:15









timiTao

1,20131729




1,20131729










asked Mar 6 '18 at 18:46









KeerthivasanKeerthivasan

1,066937




1,066937








  • 1





    You can contol the positioning ("absolute", "relative" etc) or the z index property with d3. Maybe start there.

    – Ryan Morton
    Mar 6 '18 at 18:57











  • @RyanMorton Already tried. Not working.

    – Keerthivasan
    Mar 6 '18 at 19:03






  • 3





    v4 has a selection.raise() method which does just that. Unfortunately v3 does not, but selection.order might do it.

    – pmkro
    Mar 6 '18 at 19:19






  • 2





    moveToFront()? bl.ocks.org/eesur/4e0a69d57d3bfc8a82c2

    – Ryan Morton
    Mar 6 '18 at 19:19






  • 2





    Please provide the data that you're using in var url so that we can replicate the issue.

    – SilentStone
    Mar 9 '18 at 20:24














  • 1





    You can contol the positioning ("absolute", "relative" etc) or the z index property with d3. Maybe start there.

    – Ryan Morton
    Mar 6 '18 at 18:57











  • @RyanMorton Already tried. Not working.

    – Keerthivasan
    Mar 6 '18 at 19:03






  • 3





    v4 has a selection.raise() method which does just that. Unfortunately v3 does not, but selection.order might do it.

    – pmkro
    Mar 6 '18 at 19:19






  • 2





    moveToFront()? bl.ocks.org/eesur/4e0a69d57d3bfc8a82c2

    – Ryan Morton
    Mar 6 '18 at 19:19






  • 2





    Please provide the data that you're using in var url so that we can replicate the issue.

    – SilentStone
    Mar 9 '18 at 20:24








1




1





You can contol the positioning ("absolute", "relative" etc) or the z index property with d3. Maybe start there.

– Ryan Morton
Mar 6 '18 at 18:57





You can contol the positioning ("absolute", "relative" etc) or the z index property with d3. Maybe start there.

– Ryan Morton
Mar 6 '18 at 18:57













@RyanMorton Already tried. Not working.

– Keerthivasan
Mar 6 '18 at 19:03





@RyanMorton Already tried. Not working.

– Keerthivasan
Mar 6 '18 at 19:03




3




3





v4 has a selection.raise() method which does just that. Unfortunately v3 does not, but selection.order might do it.

– pmkro
Mar 6 '18 at 19:19





v4 has a selection.raise() method which does just that. Unfortunately v3 does not, but selection.order might do it.

– pmkro
Mar 6 '18 at 19:19




2




2





moveToFront()? bl.ocks.org/eesur/4e0a69d57d3bfc8a82c2

– Ryan Morton
Mar 6 '18 at 19:19





moveToFront()? bl.ocks.org/eesur/4e0a69d57d3bfc8a82c2

– Ryan Morton
Mar 6 '18 at 19:19




2




2





Please provide the data that you're using in var url so that we can replicate the issue.

– SilentStone
Mar 9 '18 at 20:24





Please provide the data that you're using in var url so that we can replicate the issue.

– SilentStone
Mar 9 '18 at 20:24












3 Answers
3






active

oldest

votes


















2














d3.v4 - d3.v5



.on('mouseenter', function() {

d3.select(this).raise()

})


Example






share|improve this answer



















  • 1





    yes.. right. but we can able to create the same node map in v4-v5 ?

    – Keerthivasan
    Mar 16 '18 at 14:44











  • Yes, you will be able

    – bumbeishvili
    Aug 20 '18 at 17:02











  • thanks for your reply, if possible can you give example for my problem. still i can't able to achieve that solution.

    – Keerthivasan
    Aug 20 '18 at 17:23





















1














As already mentioned, drawing order of SVG elements is determined by their order in DOM.



The this.parentNode.appendChild(this) trick works as long as it's performed on the right element. In your case, it's not the <image> but its parent <g> that has to move.



images.on('mouseenter', function() {
var parent = this.parentNode; // <g>
parent.parentNode.appendChild(parent);
var currNode = d3.select(this);
//...
})


Example (with placeholder images)






share|improve this answer
























  • but, that append text not displaying when image comes to front.. any clue to append the text below image

    – Keerthivasan
    Mar 12 '18 at 16:33











  • @Keerthivasan Are you using code from @silentstone as well? His version removes the image from <g> and places it at the end of the main <svg> element. Could be the reason why text gets hidden under the image.

    – p4m
    Mar 12 '18 at 18:15











  • i'm tried both of you code. getting the same only

    – Keerthivasan
    Mar 13 '18 at 4:20











  • any clue, to come the text, below image when over the mouse.

    – Keerthivasan
    Mar 15 '18 at 16:51



















1














I'm sure you know by now that SVG doesn't have z-index. Instead, it layers the elements based on insertion order. To solve your issue, add the node again so that it's inserted after (and displayed over) everything else.



Here's the JSFiddle with the solution, running with a sample image.



To summarize:





  1. Add an ID to the SVG so that selections are simple and unambiguous:



    vis = d3.select("#visfel_map").append("svg")
    .attr("width", w).attr("height", h).attr("id", "mainSvg");



  2. Add an ID for each node during setup:



    var idStart = "letter_";
    // Append images
    var images = nodeEnter.append("svg:image")
    .attr("xlink:href", "https://www.freeclipartnow.com/d/39895-1/decorative-letter-A.jpg")
    .attr("id", function(d){return idStart + d.name;})


    ...`




  3. Next, add the function to move the desired element to the end of the insertion list. I based it on this block but I'm specifying the ID of the SVG element to avoid confusion.



    d3.selection.prototype.moveToSvgFront = function() {  
    return this.each(function(){
    d3.select("#mainSvg").node().appendChild(this);
    });
    };



  4. Finally, update the mouseenter function, using moveToSvgFront. Also invoke your existing nodeTransform function to ensure that the element is positioned correctly:



    .on( 'mouseenter', function() {
    var currNode = d3.select(this);
    if(currNode.attr("id").length === idStart.length + 1){ // only run for letters, not for titles (which cause an error in nodeTransform)
    currNode.moveToSvgFront();
    currNode.attr("transform", nodeTransform);
    ...
    }
    })



**Edit: @Keerthivasan Here's a JSFiddle with code to also moves and styles the text. Note that



a. CSS has been added for the new class (focusLabel)



b. The labels have been given an ID related to the node's ID; this is used to access the label in the mouseenter function, which sets the translation based on the node's position and size



c. The original coordinates are reapplied in the mouseleave function.






share|improve this answer


























  • Thanks for the effort. it's working fine for image come to front.. that append text not displaying when image comes to front.. any clue to append the text below image ??

    – Keerthivasan
    Mar 12 '18 at 16:58











  • A new JSFiddle has been added to the answer. You may need to add a rectangle if you want the label background to have full opacity

    – SilentStone
    Mar 13 '18 at 12:58











  • after updating your code. old paths can't able to replace. some nodes position is changed to static. Check in your JSFiddle itself.

    – Keerthivasan
    Mar 15 '18 at 2:01













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%2f49137943%2fhow-to-bring-the-selected-node-front-in-d3-js%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























3 Answers
3






active

oldest

votes








3 Answers
3






active

oldest

votes









active

oldest

votes






active

oldest

votes









2














d3.v4 - d3.v5



.on('mouseenter', function() {

d3.select(this).raise()

})


Example






share|improve this answer



















  • 1





    yes.. right. but we can able to create the same node map in v4-v5 ?

    – Keerthivasan
    Mar 16 '18 at 14:44











  • Yes, you will be able

    – bumbeishvili
    Aug 20 '18 at 17:02











  • thanks for your reply, if possible can you give example for my problem. still i can't able to achieve that solution.

    – Keerthivasan
    Aug 20 '18 at 17:23


















2














d3.v4 - d3.v5



.on('mouseenter', function() {

d3.select(this).raise()

})


Example






share|improve this answer



















  • 1





    yes.. right. but we can able to create the same node map in v4-v5 ?

    – Keerthivasan
    Mar 16 '18 at 14:44











  • Yes, you will be able

    – bumbeishvili
    Aug 20 '18 at 17:02











  • thanks for your reply, if possible can you give example for my problem. still i can't able to achieve that solution.

    – Keerthivasan
    Aug 20 '18 at 17:23
















2












2








2







d3.v4 - d3.v5



.on('mouseenter', function() {

d3.select(this).raise()

})


Example






share|improve this answer













d3.v4 - d3.v5



.on('mouseenter', function() {

d3.select(this).raise()

})


Example







share|improve this answer












share|improve this answer



share|improve this answer










answered Mar 16 '18 at 11:25









bumbeishvilibumbeishvili

9171119




9171119








  • 1





    yes.. right. but we can able to create the same node map in v4-v5 ?

    – Keerthivasan
    Mar 16 '18 at 14:44











  • Yes, you will be able

    – bumbeishvili
    Aug 20 '18 at 17:02











  • thanks for your reply, if possible can you give example for my problem. still i can't able to achieve that solution.

    – Keerthivasan
    Aug 20 '18 at 17:23
















  • 1





    yes.. right. but we can able to create the same node map in v4-v5 ?

    – Keerthivasan
    Mar 16 '18 at 14:44











  • Yes, you will be able

    – bumbeishvili
    Aug 20 '18 at 17:02











  • thanks for your reply, if possible can you give example for my problem. still i can't able to achieve that solution.

    – Keerthivasan
    Aug 20 '18 at 17:23










1




1





yes.. right. but we can able to create the same node map in v4-v5 ?

– Keerthivasan
Mar 16 '18 at 14:44





yes.. right. but we can able to create the same node map in v4-v5 ?

– Keerthivasan
Mar 16 '18 at 14:44













Yes, you will be able

– bumbeishvili
Aug 20 '18 at 17:02





Yes, you will be able

– bumbeishvili
Aug 20 '18 at 17:02













thanks for your reply, if possible can you give example for my problem. still i can't able to achieve that solution.

– Keerthivasan
Aug 20 '18 at 17:23







thanks for your reply, if possible can you give example for my problem. still i can't able to achieve that solution.

– Keerthivasan
Aug 20 '18 at 17:23















1














As already mentioned, drawing order of SVG elements is determined by their order in DOM.



The this.parentNode.appendChild(this) trick works as long as it's performed on the right element. In your case, it's not the <image> but its parent <g> that has to move.



images.on('mouseenter', function() {
var parent = this.parentNode; // <g>
parent.parentNode.appendChild(parent);
var currNode = d3.select(this);
//...
})


Example (with placeholder images)






share|improve this answer
























  • but, that append text not displaying when image comes to front.. any clue to append the text below image

    – Keerthivasan
    Mar 12 '18 at 16:33











  • @Keerthivasan Are you using code from @silentstone as well? His version removes the image from <g> and places it at the end of the main <svg> element. Could be the reason why text gets hidden under the image.

    – p4m
    Mar 12 '18 at 18:15











  • i'm tried both of you code. getting the same only

    – Keerthivasan
    Mar 13 '18 at 4:20











  • any clue, to come the text, below image when over the mouse.

    – Keerthivasan
    Mar 15 '18 at 16:51
















1














As already mentioned, drawing order of SVG elements is determined by their order in DOM.



The this.parentNode.appendChild(this) trick works as long as it's performed on the right element. In your case, it's not the <image> but its parent <g> that has to move.



images.on('mouseenter', function() {
var parent = this.parentNode; // <g>
parent.parentNode.appendChild(parent);
var currNode = d3.select(this);
//...
})


Example (with placeholder images)






share|improve this answer
























  • but, that append text not displaying when image comes to front.. any clue to append the text below image

    – Keerthivasan
    Mar 12 '18 at 16:33











  • @Keerthivasan Are you using code from @silentstone as well? His version removes the image from <g> and places it at the end of the main <svg> element. Could be the reason why text gets hidden under the image.

    – p4m
    Mar 12 '18 at 18:15











  • i'm tried both of you code. getting the same only

    – Keerthivasan
    Mar 13 '18 at 4:20











  • any clue, to come the text, below image when over the mouse.

    – Keerthivasan
    Mar 15 '18 at 16:51














1












1








1







As already mentioned, drawing order of SVG elements is determined by their order in DOM.



The this.parentNode.appendChild(this) trick works as long as it's performed on the right element. In your case, it's not the <image> but its parent <g> that has to move.



images.on('mouseenter', function() {
var parent = this.parentNode; // <g>
parent.parentNode.appendChild(parent);
var currNode = d3.select(this);
//...
})


Example (with placeholder images)






share|improve this answer













As already mentioned, drawing order of SVG elements is determined by their order in DOM.



The this.parentNode.appendChild(this) trick works as long as it's performed on the right element. In your case, it's not the <image> but its parent <g> that has to move.



images.on('mouseenter', function() {
var parent = this.parentNode; // <g>
parent.parentNode.appendChild(parent);
var currNode = d3.select(this);
//...
})


Example (with placeholder images)







share|improve this answer












share|improve this answer



share|improve this answer










answered Mar 12 '18 at 0:17









p4mp4m

364




364













  • but, that append text not displaying when image comes to front.. any clue to append the text below image

    – Keerthivasan
    Mar 12 '18 at 16:33











  • @Keerthivasan Are you using code from @silentstone as well? His version removes the image from <g> and places it at the end of the main <svg> element. Could be the reason why text gets hidden under the image.

    – p4m
    Mar 12 '18 at 18:15











  • i'm tried both of you code. getting the same only

    – Keerthivasan
    Mar 13 '18 at 4:20











  • any clue, to come the text, below image when over the mouse.

    – Keerthivasan
    Mar 15 '18 at 16:51



















  • but, that append text not displaying when image comes to front.. any clue to append the text below image

    – Keerthivasan
    Mar 12 '18 at 16:33











  • @Keerthivasan Are you using code from @silentstone as well? His version removes the image from <g> and places it at the end of the main <svg> element. Could be the reason why text gets hidden under the image.

    – p4m
    Mar 12 '18 at 18:15











  • i'm tried both of you code. getting the same only

    – Keerthivasan
    Mar 13 '18 at 4:20











  • any clue, to come the text, below image when over the mouse.

    – Keerthivasan
    Mar 15 '18 at 16:51

















but, that append text not displaying when image comes to front.. any clue to append the text below image

– Keerthivasan
Mar 12 '18 at 16:33





but, that append text not displaying when image comes to front.. any clue to append the text below image

– Keerthivasan
Mar 12 '18 at 16:33













@Keerthivasan Are you using code from @silentstone as well? His version removes the image from <g> and places it at the end of the main <svg> element. Could be the reason why text gets hidden under the image.

– p4m
Mar 12 '18 at 18:15





@Keerthivasan Are you using code from @silentstone as well? His version removes the image from <g> and places it at the end of the main <svg> element. Could be the reason why text gets hidden under the image.

– p4m
Mar 12 '18 at 18:15













i'm tried both of you code. getting the same only

– Keerthivasan
Mar 13 '18 at 4:20





i'm tried both of you code. getting the same only

– Keerthivasan
Mar 13 '18 at 4:20













any clue, to come the text, below image when over the mouse.

– Keerthivasan
Mar 15 '18 at 16:51





any clue, to come the text, below image when over the mouse.

– Keerthivasan
Mar 15 '18 at 16:51











1














I'm sure you know by now that SVG doesn't have z-index. Instead, it layers the elements based on insertion order. To solve your issue, add the node again so that it's inserted after (and displayed over) everything else.



Here's the JSFiddle with the solution, running with a sample image.



To summarize:





  1. Add an ID to the SVG so that selections are simple and unambiguous:



    vis = d3.select("#visfel_map").append("svg")
    .attr("width", w).attr("height", h).attr("id", "mainSvg");



  2. Add an ID for each node during setup:



    var idStart = "letter_";
    // Append images
    var images = nodeEnter.append("svg:image")
    .attr("xlink:href", "https://www.freeclipartnow.com/d/39895-1/decorative-letter-A.jpg")
    .attr("id", function(d){return idStart + d.name;})


    ...`




  3. Next, add the function to move the desired element to the end of the insertion list. I based it on this block but I'm specifying the ID of the SVG element to avoid confusion.



    d3.selection.prototype.moveToSvgFront = function() {  
    return this.each(function(){
    d3.select("#mainSvg").node().appendChild(this);
    });
    };



  4. Finally, update the mouseenter function, using moveToSvgFront. Also invoke your existing nodeTransform function to ensure that the element is positioned correctly:



    .on( 'mouseenter', function() {
    var currNode = d3.select(this);
    if(currNode.attr("id").length === idStart.length + 1){ // only run for letters, not for titles (which cause an error in nodeTransform)
    currNode.moveToSvgFront();
    currNode.attr("transform", nodeTransform);
    ...
    }
    })



**Edit: @Keerthivasan Here's a JSFiddle with code to also moves and styles the text. Note that



a. CSS has been added for the new class (focusLabel)



b. The labels have been given an ID related to the node's ID; this is used to access the label in the mouseenter function, which sets the translation based on the node's position and size



c. The original coordinates are reapplied in the mouseleave function.






share|improve this answer


























  • Thanks for the effort. it's working fine for image come to front.. that append text not displaying when image comes to front.. any clue to append the text below image ??

    – Keerthivasan
    Mar 12 '18 at 16:58











  • A new JSFiddle has been added to the answer. You may need to add a rectangle if you want the label background to have full opacity

    – SilentStone
    Mar 13 '18 at 12:58











  • after updating your code. old paths can't able to replace. some nodes position is changed to static. Check in your JSFiddle itself.

    – Keerthivasan
    Mar 15 '18 at 2:01


















1














I'm sure you know by now that SVG doesn't have z-index. Instead, it layers the elements based on insertion order. To solve your issue, add the node again so that it's inserted after (and displayed over) everything else.



Here's the JSFiddle with the solution, running with a sample image.



To summarize:





  1. Add an ID to the SVG so that selections are simple and unambiguous:



    vis = d3.select("#visfel_map").append("svg")
    .attr("width", w).attr("height", h).attr("id", "mainSvg");



  2. Add an ID for each node during setup:



    var idStart = "letter_";
    // Append images
    var images = nodeEnter.append("svg:image")
    .attr("xlink:href", "https://www.freeclipartnow.com/d/39895-1/decorative-letter-A.jpg")
    .attr("id", function(d){return idStart + d.name;})


    ...`




  3. Next, add the function to move the desired element to the end of the insertion list. I based it on this block but I'm specifying the ID of the SVG element to avoid confusion.



    d3.selection.prototype.moveToSvgFront = function() {  
    return this.each(function(){
    d3.select("#mainSvg").node().appendChild(this);
    });
    };



  4. Finally, update the mouseenter function, using moveToSvgFront. Also invoke your existing nodeTransform function to ensure that the element is positioned correctly:



    .on( 'mouseenter', function() {
    var currNode = d3.select(this);
    if(currNode.attr("id").length === idStart.length + 1){ // only run for letters, not for titles (which cause an error in nodeTransform)
    currNode.moveToSvgFront();
    currNode.attr("transform", nodeTransform);
    ...
    }
    })



**Edit: @Keerthivasan Here's a JSFiddle with code to also moves and styles the text. Note that



a. CSS has been added for the new class (focusLabel)



b. The labels have been given an ID related to the node's ID; this is used to access the label in the mouseenter function, which sets the translation based on the node's position and size



c. The original coordinates are reapplied in the mouseleave function.






share|improve this answer


























  • Thanks for the effort. it's working fine for image come to front.. that append text not displaying when image comes to front.. any clue to append the text below image ??

    – Keerthivasan
    Mar 12 '18 at 16:58











  • A new JSFiddle has been added to the answer. You may need to add a rectangle if you want the label background to have full opacity

    – SilentStone
    Mar 13 '18 at 12:58











  • after updating your code. old paths can't able to replace. some nodes position is changed to static. Check in your JSFiddle itself.

    – Keerthivasan
    Mar 15 '18 at 2:01
















1












1








1







I'm sure you know by now that SVG doesn't have z-index. Instead, it layers the elements based on insertion order. To solve your issue, add the node again so that it's inserted after (and displayed over) everything else.



Here's the JSFiddle with the solution, running with a sample image.



To summarize:





  1. Add an ID to the SVG so that selections are simple and unambiguous:



    vis = d3.select("#visfel_map").append("svg")
    .attr("width", w).attr("height", h).attr("id", "mainSvg");



  2. Add an ID for each node during setup:



    var idStart = "letter_";
    // Append images
    var images = nodeEnter.append("svg:image")
    .attr("xlink:href", "https://www.freeclipartnow.com/d/39895-1/decorative-letter-A.jpg")
    .attr("id", function(d){return idStart + d.name;})


    ...`




  3. Next, add the function to move the desired element to the end of the insertion list. I based it on this block but I'm specifying the ID of the SVG element to avoid confusion.



    d3.selection.prototype.moveToSvgFront = function() {  
    return this.each(function(){
    d3.select("#mainSvg").node().appendChild(this);
    });
    };



  4. Finally, update the mouseenter function, using moveToSvgFront. Also invoke your existing nodeTransform function to ensure that the element is positioned correctly:



    .on( 'mouseenter', function() {
    var currNode = d3.select(this);
    if(currNode.attr("id").length === idStart.length + 1){ // only run for letters, not for titles (which cause an error in nodeTransform)
    currNode.moveToSvgFront();
    currNode.attr("transform", nodeTransform);
    ...
    }
    })



**Edit: @Keerthivasan Here's a JSFiddle with code to also moves and styles the text. Note that



a. CSS has been added for the new class (focusLabel)



b. The labels have been given an ID related to the node's ID; this is used to access the label in the mouseenter function, which sets the translation based on the node's position and size



c. The original coordinates are reapplied in the mouseleave function.






share|improve this answer















I'm sure you know by now that SVG doesn't have z-index. Instead, it layers the elements based on insertion order. To solve your issue, add the node again so that it's inserted after (and displayed over) everything else.



Here's the JSFiddle with the solution, running with a sample image.



To summarize:





  1. Add an ID to the SVG so that selections are simple and unambiguous:



    vis = d3.select("#visfel_map").append("svg")
    .attr("width", w).attr("height", h).attr("id", "mainSvg");



  2. Add an ID for each node during setup:



    var idStart = "letter_";
    // Append images
    var images = nodeEnter.append("svg:image")
    .attr("xlink:href", "https://www.freeclipartnow.com/d/39895-1/decorative-letter-A.jpg")
    .attr("id", function(d){return idStart + d.name;})


    ...`




  3. Next, add the function to move the desired element to the end of the insertion list. I based it on this block but I'm specifying the ID of the SVG element to avoid confusion.



    d3.selection.prototype.moveToSvgFront = function() {  
    return this.each(function(){
    d3.select("#mainSvg").node().appendChild(this);
    });
    };



  4. Finally, update the mouseenter function, using moveToSvgFront. Also invoke your existing nodeTransform function to ensure that the element is positioned correctly:



    .on( 'mouseenter', function() {
    var currNode = d3.select(this);
    if(currNode.attr("id").length === idStart.length + 1){ // only run for letters, not for titles (which cause an error in nodeTransform)
    currNode.moveToSvgFront();
    currNode.attr("transform", nodeTransform);
    ...
    }
    })



**Edit: @Keerthivasan Here's a JSFiddle with code to also moves and styles the text. Note that



a. CSS has been added for the new class (focusLabel)



b. The labels have been given an ID related to the node's ID; this is used to access the label in the mouseenter function, which sets the translation based on the node's position and size



c. The original coordinates are reapplied in the mouseleave function.







share|improve this answer














share|improve this answer



share|improve this answer








edited Mar 16 '18 at 7:21









Bhuwan

13.2k41540




13.2k41540










answered Mar 12 '18 at 0:02









SilentStoneSilentStone

40337




40337













  • Thanks for the effort. it's working fine for image come to front.. that append text not displaying when image comes to front.. any clue to append the text below image ??

    – Keerthivasan
    Mar 12 '18 at 16:58











  • A new JSFiddle has been added to the answer. You may need to add a rectangle if you want the label background to have full opacity

    – SilentStone
    Mar 13 '18 at 12:58











  • after updating your code. old paths can't able to replace. some nodes position is changed to static. Check in your JSFiddle itself.

    – Keerthivasan
    Mar 15 '18 at 2:01





















  • Thanks for the effort. it's working fine for image come to front.. that append text not displaying when image comes to front.. any clue to append the text below image ??

    – Keerthivasan
    Mar 12 '18 at 16:58











  • A new JSFiddle has been added to the answer. You may need to add a rectangle if you want the label background to have full opacity

    – SilentStone
    Mar 13 '18 at 12:58











  • after updating your code. old paths can't able to replace. some nodes position is changed to static. Check in your JSFiddle itself.

    – Keerthivasan
    Mar 15 '18 at 2:01



















Thanks for the effort. it's working fine for image come to front.. that append text not displaying when image comes to front.. any clue to append the text below image ??

– Keerthivasan
Mar 12 '18 at 16:58





Thanks for the effort. it's working fine for image come to front.. that append text not displaying when image comes to front.. any clue to append the text below image ??

– Keerthivasan
Mar 12 '18 at 16:58













A new JSFiddle has been added to the answer. You may need to add a rectangle if you want the label background to have full opacity

– SilentStone
Mar 13 '18 at 12:58





A new JSFiddle has been added to the answer. You may need to add a rectangle if you want the label background to have full opacity

– SilentStone
Mar 13 '18 at 12:58













after updating your code. old paths can't able to replace. some nodes position is changed to static. Check in your JSFiddle itself.

– Keerthivasan
Mar 15 '18 at 2:01







after updating your code. old paths can't able to replace. some nodes position is changed to static. Check in your JSFiddle itself.

– Keerthivasan
Mar 15 '18 at 2:01




















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%2f49137943%2fhow-to-bring-the-selected-node-front-in-d3-js%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

To store a contact into the json file from server.js file using a class in NodeJS

Redirect URL with Chrome Remote Debugging Android Devices

Dieringhausen