Lindsay Morsillo Lindsay Morsillo - 7 months ago 13
Javascript Question

D3 Line Graph Example - Having problems with dates

I have taken a D3 Line Graph example and changed it slightly to run using local data. I have commented out the data.tsv() and replaced it with a data.forEach() on the var data array, which should accomplish the same thing.

I am trying this example, because I have been having trouble with a line graph and dates. I end up with lot's of NaN in the line's, which then won't draw.

My current version simply and erroneously draws a vertical line at x=0 (over the axis). Here's the SVG path:

<path class="line"
d="M0,450
L0,307.85928143712596
L0,72.7544910179639
L0,0
L0,8.083832335329655
L0,30.314371257485206"></path>


I don't know what I am getting wrong. I simply want to graph the data, the 5 points in the data array. I realize I probably have a syntax error (that will cause a head slap when it's pointed out) but more to the point, I can tell I don't understand what D3 wants from time-based data. What type does D3 need for date data, what does time.format.parse() do compared to the javascript date parser? Why use one and not the other? When do I get NaN returns and 0's?

Here's my code:

<!DOCTYPE html>
<head>
<meta charset="utf-8" />
<title>LGTest</title>
</head>
<style>
body {
font: 10px sans-serif;
}

.axis path,
.axis line {
fill: none;
stroke: #000;
shape-rendering: crispEdges;
}

.x.axis path {
display: none;
}

.line {
fill: none;
stroke: steelblue;
stroke-width: 1.5px;
}
</style>
<body>
<script src="d3.min.js"></script>
<script>
var data = [
{ date: "24-Apr-07", close: 93.24 },
{ date: "25-Apr-07", close: 95.35 },
{ date: "26-Apr-07", close: 98.84 },
{ date: "27-Apr-07", close: 99.92 },
{ date: "30-Apr-07", close: 99.80 },
{ date: "1-May-07", close: 99.47 }
];
var margin = {top: 20, right: 20, bottom: 30, left: 50},
width = 960 - margin.left - margin.right,
height = 500 - margin.top - margin.bottom;

var formatDate = d3.time.format("%e-%b-%y");

data.forEach(function (d) { d = type(d) });


var x = d3.time.scale()
.range([0, width]);

var y = d3.scale.linear()
.range([height, 0]);

var xAxis = d3.svg.axis()
.scale(x)
.orient("bottom");

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

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

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 + ")");

//d3.tsv("data.tsv", type, function(error, data) {
// if (error) throw error;

x.domain(d3.extent(data, function(d) { return d.date; }));
y.domain(d3.extent(data, function(d) { return d.close; }));

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

svg.append("g")
.attr("class", "y axis")
.call(yAxis)
.append("text")
.attr("transform", "rotate(-90)")
.attr("y", 6)
.attr("dy", ".71em")
.style("text-anchor", "end")
.text("Price ($)");

svg.append("path")
.datum(data)
.attr("class", "line")
.attr("d", line);
//});

function type(d) {
d.date = formatDate.parse(d.date);
d.close = +d.close;
return d;
}

</script>
</body>


Thanks for any help, advice, remonstrations, etc.

Answer

I made it a fiddle. It seems to be working fine, what's the problem here?

I used these data:

var data = [
        { date: "24-Apr-07", close: 93.24 },
        { date: "25-Apr-07", close: 95.35 },
        { date: "26-Apr-07", close: 98.84 },
        { date: "27-Apr-07", close: 99.92 },
        { date: "30-Apr-07", close: 99.80 },
        { date: "1-May-07", close: 99.47 }
    ];

https://jsfiddle.net/xcn35ycm/5/

Comments