jfree / jfreesvg

A fast, lightweight Java library for creating Scalable Vector Graphics (SVG) output.
http://www.jfree.org/jfreesvg
GNU General Public License v3.0
319 stars 58 forks source link

draw a element can set a id or class? #47

Closed touchmii closed 2 years ago

touchmii commented 2 years ago

like this, can help web identify element name.

jfree commented 2 years ago

This would be better in Discussions.

Anyway, setting the id for an element is supported. Since JFreeSVG targets the Graphics2D API, it makes use of the rendering hints facility (slightly non-obvious, but it has the advantage that your code will still run against other Graphics2D implementations - the hints get ignored, but otherwise the rendering is the same). You can do something like this:

Graphics2D g2 = ... (from wherever you create your Graphics2D instance)
g2.setRenderingHint(SVGHints.KEY_ELEMENT_ID, "XYZ");
g2.fill(new Rectangle2D.Double(1.0, 2.0, 3.0, 4.0));

...and then the rectangle in the SVG output will be tagged with the specified element ID.