score:17
You can use the title element as Phrogz indicated. There are also some good tooltips like jQuery's Tipsy http://onehackoranother.com/projects/jquery/tipsy/ (which can be used to replace all title elements), Bob Monteverde's nvd3 or even the Twitter's tooltip from their Bootstrap http://twitter.github.com/bootstrap/
score:0
I use heroicons for the project I am working on. (This is JSX format) I will handle the tooltip issue with this code.
<svg className="h-6 w-6">
<title>{reasons.join(" ")}</title>
<QuestionMarkCircleIcon className={style} />
</svg>
score:4
I always go with the generic css title with my setup. I'm just building analytics for my blog admin page. I don't need anything fancy. Here's some code...
let comps = g.selectAll('.myClass')
.data(data)
.enter()
.append('rect')
...styling...
...transitions...
...whatever...
g.selectAll('.myClass')
.append('svg:title')
.text((d, i) => d.name + '-' + i);
And a screenshot of chrome...
score:6
I came up with something using HTML + CSS only. Hope it works for you
.mzhrttltp {
position: relative;
display: inline-block;
}
.mzhrttltp .hrttltptxt {
visibility: hidden;
width: 120px;
background-color: #040505;
font-size:13px;color:#fff;font-family:IranYekanWeb;
text-align: center;
border-radius: 3px;
padding: 4px 0;
position: absolute;
z-index: 1;
top: 105%;
left: 50%;
margin-left: -60px;
}
.mzhrttltp .hrttltptxt::after {
content: "";
position: absolute;
bottom: 100%;
left: 50%;
margin-left: -5px;
border-width: 5px;
border-style: solid;
border-color: transparent transparent #040505 transparent;
}
.mzhrttltp:hover .hrttltptxt {
visibility: visible;
}
<div class="mzhrttltp"><svg xmlns="http://www.w3.org/2000/svg" width="100" height="100" viewBox="0 0 24 24" fill="none" stroke="#e2062c" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round" class="feather feather-heart"><path d="M20.84 4.61a5.5 5.5 0 0 0-7.78 0L12 5.67l-1.06-1.06a5.5 5.5 0 0 0-7.78 7.78l1.06 1.06L12 21.23l7.78-7.78 1.06-1.06a5.5 5.5 0 0 0 0-7.78z"></path></svg><div class="hrttltptxt">علاقه‌مندی‌ها</div></div>
score:8
On svg, the right way to write the title
<svg>
<title id="unique-id">Checkout</title>
</svg>
check here for more details https://css-tricks.com/svg-title-vs-html-title-attribute/
score:62
The only good way I found was to use Javascript to move a tooltip <div>
around. Obviously this only works if you have SVG inside an HTML document - not standalone. And it requires Javascript.
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";
}
#tooltip {
background: cornsilk;
border: 1px solid black;
border-radius: 5px;
padding: 5px;
}
<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>
score:198
Can you use simply the SVG <title>
element and the default browser rendering it conveys? (Note: this is not the same as the title
attribute you can use on div/img/spans in html, it needs to be a child element named title
)
rect {
width: 100%;
height: 100%;
fill: #69c;
stroke: #069;
stroke-width: 5px;
opacity: 0.5
}
<p>Mouseover the rect to see the tooltip on supporting browsers.</p>
<svg xmlns="http://www.w3.org/2000/svg">
<rect>
<title>Hello, World!</title>
</rect>
</svg>
Alternatively, if you really want to show HTML in your SVG, you can embed HTML directly:
rect {
width: 100%;
height: 100%;
fill: #69c;
stroke: #069;
stroke-width: 5px;
opacity: 0.5
}
foreignObject {
width: 100%;
}
svg div {
text-align: center;
line-height: 150px;
}
<svg xmlns="http://www.w3.org/2000/svg">
<rect/>
<foreignObject>
<body xmlns="http://www.w3.org/1999/xhtml">
<div>
Hello, <b>World</b>!
</div>
</body>
</foreignObject>
</svg>
…but then you'd need JS to turn the display on and off. As shown above, one way to make the label appear at the right spot is to wrap the rect and HTML in the same <g>
that positions them both together.
To use JS to find where an SVG element is on screen, you can use getBoundingClientRect()
, e.g. http://phrogz.net/svg/html_location_in_svg_in_html.xhtml
Source: stackoverflow.com
Related Query
- How to add a tooltip to an svg graphic?
- How to add tooltip to an svg graphics element using javascript
- How to add a tooltip to existing svg circles?
- How to add an image to an svg container using D3.js
- How to add border/outline/stroke to SVG elements in CSS?
- How do I show a bootstrap tooltip with an SVG object?
- How to show a tooltip with value when mouseover a svg line graph using d3.js?
- How to add filled sections to SVG circles using d3.js
- How to add external svg file (by D3.js) to Leaflet map
- How can I add a click event to show a tooltip in an NVD3 Pie Chart?
- how to add dragend event on svg element
- how to add tooltip for rectangles in d3.js reading from json
- How to add a list of node's name with tooltip in sankey
- How to add links from CSV file to SVG elements generated with D3?
- How can I add an external group to an existing svg with d3.js?
- How to add a (mouse)event to a XMLDocument of a SVG
- How do I add a transform attribute to svg subgroups in d3.js?
- How to add svg rectangles as background behind axis tick labels in d3js if ticks are inside chart
- How to add the data in the tooltip after the dataset switching
- How to add back to DOM an existing D3 SVG chart?
- How to add text to svg circle in d3
- svg how to add a border on a svg symbol?
- how to add tooltip bar chart d3.js
- How to add tooltip to line chart written in typescript and angular
- How to add multiple images to svg in D3 (quantity based on value in dataset)
- how to add tooltip for donut chart in loop using d3js
- How to add a tooltip that shows a bar chart onto a d3.js map
- How to add SVG as an image+xml in d3 version 4?
- How to show tooltip for an SVG element that is inside a zoomable SVG rectangle?
- How to add a tooltip to D3.js subplot
More Query from same tag
- How to specify a data column as the property used in a line generator?
- D3 scale not mapping colors to values
- Download C3 chart as pdf as given download option in D3 graphs
- modify circle elements by event listener on text matching id of circle
- Simple D3.js bar chart : group "g" position and dimensions messed up
- D3 circle pack: html in text attribute
- Trying to incorporate svg circle within a d3 donut
- d3 synchronizing 2 separate zoom behaviors
- Text value in axis ticks using scaleBand()
- d3js timeline -- on scrub day generate a clean granular view for date and time
- Scale y-tick labels in DC.js chart (D3.js underneath)
- How to draw grid lines fir every ticks?
- Add a div inside the text tag
- Populating a Javascript array using MYSQL and JSP
- GeoJSON in D3: Paths aren't being bound to data
- Distinguishing click and double-click in D3 Version 4
- D3 - tree nodes not built from external JSON file
- How to add icons to D3 axis
- How do I select an SVG inside an `object` tag?
- Why does d3 date axis fail in Firefox for some particular date domains?
- d3 tooltips not showing up
- Multiline chart x-axis ticks not aligned with data points
- Sized Pie Charts
- D3 Table Filter in R Shiny update SQL server
- D3.JS: Multi-series line chart, show tooltip for all lines at date?
- How does each function and this keyword work together in d3?[chaining transitions]
- Why are my SVG foreign object text not showing up?
- Set different diameters to graph nodes in javascript d3
- Get the data for the entering selection in d3.js
- Only a part of the y axis is showing in my graph