【问题标题】:d3.csv vs d3.json: Cannot read property 'length' of undefined using JSON datad3.csv vs d3.json:无法使用 JSON 数据读取未定义的属性“长度”
【发布时间】:2015-11-10 14:43:19
【问题描述】:

试图理解 mbostock 的例子 (here),我遇到了一个奇怪的问题:

使用 CSV 数据(加载有 d3.csv()),一切正常。使用d3.json() 时,我得到“无法使用 JSON 数据读取未定义的属性‘长度’” 我们说的是相同的数据,具有相同的数据格式,只是使用在线 convertcsv 进行转换,并且数据也通过了 JSONLint 验证。

(我已经阅读了类似的帖子及其关于使用 csv 和 json 中的对象在数组中的数据的相关答案,但是使用本机 d3.csv()d3.json() 函数,结果数据对象不应该相同吗?)

CSV 数据:

date,price
Jan 2000,1394.46
Feb 2000,1366.42
Mar 2000,1498.58
Apr 2000,1452.43
May 2000,1420.6
Jun 2000,1454.6
Jul 2000,1430.83
Aug 2000,1517.68
Sep 2000,1436.51
Oct 2000,1429.4
Nov 2000,1314.95
Dec 2000,1320.28

转换后的 JSON:

[
  {
    "date":"Jan 2000",
    "price":1394.46
  },
  {
    "date":"Feb 2000",
    "price":1366.42
  },
  {
    "date":"Mar 2000",
    "price":1498.58
  },
  {
    "date":"Apr 2000",
    "price":1452.43
  },
  {
    "date":"May 2000",
    "price":1420.6
  },
  {
    "date":"Jun 2000",
    "price":1454.6
  },
  {
    "date":"Jul 2000",
    "price":1430.83
  },
  {
    "date":"Aug 2000",
    "price":1517.68
  },
  {
    "date":"Sep 2000",
    "price":1436.51
  },
  {
    "date":"Oct 2000",
    "price":1429.4
  },
  {
    "date":"Nov 2000",
    "price":1314.95
  },
  {
    "date":"Dec 2000",
    "price":1320.28
  }
]

最后,代码是:

<!DOCTYPE html>
<meta charset="utf-8">
<style>

svg {
  font: 10px sans-serif;
}

