londonfed
londonfed

Reputation: 1220

Two lined D3.js line chart, with one line starting at different position

I am creating a Line Chart in D3.js with two lines. The second line needs to start (and be rendered) and at a later date than the first. The data I am using for the graph looks like this:

var data = [{
    date: "1-May-12",
    close: "58.13",
    open: "58.13"
}, {
    date: "30-Apr-12",
    close: "53.98",
    open: "53.98"
}, {
    date: "27-Apr-12",
    close: "67.00",
    open: "67.00"
}, {
    date: "26-Apr-12",
    close: 0,
    open: "89.70"
}, {
    date: "25-Apr-12",
    close: 0,
    open: "99.00"
}];

In my current example, both lines are plotted but the second line (close) is plotted at the bottom of the graph and then jumps to the third value. I need the line to start when there is data. I have tried changing the value from 0 to null without any success. How do I change the graph so the second line only starts when there is actual data?

See this JSFiddle for the example: https://jsfiddle.net/londonfed/hprd452q/4/

Thanks in advance.

Upvotes: 4

Views: 1632

Answers (2)

Mikhail Shabrikov
Mikhail Shabrikov

Reputation: 8509

I can suggest you create two datasets - first for open line and second for close line;

var dataForClose = data.reduce(function(store, d) {
    if (d.close) {
    store.push(d);
  }

  return store;
}, []);


var dataForOpen = data.reduce(function(store, d) {
    if (d.open) {
    store.push(d);
  }

  return store;
}, []);

// Add the 2nd valueline path.
svg.append("path")
    .attr("class", "line")
    .style("stroke", "red")
    .attr("d", valueline2(dataForOpen)); // <--!!


// Add the valueline path.
svg.append("path")
    .attr("class", "line")
    .attr("d", valueline(dataForClose)); // <--!!

Open hidden snippet below to see how it looks:

// Set the dimensions of the canvas / graph
var margin = {top: 30, right: 20, bottom: 30, left: 50},
    width = 600 - margin.left - margin.right,
    height = 270 - margin.top - margin.bottom;

// Parse the date / time
var parseDate = d3.time.format("%d-%b-%y").parse;

// Set the ranges
var x = d3.time.scale().range([0, width]);
var y = d3.scale.linear().range([height, 0]);

// Define the axes
var xAxis = d3.svg.axis().scale(x)
    .orient("bottom").ticks(5);

var yAxis = d3.svg.axis().scale(y)
    .orient("left").ticks(5);

// Define the line
var valueline = d3.svg.line()
    .x(function(d) { return x(d.date); })
    .y(function(d) { return y(d.close); });

// Define the 2nd line
var valueline2 = d3.svg.line()
    .x(function(d) { return x(d.date); })
    .y(function(d) { return y(d.open); });

// Adds the svg canvas
var 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 + ")");

    // Get the data
    var data = [{
        date: "1-May-12",
        close: "20.13",
        open: "58.13"
    }, {
        date: "30-Apr-12",
        close: "58.98",
        open: "53.98"
    }, {
        date: "27-Apr-12",
        close: "68.00",
        open: "67.00"
    }, {
        date: "26-Apr-12",
        close: 0,
        open: "89.70"
    }, {
        date: "25-Apr-12",
        close: 0,
        open: "99.00"
    }];

data.forEach(function(d) {
  d.date = parseDate(d.date);
  d.close = +d.close;
  d.open = +d.open;
});
    
var dataForClose = data.reduce(function(store, d) {
	if (d.close) {
  	store.push(d);
  }
  
  return store;
}, []);


var dataForOpen = data.reduce(function(store, d) {
	if (d.open) {
  	store.push(d);
  }
  
  return store;
}, []);

    // Scale the range of the data
    x.domain(d3.extent(data, function(d) { return d.date; }));
    y.domain([0, d3.max(data, function(d) { return Math.max(d.close, d.open); })]);
    
    // Add the 2nd valueline path.
    svg.append("path")
        .attr("class", "line")
        .style("stroke", "red")
        .attr("d", valueline2(dataForOpen));


    // Add the valueline path.
    svg.append("path")
        .attr("class", "line")
        .attr("d", valueline(dataForClose));


    // Add the X Axis
    svg.append("g")
        .attr("class", "x axis")
        .attr("transform", "translate(0," + height + ")")
        .call(xAxis);

    // Add the Y Axis
    svg.append("g")
        .attr("class", "y axis")
        .call(yAxis);
body { font: 12px Arial;}

path {
    stroke: steelblue;
    stroke-width: 2;
    fill: none;
}

.axis path,
.axis line {
    fill: none;
    stroke: grey;
    stroke-width: 1;
    shape-rendering: crispEdges;
}
<script src="https://d3js.org/d3.v3.min.js"></script>
<body></body>

Upvotes: 1

Mark
Mark

Reputation: 92440

You can define where the line is and isn't defined using defined(). For example to set the line as undefined when it is 0 (or less) you can use:

var valueline = d3.svg.line()
   .defined(function(d) {return d.close > 0})
   .x(function(d) { return x(d.date); })
   .y(function(d) { return y(d.close); });

This will cause the line not to draw anywhere d.close is <= 0.

Dos for defined() here: https://github.com/d3/d3-shape/blob/master/README.md#line_defined

Here's an updated Fiddle: https://jsfiddle.net/4hkLqn84/

Upvotes: 7

Related Questions