我正在使用D3编写一个可缩放的树形图。我以https://gist.github.com/vibster/3257874的基本代码开始。我包含整个代码的git,但将包括下面的相关部分。未捕获的SyntaxError:在D3树形图中带有鼠标悬停事件的意外的标记
对于cell
变量,我在上面的回答中添加了一个鼠标悬停功能:Zoomable treemap with tooltips at the bottom,它应该提供悬停时的工具提示。
var cell = svg.selectAll("g")
.data(nodes)
.enter().append("svg:g")
.attr("class", "cell")
.attr("transform", function(d) { return "translate(" + d.x + "," + d.y + ")"; })
.on("click", function(d) { return zoom(node == d.parent ? root : d.parent); });
这导致了这个代码可变单元:
var cell = svg.selectAll("g")
.data(nodes)
.enter().append("svg:g")
.attr("class", "cell")
.attr("transform", function(d) { return "translate(" + d.x + "," + d.y + ")"; })
.on("click", function(d) { return zoom(node == d.parent ? root : d.parent); });
.on('mouseover', function(d) {
// this variable will be used in a loop to store the current node being inspected
var currentNode = d;
// this array will hold the names of each subsequent parent node
var nameList = [currentNode.name];
// as long as the current node has a parent...
while (typeof currentNode.parent === 'object') {
// go up a level in the hierarchy
currentNode = currentNode.parent;
// add the name to the beginning of the list
nameList.unshift(currentNode.name);
}
// now nameList should look like ['flare','animate','interpolate']
// join the array with slashes (as you have in your example)
nameList = nameList.join('/');
// now nameList should look like 'flare/animate/interpolate'
// use this to set the tooltip text
$('#tooltip').text('Mouse hovering ' + nameList + '. Cell size = ' + d.area);
}
不幸的是,这会导致树形图完全无法显示了。我在Chrome控制台上做了一些挖掘工作,得到Uncaught SyntaxError: Unexpected token
作为错误。我完全不熟悉JavaScript和D3,所以我不确定问题是什么。
感谢关于这两行代码的指针。我明白你为什么加入了div标签。当我尝试将JSFiddle重新组合到一个嵌入了CSS和Javascript的HTML文件时,工具提示不会显示出来。有什么建议? – user2242044
我无法编辑我的评论,但我确实发现它是一个JavaScript错误'Uncaught ReferenceError:$未定义在工具提示行 – user2242044
@ user2242044工具提示示例还使用jQuery库。您应该将其添加到标题(在d3.v2.js下方或上方)。来源是例如这里:'http:// code.jquery.com/jquery-1.11.3.min.js'。 – user3714582