简体   繁体   English

d3 输入未放置数据

[英]d3 enter not placing data

Using d3, I am reading in 8760 data points (one for each hour of 2013) from a csv into a 2-d array and then trying to create them as circles in an <svg> element.使用 d3,我正在将 8760 个数据点(2013 年每小时一个)从 csv 读取到二维数组中,然后尝试将它们创建为<svg>元素中的圆圈。 The code successfully imports the data, as I can view it in the browser's console.代码成功导入了数据,因为我可以在浏览器的控制台中查看它。 However, the enter function does not seem to be applying the data to create the circles.但是, enter 函数似乎没有应用数据来创建圆圈。 No errors are raised.没有错误被提出。

Code:代码:

  <script src="https://d3js.org/d3.v4.min.js"></script>
  <script>
  var w = 900, h = 500;
  var dataset = [];

  d3.csv("Jsorted2.csv", function(error, data) {
    dataset = data.map(function(d) { return [ +d["Load"], new Date(d["dtDateTime"]), +d["Month"], +d["Hour"], +d["DayofYear"], +d["NetLoad"]]; });
  });

  var svg = d3.select("body")
    .append("svg")
    .attr("width", w)
    .attr("height", h);

  svg.selectAll("circle")
    .data( dataset )
    .enter()
    .append("circle")
    .attr("cx", function(d,i) { return i; })
    .attr("cy", function(d,i) { return d[0]; })
    .attr("r", function(d,i) { return d[5]; });

</script>

The data loads properly:数据正确加载:

Browser Inspector Console Screenshot浏览器检查器控制台屏幕截图

But the circles are missing:但圆圈不见了:

Browser Inspector Source Screenshot浏览器检查器源屏幕截图

I have found different errors if I change the initial definition of dataset.如果更改数据集的初始定义,我会发现不同的错误。 If I use如果我使用

var dataset = [[]];

the data still loads, but now I receive an error when d3 tries the grab the data:数据仍然加载,但现在当 d3 尝试抓取数据时我收到一个错误:

d3.v4.min.js:3 Error: <circle> attribute cy: Expected length, "NaN".
(anonymous function) 
@ d3.v4.min.js:3Q_ 
@ d3.v4.min.js:6K_ 
@ d3.v4.min.js:6(anonymous function) 
@ D3test4LD.html:29

And d3 gets a little further in creating the circles: d3 在创建圆圈方面更进一步:

<svg width="900" height="500">
<circle cx="0" cy="NaN"></circle>
</svg>

Why does d3 not load the data to circles?为什么 d3 不将数据加载到圆圈中? It appears as if d3 is not passing the data to d as it should, as d[0] has no length.似乎 d3 没有像它应该的那样将数据传递给 d,因为 d[0] 没有长度。 If I cannot get this to work, I'll just write a Javascript for loop to place them myself, but I'd like to figure out why this doesn't work.如果我不能让它工作,我只会写一个 Javascript for 循环来自己放置它们,但我想弄清楚为什么这不起作用。 It looks like a type conversion issue, but I import the data with a +, which is supposed to set the data as a number.它看起来像一个类型转换问题,但我用 + 导入数据,它应该将数据设置为数字。 I can even test that in the console:我什至可以在控制台中测试:

> typeof(dataset[0][0])
"number" 

Please do not include a js for loop as a solution.请不要包含 js for 循环作为解决方案。


UPDATE更新

So, I did solve my problem after researching asynchronous data loading, as per the answer below.所以,在研究异步数据加载后,我确实解决了我的问题,如下面的答案。 For clear and easy future reference, here is one way to code a solution:为了清晰和方便的未来参考,这里是一种编写解决方案的方法:

setInterval(drawLD, 2000);

function drawLD(){
    svg.selectAll("circle")
      .data( dataset )
      .enter()
      .append("circle")
      .attr("cx", function(d,i) { return Math.round(i/8760); })
      .attr("cy", function(d,i) { return Math.round(d[0]/maxload); })
      .attr("r", function(d,i) { return 2; });
  }

d3.csv is an asynchronous function. d3.csv是一个异步函数。 The code that creates the circles is executed before your csv data has returned.创建圆圈的代码在您的 csv 数据返回之前执行。 Place your d3 code inside the callback.将您的 d3 代码放在回调中。

<script>
  var w = 900, h = 500;
  var dataset = [];

  d3.csv("Jsorted2.csv", function(error, data) {
    dataset = data.map(function(d) { return [ +d["Load"], new Date(d["dtDateTime"]), +d["Month"], +d["Hour"], +d["DayofYear"], +d["NetLoad"]]; });

    var svg = d3.select("body")
    .append("svg")
    .attr("width", w)
    .attr("height", h);

  svg.selectAll("circle")
    .data( dataset )
    .enter()
    .append("circle")
    .attr("cx", function(d,i) { return i; })
    .attr("cy", function(d,i) { return d[0]; })
    .attr("r", function(d,i) { return d[5]; });

  });
</script>

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

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