score:122
tl;dr
with latest versions of d3, you can use selection.raise()
as explained by tmpearce in its answer. please scroll down and upvote!
original answer
you will have to change the order of object and make the circle you mouseover being the last element added. as you can see here: https://gist.github.com/3922684 and as suggested by nautat, you have to define the following before your main script:
d3.selection.prototype.movetofront = function() {
return this.each(function(){
this.parentnode.appendchild(this);
});
};
then you will just have to call the movetofront
function on your object (say circles
) on mouseover:
circles.on("mouseover",function(){
var sel = d3.select(this);
sel.movetofront();
});
edit: as suggested by henrik nordberg it is necessary to bind the data to the dom by using the second argument of the .data()
. this is necessary to not lose binding on elements. please read henrick's answer (and upvote it!) for more info. as a general advice, always use the second argument of .data()
when binding data to the dom in order to leverage the full performance capabilities of d3.
edit: as mentioned by clemens tolboom, the reverse function would be:
d3.selection.prototype.movetoback = function() {
return this.each(function() {
var firstchild = this.parentnode.firstchild;
if (firstchild) {
this.parentnode.insertbefore(this, firstchild);
}
});
};
score:13
from my painful experience with ie9, using parentnode.appendchild may lead to lost event handlers in the nodes. so i tend to use another approach, that is, sorting the nodes so that the selected one is above the others:
.on("mouseover", function(selected) {
vis.selectall('.node')
.sort(function(a, b) {
if (a.id === selected.id) {
return 1;
} else {
if (b.id === selected.id) {
return -1;
} else {
return 0;
}
}
});
})
score:25
if you use the movetofront()
method, make sure you are specifying the second argument to the data()
join method, or your data will be out of synch with your dom.
d3.js joins your data (provided to a parse()
) method with dom elements you create. if you manipulate the dom as proposed above, d3.js won't know what dom element corresponds to what data 'point' unless you specify a unique value for each data 'point' in the data()
call as the api reference shows:
.data(data, function(d) { return d.name; })
score:32
as of d3 version 4, there are a set of built in functions that handle this type of behavior without needing to implement it yourself. see the d3v4 documentation for more info.
long story short, to bring an element to the front, use selection.raise()
selection.raise()
re-inserts each selected element, in order, as the last child of its parent. equivalent to:
selection.each(function() {
this.parentnode.appendchild(this);
});
Source: stackoverflow.com
Related Query
- How can I bring a circle to the front with d3?
- How can I make the animation of a circle continous with hovering like this gif in d3.js?
- D3.js: How can I put an image on the background of a circle with the remaining parts trimmed and make it resize along with the circle onclick?
- How can I connect a circle to a path with a line that is perpendicular to the x axis to create an "error line"?
- MultiBar chart with nvd3 / d3 only shows labels for every other tick on the x-axis. How can I get them all to show up?
- How can I change the radius and opacity of a circle in d3?
- How can I start with all the nodes collapsed in d3js?
- how to bring the selected node front in d3 js?
- How can I set the position of a drop down list with D3.js?
- How can I make my lines spin with the globe?
- How can you color each circle in the D3 circle pack layout
- Using d3 in meteor on windows 8.1, how can I display a world map, zoomed in on the US, with country borders and US state borders?
- How can I change the color of a circle created via d3 each time I click the mouse?
- How can i bring gridlines to the front?
- How can I apply a clipPath in SVG with multiple paths and NOT clip out the area between the paths?
- How can I detect panning and zooming by the user with d3 zoomBehavior?
- How can you change the height of an x-axis with d3.js
- How can I create a basic bar chart with unique counts on the y axis and a category on the x axis?
- D3: How can I cause the stroke of an SVG path generated with D3 to be clipped to the interior of the same path?
- How can I use the latest version of the d3 js library with Angular Dart?
- DC.js choropleth map chart CSS conflicting with colouring, no map showing. How can I turn off the fill:none?
- With d3.js, how can I use the same script to create 3 charts with 3 datasources?
- how to append a circle with a label to indicate information to rectangle node and remove the circle before download
- D3.js: How can I give the same style to Top and Right line of a grid with the same style than grid lines?
- How can I properly aggregate / group multiple line graphs into one overall graph with d3.js when the x-values aren't matching exactly?
- How can I change the attribute of the circle node in d3.js?
- how to change the color of circle to back with original color on clicking of another circle in D3 Js chart
- D3 - How can I show/hide a text element when hovering a circle element created from different attributes of the same data entry?
- how do I set up a grouping query on the server side. I want set up the query so I can use it for a pie chart in front end, using d3js
- How can I toggle the value of an svg text element with D3.js when user clicks the text?
More Query from same tag
- D3 mousedown event deleting the wrong node
- svg coordinate of g element larger than other element
- How to set default value appearing in dropdown list with D3?
- Add color to multiple paths from an array of exact same data in D3.js
- Having trouble drawing d3.js Focus+Context via Brushing chart with multiple paths
- Applying gravity to a bounded d3.js force layout
- Radio button selected is always on
- Efficient data storage, partitioning, and selection in d3
- d3 attach attribute if it exists
- Making a D3.js stacked bar chart responsive
- Can't read json in D3
- Transition chaining and cancellation
- D3.js - Animated exit for nested object
- maximum number of svg elements for the browser in a map
- Import a package as an extension of another package with ES6 syntax
- Can an array be used as a d3 nest key?
- How to send HTML to an embedded WebView without running a local webserver?
- D3 mercator function NaN
- Adding second click event listener within a click event listener
- Recovering x-axis value on click - NVD3 LineChart
- D3JS Plotting Line Graph From Array Data (Data is undefined error)
- D3 how do I start my y-axis at something other than 0
- How to format the units to avoid decimal points?
- Repainting/Refreshing Graph in D3
- Functions don't recognize variables
- Retrieve stroke-width attribute of d3 axis path
- Word cloud not displaying correctly with d3.v4
- d3.js Line Chart with a barchart
- D3 force layout with highlighting and fading
- Design a SVG using D3js