我用 d3 和 json 文件绘制了一张世界地图,并尝试根据我的数据在 map 上绘制圆圈。
我使用 d3.csv()
函数导入了数据,如下所示。但是,我的函数functionplot_points(data)
似乎无法识别data
。当我在函数的控制台中输入data
时,它不断告诉我data is undefined
。这很有趣,因为我之前在另一个项目中使用了完全相同的代码,并且 data
会被识别为对象数组。你能告诉我出了什么问题吗?
<head>
<script type="text/javascript">
function draw(geo_data) {
"use strict";
var margin = 75,
width = 1400 - margin,
height = 600 - margin;
d3.select("body")
.append("h2")
.text("Circle Graph");
var svg = d3.select("body")
.append("svg")
.attr("width", width + margin)
.attr("height", height + margin)
.append('g')
.attr('class', 'map');
var projection = d3.geo.mercator()
.scale(150)
.translate([width / 2, height / 1.2]);
var path = d3.geo.path().projection(projection);
var map = svg.selectAll('path')
.data(geo_data.features)
.enter()
.append('path')
.attr('d', path)
.style('fill', 'lightBlue')
.style('stroke', 'black')
.style('stroke-width', 0.5);
// debugger;
// here data is undefined
function plot_points(data) {
// debugger;
// here data is undefined as well
};
d3.csv("data/olympics_editions.csv", function(d) {
// debugger;
// here d is each data point/object, and seems to be working just fine
d['Year'] = format.parse(d['Year']);
d['Latitude'] = +d['Latitude'];
d['Longitude'] = +d['Longitude'];
return d;
}, plot_points);
};
</script>
</head>
<body>
<script type="text/javascript">
d3.json("data/world_countries.json", draw);
</script>
</body>
最佳答案
根据 d3.csv 文档,回调的签名如下:
function(error, rows) {
});
尝试向您的plot_points函数添加error
参数:
function plot_points(error,data) {
};
关于javascript - 使用 d3 从 csv 文件导入后数据未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30427623/