我有一个具有缩放功能的D3 JS散点图,为此我预先计算了一个线性回归(OLS),并且我可以绘制如下趋势线:
var objects = svg.append("svg")
.classed("objects", true)
.attr("width", width)
.attr("height", height);
var x = d3.scale.linear()
.range([0, width]).nice();
var y = d3.scale.linear()
.range([height, 0]).nice();
objects.append("svg:line")
.classed("trendline", true)
.attr("x1", x(xMin))
.attr("y1", y(lineIntercept + xMin*lineSlope))
.attr("x2", x(xMax))
.attr("y2", y(lineIntercept + xMax*lineSlope))
.attr("stroke", colorVal)
.attr("stroke-width", 2);这做了我想做的,除了行从xMin到xMax,我希望它从-Inf到Inf,当用户放大时,这很明显,我怎么做呢?
发布于 2017-04-24 14:02:49
解决方案是(如Q注释中所讨论的),每次缩放后,找出x轴末端的当前域位置(获得调用x.domain()),然后更新行。完整的解决方案如下:
objects
.selectAll(".trendline")
.attr("x1", x(x.domain()[0]))
.attr("y1", y(lineIntercept + x.domain()[0]*lineSlope))
.attr("x2", x(x.domain()[1]))
.attr("y2", y(lineIntercept + x.domain()[1]*lineSlope))
.attr("stroke", colorVal)
.attr("stroke-width", 2);这给人一种从-Inf到Inf的视觉印象。
https://stackoverflow.com/questions/43588840
复制相似问题