【发布时间】:2016-09-21 05:57:36
【问题描述】:
我想用这个小提琴显示的以下方式放大我的节点。 http://jsfiddle.net/HF57g/2/
小提琴来源:How to zoom in/out d3.time.scale without scaling other shapes bound to timeline
在小提琴的帮助下,我能够使用 SVG Circles 应用缩放。
注意:下面的代码显示,通过使用 SVG Circles,您可以获得 x 和 y 轴属性。
.enter().append("circle")
.attr("cx", function(d) { return xScale(d); })
.attr("cy", function(d) { return yScale(d); })
我想要的是使用此缩放功能同时拥有圆形和菱形
所以我选择了 D3 的“d3.svg.symbol”,允许圆形和菱形。
但是,我面临的问题是,通过使用 D3 的 SVG 符号,我无法专门操作 x 轴,因为它只允许平移。
.enter().append("path")
.attr("transform", function(d) {
return "translate(" + d.x + "," + d.y + ")"; })
.attr("d", d3.svg.symbol()
.size(145)
.type(function(d) {
if(d.value <= 10)
return "diamond";
else
return "circle";
}));
小提琴下面的代码显示了从小提琴中获取缩放的 x 轴操作。如果可能的话,我想对翻译做同样的事情。
return svg.selectAll("rect.item")
.attr("x", function(d){return scale(d);})
下面的代码展示了它与 SVG Circles 一起工作的方式,它展示了我认为使缩放工作的最合乎逻辑的方式。但不幸的是,它不起作用。
var zoom = d3.behavior.zoom()
.on("zoom", function(){
//If I use SVG Circles the following code works.
//chart.selectAll(".item").attr("cx", function(d){ return xScale(d); });
//By using SVG Symbols and translate, this does not work
chart.selectAll(".item").attr("transform", function(d) { return "translate("+ d.x +", 0)";});
}).x(xScale);
这是一个用菱形和圆圈编辑的小提琴。 https://jsfiddle.net/g67cgt4w/
【问题讨论】:
-
你能实现你尝试过的小提琴吗?
-
在您的小提琴中,您有一个注释掉的部分,其中包括以下内容作为对
transform属性的更改的一部分:scale(" + d3.event.scale+", 1)。而不是“1”(即y轴没有变化),将其更改为scale(" + d3.event.scale + "," + d3.event.scale + ")不会给出您想要完成的任务吗? (也许我误解了这个问题......毕竟我看到的是正方形而不是菱形)。 -
@thatOneGuy 给你。 jsfiddle.net/g67cgt4w
-
@MaxStarkenburg 感谢您的回复。我提到的小提琴不是我个人的作品。我试图包含 return svg.selectAll("circleDiamond.item") .attr("transform", function(d) { return "translate(" + scale(d) + ",0)scale(" + d3.event。 scale + "," + d3.event.scale + ")"; }) } 在更新事件函数中,但是不起作用。
-
感谢 OneGuy 和 Max Sarkenburg 抽出宝贵时间帮助我。
标签: javascript d3.js svg transform translate