使用 d3 SVG 比例而不进行翻译
Using d3 SVG Scale without translate
我正在尝试在SVG和d3中使用scale()转换。 我知道它通过增加坐标比例来工作,但它似乎也在平移我的对象。 当我有一个位于 (100,100) 的矩形并且我做一个 scale(2) 时,矩形的大小加倍并移动到 (0,0)。 如何让它在缩放时停止从 (100,100) 移动到 (0,0)。 以下是我的代码:
var mysvg = d3.select("#viz")
.append("svg")
.attr("width", 500)
.attr("height", 500)
.attr("class","mylist");
var node = mysvg.selectAll("g.node")
.data(mydata)
.enter().append("g")
.attr("class","node")
.attr("transform", function(d) { return "translate(" + d.xpos + "," + d.ypos + ")"; });
node.append("rect")
.attr("width",tileWidth)
.attr("height",tileWidth)
.attr("fill","orange")
.attr("rx",10)
.attr("ry",10);
node.append("text")
.attr("transform",function(d) { return "translate(" + tileWidth/2 + "," + tileWidth/2 +")" })
.attr("text-anchor", "middle")
.attr("dy", ".3em")
.attr("font-family","serif")
.text(function(d) { return d.symbol; });
node.on("mouseover",function(){ d3.select(this).transition().attr("transform","scale(1.2)") });
node.on("mouseout",function(){ d3.select(this).transition().attr("transform","scale(1)") });
无需翻译即可扩展的通用解决方案可在此处找到:围绕中心点缩放
想法:
translate(-centerX*(factor-1), -centerY*(factor-1))
scale(factor)
您的mouseover
和mouseout
事件将覆盖原始translate(" + d.xpos + "," + d.ypos + ")
。
简单方法是添加一个父g
并对其进行翻译,例如......
var mysvg = d3.select("#viz")
.append("svg")
.attr("width", 500)
.attr("height", 500)
.attr("class","mylist");
var parent = mysvg.selectAll("g.parent")
.data(mydata)
.enter().append("g")
.attr("class","parent")
.attr("transform", function(d) { return "translate(" + d.xpos + "," + d.ypos + ")"; });
var node = mysvg.selectAll("g.parent")
.enter().append("g")
.attr("class","node")
});
node.append("rect")
.attr("width",tileWidth)
.attr("height",tileWidth)
.attr("fill","orange")
.attr("rx",10)
.attr("ry",10);
node.append("text")
.attr("transform",function(d) { return "translate(" + tileWidth/2 + "," + tileWidth/2 +")" })
.attr("text-anchor", "middle")
.attr("dy", ".3em")
.attr("font-family","serif")
.text(function(d) { return d.symbol; });
node.on("mouseover",function(){ d3.select(this).transition().attr("transform","scale(1.2)") });
node.on("mouseout",function(){ d3.select(this).transition().attr("transform","scale(1)") });
相关文章:
- D3在一个调用中绘制不同的SVG形状,没有可见性
- 为什么在单独的函数中应用时转换会闪烁/断断续续(D3)
- 如何在javascript上使用flak-babel进行翻译
- 如何在d3.js中返回路径的y坐标
- d3基于用户选择动态更新节点
- 有条件更新d3.js力图中节点的最佳方法
- 为什么我的d3.jsselectAll+过滤器没有过滤
- 使用D3.js计算带有字母间距的文本长度
- d3中堆栈函数和嵌套函数之间的差异
- D3.js模式不适用于弧形或圆环图
- d3.js Chord图的动态工具提示
- 在D3.js中,有没有任何方法可以将x和y方向上的滚动事件绑定到平移svg
- 使用谷歌网站翻译器自动翻译网页
- D3嵌套组作为x轴
- d3.hexbin插件-动态定义颜色域以适应数据
- HTML5FileReader输出到D3.js图表
- 如何在d3上的图形中添加放大和缩小按钮
- 使用 d3 SVG 比例而不进行翻译
- 用i18next翻译d3工具提示
- D3缩放行为后的目标已被翻译