我正在尝试根据.csv文件中的数据创建折线图。我使用了一个我在网上找到的例子,但是我收到了一个错误:"错误:属性d:预期的数字,"我不确定这意味着什么或我必须在我的代码中修复。
这是我的代码:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Arduino Server</title>
<link rel="shortcut icon" href="favicon.ico">
<link rel="stylesheet" type="text/css" href="style.css">
<script type="text/javascript" src="d3.min.js"></script>
<script type="text/JavaScript" src="jquery-3.2.1.min.js"></script>
</head>
<body>
<!--div that will contain everything-->
<div class ="container">
<div>
<h1 style="text-align: center; color:red;">My Arduino
Server</h1>
</div>
<!--div that will contain the Temperature Link-->
<div >
<h1 style="text-align: center; color: #ff6961">Temperature Over
Some Time</h1>
<script>
// set the dimensions and margins of the graph
var margin = {top: 20, right: 20, bottom: 30, left: 50},
width = 960 - margin.left - margin.right,
height = 500 - margin.top - margin.bottom;
// parse the date / time
var parseTime = d3.timeParse("%d-%b-%y");
// set the ranges
var x = d3.scaleTime().range([0, width]);
var y = d3.scaleLinear().range([height, 0]);
// define the line
var line = d3.line()
.x(function(d) { return x(d.date); })
.y(function(d) { return y(d.temp); });
// append the svg obgect to the body of the page
// appends a 'group' element to 'svg'
// moves the 'group' element to the top left margin
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
d3.csv("p.csv", function(error, data) {
if (error) throw error;
// format the data
data.forEach(function(d) {
d.date = parseTime(d.date);
d.temp = +d.temp;
});
// 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 d.temp; })]);
// Add the X Axis
svg.append("g")
.attr("transform", "translate(0," + height + ")")
.call(d3.axisBottom(x));
// Add the Y Axis
svg.append("g")
.call(d3.axisLeft(y));
// Add the valueline path.
svg.append("path")
.data([data])
.attr("class", "line")
.attr("d", line);
});
</script>
</div>
</div>
</body>
</html>
以下是我尝试绘制的数据示例:
date,temp
1-Apr-17,70
2-Apr-17,80
3-Apr-17,75
4-Apr-17,64
5-Apr-17,54
6-Apr-17,67
7-Apr-17,89
8-Apr-17,87
9-Apr-17,25
10-Apr-17,56
如何更好地理解错误?