Reaching for the Web (SVG generation)

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
mhilpert
Posts: 497
Joined: Wed Apr 02, 2003 1:57 pm
Location: Germany

Reaching for the Web (SVG generation)

Post by mhilpert » Tue Jul 16, 2013 3:48 pm

Related to your article "Reaching for the Web" (http://www.jroller.com/dgilbert/category/Java): this sounds interesting - if it can replace the Batik SVGGenerator. I have just a small method converting charts to SVG:

Code: Select all

    /**
     * Save chart as SVG file.
     * Required libs: Apache Batik (batik-svggen.jar, batik-dom.jar, dom.jar).
     * 
     * @param chart JFreeChart to save.
     * @param fileName Name of file to save chart in.
     * @param width Width of chart graphic.
     * @param height Height of chart graphic.
     * @return Final file name used.
     * @throws IOException if failed.
     */
    static public final String saveChartToSVG(final JFreeChart chart, String fileName, final int width, final int height) throws IOException {
        String result = null;
        
        if (chart != null) {
            if (fileName == null) {
                final String chartTitle = chart.getTitle().getText();
                if (chartTitle != null) {
                    fileName = chartTitle;
                } else {
                    fileName = "chart";
                }
            }
            result = fileName + ".svg";
            
            final DOMImplementation domImpl = GenericDOMImplementation.getDOMImplementation();
            final Document document = domImpl.createDocument(null, "svg", null);
            final SVGGraphics2D svgGenerator = new SVGGraphics2D(document);
            final boolean antiAlias = isAntiAliasOn(chart);
            final RenderingHints rh = chart.getRenderingHints();

            if (antiAlias) {
                rh.put(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
                rh.put(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_OFF); //fix
            } else {
                rh.put(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_OFF);
                rh.put(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON); //fix
            }
            chart.draw(svgGenerator, new Rectangle2D.Double(0, 0, width, height), new ChartRenderingInfo());
            
            //undo anti-aliasing bugfix settings for chart to use correct settings:
            if (antiAlias) {
                rh.put(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
                rh.put(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
            } else {
                rh.put(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_OFF);
                rh.put(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_OFF);
            }            
            
            final boolean useCSS = true;
            Writer out = null;
            try {
                out = new OutputStreamWriter(new BufferedOutputStream(new FileOutputStream(new File(result), false)),"iso-8859-1"); //UTF-8
                svgGenerator.stream(out, useCSS);
            } finally {
                svgGenerator.dispose();
                IOUtils.close(out);
            }
        }//else: input unavailable
        
        return result;
    }//saveChartToSVG()
Of course, it woul dbe nice to have a built in SVG genartor in jFreeChart. But I hope it would also support misc rendering hints to switch on/off anti-aliasing and other rendering hints. So, the upcoming JFreeChart will have a ChartUtilities.saveChartAsSVG()?

By the way: SVG is not only good for the web: it's also good for clean charts in static PDF documents!
Java 11, JFreeChart 1.0.15, JFreeSVG 4.0

Locked