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

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

Introduction

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

Prototype

public SVGGraphics2D(int width, int height) 

Source Link

Document

Creates a new instance with the specified width and height.

Usage

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

/**
 * Starting point for the demo./*w ww .j av  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(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.ImageTest.java

/**
 * Starting point for the demo.//from  w  ww  .  ja va 2s  .  co 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.SVGPieChartDemo1.java

/**
 * Starting point for the demo.//from  ww w  .java  2 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.SVGChartWithAnnotationsDemo1.java

/**
 * Starting point for the demo.// w w  w.j a v  a 2s  .com
 * 
 * @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.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);//from  w ww .ja va  2s  .  c o  m
    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  w w  w.  j  a  v a2  s . co m
    File f = new File(path);
    SVGUtils.writeToSVG(f, g2.getSVGElement());
}

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

/**
 * Starting point for the demo.//from ww w.j  ava 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(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: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  ww w  . ja  va2  s  . c  om*/

                    }

                } 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:fr.amap.commons.javafx.chart.ChartViewer.java

/**
* A handler for the export to SVG option in the context menu.
*//*from   www .  j a  v  a 2  s  .  co  m*/
private void handleExportToSVG() {
    FileChooser fileChooser = new FileChooser();
    fileChooser.setTitle("Export to SVG");
    fileChooser.setSelectedExtensionFilter(
            new FileChooser.ExtensionFilter("Scalable Vector Graphics (SVG)", "svg"));
    File file = fileChooser.showSaveDialog(stage);
    if (file != null) {

        CanvasPositionsAndSize canvasPositionAndSize = getCanvasPositionAndSize();

        SVGGraphics2D sVGGraphics2D = new SVGGraphics2D((int) canvasPositionAndSize.totalWidth,
                (int) canvasPositionAndSize.totalHeight);

        Graphics2D graphics2D = (Graphics2D) sVGGraphics2D.create();

        int index = 0;
        for (ChartCanvas canvas : chartCanvasList) {

            ((Drawable) canvas.chart).draw(graphics2D, canvasPositionAndSize.positionsAndSizes.get(index));
            index++;
        }

        try {
            SVGUtils.writeToSVG(file, sVGGraphics2D.getSVGElement());
        } catch (IOException ex) {
            Logger.getLogger(ChartViewer.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}

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 w w . j a v a 2s .  c  o 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());
    }

}