Package elki.visualization.svg
Class SVGPlot
- java.lang.Object
-
- elki.visualization.svg.SVGPlot
-
- Direct Known Subclasses:
VisualizationPlot
public class SVGPlot extends java.lang.Object
Base class for SVG plots. Provides some basic functionality such as element creation, axis plotting, markers and number formatting for SVG.- Since:
- 0.2
- Author:
- Erich Schubert
-
-
Field Summary
Fields Modifier and Type Field Description private static org.w3c.dom.DOMImplementation
BATIK_DOM
Batik DOM implementation.private static java.lang.String[]
BATIK_DOMS
DOM implementations to try.private CSSClassManager
cssman
CSS class managerstatic float
DEFAULT_QUALITY
Default JPEG quality settingprivate org.w3c.dom.Element
defs
Definitions element of the document.private boolean
disableInteractions
Flag whether Batik interactions should be disabled.private org.w3c.dom.svg.SVGDocument
document
SVG document we plot to.static java.lang.String
NO_EXPORT_ATTRIBUTE
Attribute to block export of element.private java.util.HashMap<java.lang.String,java.lang.ref.WeakReference<org.w3c.dom.Element>>
objWithId
Manage objects with an id.private org.w3c.dom.Element
root
Root element of the document.private UpdateRunner
runner
Registers changes of this SVGPlot.private org.w3c.dom.Element
style
Primary style information
-
Constructor Summary
Constructors Constructor Description SVGPlot()
Create a new plotting document.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description void
addCSSClassOrLogError(CSSClass cls)
Convenience method to add a CSS class or log an error.protected org.w3c.dom.svg.SVGDocument
cloneDocument()
Clone the SVGPlot document for transcoding.void
dispose()
Clean up the plot.void
dumpDebugFile()
Dump the SVG plot to a debug file.org.w3c.dom.svg.SVGPoint
elementCoordinatesFromEvent(org.w3c.dom.Element tag, org.w3c.dom.events.Event evt)
Convert screen coordinates to element coordinates.protected java.util.Collection<java.lang.String>
getAllIds()
Get all used DOM Ids in this plot.CSSClassManager
getCSSClassManager()
Get the plots CSS class manager.org.w3c.dom.Element
getDefs()
Getter for definitions sectionboolean
getDisableInteractions()
Get Batik disable default interactions flag.org.w3c.dom.svg.SVGDocument
getDocument()
Retrieve the SVG document.static org.w3c.dom.DOMImplementation
getDomImpl()
Get a suitable SVG DOM implementation from Batik 1.7 or 1.8.org.w3c.dom.Element
getIdElement(java.lang.String id)
Get an element by its id.org.w3c.dom.Element
getRoot()
Getter for root element.java.awt.image.BufferedImage
makeAWTImage(int width, int height)
Convert the SVG to a thumbnail image.void
putIdElement(java.lang.String id, org.w3c.dom.Element obj)
Add an object id.void
saveAsANY(java.nio.file.Path file, int width, int height, float quality)
Save a file trying to auto-guess the file type.void
saveAsEPS(java.nio.file.Path file)
Transcode file to EPS.void
saveAsJPEG(java.nio.file.Path file, int width, int height)
Transcode file to JPEG.void
saveAsJPEG(java.nio.file.Path file, int width, int height, float quality)
Transcode file to JPEG.void
saveAsPDF(java.nio.file.Path file)
Transcode file to PDF.void
saveAsPNG(java.nio.file.Path file, int width, int height)
Transcode file to PNG.void
saveAsPS(java.nio.file.Path file)
Transcode file to PS.void
saveAsSVG(java.nio.file.Path file)
Save document into a SVG file.void
scheduleUpdate(java.lang.Runnable runnable)
Schedule an update.void
setDisableInteractions(boolean disable)
Disable Batik predefined interactions.org.w3c.dom.Element
svgCircle(double cx, double cy, double r)
Create a SVG circleorg.w3c.dom.Element
svgElement(java.lang.String name)
Create a SVG element in the SVG namespace.org.w3c.dom.Element
svgElement(java.lang.String name, java.lang.String cssclass)
Create a SVG element in the SVG namespace.org.w3c.dom.Element
svgLine(double x1, double y1, double x2, double y2)
Create a SVG line elementorg.w3c.dom.Element
svgRect(double x, double y, double w, double h)
Create a SVG rectangleorg.w3c.dom.Element
svgText(double x, double y, java.lang.String text)
Create a SVG text element.void
synchronizeWith(UpdateSynchronizer sync)
Assign an update synchronizer.protected void
transcode(java.nio.file.Path file, org.apache.batik.transcoder.Transcoder transcoder)
Transcode a document into a file using the given transcoder.void
unsynchronizeWith(UpdateSynchronizer sync)
Detach from synchronization.void
updateStyleElement()
Update style element - invoke this appropriately after any change to the CSS styles.
-
-
-
Field Detail
-
DEFAULT_QUALITY
public static final float DEFAULT_QUALITY
Default JPEG quality setting- See Also:
- Constant Field Values
-
NO_EXPORT_ATTRIBUTE
public static final java.lang.String NO_EXPORT_ATTRIBUTE
Attribute to block export of element.- See Also:
- Constant Field Values
-
BATIK_DOM
private static final org.w3c.dom.DOMImplementation BATIK_DOM
Batik DOM implementation.
-
BATIK_DOMS
private static final java.lang.String[] BATIK_DOMS
DOM implementations to try.
-
document
private org.w3c.dom.svg.SVGDocument document
SVG document we plot to.
-
root
private org.w3c.dom.Element root
Root element of the document.
-
defs
private org.w3c.dom.Element defs
Definitions element of the document.
-
style
private org.w3c.dom.Element style
Primary style information
-
cssman
private CSSClassManager cssman
CSS class manager
-
objWithId
private java.util.HashMap<java.lang.String,java.lang.ref.WeakReference<org.w3c.dom.Element>> objWithId
Manage objects with an id.
-
runner
private UpdateRunner runner
Registers changes of this SVGPlot.
-
disableInteractions
private boolean disableInteractions
Flag whether Batik interactions should be disabled.
-
-
Method Detail
-
getDomImpl
public static org.w3c.dom.DOMImplementation getDomImpl()
Get a suitable SVG DOM implementation from Batik 1.7 or 1.8.- Returns:
- DOM implementation
-
dispose
public void dispose()
Clean up the plot.
-
svgElement
public org.w3c.dom.Element svgElement(java.lang.String name)
Create a SVG element in the SVG namespace. Non-static version.- Parameters:
name
- node name- Returns:
- new SVG element.
-
svgElement
public org.w3c.dom.Element svgElement(java.lang.String name, java.lang.String cssclass)
Create a SVG element in the SVG namespace. Non-static version.- Parameters:
name
- node namecssclass
- CSS class- Returns:
- new SVG element.
-
svgRect
public org.w3c.dom.Element svgRect(double x, double y, double w, double h)
Create a SVG rectangle- Parameters:
x
- X coordinatey
- Y coordinatew
- Widthh
- Height- Returns:
- new element
-
svgCircle
public org.w3c.dom.Element svgCircle(double cx, double cy, double r)
Create a SVG circle- Parameters:
cx
- center Xcy
- center Yr
- radius- Returns:
- new element
-
svgLine
public org.w3c.dom.Element svgLine(double x1, double y1, double x2, double y2)
Create a SVG line element- Parameters:
x1
- first point xy1
- first point yx2
- second point xy2
- second point y- Returns:
- new element
-
svgText
public org.w3c.dom.Element svgText(double x, double y, java.lang.String text)
Create a SVG text element.- Parameters:
x
- first point xy
- first point ytext
- Content of text element.- Returns:
- New text element.
-
elementCoordinatesFromEvent
public org.w3c.dom.svg.SVGPoint elementCoordinatesFromEvent(org.w3c.dom.Element tag, org.w3c.dom.events.Event evt)
Convert screen coordinates to element coordinates.- Parameters:
tag
- Element to convert the coordinates forevt
- Event object- Returns:
- Coordinates
-
getDocument
public org.w3c.dom.svg.SVGDocument getDocument()
Retrieve the SVG document.- Returns:
- resulting document.
-
getRoot
public org.w3c.dom.Element getRoot()
Getter for root element.- Returns:
- DOM element
-
getDefs
public org.w3c.dom.Element getDefs()
Getter for definitions section- Returns:
- DOM element
-
getCSSClassManager
public CSSClassManager getCSSClassManager()
Get the plots CSS class manager.Note that you need to invoke
updateStyleElement()
to make changes take effect.- Returns:
- CSS class manager.
-
addCSSClassOrLogError
public void addCSSClassOrLogError(CSSClass cls)
Convenience method to add a CSS class or log an error.- Parameters:
cls
- CSS class to add.
-
updateStyleElement
public void updateStyleElement()
Update style element - invoke this appropriately after any change to the CSS styles.
-
saveAsSVG
public void saveAsSVG(java.nio.file.Path file) throws java.io.IOException, javax.xml.transform.TransformerFactoryConfigurationError, javax.xml.transform.TransformerException
Save document into a SVG file.References PNG images from the temporary files will be inlined automatically.
- Parameters:
file
- Output filename- Throws:
java.io.IOException
- On write errorsjavax.xml.transform.TransformerFactoryConfigurationError
- Transformation errorjavax.xml.transform.TransformerException
- Transformation error
-
transcode
protected void transcode(java.nio.file.Path file, org.apache.batik.transcoder.Transcoder transcoder) throws java.io.IOException, org.apache.batik.transcoder.TranscoderException
Transcode a document into a file using the given transcoder.- Parameters:
file
- Output filetranscoder
- Transcoder to use- Throws:
java.io.IOException
- On write errorsorg.apache.batik.transcoder.TranscoderException
- On input/parsing errors
-
cloneDocument
protected org.w3c.dom.svg.SVGDocument cloneDocument()
Clone the SVGPlot document for transcoding.This will usually be necessary for exporting the SVG document if it is currently being displayed: otherwise, we break the Batik rendering trees. (Discovered by Simon).
- Returns:
- cloned document
-
saveAsPDF
public void saveAsPDF(java.nio.file.Path file) throws java.io.IOException, org.apache.batik.transcoder.TranscoderException, java.lang.ClassNotFoundException
Transcode file to PDF.- Parameters:
file
- Output filename- Throws:
java.io.IOException
- On write errorsorg.apache.batik.transcoder.TranscoderException
- On input/parsing errors.java.lang.ClassNotFoundException
- PDF transcoder not installed
-
saveAsPS
public void saveAsPS(java.nio.file.Path file) throws java.io.IOException, org.apache.batik.transcoder.TranscoderException, java.lang.ClassNotFoundException
Transcode file to PS.- Parameters:
file
- Output filename- Throws:
java.io.IOException
- On write errorsorg.apache.batik.transcoder.TranscoderException
- On input/parsing errors.java.lang.ClassNotFoundException
- PS transcoder not installed
-
saveAsEPS
public void saveAsEPS(java.nio.file.Path file) throws java.io.IOException, org.apache.batik.transcoder.TranscoderException, java.lang.ClassNotFoundException
Transcode file to EPS.- Parameters:
file
- Output filename- Throws:
java.io.IOException
- On write errorsorg.apache.batik.transcoder.TranscoderException
- On input/parsing errors.java.lang.ClassNotFoundException
- EPS transcoder not installed
-
saveAsPNG
public void saveAsPNG(java.nio.file.Path file, int width, int height) throws java.io.IOException, org.apache.batik.transcoder.TranscoderException
Transcode file to PNG.- Parameters:
file
- Output filenamewidth
- Widthheight
- Height- Throws:
java.io.IOException
- On write errorsorg.apache.batik.transcoder.TranscoderException
- On input/parsing errors.
-
saveAsJPEG
public void saveAsJPEG(java.nio.file.Path file, int width, int height, float quality) throws java.io.IOException, org.apache.batik.transcoder.TranscoderException
Transcode file to JPEG.- Parameters:
file
- Output filenamewidth
- Widthheight
- Heightquality
- JPEG quality setting, between 0.0 and 1.0- Throws:
java.io.IOException
- On write errorsorg.apache.batik.transcoder.TranscoderException
- On input/parsing errors.
-
saveAsJPEG
public void saveAsJPEG(java.nio.file.Path file, int width, int height) throws java.io.IOException, org.apache.batik.transcoder.TranscoderException
Transcode file to JPEG.- Parameters:
file
- Output filenamewidth
- Widthheight
- Height- Throws:
java.io.IOException
- On write errorsorg.apache.batik.transcoder.TranscoderException
- On input/parsing errors.
-
saveAsANY
public void saveAsANY(java.nio.file.Path file, int width, int height, float quality) throws java.io.IOException, org.apache.batik.transcoder.TranscoderException, javax.xml.transform.TransformerFactoryConfigurationError, javax.xml.transform.TransformerException, java.lang.ClassNotFoundException
Save a file trying to auto-guess the file type.- Parameters:
file
- File namewidth
- Width (for pixel formats)height
- Height (for pixel formats)quality
- Quality (for lossy compression)- Throws:
java.io.IOException
- on file write errors or unrecognized file extensionsorg.apache.batik.transcoder.TranscoderException
- on transcoding errorsjavax.xml.transform.TransformerFactoryConfigurationError
- on transcoding errorsjavax.xml.transform.TransformerException
- on transcoding errorsjava.lang.ClassNotFoundException
- when the transcoder was not installed
-
makeAWTImage
public java.awt.image.BufferedImage makeAWTImage(int width, int height) throws org.apache.batik.transcoder.TranscoderException
Convert the SVG to a thumbnail image.- Parameters:
width
- Width of thumbnailheight
- Height of thumbnail- Returns:
- Buffered image
- Throws:
org.apache.batik.transcoder.TranscoderException
-
dumpDebugFile
public void dumpDebugFile()
Dump the SVG plot to a debug file.
-
putIdElement
public void putIdElement(java.lang.String id, org.w3c.dom.Element obj)
Add an object id.- Parameters:
id
- IDobj
- Element
-
getIdElement
public org.w3c.dom.Element getIdElement(java.lang.String id)
Get an element by its id.- Parameters:
id
- ID- Returns:
- Element
-
getAllIds
protected java.util.Collection<java.lang.String> getAllIds()
Get all used DOM Ids in this plot.- Returns:
- Collection of DOM element IDs.
-
scheduleUpdate
public void scheduleUpdate(java.lang.Runnable runnable)
Schedule an update.- Parameters:
runnable
- Runnable to schedule
-
synchronizeWith
public void synchronizeWith(UpdateSynchronizer sync)
Assign an update synchronizer.- Parameters:
sync
- Update synchronizer
-
unsynchronizeWith
public void unsynchronizeWith(UpdateSynchronizer sync)
Detach from synchronization.- Parameters:
sync
- Update synchronizer to detach from.
-
getDisableInteractions
public boolean getDisableInteractions()
Get Batik disable default interactions flag.- Returns:
- true when Batik default interactions are disabled
-
setDisableInteractions
public void setDisableInteractions(boolean disable)
Disable Batik predefined interactions.- Parameters:
disable
- Flag
-
-