Reputation: 1368
This is likely a very easy question (but I'm new to D3 and trying to go through some of the examples to get a better understanding of how it works). I'm trying to modify one of the base examples for D3 (http://bl.ocks.org/mbostock/1667367). I've basically kept everything the same... I'm just trying to use a different csv file with my own data (vs. the S&P 500 stock data). In the example file, the csv file has a date (month year) and a stock price. In my data, I have a UTC time stamp and a light value (between 0-1000). Here's a small example of the csv:
date, light
2013-01-01T09:00:00.000Z,554.22
2013-01-01T09:01:00.000Z,480.83
2013-01-01T09:02:00.000Z,433.19
2013-01-01T09:03:00.000Z,596.89
2013-01-01T09:04:00.000Z,421.78
2013-01-01T09:05:00.000Z,461.23
2013-01-01T09:06:00.000Z,560.04
When, I run my code I get an error in the console window saying I have a parsing error (not sure if it's getting caught up in parsing the data or the light value)... Does anyone see a problem with how I'm setting up the csv file (or how I might be parsing it incorrectly)? Here's the D3 code I'm working with.
<!DOCTYPE html>
<meta charset="utf-8">
<style>
svg {
font: 10px sans-serif;
}
path {
fill: steelblue;
}
.axis path,
.axis line {
fill: none;
stroke: #000;
shape-rendering: crispEdges;
}
.brush .extent {
stroke: #fff;
fill-opacity: .125;
shape-rendering: crispEdges;
}
</style>
<body>
<script src="http://d3js.org/d3.v3.min.js"></script>
<script>
var margin = {top: 10, right: 10, bottom: 100, left: 40},
margin2 = {top: 430, right: 10, bottom: 20, left: 40},
width = 960 - margin.left - margin.right,
height = 500 - margin.top - margin.bottom,
height2 = 500 - margin2.top - margin2.bottom;
var parseDate = d3.time.format("%Y-%m-%dT%H:%M:%S.%LZ").parse;
var x = d3.time.scale().range([0, width]),
x2 = d3.time.scale().range([0, width]),
y = d3.scale.linear().range([height, 0]),
y2 = d3.scale.linear().range([height2, 0]);
var xAxis = d3.svg.axis().scale(x).orient("bottom"),
xAxis2 = d3.svg.axis().scale(x2).orient("bottom"),
yAxis = d3.svg.axis().scale(y).orient("left");
var brush = d3.svg.brush()
.x(x2)
.on("brush", brush);
var area = d3.svg.area()
.interpolate("monotone")
.x(function(d) { return x(d.date); })
.y0(height)
.y1(function(d) { return y(d.light); });
var area2 = d3.svg.area()
.interpolate("monotone")
.x(function(d) { return x2(d.date); })
.y0(height2)
.y1(function(d) { return y2(d.light); });
var svg = d3.select("body").append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom);
svg.append("defs").append("clipPath")
.attr("id", "clip")
.append("rect")
.attr("width", width)
.attr("height", height);
var focus = svg.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
var context = svg.append("g")
.attr("transform", "translate(" + margin2.left + "," + margin2.top + ")");
d3.csv("Light.csv", function(error, data) {
console.log(data);
data.forEach(function(d) {
d.date = parseDate(d.date);
//d.light = +d.light;
//console.log(d);
});
x.domain(d3.extent(data.map(function(d) { return d.date; })));
y.domain([0, d3.max(data.map(function(d) { return d.light; }))]);
x2.domain(x.domain());
y2.domain(y.domain());
focus.append("path")
.datum(data)
.attr("clip-path", "url(#clip)")
.attr("d", area);
focus.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis);
focus.append("g")
.attr("class", "y axis")
.call(yAxis);
context.append("path")
.datum(data)
.attr("d", area2);
context.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height2 + ")")
.call(xAxis2);
context.append("g")
.attr("class", "x brush")
.call(brush)
.selectAll("rect")
.attr("y", -6)
.attr("height", height2 + 7);
});
function brush() {
x.domain(brush.empty() ? x2.domain() : brush.extent());
focus.select("path").attr("d", area);
focus.select(".x.axis").call(xAxis);
}
</script>
Upvotes: 0
Views: 3658
Reputation: 1959
In the header of your csv file, the "light" header has an extra space in front of it. That leads to processing problems with d3.csv.
data.forEach(function(d) {
d.date = parseDate(d.date);
d.light = +d.light; // won't be able to access the light column data with the space
d.light = d[' light']; // this would work if you can't fix the header at the csv source
});
Hmmm, maybe I'll submit a patch to d3 to fix this...
Upvotes: 1
Reputation: 109232
You are probably getting that error because of your time format specification -- there is no %L
placeholder (see the documentation). This should work.
var parseDate = d3.time.format("%Y-%m-%dT%H:%M:%S.000Z").parse;
Upvotes: 0