简体   繁体   English

d3.js,属性 d:预期数字,“MNaN,NaNLNaN,NaN”

[英]d3.js, attribute d: Expected number, “MNaN,NaNLNaN,NaN”

First off, let me say that I have tried other solutions to this error posted on Stack Overflow to no avail.首先,让我说我已经尝试了其他解决方案来解决 Stack Overflow 上发布的这个错误,但无济于事。

I am making a line graph with D3.js.我正在用 D3.js 制作折线图。 It all works except for this part:除了这部分之外,一切都有效:

let lineGen = d3.svg.line()
                        .x(function(d) {
                            //d.month is a string in format MM/YY"
                            return xScale(d.month);
                        })
                        .y(function(d) {
                            console.log('d.feeling: ',d.feeling);
                            //d.feeling is an integer from 1-10
                            return yScale(parseInt(d.feeling));
                        })
                        .interpolate("basis");

When I run this, the browser console gives me the error: d3.v3.min.js:1 Error: <path> attribute d: Expected number, "MNaN,261.66666666…".当我运行它时,浏览器控制台给了我错误: d3.v3.min.js:1 Error: <path> attribute d: Expected number, "MNaN,261.66666666…". . .

I believe the issue is that the X-axis is made up of strings.我认为问题在于 X 轴是由字符串组成的。 I don't know how to properly convert these to the right numbers, though.不过,我不知道如何将这些正确转换为正确的数字。 They display in the right order on the axis (I'm using parseInt() on the output of momentJS).它们以正确的顺序显示在轴上(我在 momentJS 的输出上使用 parseInt())。

What do I do?我该怎么办?

EDIT: Here is the code for the entire chart:编辑:这是整个图表的代码:

function InitChart() {
                    let data = [{
                        "feeling": "5",
                        "month": "01/17"
                    }, {
                        "feeling": "5",
                        "month": "02/17"
                    }, {
                        "feeling": "7",
                        "month": "03/17"
                    }, {
                        "feeling": "2",
                        "month": "04/17"
                    }, {
                        "feeling": "9",
                        "month": "05/17"
                    }, {
                        "feeling": "4",
                        "month": "06/17"
                    }, {
                        "feeling": "9",
                        "month": "07/17"
                    }, {
                        "feeling": "9",
                        "month": "08/17"
                    }, {
                        "feeling": "10",
                        "month": "09/17"
                    }, {
                        "feeling": "1",
                        "month": "10/17"
                    }, {
                        "feeling": "6",
                        "month": "11/17"
                    }, {
                        "feeling": "5",
                        "month": "12/17"
                    }];

                    let today = new Date();
                    let sixMonthsAgo;
                    let yearSixMonthsAgo;

                    if ((parseInt(moment().format('MM')) - 6) > 0) {
                      sixMonthsAgo = parseInt(moment().format('M') - 6);
                      yearSixMonthsAgo = parseInt(moment().format('YYYY'));
                    }
                     else if ((parseInt(moment().format('MM')) - 6) <= 0) {
                      sixMonthsAgo = parseInt(moment().format('M')) + 6;
                      yearSixMonthsAgo = parseInt(moment().format('YYYY')) - 1;
                    };

                    let vis = d3.select("#visualisation"),
                        WIDTH = 1000,
                        HEIGHT = 475,
                        MARGINS = {
                            top: 20,
                            right: 20,
                            bottom: 20,
                            left: 50
                        },
                        xScale = d3.time.scale()
                          .domain([new Date(yearSixMonthsAgo, sixMonthsAgo, 1), new Date()])
                          .range([MARGINS.left, WIDTH - MARGINS.right]),
                        yScale = d3.scale.linear().range([HEIGHT - MARGINS.top, MARGINS.bottom]).domain([1, 10]),
                        xAxis = d3.svg.axis()
                          .scale(xScale)
                          .orient("bottom")
                          .ticks(d3.time.months)
                          .tickFormat(d3.time.format("%B")),
                        yAxis = d3.svg.axis()
                          .scale(yScale)
                          .orient("left");

                    vis.append("svg:g")
                        .attr("class", "x axis")
                        .attr("transform", "translate(0," + (HEIGHT - MARGINS.bottom) + ")")
                        .call(xAxis);
                    vis.append("svg:g")
                        .attr("class", "y axis")
                        .attr("transform", "translate(" + (MARGINS.left) + ",0)")
                        .call(yAxis);


                    let lineGen = d3.svg.line()
                        .x(function(d) {
                            //d.month is a string in format MM/YY"
                            return xScale(d.month);
                        })
                        .y(function(d) {
                            console.log('d.feeling: ',d.feeling);
                            //d.feeling is an integer from 1-10
                            return yScale(parseInt(d.feeling));
                        })
                        .interpolate("basis");
                    vis.append('svg:path')
                        .attr('d', lineGen(data))
                        .attr('stroke', 'green')
                        .attr('stroke-width', 2)
                        .attr('fill', 'none');
                }
                InitChart();

I would use d3's time formatting to get those dates into something that d3 can use for a scale, using %-m/%y as the specifier.我会使用 d3 的时间格式将这些日期转换为 d3 可以用于比例的内容,使用%-m/%y作为说明符。 %-m is a non-padded decimal month and %y is the last 2 digits of the year. %-m是未填充的十进制月份, %y是年份的最后 2 位数字。

暂无
暂无

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

相关问题 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” 错误: <path> 属性d:预期数,“MNaN,NaNLNaN,NaN” - Error: <path> attribute d: Expected number, “MNaN,NaNLNaN,NaN” 错误: <path> 属性d:预期的数字,“MNaN,NaNLNaN,NaNL ......”。 在d3.js - Error: <path> attribute d: Expected number, “MNaN,NaNLNaN,NaNL…”. in d3.js (D3折线图)错误: <path> 属性d:预期数字“ MNaN,NaNLNaN,NaN” - (D3 line chart) Error: <path> attribute d: Expected number, “MNaN,NaNLNaN,NaN” 错误:<path> 属性 d:预期数字,“MNaN” - D3.js - Error: <path> attribute d: Expected number, “MNaN” - D3.js D3.js折线图错误:的值无效 <path> 属性d =“ MNaN,NaNLNaN - D3.js Line Chart Error: Invalid value for <path> attribute d="MNaN,NaNLNaN D3多折线图-错误: <path> 属性d:预期数字“ MNaN,NaNLNaN,NaNC ...” - D3 multi-line chart - Error: <path> attribute d: Expected number, “MNaN,NaNLNaN,NaNC…” d3路径d = MNaN,NaNLNaN,NaN - d3 path d = MNaN,NaNLNaN,NaN 带有画笔过滤器的d3.v4.js线图-错误: <path> 属性d:预期数字“ MNaN,NaNLNaN,NaNC…”。 和预期的数字“ M0,NaNL0.1167307672…” - d3.v4.js Line Graph with brush filter - Error: <path> attribute d: Expected number, “MNaN,NaNLNaN,NaNC…”. and Expected number, “M0,NaNL0.1167307672…” D3.JS V5:错误:<path> 属性 d:预期数字,“MNaN,242.20533333...” - D3.JS V5: Error: <path> attribute d: Expected number, "MNaN,242.20533333…"
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM