javascript - 错误 : <path> attribute d: Expected number, "MNaN,191.94630872…"。 d3 错误并使用react

标签 javascript reactjs d3.js

我正在尝试将 D3 与 React 集成,请在下面找到代码片段。

在代码中,我试图在 D3 中实现一个简单的行并使用react。但我最终得到 Error: <path> attribute d: Expected number, "MNaN,191.94630872…

在 stackoverflow 上有类似的问题,但他们也没有帮助。

attribute d: Expected number, "MNaN,NaNLNaN,NaNL…". react with d3

"Error: <path> attribute d: Expected number, "MNaN,NaNLNaN,NaNL…". " Error with D3

我尝试做的几件事。

A:最初我认为日期解析有问题,所以我更改了 day number 的关键并删除了日期解析,我最终遇到了同样的错误。

B:A 假设 x 和 y 的域声明存在一些问题。我删除了扩展并给出了一个离散值,仍然面临同样的错误。

import React, { Component } from 'react';
import * as d3 from "d3";

class LineChart extends Component {
    constructor(props) {
      super(props);
      this.state = {selected: props.width || null};
    }

    componentWillMount() {
    }

    componentWillReceiveProps(nextProps) {
    }

    render() {
        let data=[
            {day:'02-11-2016',count:180},
            {day:'02-12-2016',count:250},
            {day:'02-13-2016',count:150},
            {day:'02-14-2016',count:496},
            {day:'02-15-2016',count:140},
            {day:'02-16-2016',count:380},
            {day:'02-17-2016',count:100},
            {day:'02-18-2016',count:150}
        ];

        let margin = {top: 5, right: 50, bottom: 20, left: 50},
            w = this.state.width - (margin.left + margin.right),
            h = this.props.height - (margin.top + margin.bottom);

        let parseDate = d3.timeParse("%m-%d-%Y");
        data.forEach(function (d) {
            d.date = parseDate(d.day);
        });

        let x = d3.scaleTime()
            .domain(d3.extent(data, function (d) {
                return d.date;
            }))
            .range([0, w]);

        let y = d3.scaleLinear()
            .domain([ 0 , d3.max( data, function(d) {
                return d.count + 100;
            })])
            .range([ h, 0 ]);

        let line = d3.line()
            .x(function (d) {
                return x(d.date);
            })
            .y(function (d) {
                return y(d.count);
            }).curve(d3.curveCardinal);

        let transform = 'translate(' + margin.left + ',' + margin.top + ')';
        return (
            <div>
                <p>LineChart</p>
                <svg id={this.props.chartId} width={this.state.width} height={this.props.height}>
                    <g transform={transform}>
                        <path className="line shadow" d={line(data)} strokeLinecap="round"/>
                    </g>            
                </svg>
            </div>
      )
    }
  }

  LineChart.defaultProps = {
    width: 800,
    height: 300,
    chartId: 'v1_chart'
  }
  export default LineChart;

最佳答案

你在这个字符串上的错误:

w = this.state.width - (margin.left + margin.right)

如果你输入 console.log(this.state.width) 你会得到 undefined。您将 w 变量用于 d3 标度,因此它们会产生不正确的值。 您可以通过这种方式将图表 width 作为组件 Prop 传递:

<LineChart width={600} height={300} />

并指定w变量:

    let margin = {top: 5, right: 50, bottom: 20, left: 50},
        w = this.props.width - (margin.left + margin.right), // <== !!!
        h = this.props.height - (margin.top + margin.bottom);

检查下面的工作演示:

class LineChart extends React.Component {
    constructor(props) {
      super(props);
      this.state = {selected: props.width || null};
    }

    componentWillMount() {
    }

    componentWillReceiveProps(nextProps) {
    }

    render() {
        let data=[
            {day:'02-11-2016',count:180},
            {day:'02-12-2016',count:250},
            {day:'02-13-2016',count:150},
            {day:'02-14-2016',count:496},
            {day:'02-15-2016',count:140},
            {day:'02-16-2016',count:380},
            {day:'02-17-2016',count:100},
            {day:'02-18-2016',count:150}
        ];

        let margin = {top: 5, right: 50, bottom: 20, left: 50},
            w = this.props.width - (margin.left + margin.right),
            h = this.props.height - (margin.top + margin.bottom);

        let parseDate = d3.timeParse("%m-%d-%Y");
        data.forEach(function (d) {
            d.date = parseDate(d.day);
        });

        let x = d3.scaleTime()
            .domain(d3.extent(data, function (d) {
                return d.date;
            }))
            .range([0, w]);

        let y = d3.scaleLinear()
            .domain([ 0 , d3.max( data, function(d) {
                return d.count + 100;
            })])
            .range([ h, 0 ]);

        let line = d3.line()
            .x(function (d) {
                return x(d.date);
            })
            .y(function (d) {
                return y(d.count);
            }).curve(d3.curveCardinal);

        let transform = 'translate(' + margin.left + ',' + margin.top + ')';
        return (
            <div>
                <p>LineChart</p>
                <svg id={this.props.chartId} width={this.state.width} height={this.props.height}>
                    <g transform={transform}>
                        <path className="line shadow" d={line(data)} strokeLinecap="round"/>
                    </g>            
                </svg>
            </div>
      )
    }
  }

ReactDOM.render(
  <LineChart width={600} height={200} />,
  document.getElementById('container')
);
body { font: 12px Arial;}

path { 
    stroke: steelblue;
    stroke-width: 2;
    fill: none;
}

.axis path,
.axis line {
    fill: none;
    stroke: grey;
    stroke-width: 1;
    shape-rendering: crispEdges;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/4.12.0/d3.min.js"></script>
<script src="https://unpkg.com/react@16/umd/react.development.js"></script>
<script src="https://unpkg.com/react-dom@16/umd/react-dom.development.js"></script>


<div id="container">
    <!-- This element's contents will be replaced with your component. -->
</div>

关于javascript - 错误 : <path> attribute d: Expected number, "MNaN,191.94630872…"。 d3 错误并使用react,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47808553/

相关文章:

javascript - Google map 不在选项卡上显示 map

javascript - Starter "Hello World"React 项目给出空页面

javascript - 根据字节值设置 KiB、MiB、GiB 单位的 d3 轴

javascript - 在所有适度使用的浏览器中检测缩放级别?

javascript - Chrome 的最佳 JavaScript 控制台

reactjs - Webpack 2 配置用于 React 项目上的 System.import 树摇动和延迟加载

d3.js - Typescript 期望从 d3.nest().rollup() 中的辅助函数返回 undefined

angular - 数据驱动文档 (D3.js) 单击时会改变颜色

javascript - 类型 'isvalid' 上不存在属性 'KnockoutObservable'

reactjs - 如何将 props 传递给具有相同键和值的 React 组件