2013-03-22 83 views
0

这可能是一个非常简单的问题(但我是D3新手,试图通过一些示例来更好地理解它的工作原理)。我试图修改D3的一个基本示例(http://bl.ocks.org/mbostock/1667367)。我基本上保持了一切......我只是试图用我自己的数据使用一个不同的csv文件(与S & P 500股票数据)。在示例文件中,csv文件具有日期(月份年)和股票价格。在我的数据中,我有一个UTC时间戳和一个亮度值(在0-1000之间)。下面是CSV的一个小例子:D3解析错误

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 

时,我跑我的代码,我得到在控制台窗口说我有一个解析错误(不知道这是否是在解析数据或轻陷入了一个错误价值)...有没有人看到我如何设置CSV文件(或我可能会解析它不正确)的问题?这是我正在使用的D3代码。

<!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> 

回答

0

你可能得到的是错误的,因为你的时间格式规范 - 没有%L占位符(见the documentation)。这应该工作。

var parseDate = d3.time.format("%Y-%m-%dT%H:%M:%S.000Z").parse; 
+0

感谢您的快速答复。我尝试修改格式声明,但我仍然收到解析错误。从csv文件获取数据后,您会注意到,我将数据打印到控制台。当我看到这些数据时,它会打印出光:“554.22” date:Tue Jan 01 2013 09:00:00 GMT-0500(Eastern Standard Time)...但是,日期对象旁边有一个小箭头(你可以扩展),当我这样做,它说__proto__:无效的日期。有什么想法吗? – andyopayne 2013-03-22 22:33:13

+0

好的,你必须在这里帮助我一下 - 它从哪里得到解析错误? – 2013-03-22 22:36:45

+0

这很难说。错误信息非常长(如50行)。它基本上说:错误:解析问题...然后它有一大堆数字和NAN的东西,然后在错误消息的底部它指向d3.v3.min.js:1这有帮助吗? – andyopayne 2013-03-22 22:41:33

1

在csv文件的标题中,“light”标题前面有一个额外的空格。这导致了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 
}); 

嗯,也许我会提交一个补丁,以D3来解决这个问题...