例如我有一个转换:
var sel = container.selectAll('div')
.transition()
.duration(1000)
.attr('transform', 'translate(100,500)');
在某些时候,我需要知道某些元素落在何处,例如
setTimeout(() => {
var value = d3.select('div#target')
.expectedAttr('transform');
assertEqual(value, 'translate(100,500)');
}, 500);
D3 中有这样的内置功能吗?否则我将不得不在 d3.transition().attr() 方法上编写自己的包装器来存储传递给它的值。
编辑
我发现 D3 在元素上创建了 __transition__ 字段,它似乎包含有关转换的信息,但我看不到在那里找到目标属性值的方法。
最佳答案
起初我认为这是不可能的,因为目标值似乎无法被闭包隐藏。不过,通过一些小技巧,可以检索此值。
您必须牢记,在调用 transition.attr() 时, D3 将执行以下操作:
For each selected element, creates an attribute tween for the attribute with the specified name to the specified target value.
可以通过调用 transition.attrTween(attrName) 来访问这个自动创建的补间动画.
当这个 tween 被 D3 调用时,它会返回一个 interpolator。 .这反过来又可以访问在创建插值器时关闭的目标值。当进一步阅读文档时,真正的技巧变得显而易见:
The returned interpolator is then invoked for each frame of the transition, in order, being passed the eased time t, typically in the range [0, 1].
知道 t 的最终值(在转换结束时)将为 1,您可以使用该值调用先前获得的插值器,这将产生转换的目标值。
var targetValue = transition
.attrTween("x2") // Get the tween for the desired attribute
.call(line.node()) // Call the tween to get the interpolator
(1); // Call the interpolator with 1 to get the target value
以下示例通过打印已运行转换的目标值来说明这一点。
var line = d3.select("line");
line
.transition()
.duration(2000)
.attr("x2", 100);
setTimeout(function() {
var transition = d3.active(line.node()) // Get the active transition on the line
var targetValue = transition
.attrTween("x2") // Get the tween for the desired attribute
.call(line.node()) // Call the tween to get the interpolator
(1); // Call the interpolator with 1 to get the target value
console.log(targetValue); // 100
}, 1000);<script src="https://d3js.org/d3.v4.js"></script>
<svg><line x2="0" y2="100" stroke="black"></line></svg>
这同样适用于您将使用 transition.styleTween() 获取补间的样式转换。
关于javascript - 在 D3 转换中获取预期的属性值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39014722/
我的目标是转换表单输入,例如“100兆字节”或“1GB”,并将其转换为我可以存储在数据库中的文件大小(以千字节为单位)。目前,我有这个:defquota_convert@regex=/([0-9]+)(.*)s/@sizes=%w{kilobytemegabytegigabyte}m=self.quota.match(@regex)if@sizes.include?m[2]eval("self.quota=#{m[1]}.#{m[2]}")endend这有效,但前提是输入是倍数(“gigabytes”,而不是“gigabyte”)并且由于使用了eval看起来疯狂不安全。所以,功能正常,
我想将html转换为纯文本。不过,我不想只删除标签,我想智能地保留尽可能多的格式。为插入换行符标签,检测段落并格式化它们等。输入非常简单,通常是格式良好的html(不是整个文档,只是一堆内容,通常没有anchor或图像)。我可以将几个正则表达式放在一起,让我达到80%,但我认为可能有一些现有的解决方案更智能。 最佳答案 首先,不要尝试为此使用正则表达式。很有可能你会想出一个脆弱/脆弱的解决方案,它会随着HTML的变化而崩溃,或者很难管理和维护。您可以使用Nokogiri快速解析HTML并提取文本:require'nokogiri'h
我需要读入一个包含数字列表的文件。此代码读取文件并将其放入二维数组中。现在我需要获取数组中所有数字的平均值,但我需要将数组的内容更改为int。有什么想法可以将to_i方法放在哪里吗?ClassTerraindefinitializefile_name@input=IO.readlines(file_name)#readinfile@size=@input[0].to_i@land=[@size]x=1whilex 最佳答案 只需将数组映射为整数:@land边注如果你想得到一条线的平均值,你可以这样做:values=@input[x]
这道题是thisquestion的逆题.给定一个散列,每个键都有一个数组,例如{[:a,:b,:c]=>1,[:a,:b,:d]=>2,[:a,:e]=>3,[:f]=>4,}将其转换为嵌套哈希的最佳方法是什么{:a=>{:b=>{:c=>1,:d=>2},:e=>3,},:f=>4,} 最佳答案 这是一个迭代的解决方案,递归的解决方案留给读者作为练习:defconvert(h={})ret={}h.eachdo|k,v|node=retk[0..-2].each{|x|node[x]||={};node=node[x]}node[
我希望我的UserPrice模型的属性在它们为空或不验证数值时默认为0。这些属性是tax_rate、shipping_cost和price。classCreateUserPrices8,:scale=>2t.decimal:tax_rate,:precision=>8,:scale=>2t.decimal:shipping_cost,:precision=>8,:scale=>2endendend起初,我将所有3列的:default=>0放在表格中,但我不想要这样,因为它已经填充了字段,我想使用占位符。这是我的UserPrice模型:classUserPrice回答before_val
我有一个包含模块的模型。我想在模块中覆盖模型的访问器方法。例如:classBlah这显然行不通。有什么想法可以实现吗? 最佳答案 您的代码看起来是正确的。我们正在毫无困难地使用这个确切的模式。如果我没记错的话,Rails使用#method_missing作为属性setter,因此您的模块将优先,阻止ActiveRecord的setter。如果您正在使用ActiveSupport::Concern(参见thisblogpost),那么您的实例方法需要进入一个特殊的模块:classBlah
我有一个具有一些属性的模型:attr1、attr2和attr3。我需要在不执行回调和验证的情况下更新此属性。我找到了update_column方法,但我想同时更新三个属性。我需要这样的东西:update_columns({attr1:val1,attr2:val2,attr3:val3})代替update_column(attr1,val1)update_column(attr2,val2)update_column(attr3,val3) 最佳答案 您可以使用update_columns(attr1:val1,attr2:val2
我有这个html标记:我想得到这个:我如何使用Nokogiri做到这一点? 最佳答案 require'nokogiri'doc=Nokogiri::HTML('')您可以通过xpath删除所有属性:doc.xpath('//@*').remove或者,如果您需要做一些更复杂的事情,有时使用以下方法遍历所有元素会更容易:doc.traversedo|node|node.keys.eachdo|attribute|node.deleteattributeendend 关于ruby-Nokog
对于Rails模型,是否可以/建议让一个类的成员不持久保存到数据库中?我想将用户最后选择的类型存储在session变量中。由于我无法从我的模型中设置session变量,我想将值存储在一个“虚拟”类成员中,该成员只是将值传递回Controller。你能有这样的类(class)成员吗? 最佳答案 将非持久属性添加到Rails模型就像任何其他Ruby类一样:classUser扩展解释:在Ruby中,所有实例变量都是私有(private)的,不需要在赋值前定义。attr_accessor创建一个setter和getter方法:classUs
有没有办法在这个简单的get方法中添加超时选项?我正在使用法拉第3.3。Faraday.get(url)四处寻找,我只能先发起连接后应用超时选项,然后应用超时选项。或者有什么简单的方法?这就是我现在正在做的:conn=Faraday.newresponse=conn.getdo|req|req.urlurlreq.options.timeout=2#2secondsend 最佳答案 试试这个:conn=Faraday.newdo|conn|conn.options.timeout=20endresponse=conn.get(url