javascript - D3 Chart: Adding a line to a date chart, without a date value -


i want add goal line across chart, goal not have date value, should range 1st date last.

http://jsfiddle.net/90fu4dsj/ - uncomment goal function.

    var data = [         {date: "2-may-15", close: 50},         {date: "1-may-15", close: 50},         {date: "30-apr-15", close: 50},         {date: "27-apr-15", close: 60},         {date: "26-apr-15", close: 40},         {date: "25-apr-15", close: 35},         {date: "24-apr-15", close: 40},         {date: "23-apr-15", close: 30},         {date: "20-apr-15", close: 20},         {date: "19-apr-15", close: 15},         {date: "15-apr-15", close: 10}     ],      // add line across chart     goal = [         {close: 53},     ],      margin = {top: 20, right: 50, bottom: 30, left: 50},     width = 500 - margin.left - margin.right,     height = 350 - margin.top - margin.bottom,      parsedate = d3.time.format("%d-%b-%y").parse,     bisectdate = d3.bisector(function(d) { return d.date; }).left,     formatvalue = d3.format(",.2f"),      x = d3.scale.linear()         .range([0, width]),      y = d3.time.scale()         .range([height, 0]),      xaxis = d3.svg.axis()         .scale(x)         .orient("top"),      yaxis = d3.svg.axis()         .scale(y)         .orient("left")         .tickformat(d3.time.format("%e %b")),      line = d3.svg.line()         .y(function(d) { return y(d.date); })         .x(function(d) { return x(d.close); })         .interpolate('cardinal'),      svg = d3.select("body").append("svg")         .attr("width", width + margin.left + margin.right)         .attr("height", height + margin.top + margin.bottom)         .append("g")         .attr("transform", "translate(" + margin.left + "," + margin.top + ")");   enter(data); function enter(data) {      data.foreach(function(d) {         d.date = parsedate(d.date);         d.close = +d.close;     });      data.sort(function(a, b) {         return a.date - b.date;     });      y.domain([data[0].date, data[data.length - 1].date]);     x.domain(d3.extent(data, function(d) { return d.close; }));      svg.append("g")         .attr("class", "x axis")         .call(xaxis);      svg.append("g")         .attr("class", "y axis")         .call(yaxis);      svg.append("path")         .datum(data)         .attr("class", "line")         .attr("d", line);   }              goalline(goal); function goalline(data){     data.foreach(function(d) {         d.close = +d.close;     });      var olddata = d3.select('.line').datum();     data = olddata.concat(data);      data.sort(function(a, b) {         return a.date - b.date;     });      y.domain([data[0].date, data[data.length - 1].date]);     x.domain(d3.extent(data, function(d) { return d.close; }));      svg = d3.select("svg")         .attr("height", height + margin.top + margin.bottom + 1000);      svg.select(".x.axis") // change x axis         .call(xaxis);     svg.select(".y.axis") // change y axis         .call(yaxis);      svg.select('.line')         .datum(data)         .attr('d', line); } 

i append svg line using scales. y values, use border cases of y scale, so:

svg.append("line")             .attr("x1",x(53))             .attr("y1",y(data[0].date))             .attr("x2",x(53))             .attr("y2",y(data[data.length - 1].date))             .attr("stroke","blue"); 

i have tried in fiddle code, put code right after "svg.append("path")" code.


Comments

Popular posts from this blog

Fail to load namespace Spring Security http://www.springframework.org/security/tags -

sql - MySQL query optimization using coalesce -

unity3d - Unity local avoidance in user created world -