*我是 d3.js 中的新蜜蜂,并尝试使用 json 文件绘制折线图,但我遇到了一个错误,请有人帮助我。我在哪里犯了错误。
实际上我尝试了不同的文件格式,如 .csv 和 .tsv,但仍然有同样的错误......
错误:未捕获类型错误:无法读取未定义的属性“长度”。
代码:
<!DOCTYPE html>
<meta charset="utf-8">
<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="http://code.jquery.com/jquery-1.8.2.min.js"></script>
<script src="http://d3js.org/d3.v2.js"></script>
<script>
var arrData = [
{
"Engine": "L0",
"date": "01-Aug-13",
"value": 37.92625
},
{
"Engine": "L0",
"date": "02-Aug-13",
"value": 39.19708333
},
{
"Engine": "L1",
"date": "01-Aug-13",
"value": 44.13875
},
{
"Engine": "L1",
"date": "02-Aug-13",
"value": 43.15
},
{
"Engine": "L2",
"date": "01-Aug-13",
"value": 34.765
}];
var margin = {top: 20, right: 20, bottom: 30, left: 50},
width = 960 - margin.left - margin.right,
height = 500 - margin.top - margin.bottom;
var parseDate = d3.time.format.utc("%d-%b-%y").parse;
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.value); });
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 + ")");
var data = arrData.map(function(d) {
return {
Engine:d[0],
date: parseDate(d[1]),
value: d[2]
};
});
x.domain(d3.extent(data, function(d) { return d.date; }));
y.domain(d3.extent(data, function(d) { return d.value; }));
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);
</script>
</body>