Class 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 manager
      static float DEFAULT_QUALITY
      Default JPEG quality setting
      private 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 section
      boolean 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 circle
      org.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 element
      org.w3c.dom.Element svgRect​(double x, double y, double w, double h)
      Create a SVG rectangle
      org.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.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • 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
      • 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.
    • Constructor Detail

      • SVGPlot

        public SVGPlot()
        Create a new plotting document.
    • 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 name
        cssclass - 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 coordinate
        y - Y coordinate
        w - Width
        h - Height
        Returns:
        new element
      • svgCircle

        public org.w3c.dom.Element svgCircle​(double cx,
                                             double cy,
                                             double r)
        Create a SVG circle
        Parameters:
        cx - center X
        cy - center Y
        r - 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 x
        y1 - first point y
        x2 - second point x
        y2 - 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 x
        y - first point y
        text - 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 for
        evt - 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 errors
        javax.xml.transform.TransformerFactoryConfigurationError - Transformation error
        javax.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 file
        transcoder - Transcoder to use
        Throws:
        java.io.IOException - On write errors
        org.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 errors
        org.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 errors
        org.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 errors
        org.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 filename
        width - Width
        height - Height
        Throws:
        java.io.IOException - On write errors
        org.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 filename
        width - Width
        height - Height
        quality - JPEG quality setting, between 0.0 and 1.0
        Throws:
        java.io.IOException - On write errors
        org.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 filename
        width - Width
        height - Height
        Throws:
        java.io.IOException - On write errors
        org.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 name
        width - 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 extensions
        org.apache.batik.transcoder.TranscoderException - on transcoding errors
        javax.xml.transform.TransformerFactoryConfigurationError - on transcoding errors
        javax.xml.transform.TransformerException - on transcoding errors
        java.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 thumbnail
        height - 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 - ID
        obj - 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