d3.js:d3.min.js:1错误:< path>属性d:预期数目,“MNaN,NaNLNaN,NaN” [英] d3.js : d3.min.js:1 Error: <path> attribute d: Expected number, "MNaN,NaNLNaN,NaN"

查看:5164
本文介绍了d3.js:d3.min.js:1错误:< path>属性d:预期数目,“MNaN,NaNLNaN,NaN”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我导入了一个csv文件,并尝试映射d3上的信息。我想我已经把一切正常。

I imported a csv file and tried to map the info on d3. I guess I have scaled everything properly. Can anyone help me out and guide me through this?

我得到以下错误:

d3.min.js:1 Error: <path> attribute d: Expected number, "MNaN,NaNLNaN,NaNL…".

csv文件中的数据如下:

The data in the csv file is like this:

------------------------------------------
|       00:00:01      | 1                |
------------------------------------------
|       00:05:01      | 2                |
------------------------------------------
|       00:10:01      | 3                |
------------------------------------------
|       00:15:01      | 5                |
------------------------------------------

以下是代码。

var Chart = (function(window,d3) {

  var svg, data, x, y, xAxis, yAxis, dim, chartWrapper, line, path, margin = {}, width, height;

  d3.csv('Book1.csv', init); //load data, then initialize chart

  //called once the data is loaded
  function init(csv) {
    data = csv;

    //initialize scales
    xExtent = d3.extent(data, function(d,i) { return new Date(d.date) });
    yExtent = d3.extent(data, function(d,i) { return d.value });
    x = d3.time.scale().domain(xExtent);
    y = d3.scale.linear().domain(yExtent);

    //initialize axis
    xAxis = d3.svg.axis().orient('bottom');
    yAxis = d3.svg.axis().orient('left');

    //the path generator for the line chart
    line = d3.svg.line()
      .x(function(d) { return x(new Date(d.date)) })
      .y(function(d) { return y(d.value) });

    //initialize svg
    svg = d3.select('#chart').append('svg');
    chartWrapper = svg.append('g');
    path = chartWrapper.append('path').datum(data).classed('line', true);
    chartWrapper.append('g').classed('x axis', true);
    chartWrapper.append('g').classed('y axis', true);

    //render the chart
    render();
  }

  function render() {

    //get dimensions based on window size
    updateDimensions(window.innerWidth);

    //update x and y scales to new dimensions
    x.range([0, width]);
    y.range([height, 0]);

    //update svg elements to new dimensions
    svg
      .attr('width', width + margin.right + margin.left)
      .attr('height', height + margin.top + margin.bottom);
    chartWrapper.attr('transform', 'translate(' + margin.left + ',' + margin.top + ')');

    //update the axis and line
    xAxis.scale(x);
    yAxis.scale(y);

    svg.select('.x.axis')
      .attr('transform', 'translate(0,' + height + ')')
      .call(xAxis);

    svg.select('.y.axis')
      .call(yAxis);

    path.attr('d', line);
  }

  function updateDimensions(winWidth) {
    margin.top = 20;
    margin.right = 50;
    margin.left = 50;
    margin.bottom = 50;

    width = winWidth - margin.left - margin.right;
    height = 500 - margin.top - margin.bottom;
  }

  return {
    render : render
  }

})(window,d3);


推荐答案

根据文档解析CSV时产生的所有值都将是字符串:

According to the documentation all values resulting from parsing the CSV will be strings:


请注意,值本身总是字符串;它们不会自动转换为数字。

Note that the values themselves are always strings; they will not be automatically converted to numbers.

您必须指定一个访问器函数来处理转换

You will have to specify an accessor function which takes care of the conversion

d3.csv('Book1.csv', convert, init); //load data, convert, then initialize chart

function convert(d) {
  return {
    date: new Date(d.date),
    value: +d.value         // convert string to number
  };
} 

作为副作用,这也将简化您的代码,每次访问值时都必须进行转换。这是完整的代码:

As a side-effect this will also simplify your code because it frees you from having to do a conversion every time you access the values. Here is the full code:

var Chart = (function(window, d3) {

  var svg, data, x, y, xAxis, yAxis, dim, chartWrapper, line, path, margin = {},
    width, height;

  d3.csv('Book1.csv', convert, init); //load data, convert, then initialize chart

  function convert(d) {
    return {
      date: new Date(d.date),
      value: +d.value         // convert string to number
    };
  } 

  //called once the data is loaded
  function init(csv) {
    data = csv;

    //initialize scales
    xExtent = d3.extent(data, function(d, i) {
      return d.date;
    });
    yExtent = d3.extent(data, function(d, i) {
      return d.value;
    });
    x = d3.time.scale().domain(xExtent);
    y = d3.scale.linear().domain(yExtent);

    //initialize axis
    xAxis = d3.svg.axis().orient('bottom');
    yAxis = d3.svg.axis().orient('left');

    //the path generator for the line chart
    line = d3.svg.line()
      .x(function(d) {
        return x(d.date)
      })
      .y(function(d) {
        return y(d.value)
      });

    //initialize svg
    svg = d3.select('#chart').append('svg');
    chartWrapper = svg.append('g');
    path = chartWrapper.append('path').datum(data).classed('line', true);
    chartWrapper.append('g').classed('x axis', true);
    chartWrapper.append('g').classed('y axis', true);

    //render the chart
    render();
  }

  function render() {

    //get dimensions based on window size
    updateDimensions(window.innerWidth);

    //update x and y scales to new dimensions
    x.range([0, width]);
    y.range([height, 0]);

    //update svg elements to new dimensions
    svg
      .attr('width', width + margin.right + margin.left)
      .attr('height', height + margin.top + margin.bottom);
    chartWrapper.attr('transform', 'translate(' + margin.left + ',' + margin.top + ')');

    //update the axis and line
    xAxis.scale(x);
    yAxis.scale(y);

    svg.select('.x.axis')
      .attr('transform', 'translate(0,' + height + ')')
      .call(xAxis);

    svg.select('.y.axis')
      .call(yAxis);

    path.attr('d', line);
  }

  function updateDimensions(winWidth) {
    margin.top = 20;
    margin.right = 50;
    margin.left = 50;
    margin.bottom = 50;

    width = winWidth - margin.left - margin.right;
    height = 500 - margin.top - margin.bottom;
  }

  return {
    render: render
  }
})(window, d3);

这篇关于d3.js:d3.min.js:1错误:&lt; path&gt;属性d:预期数目,“MNaN,NaNLNaN,NaN”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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