score:0
Had a very similar issue. Was being caused by me declaring the event listener twice EG:
.on('mousemove', function (event, d) {
// Mouse event stuffs
}
.on('mousemove', function (event, d) {
// Mouse event stuffs again (overwrites above declaration).
}
Fixed by only declaring one listener function per event instead of two.
D3 will not warn you about being this stupid. So make sure you only are declaring each mouse related event listener once.
score:0
I also faced the same problems. I had the following code
svg.append("rect")
.attr("fill", "none")
.attr("width", 20)
.attr("height", 20)
.on("mouseover", (event, data)=> { console.log(data)})
.on("mouseout", (event, data)=> { console.log(data)});
where fill attribute is set to none. Once I removed fill attribute or set it to white, events started working as expected. So my new code looks like
svg.append("rect")
.attr("fill", "white")
.attr("width", 20)
.attr("height", 20)
.on("mouseover", (event, data)=> { console.log(data)})
.on("mouseout", (event, data)=> { console.log(data)})
score:1
I also face the same problems.
The solution is to add the css property to parent element
pointer-events: stroke;
or
pointer-events: visibleStroke;
score:6
Apart from using a rect
element, it should have a CSS property set like this pointer-events: all;
for the events to be triggered.
score:14
One approach is to add a rectangle filling the whole surface as the first element to catch mouse events that are not caught by elements added later:
something.append('svg:rect')
.attr('width', width) // the whole width of g/svg
.attr('height', height) // the whole heigh of g/svg
.attr('fill', 'none')
.attr('pointer-events', 'all')
.on('mouseover', function() {
// do something
}
});
I believe the <g>
element (mainGraph in your example) is just a container, not an actual shape that can receive mouse events.
You might be able to add the mouse event listener on the svg element itself, but I don't think the <g>
will work.
Source: stackoverflow.com
Related Query
- d3.on("mouseover") event does not work with nested SVG elements
- d3 multiline update path and transition path does not work with nested data
- hovering does not work with all elements of the same class in d3.js
- document.getElementById() does not work with puppeteer finding and id on a <path> element on SVG
- Svg clip-path within rectangle does not work
- Why are programmatically inserted SVG <tspan> elements not drawn, except with d3.js?
- Elements appended to SVG with D3 are not appearing
- The mouseover event for D3.js does not work in Leaflet
- Data Join with Custom Key does not work as expected
- Displaying SVG elements with D3 and d3.slider: Uncaught TypeError: .exit is not a function
- "Property does not exist on type {}" error when using anonymous function with D3 SVG Symbol
- d3 svg zoom does not work when neighbouring element exists
- Constructed SVG does not work where HTML-drawn does
- order a capped chart with fake group does not work in dc.js
- Microsoft Edge does not render SVG style elements on ajax call to a gzipped SVG file
- d3 SVG Line elements not firing onclick event
- The nested in foreignobject svg does not appear
- Bootstrap popovers for SVG elements not working with jQuery 3.0.0
- Why does external css doesnt work on svg elements in IE using D3
- Displaying SVG elements with D3 and d3.slider: Uncaught ReferenceError: svg is not defined
- Why does not this chart d3.js work with require.js?
- pykcharts scatter example does not work with local file
- d3.pointer does not work with touch (d3.js, v7)
- I want to remove the commas from the X axis with d3, but tickFormat does not work and instead, throws an error
- How to Properly Parse a CSV File to Work With SVG Elements
- D3: selecting a <g> in an SVG does not work in Safari
- Appending nested svg elements with D3js data binding?
- Updating elements does not work
- Importing local json file using d3.json does not work
- d3.js - mouseover event not working properly on svg group
More Query from same tag
- Rendering a d3 chart in two places makes it render twice in the first area?
- D3js unable to show gradient area chart
- Rollup returns undefined or array with value when using map
- Multichart(NVD3) x-axis labels not displaying
- Angular-nvd3-directives adjust height of the chart according to the width of the chart
- drawing on an externally loaded svg graphic in D3
- Can it is possible to use click event in tag cloud of D3 and If yes how?
- Scatterplot graphs line correctly, but points along the line have flipped along y-axis
- d3js v7: add node to forceSimulation
- D3 select sibling, give it a class
- D3 tree layout add 'title' to leaf nodes only
- how to add directive to d3 library in angular
- d3.js create same svg object with different data
- Resize svg when window is resized in d3.js
- How to get element on mouse position within d3.xhr and async queue?
- dynamic tooltip for multiline in d3.js
- How can I call Servlet from d3.json()
- D3.js column chart inversing when adding text
- Parenting D3.js-Nodes for dragging
- Change elements of page with .on change event in an elegant way
- d3.js dendrogram : binding old and new properties to the svg element
- d3js multi-line graph to iterate multiple nested arrays with multiple nested objects
- Clip SVG text to width of rectangle in a D3 treemap
- How to fasten the d3 zoomable sunburst transitions?
- Creating a legend for a bubble chart using D3
- Transform DOM Elements into Vue.js Components
- String does not match expected pattern d3 select
- Performance issue using dc.js
- Efficiently calculating convex hulls in D3 force-directed graph
- d3 How to limit drag and drop area