Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best Way To Place SVG Content Within HTML

Tags:

From my research, i understand there are three ways to place an svg file inside HTML:

using embed:

<embed src="plot1.svg" width="500" height="320" type="image/svg+xml" /> 


using object:

<object data="plot1.svg" width="500" height="320" type="image/svg+xml" /> 


using iframe:

<iframe src="plot1.svg" width="500" height="320"> </iframe> 


I've experimented with all three on a test rig (django built-in dev server, rendering the pages in Firefox 3.6). Under this obviously sterile environment, i haven't noticed any difference between the three--w/r/t performance or resolution.

My Question is whether one of these is better than the other two, and if so, which one. The answer might of course depend on the facts, which i've tried to limit to what's relevant:

We frequently display data (e.g, time series) on our website, usually created in response to some user action; based on that user action, a call is made to a database, the data returned is crunched and sent to the plotting engine, which renders a static image which is then embedded in the page--very standard stuff.

This works fine, but i would like to add some interactive features to these charts (e.g., tooltips, hyperlinked axis labels, highlighting a group of points w/in a plot). Some of the charts are fairly sophisticated (e.g., multi-panel conditioning) and i have not found a javascript chart library that includes these features. I finally settled on using the same plotting library (Lattice in R) but rendering each chart in svg and then adding the user-interaction features in a post-processing step, which consists essentially of javascript functions which manipulate the XML directly.

like image 238
doug Avatar asked Sep 29 '10 09:09

doug


People also ask

Can you embed SVG in HTML?

You can embed SVG elements directly into your HTML pages.

Is it good to use SVG in HTML?

7 Reasons Why You Should Be Using Scalable Vector GraphicsThey are SEO friendly allowing you to add keywords, descriptions and links directly to the markup. SVGs can be embedded into the HTML which means they can be cached, edited directly using CSS and indexed for greater accessibility. They are future proof.

What is SVG can we use it inside an HTML document?

SVG is a language for describing 2D graphics in XML. Canvas draws 2D graphics, on the fly (with a JavaScript). SVG is XML based, which means that every element is available within the SVG DOM. You can attach JavaScript event handlers for an element.

How do I move SVG to HTML?

As mentioned in the other comment, the transform attribute on the g element is what you want. Use transform="translate(x,y)" to move the g around and things within the g will move in relation to the g .


1 Answers

<embed> I would generally avoid. It's deprecated in HTML4, doesn't allow proper fallback content, and has had a selection of problems in IE.

<object> will allow you to include fallback HTML content for browsers without SVG support. <iframe> will fall back to prompting you to download the .svg file. Which of those is best probably depends on the application.

The other alternative, for modern browsers (including IE from version 9) is to serve your web page as application/xhtml+html and include the SVG elements in the page itself.

like image 167
bobince Avatar answered Sep 29 '22 00:09

bobince