Example usage for org.jfree.chart JFreeChart draw

List of usage examples for org.jfree.chart JFreeChart draw

Introduction

In this page you can find the example usage for org.jfree.chart JFreeChart draw.

Prototype

@Override
public void draw(Graphics2D g2, Rectangle2D area) 

Source Link

Document

Draws the chart on a Java 2D graphics device (such as the screen or a printer).

Usage

From source file:net.sf.mzmine.chartbasics.graphicsexport.ChartExportUtil.java

public static void writeChartToSVG(JFreeChart chart, int width, int height, File name) throws Exception {
    // Get a DOMImplementation
    DOMImplementation domImpl = SVGDOMImplementation.getDOMImplementation();
    org.w3c.dom.Document document = domImpl.createDocument(null, "svg", null);
    SVGGraphics2D svgGenerator = new SVGGraphics2D(document);
    svgGenerator.setSVGCanvasSize(new Dimension(width, height));
    chart.draw(svgGenerator, new Rectangle2D.Double(0, 0, width, height));

    boolean useCSS = true; // we want to use CSS style attribute

    Writer out = null;/* w w  w  .j a  v  a 2 s  .  c  om*/
    try {
        out = new OutputStreamWriter(new FileOutputStream(name), "UTF-8");
        svgGenerator.stream(out, useCSS);
    } catch (UnsupportedEncodingException | FileNotFoundException e) {
        e.printStackTrace();
        throw e;
    } catch (SVGGraphics2DIOException e) {
        e.printStackTrace();
        throw e;
    } finally {
        try {
            out.close();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            throw e;
        }
    }
}

From source file:de.mpg.mpi_inf.bioinf.netanalyzer.ui.charts.JFreeChartConn.java

/**
 * Saves the given chart to a SVG image file.
 * <p>//  w w w.  j a v  a 2s .c  o  m
 * This method uses the FreeHEP VectorGraphics library.
 * </p>
 * 
 * @param aFile
 *            File to be saved to.
 * @param aChart
 *            Chart to be saved.
 * @param aWidth
 *            Desired width of the image.
 * @param aHeight
 *            Desired height of the image.
 * @throws IOException
 *             If I/O error occurs.
 */
public static void saveAsSvg(File aFile, JFreeChart aChart, int aWidth, int aHeight) throws IOException {
    final VectorGraphics graphics = new SVGGraphics2D(aFile, new Dimension(aWidth, aHeight));
    graphics.startExport();
    aChart.draw(graphics, new Rectangle2D.Double(0, 0, aWidth, aHeight));
    graphics.endExport();
}

From source file:org.jfree.chart.demo.PDFChartTransferable.java

public static void writeChartAsPDF(ByteArrayOutputStream bytearrayoutputstream, JFreeChart jfreechart, int i,
        int j, FontMapper fontmapper) throws IOException {
    Rectangle rectangle = new Rectangle(i, j);
    Document document = new Document(rectangle, 50F, 50F, 50F, 50F);
    try {//w  w  w. ja  v a  2  s .c o  m
        PdfWriter pdfwriter = PdfWriter.getInstance(document, bytearrayoutputstream);
        document.addAuthor("JFreeChart");
        document.addSubject("Demonstration");
        document.open();
        PdfContentByte pdfcontentbyte = pdfwriter.getDirectContent();
        PdfTemplate pdftemplate = pdfcontentbyte.createTemplate(i, j);
        Graphics2D graphics2d = pdftemplate.createGraphics(i, j, fontmapper);
        java.awt.geom.Rectangle2D.Double double1 = new java.awt.geom.Rectangle2D.Double(0.0D, 0.0D, i, j);
        jfreechart.draw(graphics2d, double1);
        graphics2d.dispose();
        pdfcontentbyte.addTemplate(pdftemplate, 0.0F, 0.0F);
    } catch (DocumentException documentexception) {
        System.err.println(documentexception.getMessage());
    }
    document.close();
}

From source file:org.dkpro.lab.reporting.ChartUtil.java

/**
 * Exports a JFreeChart to a scalable PDF file.
 *
 * @param chart JFreeChart to export//from  w  ww. j  a v a  2 s  .c om
 * @param aOS stream to write to.
 * @param aWidth width of the chart in pixels
 * @param aHeight height of the chart in pixels
 * @throws IOException if writing the svgFile fails.
 */
