[英]Add points (x,y) to a d3 multi-line graph with time scale x-axis
我目前正在尝试使用d3.time.scale()
为x轴构建一个多线图。
我正在尝试向图形的线条上的每个点添加圆圈,但是到目前为止还没有成功。
当我做类似的事情时:
.attr('cx', function(d){ return x(d.price) })
我得到一个负数。
我当时在考虑设置另一个scale (pointsScale)
来处理此问题,但在很大程度上没有成功。
我究竟做错了什么?
请参考我的JSBin代码。
您在这里遇到了一些问题:
x(d.price)
为负的原因x(d.price)
试图将价格解释为日期,但最终没有多大意义。 因此,用以下代码替换上面的代码行: .attr('cy', function(d){ return y(d.price) })
cx
, cy
和r
。 由于d3已经知道您的x轴是时间标度,因此可以使用.attr('cx', function(d){ return x(d.date) })
设置cx
。 您可以将r
设为圆的半径。 只需选择一个,否则它将默认为0,您将看不到圆圈。 例如, .attr('r', 4)
会将半径设置为完全可见的值4。 综上所述,这大致就是创建圆的代码的样子,并且应该在声明var paths
:
var circles = company.selectAll('circle')
.data(function(d){ return d.values; })
.enter().append('circle')
.attr('cy', function(d){
return y(d.price);}) //Price is the y variable, not the x
.attr('cx', function(d){
return x(d.date);}) //You also need an x variable
.attr('r',4); //And a radius - otherwise your circles have
//radius 0 and you can't see them!
更新的jsbin: http ://jsbin.com/gorukojoxu/edit?html,控制台,输出
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.