score:0
It seems to me that you just want the listener to fire when the user clicks on the stroke of the path, not on its fill (the confusion arises because you're using the term "line", which means another thing in a SVG).
If that's correct the solution is simple, you just need to set the pointer-events of those paths to visibleStroke
or stroke
:
.attr("pointer-events", "visibleStroke");
Here is a demo, you can click in the fill or outside the path and nothing happens, click on its stroke and the listener fires:
var svg = d3.select("svg");
var path = svg.append("path")
.attr("d", "M50,20 L250,20 L250,130 L50,130 Z")
.style("fill", "teal")
.style("stroke", "black")
.style("stroke-width", 4)
.attr("pointer-events", "visibleStroke")
.on("click", function() {
console.log("clicked")
});
<script src="https://d3js.org/d3.v4.min.js"></script>
<svg></svg>
Also, it's worth mentioning that something is wrong in your code: if you set the fill to none
, the default pointer-events
should not fire. Here is a demo:
var svg = d3.select("svg");
var path = svg.append("path")
.attr("d", "M50,20 L250,20 L250,130 L50,130 Z")
.style("fill", "none")
.style("stroke", "black")
.style("stroke-width", 4)
.on("click", function() {
console.log("clicked")
});
<script src="https://d3js.org/d3.v4.min.js"></script>
<svg></svg>
Source: stackoverflow.com
Related Query
- Remove Path Fills Completely in D3
- How to remove shapes from filled SVG path
- D3: "SVG4601: SVG Path data has incorrect format and could not be completely parsed."
- D3 remove 'zoom' completely
- d3 exit transition: how to flatten path and then remove
- Map created from geojson file just fills the entire canvas with the color of the path fill
- Remove path on double click d3
- Remove circle and path on click d3
- How can I remove or replace SVG content?
- How do I remove all children elements from a node and then apply them again with different color and size?
- How to remove an attribute in D3.js?
- Unclosed SVG path appears to be closed
- Remove end-ticks from D3.js axis
- How do you remove a handler using a d3.js selector
- How do I return y coordinate of a path in d3.js?
- D3 remove comma delimiters for thousands
- Calculate SVG Path Centroid with D3.js
- Convert SVG Path d attribute to a array of points
- How to select a d3 svg path with a particular ID
- internet explorer 10 not showing svg path (d3.js graph)
- How to update an svg path with d3.js
- How do you remove the background gridlines in nvd3.js?
- Find intersection of SVG path
- Pass null values to SVG path (using d3.js) to suppress missing data
- How to draw a path smoothly from start point to end point in D3.js
- D3.js: Remove force.drag from a selection
- Modifying SVG path opacity and it's marker
- Month Path only calculating month, not actual date range
- mouseout/mouseleave gets fired when mouse moves INSIDE the svg path element
- D3 "Sunburst" Center Path Size
More Query from same tag
- Why is hover on my d3 map not working properly?
- Use elements from a predefined list as variables on a JavaScript function
- D3 area graph transition from bottom
- JavaScript API documentation - what does functionName([value[, arguments]]) notation mean?
- D3 tree resize SVG in Angular
- Need pauses - timing not working on iterating through text strings w/ d3/svg (piling up on each other)
- NVD3 Scatter Chart Circle Radius
- SVG D3.js append element to SVG g:circle on text mouseover
- Multiline chart, can't get line data
- Processing nested json data into data joins
- Javascript - pass THIS in function doesnt work
- d3js angular directive: svg elements render incorrectly when applying filters via url
- Violin chart in D3
- Split CSV Column by pipe character and count unique values
- is there a way to convert svg <line> to svg <path> in illustrator?
- D3 v4 Get translate values of an element
- C3 timeseries chart giving error Invalid value for <path> attribute
- d3 bisector not working with array of objects
- Difference in chaining D3.js transitions
- Uncaught TypeError: Cannot read properties of undefined (reading 'document')
- d3.js - understand arrow function to append element to g
- StopPropagation() with SVG element and G
- D3.js sunburst partition rendering incorrectly
- How to send HTML to an embedded WebView without running a local webserver?
- How to obtain coordinate of d3 label text?
- Existing Triangles Cannot be Removed
- d3js force node xy start position
- Returning value for given key in js and d3 - do i have to loop?
- d3.js graphing sub arrays from JSON
- How to Deploy D3 in Heroku (Django App)?