I have created an svg file using Inkscape to draw a data visualisation diagram. Now I would like to add an interaction that shows a tooltip when user hovers over a group, and the final product will be added to a website.
The tooltip is something similar to this
<!DOCTYPE html>
<html>
<head>
<title>SVG Test</title>
<style>
#tooltip {
background: cornsilk;
border: 1px solid black;
border-radius: 5px;
padding: 5px;
}
</style>
</head>
<body>
<script>
function showTooltip(evt, text) {
let tooltip = document.getElementById("tooltip");
tooltip.innerHTML = text;
tooltip.style.display = "block";
tooltip.style.left = evt.pageX + 10 + 'px';
tooltip.style.top = evt.pageY + 10 + 'px';
}
function hideTooltip() {
var tooltip = document.getElementById("tooltip");
tooltip.style.display = "none";
}
</script>
<div id="tooltip" display="none" style="position: absolute; display: none;"></div>
<svg>
<rect width="100" height="50" style="fill: blue;" onmousemove="showTooltip(evt, 'This is blue');" onmouseout="hideTooltip();" >
</rect>
</svg>
</body>
</html>
Below is my original svg image. The original file is 4500+ lines when shown on VS Code Origial svg image
I have tried manipulating object properties in Inkscape Adding interactions in Inkscape
I also searched around and referenced Including JavaScript in SVG and ended up with
<svg
...
<script
type="text/javascript"
id="script2"><![CDATA[
function showTooltip(evt, text) {
let tooltip = document.getElementById("tooltip2626");
tooltip.innerHTML = text;
tooltip.style.display = "block";
tooltip.style.left = evt.pageX + 10 + 'px';
tooltip.style.top = evt.pageY + 10 + 'px';
}
function hideTooltip() {
var tooltip = document.getElementById("tooltip2626");
tooltip.style.display = "none";
}
]]></script>
<defs
id="defs6">
<style
type="text/css"
id="style4"><![CDATA[
#tooltip2626 {
background: cornsilk;
border: 1px solid black;
border-radius: 5px;
padding: 5px;
}
]]></style>
</defs>
...
<div id="tooltip2626" display="none" style="position: absolute; display: none;"></div>
<div
id="tooltip2626"
display="none"
style="position: absolute; display: none;" />
...
However I barely understand javascript so it failed too.
I would like to know whether there is a solution to this issue, whether through coding or through other means.