如何在现有有效的SVG文档中创建“信息弹出框”,使得当您悬停或点击某些元素(比如说)时,可以弹出带有任意数量(而不仅仅是单行提示)附加信息的框?
这应该至少在Firefox中正确显示,并且如果将图像光栅化为位图格式,则应该是不可见的。
如何在现有有效的SVG文档中创建“信息弹出框”,使得当您悬停或点击某些元素(比如说)时,可以弹出带有任意数量(而不仅仅是单行提示)附加信息的框?
这应该至少在Firefox中正确显示,并且如果将图像光栅化为位图格式,则应该是不可见的。
这个问题是在2008年提出的。在随后的四年中,SVG得到了快速改进。现在所有我知道的平台都完全支持工具提示。使用<title>
标签(而不是属性),您将获得本地工具提示。
以下是文档:https://developer.mozilla.org/en-US/docs/SVG/Element/title
chart.selectAll("g.cell.child").append("title").text(function(d) { return d.size;} );
。 - Pat<svg>
<text id="thingyouhoverover" x="50" y="35" font-size="14">Mouse over me!</text>
<text id="thepopup" x="250" y="100" font-size="30" fill="black" visibility="hidden">Change me
<set attributeName="visibility" from="hidden" to="visible" begin="thingyouhoverover.mouseover" end="thingyouhoverover.mouseout"/>
</text>
</svg>
更多解释请查看这里。
由于<set>
元素不适用于Firefox 3,因此我认为您需要使用ECMAScript。
如果您将以下脚本元素添加到SVG中:
<script type="text/ecmascript"> <![CDATA[
function init(evt) {
if ( window.svgDocument == null ) {
// Define SGV
svgDocument = evt.target.ownerDocument;
}
tooltip = svgDocument.getElementById('tooltip');
}
function ShowTooltip(evt) {
// Put tooltip in the right position, change the text and make it visible
tooltip.setAttributeNS(null,"x",evt.clientX+10);
tooltip.setAttributeNS(null,"y",evt.clientY+30);
tooltip.firstChild.data = evt.target.getAttributeNS(null,"mouseovertext");
tooltip.setAttributeNS(null,"visibility","visible");
}
function HideTooltip(evt) {
tooltip.setAttributeNS(null,"visibility","hidden");
}
]]></script>
onload="init(evt)"
调用init()函数。<text id="tooltip" x="0" y="0" visibility="hidden">Tooltip</text>
onmousemove="ShowTooltip(evt)"
onmouseout="HideTooltip(evt)"
mouseovertext="Whatever text you want to show"
我已经在http://www.petercollingridge.co.uk/interactive-svg-components/tooltip上编写了更详细的说明,改进了功能。
我还没有包括多行工具提示,这需要多个<tspan>
元素和手动换行。
nodeEnter.append("svg:element")
.style("fill", function(d) { return d._children ? "lightsteelblue" : "#fff"; })
.append("svg:title")
.text(function(d) {return d.Name+"\n"+d.Age+"\n"+d.Dept;}); // It shows the tool tip box with item [Name,Age,Dept] and upend to the svg dynamicaly