简体   繁体   English

画一条直线D3.js

[英]Drawing a straight line D3.js

I feel like this has a really simple solution but I have been struggling for a while now. 我觉得这是一个非常简单的解决方案,但是我已经苦苦挣扎了一段时间。 I have a d3 line graph and I want to draw a line on the graph that represents the average of the data. 我有一个d3折线图,我想在该图上画一条线,代表数据的平均值。 I am trying to do this with the line attribute but I keep getting the same errors. 我正在尝试使用line属性执行此操作,但是我一直收到相同的错误。 The errors I keep getting are: 我不断得到的错误是:

"Unexpected value NaN parsing y1 attribute. Unexpected value NaN parsing y2 attribute. " “意外值NaN解析y2属性。意外值NaN解析y2属性。”

Here is the code where I try to draw the line: 这是我尝试画线的代码:

student_av_data.forEach(function(d) { 
     d.student_average = +d.student_average; 
     });

svg.append("line")
    .style("stroke", "orange")
    .style("opacity", 1)
    .attr("x1", 0)
    .attr("y1", y0(function(d) {return y0(d.student_average);}))
    .attr("x2", width)
    .attr("y2", y0(function(d) {return y0(d.student_average);}));

function(d) {return y0(d.student_average);} should just be a number and that should work in the y0 arguments. function(d) {return y0(d.student_average);}应该只是一个数字,并且应该在y0参数中起作用。

Not really sure what is wrong. 不太确定出什么问题了。 Any suggestions would be appreciated. 任何建议,将不胜感激。 Thanks! 谢谢!

EDIT: 编辑:

As a reference, this works and is basically what I am trying to do but with d.student_average in y0() : 作为参考,这有效,并且基本上是我要尝试的工作,但在y0()使用d.student_average

svg.append("line")
    .style("stroke", "black")
    .style("opacity", .2)
    .attr("x1", 0)
    .attr("y1", y0(24))
    .attr("x2", width)
    .attr("y2", y0(24));

Try replacing with this: 尝试替换为:

 svg.append("line")
        .style("stroke", "orange")
        .style("opacity", 1)
        .data(student_av_data)
        .attr("x1", 0)
        .attr("y1", function(d) {return y0(d.student_average);})
        .attr("x2", width)
        .attr("y2", function(d) {return y0(d.student_average);});

which will take d from the data elem 从数据元素中取d

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

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