所以我正在研究 d3 中制作的标签云示例。
http://www.jasondavies.com/wordcloud/#http%3A%2F%2Fsearch.twitter.com%2Fsearch.json%3Frpp%3D100%26q%3D%7Bword%7D=cloud
我试图在每个单词悬停时将 Div 放置在每个单词的顶部,并且基本上遇到了问题,因为我放置 div 的方式取决于 svg word 元素的转换属性。
这个转换属性是我需要解析的字符串,但该字符串包含我需要获取的正值和负值。
tagCloudHover.style("top", function(){
//parses the words attributes for its position, and gives that position to tagCloudHover
var str, matches, index, num;
str = thisWord.attr("transform");
matches = str.match(/\d+/g);
for (index = 1; index < 2; ++index) {
num = (parseInt(matches[index], 10));
}
if(num<0){
return -num+"px";
}else{
return num+"px";
}
});
我的代码如上所示,其中最后一条语句不执行任何操作,但它能够从字符串中获取 Int 。问题是它不会抓住负号。
我不是最擅长解析,但我尝试了几个不同的 str.match 函数,但似乎没有任何效果。
有 parseNinjas 有什么想法吗?
任何事情都有帮助。
艾萨克
你的正则表达式应该是/-?\d+/g
,基本上添加“可选-
”到模式。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)