score:7
for a quick fix, you can move the range above the overlay and manually call the overlay event handlers from the range.
svg.append("rect")
.attr("class", "overlay")
.attr("width", width)
.attr("height", height)
.on("mouseover", function() { focus.style("display", null); })
.on("mouseout", function() { focus.style("display", "none"); })
.on("mousemove", mousemove);
// move range above overlay and call the overlay event handlers from there
svg.append("rect")
.attr("id", "range")
.attr("class", "range")
.attr("x", left)
.attr("width", wid)
.attr("height", height)
.on("mousemove", mousemove)
.on("mouseout", function() { focus.style("display", "none"); })
.on("mouseover", function() {
focus.style("display", null);
// event handling for range mouseover (alert broke mouse move)
console.log("i can see you!");
});
bubbling acts at the dom level, and since there is no way to have a rect be a child of another rect, bubbling will not take care of this for you. grouping the elements together and placing a handler that checks the event target on the group will keep you from registering the event handler twice, but suffers from the same basic problem: when elements overlap, whichever element is declared last in the source order will get the event.
score:0
all above answers are right but i wanted to give another example:
let log = console.log
let data = []
let pointstart = document.queryselector("svg").createsvgpoint()
let pointstop = document.queryselector("svg").createsvgpoint()
let divlog = d3.select("#log")
var svg = d3.select("svg")
var linearfn = d3.line()
.x(d => d.x)
.y(d => d.y)
.curve(d3.curvelinear)
function logtagname(eventname, tagname) {
divlog.html(divlog.html() + eventname + " : " + tagname + "<br/>")
}
svg.on("click", function() {
log("tagname: ", event.target.tagname)
logtagname("svg click", event.target.tagname)
pointstart.x = event.x - 8
pointstart.y = event.y - 8
data.push({
x: pointstart.x,
y: pointstart.y
})
svg.selectall("path") // svg içinde tanımlı path elemanlarını bul
.data([1]).enter() // 1 elemanlı dizinin eleman sayısı kadarı için enter()
.append('path') // dizi elemanı kadar path oluştur
.attr("fill", "none")
.attr("stroke", "black")
.attr("stroke-width", 8)
.attr("d", linearfn(data))
.on("click", function() {
log("tagname: ", event.target.tagname)
logtagname("path click", event.target.tagname)
/* click event will start from path and pass to the svg element */
// event.stoppropagation(); // letting pass event bubbling
})
svg.selectall("circle")
.data(data)
.enter()
.append("circle")
.attr("cx", d => d.x + .5)
.attr("cy", d => d.y + .5)
.attr("r", 20)
.attr("stroke-width", 3)
.attr("stroke", "red")
.attr("cursor", "move")
.style("fill", "transparent")
.attr("pointer-events", "all") // when clicked in/outside of circle, it'll handle events
.on("mouseover", function() {
log("over oldu")
d3.select(this).style("stroke", "blue");
})
.on("mouseout", function() {
log("out oldu")
d3.select(this).style("stroke", "red");
})
.on("click", function() {
event.stoppropagation(); // not letting pass event bubbling
event.preventdefault();
log("click oldu")
d3.select(this).style("stroke", "black");
})
})
.on("mousemove", function() {
// fare hareketinde de çizdireceğiz ama x,y noktasını
// tıklanıncaya kadar diziye eklemeyeceğiz
pointstop.x = event.x - 8
pointstop.y = event.y - 8
svg.select("path")
.attr("d", linearfn(data.concat({
x: pointstop.x,
y: pointstop.y
})))
})
score:16
you can also use the following style, to "hide" certain svg elements for mouse events. in my case, it was the mouseover
event, that i wanted to bubble through:
pointer-events: none;
Source: stackoverflow.com
Related Query
- How do you make an SVG element mouse event bubble up through another element?
- How do you append a svg image to a rect element of another svg image?
- How to get reference to SVG (child) elements nested in another SVG element (parent) using D3?
- How can I pass attributes of an SVG element to a new event in D3?
- How to assign click event to every svg element in d3js
- how to add dragend event on svg element
- D3: Detect mouse wheel event through overlaid SVG
- how to detect mousemove if svg is behind another element
- How do you update an existing svg element with the d3v5 join syntax
- How to pass an event from one element to another in d3?
- How to link from path elements in one svg to a different element of another svg
- d3: unable to send mouse events to another svg element (event.target.__data__ is null)
- Process and send mouse event beneath the SVG element
- How do you add and remove an information template by clicking on an SVG element in D3.js?
- d3.js mouseover event not triggered when mouse is touching another element too
- How to access the DOM element that correlates to a D3 SVG object?
- How can I make double click event on node in d3.js?
- How do I get the width of an svg element using d3js?
- SVG element loses event handlers if moved around the DOM
- how to assign unique id to SVG text element in d3 javascript
- mouseout/mouseleave gets fired when mouse moves INSIDE the svg path element
- How to simulate mouse move in D3 so when you drag nodes, other nodes move automatically?
- programmatically trigger click event in svg rect element
- D3 bubble chart / pack layout - How to make bubbles radiate out from the largest bubbles to the smallest?
- How to determine nearby SVG elements on a mouse event?
- How to show and hides nodes when you move the mouse over a node in D3 Javascript
- how to access data of a d3 SVG Element
- how to get svg element type
- Using Javascript D3 library, how can I determine mouse position in data set of an area element on mousemove event?
- How to throttle function call on mouse event with D3.js
More Query from same tag
- D3 Sunburst arc sizes
- How to remove black area below/above metricsgraphs.js line graph?
- Adding new nodes to Force-directed layout
- How to create a bar chart with C3 that doesn't group all columns in the same set
- I cannot select the ("text") in my svg when I want to change the values
- D3: Can I get better performance when drawing and redrawing a series of rects?
- Path in SVG placed in front of the circles in D3 chart, despite the order of append
- Custom Wicket (Ajax) Control with complex data model
- Preventing overlapping horizontal bars in a d3.js timeline
- d3 v4 Issue with tree.nodeSize . Root is set to 0,0, even after translate being applied
- Troubleshooting post topojson installation
- threejs render text on or next to object
- D3.js: create a fixed number of elements
- How do I minimize mercator projection using D3
- How to update the innerRadius and the outterRadius of a donut chart in d3?
- d3js how to select the element i'm currently working on
- d3: zoom on double y scale broken
- d3 text label format based on value
- D3: Can't select a subset of my dataset
- How to make a mouseover interactive line graph with multiple data series and 2 y axes?
- C3 xFormat is not working for timestamp in seconds
- Ignoring (but still working with) Non-conforming Lines in CSV for Updating Data
- D3 force directed graph, different shape according to data and value given?
- Nested selection with Update
- d3 monthly data set - update data- add new group
- Mousemove function on a d3.js topojson county/state map - Hold click to select multiple counties
- Filtering data for grouped bar chart
- How to create an array of nested objects from a csv file in D3.js?
- D3.js line chart with dates on x axes
- Defining CSS style for tooltip in d3.js body