When I click on a rectangle, ellipses are created. On drag I want to do something. The problem is, I need the data (d) of the rectangle, on which I clicked, also in my drag functions, connectStart, connectDrag and connectEnd. At the last 3 lines I tried to add data to my Ellipse, to access d from these 3 functions, but in the dragfunctions d is always the first value of my self.data and not the d of the rect I clicked on. So its not working. When I completely cut the last 3 lines out, I get 'd not defined' in my drag functions. How can I do this?
drawLine = d3.drag()
.on('start', connectStart)
.on('drag', connectDrag)
.on('end', connectEnd);
...
rects = itemRects.selectAll('rect')
.data(self.data)
.attr('x', function (d) { return newXscale(d.start); })
.attr('y', function (d) {
return yScale(d.lane) + ((getMarginHeight() / (self.lanesLength || 1)) * 0.2);
})
.attr('width', function (d) { return (newXscale(d.end) - newXscale(d.start)); })
.attr('height', itemHeight)
.attr('rx', 10)
.attr('ry', 10)
.attr('class', function (d) { return d.class + ' main'; })
.attr('id', function (d) { return 'rect_' + d.id; })
.attr('opacity', 0.5)
.attr('fill', '#0091dc')
.call(drag)
.on('click', function (i, d) {
console.log('PARENTNODE', this.parentNode);
console.log('Node', this);
d3.selectAll('g > ellipse').remove();
d3.select(this)
.attr('class', function (d) { return d.class + ' task-selected'; })
.attr('stroke-width', '3')
.attr('stroke', '#000088');
d3.select(this.parentNode)
.append('ellipse')
.attr('cx', newXscale(d.end) + 1)
.attr('cy', yScale(d.lane + d.lane + 1) / 2)
.attr('rx', 3)
.attr('ry', 6)
.attr('stroke-width', '2')
.attr('stroke', '#0091dc')
.attr('fill', '#0091dc')
.attr('class', 'dependencieStart')
.attr('opacity', '1');
d3.selectAll('ellipse.dependencieStart')
.data(self.data)
.call(drawLine);
...