score:1
Accepted answer
You could use Document.elementsFromPoint()
. Although this method is still considered experimental it is implemented by all major browsers.
The method will return all elements underneath the specified coordinates including all ancestors of the paths you are interested in, though. To get just your paths, you will have to apply an additional filtering:
d3.selectAll(document.elementsFromPoint(d3.event.x, d3.event.y)).filter("path");
Because Stack Snippets accessing the DOM tree somehow tend to freeze the browser, here is a JSFiddle demonstrating the use.
Should this shortcoming get fixed in the future, here is the working demo:
var myData = "x line1 line2 line3\n\
1 63.4 62.7 72.2\n\
2 58.0 59.9 67.7\n\
3 53.3 59.1 69.4\n\
4 55.7 55.7 55.7\n\
5 58.7 58.7 58.7\n\
6 77.0 77.0 77.0\n\
7 57.9 56.7 82.3\n\
8 61.8 56.8 78.9\n\
9 69.3 56.7 68.8\n\
10 71.2 60.1 68.7\n";
var data = d3.tsvParse(myData);
var margin = {
top: 20,
right: 20,
bottom: 40,
left: 50
};
var height = 500 - margin.bottom - margin.top,
width = 960 - margin.left - margin.right;
var line = d3.line()
.x(function(d) {
return X(d.x);
})
.y(function(d) {
return Y(d.y);
});
var X = d3.scaleLinear().range([0, width]);
var Y = d3.scaleLinear().range([height, 0]);
var colour = d3.scaleOrdinal(d3.schemeCategory10);
colour.domain(d3.keys(data[0]).filter(function(key) {
return key !== "x";
}));
var lines = colour.domain().map(function(name) {
return {
name: name,
values: data.map(function(d) {
return {
x: +d.x,
y: +d[name]
};
})
};
});
var svg = d3.select('body').append('svg')
.attr('height', height + margin.top + margin.bottom)
.attr('width', width + margin.left + margin.right)
.append('g')
.attr('transform', 'translate(' + margin.left + ',' + margin.top + ')');
X.domain(d3.extent(data, function(d) {
return d.x;
}));
Y.domain([
d3.min(lines, function(c) {
return d3.min(c.values, function(v) {
return v.y;
});
}),
d3.max(lines, function(c) {
return d3.max(c.values, function(v) {
return v.y;
});
})
]);
svg.append("g")
.attr("class", "axis axis--x")
.attr("transform", "translate(0," + height + ")")
.call(d3.axisBottom(X));
svg.append("g")
.attr("class", "axis axis--y")
.call(d3.axisLeft(Y));
var gLines = svg.selectAll('.lines')
.data(lines)
.enter().append('g')
.attr('class', 'lines');
gLines.append('path')
.attr('d', function(d) {
return line(d.values);
})
.style('stroke', function(d) {
return colour(d.name)
})
.style('fill', 'none')
.on('mouseenter', function(d) {
console.dir(d3.selectAll(document.elementsFromPoint(d3.event.x, d3.event.y)).filter("path"));
});
<script src="https://d3js.org/d3.v4.min.js"></script>
Source: stackoverflow.com
Related Query
- How to get all overlapping elements on 'mouseenter' in D3?
- How can I get the bounding box values of all the text elements in a selection?
- How do I remove all children elements from a node and then apply them again with different color and size?
- 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 toggle the class of all elements in a selection?
- How to avoid the overlapping of text elements on the TreeMap when child elements are opened in D3.js?
- How to get reference to SVG (child) elements nested in another SVG element (parent) using D3?
- Get mean of all elements at a given index across multiple arrays
- JS- how to remove duplicate JSON nodes and add one link for all nodes that get merged
- d3: How to append nested elements on enter()? My elements get appended, but subsequent select doesn't see them
- How to select all elements with a class that contains a str?
- How to get the same text of each elements in heat map as the y axis label?
- D3 - How to get width of previous elements in Text selection
- How to get a single property of all children in a nested object using javascript?
- How to update all existing elements in the same level with same parent value?
- How do I get my two codes to work so that the D3 works all the time
- How to show all data elements using D3 and Bootstrap?
- How do projects like d3 get all of their source into one large javascript file?
- D3.js - Get all possible child elements for a node
- how to get silbling elements in sunburst
- How to get mouseenter function to display data
- How to get all value displayed from loop in javascripts using d3.js? always got last value displayed in charts
- How to change all elements except clicked element?
- How can I get only elements with odd ids from a D3 selection?
- How to Fit all svg elements into div, to not overflow and hide elements?
- How to surround all selected SVG path elements with div in d3?
- How to get rid of hairline from overlapping svg shapes
- How to get all d3.js charts on one page
- D3.js: How to get the computed width and height for an arbitrary element?
- How to get coordinates of an svg element?
More Query from same tag
- D3.js cluster rollover - how to select and change another element
- How to color different data sets with different colors in dimple.js
- The array does not output properly
- How to convert Data table to tree json format dynamically?
- get updated data attribute after button click
- D3.js - Creating a rectangle?
- D3.js - did the transition behavior changed in v4 ?
- Cubism callback array seemingly replaced
- d3.js v5 changing an element from an imported svg
- Place label beside links on collapsible d3 tree layout
- NVD3 Multibar default preselect certain bars
- Drawing D3 paths one after the other
- streamgraph from .csv file in D3
- How do I arrange all bubbles in circular manner in bubble chart with force layout?
- D3 brushing on grouped bar chart
- d3.js: how to join data from more sources
- How to draw a d3.js line chart using angularjs directives
- Avoiding the use of template literals in Javascript
- D3.js violin plot not showing up
- Make An Line Chart Using Array - D3.js
- Double clicking Europe data visualization
- D3 x and y coordinates applied to last appened element instead of the group
- How to integrate real-time data into d3 charts in react
- How to select value in object with key in d3
- Problems with Topojson scale
- D3 output not rendering in Safari, Firefox, Edge and IE. Works in Chrome
- calculate spiral subsection with d3js
- Transition partition arcs back to original angles
- d3: Animate bar chart transforming into pie chart, like Heer and Robertson do?
- d3.js: how to conditionally render a curve?