.area {
  fill: steelblue;
  clip-path: url(#clip);
}

.axis path,
.axis line {
  fill: none;
  stroke: #000;
  shape-rendering: crispEdges;
}

.brush .extent {
  stroke: #fff;
  fill-opacity: .125;
  shape-rendering: crispEdges;
}

</style>
<body>
<script src="..\d3.v3.min.js"></script>
<script>

var margin = {top: 10, right: 10, bottom: 100, left: 40},
    margin2 = {top: 430, right: 10, bottom: 20, left: 40},
    width = 960 - margin.left - margin.right,
    height = 500 - margin.top - margin.bottom,
    height2 = 500 - margin2.top - margin2.bottom;

var parseDate = d3.time.format("%b %Y").parse;

var x = d3.time.scale().range([0, width]),
    x2 = d3.time.scale().range([0, width]),
    y = d3.scale.linear().range([height, 0]),
    y2 = d3.scale.linear().range([height2, 0]);

var xAxis = d3.svg.axis().scale(x).orient("bottom"),
    xAxis2 = d3.svg.axis().scale(x2).orient("bottom"),
    yAxis = d3.svg.axis().scale(y).orient("left");

var brush = d3.svg.brush()
    .x(x2)
    .on("brush", brushed);

var area = d3.svg.area()
    .interpolate("monotone")
    .x(function(d) { return x(d.date); })
    .y0(height)
    .y1(function(d) { return y(d.price); });

var area2 = d3.svg.area()
    .interpolate("monotone")
    .x(function(d) { return x2(d.date); })
    .y0(height2)
    .y1(function(d) { return y2(d.price); });

var svg = d3.select("body").append("svg")
    .attr("width", width + margin.left + margin.right)
    .attr("height", height + margin.top + margin.bottom);

svg.append("defs").append("clipPath")
    .attr("id", "clip")
  .append("rect")
    .attr("width", width)
    .attr("height", height);

var focus = svg.append("g")
    .attr("class", "focus")
    .attr("transform", "translate(" + margin.left + "," + margin.top + ")");

var context = svg.append("g")
    .attr("class", "context")
    .attr("transform", "translate(" + margin2.left + "," + margin2.top + ")");

d3.json("singlestock.json", type, function(error, data) {
//d3.csv("singlestock.csv", type, function(error, data) {
  x.domain(d3.extent(data.map(function(d) { return d.date; })));
  y.domain([price, d3.max(data.map(function(d) { return d.price; }))]);
  x2.domain(x.domain());
  y2.domain(y.domain());

  focus.append("path")
      .datum(data)
      .attr("class", "area")
      .attr("d", area);

  focus.append("g")
      .attr("class", "x axis")
      .attr("transform", "translate(0," + height + ")")
      .call(xAxis);

  focus.append("g")
      .attr("class", "y axis")
      .call(yAxis);

  context.append("path")
      .datum(data)
      .attr("class", "area")
      .attr("d", area2);

  context.append("g")
      .attr("class", "x axis")
      .attr("transform", "translate(0," + height2 + ")")
      .call(xAxis2);

  context.append("g")
      .attr("class", "x brush")
      .call(brush)
    .selectAll("rect")
      .attr("y", -6)
      .attr("height", height2 + 7);
});

function brushed() {
  x.domain(brush.empty() ? x2.domain() : brush.extent());
  focus.select(".area").attr("d", area);
  focus.select(".x.axis").call(xAxis);
}

function type(d) {
  d.date = parseDate(d.date);
  d.price = +d.price;
  return d;
}

</script>

(几乎 100% 来自 mbostock 的示例 here) 在代码中,只需取消注释唯一的注释行即可从 CSV 切换到 JSON。

-----编辑--- 根据 nikosh 的回答:问题是这样解决的:

d3.json("singlestock.json",  function(error, data) {
        data.forEach(function(d) {
        d.date = parseDate(d.date);
        d.price = +d.price;
        d.NAgg = +d.NAgg;
    });

【问题讨论】:

    标签: javascript json csv d3.js


    【解决方案1】:

    比较d3.json的签名

    d3.json(url[, callback])
    

    和你的实际通话

    d3.json("singlestock.json", type, function(error, data) {... });
    

    你忘了从你的论点中删除type,使用

    d3.json("singlestock.json", function(error, data) {... })
    

    【讨论】:

    • 实际上,这是有意为之,是我试图让它发挥作用的绝望举措之一。切换到: d3.json("singlestock.json", function(error, data) {[...] 实际上会导致这个错误:d="MNaN,4.059485530546625CNaN,4.059485530546625,NaN,4.983263929155024,NaN,4.983263929155024SNaN,0.6292499077539426,NaN,0.6292499077539426SNaN,2.1496626429813928,NaN,2.1496626429813928SNaN,3.1983026725001364,NaN,3.1983026725001364SNaN,2.078171946655427,NaN,2.078171946655427SNaN,2.8612751041062703,NaN,2.8612751041062703SNaN,0,NaN,0SNaN,2.674147382847508,NaN,2.674147382847508SNaN,2.90838648463444,NaN[...]
    • nikoshr 是对的,我又犯了一个错误:我不得不在数据循环中移动 TYPE 函数:d3.json("singlestock.json", function(error, data) { data.forEach (函数(d) { d.date = parseDate(d.date); d.price = +d.price; d.NAgg = +d.NAgg; });`
    猜你喜欢
    • 2016-03-10
    • 1970-01-01
    • 1970-01-01
    • 2016-11-27
    • 2016-06-30
    • 1970-01-01
    • 2019-04-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多