【发布时间】:2016-06-12 02:59:56
【问题描述】:
文件“LoanStats3a.csv”位于同一文件夹中。然而不知何故,这不起作用。此外,由于数量太大,我将 x 和 y 值放在行中,但是,这应该为它们分配正常值。
<html>
<title>Loans</title>
<script src="http://d3js.org/d3.v3.js" charset="utf-8"></script>
<style>
#LoanStats3a {
color: blueviolet;
}
</style>
<body>
<script>
d3.csv("LoanStats3a.csv", function (file1) {
var bg = d3.select("body").append("svg")
.attr("width", 5000)
.attr("height", 5000);
var data1 = [{
x: d.loan_amnt,
y: d.term
},
{
x: d.int_rate,
y: d.annual_inc
},
{
x: d.member_id,
y: d.id
}
];
var group = bg.append("g")
.attr("transform", "translate (100,100)");
var line = d3.svg.line("line")
.x(function (d) {
return d.x / 100;
})
.y(function (d) {
return d.y / 100;
});
bg.selectAll("path")
.data(file1)
.enter()
.append("line")
.attr("cx", 500)
.attr("cy", 500)
.attr("d", line)
.attr("fill", "black")
.attr("stroke", "black")
.attr("stroke-width", 20);
});
</script>
</body>
</html>
此外,这是我使用本地数据时的代码,但仍然没有显示:
<html>
<title>Loans</title>
<script src="http://d3js.org/d3.v3.js" charset="utf-8"></script>
<style>
#LoanStats3a {
color: blueviolet;
}
</style>
<body>
<script>
d3.csv("LoanStats3a.csv", function (file1) {
var bg = d3.select("body").append("svg")
.attr("width", 5000)
.attr("height", 5000);
var data1 = [
//{x:d.loan_amnt , y : d.term},
//{x:d.int_rate , y : d.annual_inc},
//{x:d.member_id , y : d.id}
{
x: 10,
y: 20
},
{
x: 30,
y: 40
},
{
x: 50,
y: 60
}
];
var group = bg.append("g")
.attr("transform", "translate (100,100)");
var line = d3.svg.line()
.x(function (d) {
return d.x / 100;
})
.y(function (d) {
return d.y / 100;
});
bg.selectAll("path")
.data([data1])
.enter()
.append("line")
.attr("cx", 500)
.attr("cy", 500)
.attr("d", line)
.attr("fill", "black")
.attr("stroke", "black")
.attr("stroke-width", 20);
});
</script>
</body>
</html>
【问题讨论】:
-
/ {x:d.loan_amnt , y : d.term}, ..forward slash 似乎导致语法错误
-
修复了同样的问题。
-
你能提供loanStat3a.csv的样本数据来测试它失败的地方吗
-
试试 .attr("d", line(data1))
-
请尝试使用硬编码数据代替 csv 数据进行测试,看看是读取文件数据问题还是脚本问题
标签: javascript ajax csv d3.js