public static void writeChartAsPDF(OutputStream aOS, JFreeChart chart, int aWidth, int aHeight)
        throws IOException {
    // Create an instance of the SVG Generator
    PDFDocumentGraphics2D pdfGenerator = new PDFDocumentGraphics2D(true, aOS, aWidth, aHeight);
    pdfGenerator.setDeviceDPI(PDFDocumentGraphics2D.NORMAL_PDF_RESOLUTION);
    pdfGenerator.setGraphicContext(new GraphicContext());
    pdfGenerator.setSVGDimension(aWidth, aHeight);
    pdfGenerator.setClip(0, 0, aWidth, aHeight);
    pdfGenerator.setRenderingHint(KEY_ANTIALIASING, VALUE_ANTIALIAS_ON);
    pdfGenerator.setRenderingHint(KEY_INTERPOLATION, VALUE_INTERPOLATION_BILINEAR);
    chart.setBackgroundPaint(Color.white);
    chart.getPlot().setBackgroundPaint(Color.white);
    // draw the chart in the SVG generator
    chart.draw(pdfGenerator, new Rectangle(aWidth, aHeight));
    pdfGenerator.finish();
}

From source file:jmbench.plots.UtilPlotPdf.java

public static void saveAsPdf(JFreeChart chart, String FILENAME, int width, int height) {
    File parent = new File(new File(FILENAME).getParent());
    if (!parent.exists()) {
        if (!parent.mkdirs())
            throw new RuntimeException("Can't make directory path");
    }//from  www.ja va 2  s .  c  om

    Document document = new Document(new Rectangle(width, height));
    try {
        FileOutputStream file = new FileOutputStream(FILENAME);
        PdfWriter writer = PdfWriter.getInstance(document, file);
        document.open();
        PdfContentByte cb = writer.getDirectContent();
        PdfTemplate tp = cb.createTemplate(width, height);
        Graphics2D g2d = tp.createGraphics(width, height, new DefaultFontMapper());
        Rectangle2D r2d = new Rectangle2D.Double(0, 0, width, height);
        chart.draw(g2d, r2d);
        g2d.dispose();
        cb.addTemplate(tp, 0, 0);
        document.close();
        g2d.dispose();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:de.bund.bfr.knime.pmm.common.chart.ChartUtilities.java

public static void saveChartAs(JFreeChart chart, String fileName, int width, int height) {
    if (fileName.toLowerCase().endsWith(".png")) {
        try {//from w ww .jav a 2s . c o  m
            org.jfree.chart.ChartUtilities.writeChartAsPNG(new FileOutputStream(fileName), chart, width,
                    height);
        } catch (IOException e) {
            e.printStackTrace();
        }
    } else if (fileName.toLowerCase().endsWith(".svg")) {
        try {
            DOMImplementation domImpl = GenericDOMImplementation.getDOMImplementation();
            Document document = domImpl.createDocument(null, "svg", null);
            SVGGraphics2D svgGenerator = new SVGGraphics2D(document);
            Writer outsvg = new OutputStreamWriter(new FileOutputStream(fileName), StandardCharsets.UTF_8);

            chart.draw(svgGenerator, new Rectangle2D.Double(0, 0, width, height));
            svgGenerator.stream(outsvg, true);
            outsvg.close();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (SVGGraphics2DIOException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

From source file:canreg.client.analysis.Tools.java

/**
 * Exports a JFreeChart to a SVG file.//ww  w .  jav  a  2s .  c om
 *
 * @param chart JFreeChart to export
 * @param bounds the dimensions of the viewport
 * @param svgFile the output file.
 * @throws IOException if writing the svgFile fails.
 */
public static void exportChartAsSVG(JFreeChart chart, Rectangle bounds, File svgFile) throws IOException {
    // Get a DOMImplementation and create an XML document
    DOMImplementation domImpl = GenericDOMImplementation.getDOMImplementation();
    Document document = domImpl.createDocument(null, "svg", null);

    // Create an instance of the SVG Generator
    SVGGraphics2D svgGenerator = new SVGGraphics2D(document);

    // draw the chart in the SVG generator
    chart.draw(svgGenerator, bounds);

    // Write svg file
    OutputStream outputStream = new FileOutputStream(svgFile);
    Writer out = new OutputStreamWriter(outputStream, "UTF-8");
    svgGenerator.stream(out, true /* use css */);
    outputStream.flush();
    outputStream.close();
}

From source file:com.exam.server.ConvertPDF.java

public static void addPieChart(JFreeChart chart, int width, int height, PdfWriter writer) {
    //          PdfWriter writer = null;

    //          Document document = new Document();

    try {/*w  w w .jav  a  2 s. co m*/
        //             writer = PdfWriter.getInstance(document, new FileOutputStream(
        //                   fileName));
        System.out.println("writing pie chart document ");
        //             document.open();
        PdfContentByte contentByte = writer.getDirectContent();
        PdfTemplate template = contentByte.createTemplate(width, height);
        Graphics2D graphics2d = template.createGraphics(width, height, new DefaultFontMapper());
        Rectangle2D rectangle2d = new Rectangle2D.Double(0, 0, width, height);

        chart.draw(graphics2d, rectangle2d);

        graphics2d.dispose();
        contentByte.addTemplate(template, 0, 0);

    } catch (Exception e) {
        e.printStackTrace();
    }
    System.out.println("writing done:: ");
}

From source file:net.sf.mzmine.chartbasics.graphicsexport.ChartExportUtil.java

/**
 * This method saves a chart as a PDF with given dimensions
 * /*from   w  w w .j a  va 2 s .c  o m*/
 * @param chart
 * @param width
 * @param height
 * @param fileName is a full path
 */
public static void writeChartToPDF(JFreeChart chart, int width, int height, File fileName) throws Exception {
    PdfWriter writer = null;

    Document document = new Document(new Rectangle(width, height));

    try {
        writer = PdfWriter.getInstance(document, new FileOutputStream(fileName));
        document.open();
        PdfContentByte contentByte = writer.getDirectContent();
        PdfTemplate template = contentByte.createTemplate(width, height);
        Graphics2D graphics2d = template.createGraphics(width, height, new DefaultFontMapper());
        Rectangle2D rectangle2d = new Rectangle2D.Double(0, 0, width, height);

        chart.draw(graphics2d, rectangle2d);

        graphics2d.dispose();
        contentByte.addTemplate(template, 0, 0);

    } catch (Exception e) {
        e.printStackTrace();
        throw e;
    } finally {
        document.close();
    }
}

From source file:de.bfs.radon.omsimulation.gui.data.OMExports.java

/**
 * // ww  w  . j a v  a2  s.  co  m
 * Method to export charts as PDF files using the defined path.
 * 
 * @param path
 *          The filename and absolute path.
 * @param chart
 *          The JFreeChart object.
 * @param width
 *          The width of the PDF file.
 * @param height
 *          The height of the PDF file.
 * @param mapper
 *          The font mapper for the PDF file.
 * @param title
 *          The title of the PDF file.
 * @throws IOException
 *           If writing a PDF file fails.
 */
@SuppressWarnings("deprecation")
public static void exportPdf(String path, JFreeChart chart, int width, int height, FontMapper mapper,
        String title) throws IOException {
    File file = new File(path);
    FileOutputStream pdfStream = new FileOutputStream(file);
    BufferedOutputStream pdfOutput = new BufferedOutputStream(pdfStream);
    Rectangle pagesize = new Rectangle(width, height);
    Document document = new Document();
    document.setPageSize(pagesize);
    document.setMargins(50, 50, 50, 50);
    document.addAuthor("OMSimulationTool");
    document.addSubject(title);
    try {
        PdfWriter pdfWriter = PdfWriter.getInstance(document, pdfOutput);
        document.open();
        PdfContentByte contentByte = pdfWriter.getDirectContent();
        PdfTemplate template = contentByte.createTemplate(width, height);
        Graphics2D g2D = template.createGraphics(width, height, mapper);
        Double r2D = new Rectangle2D.Double(0, 0, width, height);
        chart.draw(g2D, r2D);
        g2D.dispose();
        contentByte.addTemplate(template, 0, 0);
    } catch (DocumentException de) {
        JOptionPane.showMessageDialog(null, "Failed to write PDF document.\n" + de.getMessage(), "Failed",
                JOptionPane.ERROR_MESSAGE);
        de.printStackTrace();
    }
    document.close();
}