[英]Brush functionality in my d3 line chart is not working as expected
My implementation for Brush & Zoom functionality in my d3 line chart is not working as expected,我在 d3 折线图中对笔刷和缩放功能的实现没有按预期工作,
I followed this link - https://bl.ocks.org/EfratVil/92f894ac0ba265192411e73f633a3e2f ,我点击了这个链接 - https://bl.ocks.org/EfratVil/92f894ac0ba265192411e73f633a3e2f ,
Problems what I am facing is -我面临的问题是-
chart is not showing all the values, I have 4 data but it only shows 3 data onClick of dot I am showing the rect which is not moving with the brush functionality minor thing but chart always goes out of the box图表没有显示所有值,我有 4 个数据,但它只显示 3 个数据 onClick of dot 我正在显示不随画笔功能移动的矩形,但图表总是开箱即用
My code sandbox - https://codesandbox.io/s/proud-firefly-xy1py我的代码沙箱 - https://codesandbox.io/s/proud-firefly-xy1py
Can someone point out what I am doing wrong?有人可以指出我做错了什么吗? thanks.
谢谢。
Please suggest me what I am doing wrong, thanks.请告诉我我做错了什么,谢谢。
Your first point is going behind your clip
area.你的第一点是在你的
clip
区域后面。 For example, if you right click on the first visible circle and inspect element you will see all 4 circle elements are present in the dom.例如,如果您右键单击第一个可见圆并检查元素,您将看到 dom 中存在所有 4 个圆元素。 The first circle element is behind the axis.
第一个圆元素在轴的后面。
This means you have to move your plot to the right.这意味着您必须将情节向右移动。 Unfortunately, the way you have coded the chart you have not appended a
g
element for the main chart and then appended the circles and path to that g
element.不幸的是,您对图表进行编码的方式没有为主图表附加
g
元素,然后将圆圈和路径附加到该g
元素。 As a result this has to be done in multiple places.因此,这必须在多个地方完成。
First we adjust your clip path as:首先,我们将您的剪辑路径调整为:
svg
.append("defs")
.append("SVG:clipPath")
.attr("id", "clip")
.append("SVG:rect")
.attr("width", containerWidth)
.attr("height", height)
.attr("x", 40)
.attr("y", 0);
next we adjust your circles接下来我们调整你的圈子
scatter
.selectAll(".foo")
.data(data)
.enter()
.append("circle")
.attr("class", "foo")
.attr("transform", "translate(40,0)")
and then your line然后你的线路
scatter
.append("path")
.datum(data)
.attr("class", "line")
.attr("d", line)
.attr("transform", "translate(40,0)");
You will have to account for this 40
px translate for your other elements as well.您还必须为其他元素考虑这个
40
像素的翻译。 Although I am having a hard time destructuring your svg.虽然我很难破坏你的 svg。 I think this should give you the idea though.
我认为这应该给你这个想法。 Check the axis matches the time points as well.
检查轴是否与时间点匹配。
Check the code sand box检查代码沙箱
Update更新
To make the rectangles move with the brush, you will have to add code to your brushed
const function to recalculate the x, y, width and height using the updated scales.要使矩形与画笔一起移动,您必须向
brushed
const 函数添加代码,以使用更新的比例重新计算 x、y、宽度和高度。
Update2更新2
After going through the codesandbox presented in the comments I was able to add the code to update the rectangles to the brushed
const as below to make the rects also move with the brushing:在浏览了评论中提供的代码和框后,我能够添加代码以将矩形更新为
brushed
常量,如下所示,使矩形也随着刷刷而移动:
// update rectangles
scatter
.selectAll(".rect-elements")
.attr("x", d => {
console.log(d);
return xScale(d.startTime) - 12.5;
})
.attr("y", 0)
.attr("width", 24)
.attr("height", height + 5);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.