The SVGPathElement
API has built-in methods for getting this info. You do not need to parse the data-string yourself.
Since you stored a selection for your line as a variable, you can easily access the path element's api using myLine.node()
to refer to the path
element itself.
For example:
var pathElement = myLine.node();
Then you can access the list of commands used to construct the path by accessing the pathSegList
property:
var pathSegList = pathElement.pathSegList;
Using the length
property of this object, you can easily loop through it to get the coordinates associated with each path segment:
for (var i = 0; i < pathSegList.length; i++) {
console.log(pathSegList[i]);
}
Inspecting the console output, you will find that each path segment has properties for x
and y
representing the endpoint of that segment. For bezier curves, arcs, and the like, the control points are also given as x1
, y1
, x2
, and y2
as necessary.
In your case, regardless of whether you use this method or choose to parse the string yourself, you will run into difficulties because you used interpolate('basis')
for your line interpolation. Therefore, the line generator outputs 6 commands (in your specific case) rather than 4, and their endpoints do not always correspond to the original points in the data. If you use interpolate('linear')
you will be able to reconstruct the original dataset, since the linear interpolation has a one-to-one correspondence with the path data output.
Assuming you used linear interpolation, reconstructing the original dataset could be done as follows:
var pathSegList = myLine.node().pathSegList;
var restoredDataset = [];
// loop through segments, adding each endpoint to the restored dataset
for (var i = 0; i < pathSegList.length; i++) {
restoredDataset.push({
"x": pathSegList[i].x,
"y": pathSegList[i].y
})
}
EDIT:
As far as using the original data when appending text... I'm assuming you are looking to append labels to the points, there's no need to go through all the trouble of reconstructing the data. In fact the real issue is that you never used data-binding in the first place to make your line graph. Try binding the data using the .datum()
method for your path, and using the .data()
method for the labels. Also you might want to rename lineEnter
since you're not using an enter selection and it simply represents a group. For example:
// THIS USED TO BE CALLED `lineEnter`
var lineGroup = svgContainer.append("g");
var myLine = lineGroup.append("path")
// HERE IS WHERE YOU BIND THE DATA FOR THE PATH
.datum(lineData)
// NOW YOU SIMPLY CALL `lineFunction` AND THE BOUND DATA IS USED AUTOMATICALLY
.attr("d", lineFunction)
.attr("stroke", "blue")
.attr("stroke-width", 2)
.attr("fill", "none");
// FOR THE LABELS, CREATE AN EMPTY SELECTION
var myLabels = lineGroup.selectAll('.label')
// FILTER THE LINE DATA SINCE YOU ONLY WANT THE SECOND POINT
.data(lineData.filter(function(d,i) {return i === 1;})
// APPEND A TEXT ELEMENT FOR EACH ELEMENT IN THE ENTER SELECTION
.enter().append('text')
// NOW YOU CAN USE THE DATA TO SET THE POSITION OF THE TEXT
.attr('x', function(d) {return d.x;})
.attr('y', function(d) {return d.y;})
// FINALLY, ADD THE TEXT ITSELF
.text('Yaprak')