简体   繁体   English

D3 v5:中的“ d”属性 <path> 标签未填充值

[英]D3 v5: “d” attribute in <path> tag not populating with values

Currently trying to create a simple line chart with D3 but I'm having some difficulties getting the data to display. 当前正在尝试使用D3创建一个简单的折线图,但是在显示数据时遇到了一些困难。 The code seems to work (axes are generated fine) but for some reason, the "d" attribute in the tag is nonexistent when I run the code. 该代码似乎可以正常工作(生成的轴很好),但是由于某些原因,当我运行代码时,标记中的“ d”属性不存在。 The "fill," "stroke," and "stroke-width" attributes show up fine. “填充”,“笔划”和“笔划宽度”属性显示良好。

It appears that the line generator function I created is never used (see the console.logs below). 我创建的行生成器函数似乎从未使用过(请参见下面的console.logs)。 I'm also not getting any errors. 我也没有任何错误。

d3.csv("./data/myFile.csv").then(d => {
        return { 
            percentile: d.percentile, 
            y50: +d.y50
        };
    }).then(data => {
        var xScale = d3.scalePoint()
            .domain(myDomain)
            .range([0, width])
            .padding(.5);
        svg.append("g")
            .attr("transform", "translate(0," + height + ")")
            .call(d3.axisBottom(xScale));

        var yScale = d3.scaleLinear()
            .domain([0, 100])
            .range([height, 0]);
        svg.append("g")
            .call(d3.axisLeft(yScale));

        let line = d3.line()
            .x(d => {
                console.log("THIS DOES NOT PRINT TO CONSOLE");
                return xScale(d.percentile);
            })
            .y(d => {return yScale(d.y50)});

        console.log('THIS PRINTS TO CONSOLE');

        svg.append("path")
            .datum(data)
            .attr("d", line)
            .attr("fill", "none")
            .attr("stroke", "steelblue")
            .attr("stroke-width", 1.5);

        console.log('THIS PRINTS TO CONSOLE');
    });

This is what I see in the browser (I expect there to be a "d=..." attribute as well): 这是我在浏览器中看到的(我希望也有一个“ d = ...”属性):

<path fill="none" stroke="steelblue" stroke-width="1.5"></path>

Edit: Here's a screenshot of what I see in the browser: 编辑:这是我在浏览器中看到的屏幕截图: 屏幕截图

Here's the first few rows of data: 这是数据的前几行:

percentile,y50
P0-10,16.10
P10-20,17.10
P20-30,18.50
P30-40,19.00
P40-50,19.20
d3.csv("./data/myFile.csv").then(data => {
      data.forEach(function(d) {
        d.y50 = +d.y50;
      });
        var xScale = d3.scalePoint()
            .domain(myDomain)
            .range([0, width])
            .padding(.5);
        svg.append("g")
            .attr("transform", "translate(0," + height + ")")
            .call(d3.axisBottom(xScale));

        var yScale = d3.scaleLinear()
            .domain([0, 100])
            .range([height, 0]);
        svg.append("g")
            .call(d3.axisLeft(yScale));

        let line = d3.line()
            .x(d => {
                console.log("THIS DOES NOT PRINT TO CONSOLE");
                return xScale(d.percentile);
            })
            .y(d => {return yScale(d.y50)});

        console.log('THIS PRINTS TO CONSOLE');

        svg.append("path")
            .datum(data)
            .attr("d", line)
            .attr("fill", "none")
            .attr("stroke", "steelblue")
            .attr("stroke-width", 1.5);

        console.log('THIS PRINTS TO CONSOLE');
    });

This should fix your problem. 这应该可以解决您的问题。 You're original second then statement never retained the transformed data in your first then statement. 您原来是第二个then语句,您永远不会在第一个then语句中保留转换后的数据。

To explain the problem that was found here in the comments for future reference: 为了解释在注释中找到的问题,以供将来参考:

Rather than converting all the returned rows into the expected data format, the first .then is just returning a single object with: 而不是将所有返回的行转换为期望的数据格式,第一个.then仅返回具有以下内容的单个对象:

{ 
    percentile: undefined, 
    y50: NaN
} 

What needs to happen here is to loop, or map, over the original data, and return the transformed object for each row ie 这里需要发生的是循环或映射原始数据,并为每一行返回转换后的对象,即

.then(data => {
    return data.map(d => {
        return { percentile: d.percentile, y50: +d.y50 }
    }
));

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM