I am trying to draw a series of vertical lines on a d3 chart. The data source for this is the list of x positions (in the data), which is displayed through the scale function x() to give the correct position.
This is currently implemented as a for loop for each entry in the labels variable. The origin and endpoint y are taken from minY and maxY . A text label is attached to the end of the line.
labels = [ { 'x':50, 'label':test' } var label = svg.append("g") .attr("class","ilabel") // Labels for (var i = 0; i < labels.length; i++) { labl = labels[i]; label.append('line') .attr('x1', x( labl.x ) ) .attr('y1', y( maxY ) ) .attr('x2', x( labl.x ) .attr('y2', y( minY ) ) .attr("class","ilabel-line"); label.append("text") .attr("transform", "translate(" + x( labl.x )+"," + y( maxY )+ ") rotate(-60)") .style("text-anchor", "start") .attr("dx", ".5em") .attr("class","ilabel-label") .text(labl.label); }
How would I rewrite this using data, that is, pass the labels in the form of .data , and then repeat this to draw a series of parallel vertical lines.
var label = svg.selectAll(".ilabel") .data(labels) .enter().append("g") .attr("class","ilabel")
The bit I came across is the functions x() and y() . x() should, of course, return the x position of the string (which is constant for the given string), but how / what should the y function return in order to draw a line between two defined points ( minY and maxY ).
line = d3.svg.line() .x(function(d) { return x(dx); }) .y(function(d) { return y(dy); }); label.append('path') .attr("d", line)
I feel like I'm missing something obvious.
mfitzp
source share