简体   繁体   English

使用d3从csv文件导入后,数据未定义

[英]data is undefined after importing from a csv file with d3

I drew a world map with d3 and a json file, and are trying to draw circles on the map based on my data. 我用d3和json文件绘制了一张世界地图,并试图根据我的数据在地图上绘制圆圈。

I imported my data with the d3.csv() function, as you can see below. 我使用d3.csv()函数导入了数据,如下所示。 However, data doesn't seem to be recognized for my function function plot_points(data) . 然而, data似乎并没有得到认可,我的函数function plot_points(data) When I type data in the console in the function, it keeps telling me data is undefined . 当我在功能的控制台中输入data时,它会不断告诉我data is undefined It's interesting because I used the exact same code for another project before, and data would be recognized as an array of object. 这很有趣,因为我之前在另一个项目中使用了完全相同的代码,并且data将被识别为对象数组。 Can you tell what's wrong? 你能说出什么问题吗?

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

From the d3.csv documentation, the signature for the callback is as follows: 从d3.csv文档中,回调的签名如下:

function(error, rows) {

});

try adding an error parameter to your plot_points function: 尝试将error参数添加到您的plot_points函数:

function plot_points(error,data) {

};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM