Example usage for org.jfree.graphics2d.svg SVGGraphics2D getSVGElement

List of usage examples for org.jfree.graphics2d.svg SVGGraphics2D getSVGElement

Introduction

In this page you can find the example usage for org.jfree.graphics2d.svg SVGGraphics2D getSVGElement.

Prototype

public String getSVGElement() 

Source Link

Document

Returns the SVG element that has been generated by calls to this Graphics2D implementation.

Usage

From source file:org.jfree.graphics2d.demo.SVGBarChartDemo1.java

/**
 * Starting point for the demo.//  ww  w . ja  va2 s .  c o m
 * 
 * @param args  ignored.
 * 
 * @throws IOException 
 */
public static void main(String[] args) throws IOException {
    JFreeChart chart = createChart(createDataset());
    SVGGraphics2D g2 = new SVGGraphics2D(600, 400);
    Rectangle r = new Rectangle(0, 0, 600, 400);
    chart.draw(g2, r);
    File f = new File("SVGBarChartDemo1.svg");
    SVGUtils.writeToSVG(f, g2.getSVGElement());
}

From source file:org.jfree.graphics2d.demo.SVGChartWithAnnotationsDemo1.java

/**
 * Starting point for the demo.// w  ww.j a v  a 2  s . co m
 * 
 * @param args  ignored.
 * 
 * @throws IOException 
 */
public static void main(String[] args) throws IOException {
    JFreeChart chart = createChart(createDataset());
    SVGGraphics2D g2 = new SVGGraphics2D(500, 300);
    Rectangle r = new Rectangle(0, 0, 500, 300);
    chart.draw(g2, r);
    File f = new File("SVGChartWithAnnotationsDemo1.svg");
    SVGUtils.writeToSVG(f, g2.getSVGElement());
}

From source file:org.jfree.graphics2d.demo.SVGPieChartDemo1.java

/**
 * Starting point for the demo./*w  ww .  ja  v a2 s .  c o m*/
 * 
 * @param args  ignored.
 * 
 * @throws IOException 
 */
public static void main(String[] args) throws IOException {
    JFreeChart chart = createChart(createDataset());
    SVGGraphics2D g2 = new SVGGraphics2D(600, 400);
    g2.setRenderingHint(JFreeChart.KEY_SUPPRESS_SHADOW_GENERATION, true);
    Rectangle r = new Rectangle(0, 0, 600, 400);
    chart.draw(g2, r);
    File f = new File("SVGPieChartDemo1.svg");
    SVGUtils.writeToSVG(f, g2.getSVGElement());
}

From source file:org.jfree.graphics2d.demo.ImageTest.java

/**
 * Starting point for the demo.//w w  w  .j a  v a 2  s.c  o m
 * 
 * @param args  ignored.
 * 
 * @throws IOException 
 */
public static void main(String[] args) throws IOException {
    //        BufferedImage image = new BufferedImage(200, 200, 
    //                BufferedImage.TYPE_INT_ARGB);
    //        Graphics2D g2 = image.createGraphics();

    SVGGraphics2D g2 = new SVGGraphics2D(110, 110);
    //drawClipTest(g2);
    //drawGradientPaintTest(g2);
    drawLinearGradientPaintTest(g2);
    //drawOldLinearGradientPaintTest(g2);
    //drawRadialGradientPaintTest(g2);
    //drawArcTest(g2);
    //        ImageIO.write(image, "png", new File("oldlgp-test.png"));
    SVGUtils.writeToSVG(new File("lgp-test.svg"), g2.getSVGElement());
}

From source file:org.jfree.graphics2d.demo.SVGTimeSeriesChartDemo1.java

/**
 * Starting point for the demo.//from www . java 2s  . c  om
 * 
 * @param args  ignored.
 * 
 * @throws IOException 
 */
public static void main(String[] args) throws IOException {
    JFreeChart chart = createChart(createDataset());
    SVGGraphics2D g2 = new SVGGraphics2D(600, 400);
    g2.setRenderingHint(JFreeChart.KEY_SUPPRESS_SHADOW_GENERATION, true);
    Rectangle r = new Rectangle(0, 0, 600, 400);
    chart.draw(g2, r);
    File f = new File("SVGTimeSeriesChartDemo1.svg");
    SVGUtils.writeToSVG(f, g2.getSVGElement());
}

From source file:org.jfree.graphics2d.demo.SwingUIToSVGDemo.java

@Override
public void actionPerformed(ActionEvent e) {
    JComponent c = (JComponent) getContentPane().getComponent(0);
    SVGGraphics2D g2 = new SVGGraphics2D(c.getWidth(), c.getHeight());
    c.paint(g2);/* ww  w  . ja  va2 s  . c om*/
    File f = new File("SwingUIToSVGDemo.svg");
    try {
        SVGUtils.writeToSVG(f, g2.getSVGElement());
    } catch (IOException ex) {
        System.err.println(ex);
    }
}

From source file:gavisualizer.LineChart.java

@Override
public void saveAsSVG(String path, int width, int height) throws IOException {
    // Save image as SVG
    SVGGraphics2D g2 = new SVGGraphics2D(width, height);
    Rectangle r = new Rectangle(0, 0, width, height);
    _chart.draw(g2, r);/*from  www . j a v a  2  s . co m*/
    File f = new File(path);
    SVGUtils.writeToSVG(f, g2.getSVGElement());
}

From source file:keel.Algorithms.UnsupervisedLearning.AssociationRules.Visualization.keelassotiationrulesboxplot.ResultsProccessor.java

public void writeToFile(String outName)
        throws FileNotFoundException, UnsupportedEncodingException, IOException {
    //calcMeans();

    // Create JFreeChart Dataset
    DefaultBoxAndWhiskerCategoryDataset dataset = new DefaultBoxAndWhiskerCategoryDataset();

    HashMap<String, ArrayList<Double>> measuresFirst = algorithmMeasures.entrySet().iterator().next()
            .getValue();//from   w ww  .j  ava2 s . co  m
    for (Map.Entry<String, ArrayList<Double>> measure : measuresFirst.entrySet()) {
        String measureName = measure.getKey();
        //Double measureValue = measure.getValue();
        dataset.clear();

        for (Map.Entry<String, HashMap<String, ArrayList<Double>>> entry : algorithmMeasures.entrySet()) {
            String alg = entry.getKey();
            ArrayList<Double> measureValues = entry.getValue().get(measureName);

            // Parse algorithm name to show it correctly
            String aName = alg.substring(0, alg.length() - 1);
            int startAlgName = aName.lastIndexOf("/");
            aName = aName.substring(startAlgName + 1);

            dataset.add(measureValues, aName, measureName);
        }

        // Tutorial: http://www.java2s.com/Code/Java/Chart/JFreeChartBoxAndWhiskerDemo.htm
        final CategoryAxis xAxis = new CategoryAxis("Algorithm");
        final NumberAxis yAxis = new NumberAxis("Value");
        yAxis.setAutoRangeIncludesZero(false);
        final BoxAndWhiskerRenderer renderer = new BoxAndWhiskerRenderer();

        // Black and White
        int numItems = algorithmMeasures.size();
        for (int i = 0; i < numItems; i++) {
            Color color = Color.DARK_GRAY;
            if (i % 2 == 1) {
                color = Color.LIGHT_GRAY;
            }
            renderer.setSeriesPaint(i, color);
            renderer.setSeriesOutlinePaint(i, Color.BLACK);
        }

        renderer.setMeanVisible(false);
        renderer.setFillBox(false);
        renderer.setToolTipGenerator(new BoxAndWhiskerToolTipGenerator());
        final CategoryPlot plot = new CategoryPlot(dataset, xAxis, yAxis, renderer);

        Font font = new Font("SansSerif", Font.BOLD, 10);
        //ChartFactory.setChartTheme(StandardChartTheme.createLegacyTheme());
        JFreeChart jchart = new JFreeChart("Assotiation Rules Measures - BoxPlot", font, plot, true);
        //StandardChartTheme.createLegacyTheme().apply(jchart);

        int width = 640 * 2; /* Width of the image */
        int height = 480 * 2; /* Height of the image */

        // JPEG
        File chart = new File(outName + "_" + measureName + "_boxplot.jpg");
        ChartUtilities.saveChartAsJPEG(chart, jchart, width, height);

        // SVG
        SVGGraphics2D g2 = new SVGGraphics2D(width, height);
        Rectangle r = new Rectangle(0, 0, width, height);
        jchart.draw(g2, r);
        File BarChartSVG = new File(outName + "_" + measureName + "_boxplot.svg");
        SVGUtils.writeToSVG(BarChartSVG, g2.getSVGElement());
    }

}

From source file:com.esofthead.mycollab.ui.chart.JFreeChartWrapper.java

@Override
public Resource getSource() {
    if (res == null) {
        StreamSource streamSource = new StreamResource.StreamSource() {
            private ByteArrayInputStream bytestream = null;

            ByteArrayInputStream getByteStream() {
                if (chart != null && bytestream == null) {
                    int width = getGraphWidth();
                    int height = getGraphHeight();

                    if (mode == RenderingMode.SVG) {
                        // Create an instance of the SVG Generator
                        SVGGraphics2D svgGenerator = new SVGGraphics2D(width, height);

                        // draw the chart in the SVG generator
                        chart.draw(svgGenerator, new Rectangle(width, height));
                        // create an xml string in svg format from the drawing
                        String drawingSVG = svgGenerator.getSVGElement();
                        return new ByteArrayInputStream(drawingSVG.getBytes(StandardCharsets.UTF_8));
                    } else {
                        // Draw png to bytestream
                        try {
                            byte[] bytes = ChartUtilities.encodeAsPNG(chart.createBufferedImage(width, height));
                            bytestream = new ByteArrayInputStream(bytes);
                        } catch (Exception e) {
                            log.error("Error while generating PNG chart", e);
                        }/*from w w  w .  j  ava  2 s.com*/

                    }

                } else {
                    bytestream.reset();
                }
                return bytestream;
            }

            @Override
            public InputStream getStream() {
                return getByteStream();
            }
        };

        res = new StreamResource(streamSource, String.format("graph%d", System.currentTimeMillis())) {

            @Override
            public int getBufferSize() {
                if (getStreamSource().getStream() != null) {
                    try {
                        return getStreamSource().getStream().available();
                    } catch (IOException e) {
                        log.warn("Error while get stream info", e);
                        return 0;
                    }
                } else {
                    return 0;
                }
            }

            @Override
            public long getCacheTime() {
                return 0;
            }

            @Override
            public String getFilename() {
                if (mode == RenderingMode.PNG) {
                    return super.getFilename() + ".png";
                } else {
                    return super.getFilename() + (gzipEnabled ? ".svgz" : ".svg");
                }
            }

            @Override
            public DownloadStream getStream() {
                DownloadStream downloadStream = new DownloadStream(getStreamSource().getStream(), getMIMEType(),
                        getFilename());
                if (gzipEnabled && mode == RenderingMode.SVG) {
                    downloadStream.setParameter("Content-Encoding", "gzip");
                }
                return downloadStream;
            }

            @Override
            public String getMIMEType() {
                if (mode == RenderingMode.PNG) {
                    return "image/png";
                } else {
                    return "image/svg+xml";
                }
            }
        };
    }
    return res;
}

From source file:keel.Algorithms.UnsupervisedLearning.AssociationRules.Visualization.keelassotiationrulesbarchart.ResultsProccessor.java

public void writeToFile(String outName)
        throws FileNotFoundException, UnsupportedEncodingException, IOException {
    calcMeans();//from   ww w. j  av  a2  s  . c o m
    calcAvgRulesBySeed();

    // Create JFreeChart Dataset
    DefaultCategoryDataset dataset = new DefaultCategoryDataset();

    HashMap<String, Double> measuresFirst = algorithmMeasures.entrySet().iterator().next().getValue();
    for (Map.Entry<String, Double> measure : measuresFirst.entrySet()) {
        String measureName = measure.getKey();
        //Double measureValue = measure.getValue();
        dataset.clear();

        for (Map.Entry<String, HashMap<String, Double>> entry : algorithmMeasures.entrySet()) {
            String alg = entry.getKey();
            Double measureValue = entry.getValue().get(measureName);

            // Parse algorithm name to show it correctly
            String aName = alg.substring(0, alg.length() - 1);
            int startAlgName = aName.lastIndexOf("/");
            aName = aName.substring(startAlgName + 1);

            dataset.addValue(measureValue, aName, measureName);

            ChartFactory.setChartTheme(StandardChartTheme.createLegacyTheme());
            JFreeChart barChart = ChartFactory.createBarChart("Assotiation Rules Measures", measureName,
                    measureName, dataset, PlotOrientation.VERTICAL, true, true, false);
            StandardChartTheme.createLegacyTheme().apply(barChart);

            CategoryItemRenderer renderer = barChart.getCategoryPlot().getRenderer();

            // Black and White
            int numItems = algorithmMeasures.size();
            for (int i = 0; i < numItems; i++) {
                Color color = Color.DARK_GRAY;
                if (i % 2 == 1) {
                    color = Color.LIGHT_GRAY;
                }
                renderer.setSeriesPaint(i, color);
                renderer.setSeriesOutlinePaint(i, Color.BLACK);
            }

            int width = 640 * 2; /* Width of the image */
            int height = 480 * 2; /* Height of the image */

            // JPEG
            File BarChart = new File(outName + "_" + measureName + "_barchart.jpg");
            ChartUtilities.saveChartAsJPEG(BarChart, barChart, width, height);

            // SVG
            SVGGraphics2D g2 = new SVGGraphics2D(width, height);
            Rectangle r = new Rectangle(0, 0, width, height);
            barChart.draw(g2, r);
            File BarChartSVG = new File(outName + "_" + measureName + "_barchart.svg");
            SVGUtils.writeToSVG(BarChartSVG, g2.getSVGElement());
        }
    }
    /*
    for (Map.Entry<String, HashMap<String, Double>> entry : algorithmMeasures.entrySet())
    {
    String alg = entry.getKey();
    HashMap<String, Double> measures = entry.getValue();
            
    for (Map.Entry<String, Double> entry1 : measures.entrySet())
    {
        String measureName = entry1.getKey();
        Double measureValue = entry1.getValue();
                
        dataset.addValue(measureValue, alg, measureName);
    }
    }
        */

}