1
我想在D3中使用从CSV输入中获取的点数据绘制一条线。虽然我可以循环访问我的数据(在dataSet
中),但是甚至可以从下一个索引分配x2
和y2
坐标吗?如何在D3中使用csv绘制一条线
//variable 'dataSet' holds all data points
//x-coordinate in index [0], and y-coordinate in index [1]
svg.selectAll("line")
.data(dataSet)
.enter().append("line")
.attr("x1", function(d){
return d[0];
})
.attr("y1", function(d){
return d[1];
})
.attr("x2", function(d){
return d[0]; //here's the problem, how can I access the next x-coordinate?
})
.attr("y2", function(d){
return d[1]; //here's the problem, how can I access the next y-coordinate?
})
.attr("interpolate", "linear")
真棒,感谢高积云,这是非常合理的!由于我仍然习惯于这个'data(dataSet).enter()...'成语,请你帮我进一步告诉我怎样才能遍历'dataSet'的所有元素,而不需要迭代最后一个元素(因为那么i + 1会抛出异常错误)。通常对于许多其他语言,你会告诉编译器停在(array.length - 1)处,但我不确定这将如何完成。 – cicero38
您必须执行检查,主要取决于您在连接的线路末端所查找的内容。 1)没有更多的行:'return dataSet [Math.min(i + 1,dataSet.length - 1)]' - 2)关闭到第一个点:'return dataSet [(i + 1)%dataSet.length] '。 – altocumulus
谢谢++ !!!!!!! – cicero38