CSV不返回行 [英] CSV not returning rows

查看:137
本文介绍了CSV不返回行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

整个上午都被困住了.我一直在关注示例

been stuck on this all morning. I've been following this example

我一直在尝试让它在React中工作,但不幸的是没有运气.我以前能够读取csv并在控制台中记录数据,因此我知道它正在查找正确的文件.我在这里想念什么?

I've been trying to get this to work in React but sadly no luck. I was able to read the csv in before and console log the data so I know it's finding the right file. What am I missing here?

class Linegraph extends Component {
    componentDidMount() {
    var parseDate = d3.timeParse("%m/%d/%Y");

    var margin = { left: 50, right: 20, top: 20, bottom: 50 };

    var width = 960 - margin.left - margin.right;
    var height = 500 - margin.top - margin.bottom;

    var max = 0;

    var xNudge = 50;
    var yNudge = 20;

    var minDate = new Date();
    var maxDate = new Date();

    d3.csv("MOCK_DATA.csv")
      .row(function(d) {
        return {
          month: parseDate(d.month),
          price: Number(d.price.trim().slice(1))
        };
      })
      .get(function(error, rows) {
        max = d3.max(rows, function(d) {
          return d.price;
        });
        minDate = d3.min(rows, function(d) {
          return d.month;
        });
        maxDate = d3.max(rows, function(d) {
          return d.month;
        });
    .................

推荐答案

(注意:此问题不是关于新d3.fetch模块的现有问题的重复,因为它使用行函数,而不是涵盖在那些Q/A上)

由于使用的是D3 v5,因此必须将v4的XmlHttpRequest模式更改为v5的新Promises模式(请参见

Since you are using D3 v5, you have to change the XmlHttpRequest pattern of v4 to the new Promises pattern of v5 (see the documentation here).

因此,您的代码应为:

d3.csv("MOCK_DATA.csv", function(d) {
        return {
            month: parseDate(d.month),
            price: Number(d.price.trim().slice(1))
        };
    })
    .then(function(rows) {
    //etc...
    });

请注意以下事实,即行函数位于第二个参数d3.csv内.

Pay attention to the fact that the row function goes inside the d3.csv function, as the second argument.

这篇关于CSV不返回行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