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:peakml.graphics.JFreeChartTools.java

/**
 * This method writes the given graph to the output stream in the PDF format. As a vector
 * based file format it allows some freedom to be changed (e.g. colors, line thickness, etc.),
 * which can be convenient for presentation purposes.
 * /*  w  w  w . java2s.c  o  m*/
 * @param out         The output stream to write to.
 * @param chart         The chart to be written.
 * @param width         The width of the image.
 * @param height      The height of the image.
 * @throws IOException   Thrown when an error occurs with the IO.
 */
public static void writeAsPDF(OutputStream out, JFreeChart chart, int width, int height) throws IOException {
    Document document = new Document(new Rectangle(width, height), 50, 50, 50, 50);
    document.addAuthor("");
    document.addSubject("");

    try {
        PdfWriter writer = PdfWriter.getInstance(document, out);
        document.open();
        PdfContentByte cb = writer.getDirectContent();
        PdfTemplate tp = cb.createTemplate(width, height);

        java.awt.Graphics2D g2 = tp.createGraphics(width, height, new DefaultFontMapper());
        chart.draw(g2, new java.awt.geom.Rectangle2D.Double(0, 0, width, height));

        g2.dispose();
        cb.addTemplate(tp, 0, 0);
    } catch (DocumentException de) {
        throw new IOException(de.getMessage());
    }

    document.close();
}

From source file:edu.gmu.cs.sim.util.media.PDFEncoder.java

public static void generatePDF(JFreeChart chart, int width, int height, File file) {
    try {//www. java 2s  .  c o m
        Document document = new Document(new com.lowagie.text.Rectangle(width, height));
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(file));
        document.addAuthor("MASON");
        document.open();
        PdfContentByte cb = writer.getDirectContent();
        PdfTemplate tp = cb.createTemplate(width, height);
        Graphics2D g2 = tp.createGraphics(width, height, new DefaultFontMapper());
        Rectangle2D rectangle2D = new Rectangle2D.Double(0, 0, width, height);
        chart.draw(g2, rectangle2D);
        g2.dispose();
        cb.addTemplate(tp, 0, 0);
        document.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

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

public static void writeChartToEMF(JFreeChart chart, int width, int height, File name) throws IOException {
    try {//  w  ww .  j av a 2s.c o m
        VectorGraphics g = new EMFGraphics2D(name, new Dimension(width, height));
        g.startExport();
        Rectangle2D rectangle2d = new Rectangle2D.Double(0, 0, width, height);
        chart.draw((Graphics2D) g, rectangle2d);
        g.endExport();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        throw e;
    }
}

From source file:de.atomfrede.tools.evalutation.tools.plot.util.PlotUtil.java

protected static void writeChartAsPDF(OutputStream out, JFreeChart chart, int width, int height,
        FontMapper mapper) throws IOException {
    Rectangle pagesize = new Rectangle(width, height);
    Document document = new Document(pagesize, 50, 50, 50, 50);
    try {// www  . j  a  v a  2  s . co m
        PdfWriter writer = PdfWriter.getInstance(document, out);
        document.open();
        PdfContentByte cb = writer.getDirectContent();
        PdfTemplate tp = cb.createTemplate(width, height);
        Graphics2D g2 = tp.createGraphics(width, height, mapper);
        Rectangle2D r2D = new Rectangle2D.Double(0, 0, width, height);
        chart.draw(g2, r2D);
        g2.dispose();
        cb.addTemplate(tp, 0, 0);
    } catch (DocumentException de) {
        System.err.println(de.getMessage());
    }
    document.close();
}

From source file:de.dfki.owlsmx.gui.util.Converter.java

public static void convertToPdf(JFreeChart chart, int width, int height, String filename) {
    // step 1       
    Document document = new Document(new Rectangle(width, height));
    try {//w  ww.j a  v  a 2  s.  c  o  m
        // step 2
        PdfWriter writer;
        writer = PdfWriter.getInstance(document, new FileOutputStream(filename));
        // step 3
        document.open();
        // step 4
        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);
    } catch (DocumentException de) {

    } catch (FileNotFoundException e) {

    }
    // step 5
    document.close();
}

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

public static void writeChartToEPS(JFreeChart chart, int width, int height, File name) throws IOException {
    EpsGraphics g;//  w  w w .  j  ava 2 s.  c  o  m
    try {
        g = new EpsGraphics("EpsTools Drawable Export", new FileOutputStream(name), 0, 0, width, height,
                ColorMode.COLOR_RGB);
        Rectangle2D rectangle2d = new Rectangle2D.Double(0, 0, width, height);
        chart.draw((Graphics2D) g, rectangle2d);
        g.close();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        throw e;
    }
}

From source file:tools.EpsServletUtilities.java

public static String saveChartAsEPS(JFreeChart chart, int width, int height, HttpSession session)
        throws IOException {

    if (chart == null) {
        throw new IllegalArgumentException("Null 'chart' argument.");
    }//  ww  w . j  a  v  a  2  s .co  m
    EpsServletUtilities.createTempDir();
    String prefix = EpsServletUtilities.tempFilePrefix;
    if (session == null) {
        prefix = EpsServletUtilities.tempOneTimeFilePrefix;
    }
    File tempFile = File.createTempFile(prefix, ".eps", new File(System.getProperty("java.io.tmpdir")));

    EpsGraphics2D graphic2d = new EpsGraphics2D();
    chart.draw(graphic2d, new Rectangle2D.Double(0, 0, width, height));

    try {
        FileWriter fos = new FileWriter(tempFile);
        fos.write(graphic2d.toString());
        fos.close();

    } catch (FileNotFoundException e1) {
        e1.printStackTrace();
    } catch (IOException e2) {
        e2.printStackTrace();
    }

    if (session != null) {
        EpsServletUtilities.registerChartForDeletion(tempFile, session);
    }
    return tempFile.getPath();

}

From source file:org.encog.workbench.util.graph.DocumentPDF.java

public static void writeChartAsPDF(OutputStream out, JFreeChart chart, int width, int height, FontMapper mapper)
        throws IOException {
    Rectangle pagesize = new Rectangle(width, height);
    Document document = new Document(pagesize, 50, 50, 50, 50);
    try {//w  w  w.java 2 s .  c o  m
        PdfWriter writer = PdfWriter.getInstance(document, out);
        document.addAuthor("JFreeChart");
        document.addSubject("Demonstration");
        document.open();
        PdfContentByte cb = writer.getDirectContent();
        PdfTemplate tp = cb.createTemplate(width, height);
        Graphics2D g2 = tp.createGraphics(width, height, mapper);
        Rectangle2D r2D = new Rectangle2D.Double(0, 0, width, height);
        chart.draw(g2, r2D);
        g2.dispose();
        cb.addTemplate(tp, 0, 0);
    } catch (DocumentException de) {
        System.err.println(de.getMessage());
    }
}

From source file:com.vectorprint.report.jfree.ItextChartHelper.java

/**
 *
 * @param chart the chart tp plot/*from   w ww.j  a va  2  s  .  c o m*/
 * @param contentByte the canvas to plot to
 * @param width the width of the chart will be fit to
 * @param height the height of the chart will be fit to
 * @return
 * @throws BadElementException
 */
public static Image getChartImage(JFreeChart chart, PdfContentByte contentByte, float width, float height,
        float opacity) throws BadElementException {

    // create PdfTemplate from PdfContentByte
    PdfTemplate template = contentByte.createTemplate(width, height);
    template.saveState();
    PdfGState pgs = new PdfGState();
    pgs.setFillOpacity(opacity);
    pgs.setStrokeOpacity(opacity);
    template.setGState(pgs);

    // create Graphics2D from PdfTemplate
    Graphics2D g2 = new PdfGraphics2D(template, width, height);

    // setup the drawing area
    Rectangle2D r2D = new Rectangle2D.Double(0, 0, width, height);

    // pass the Graphics2D and drawing area to JFreeChart
    chart.draw(g2, r2D);
    g2.dispose(); // always dispose this

    template.restoreState();

    // create Image from PdfTemplate
    return Image.getInstance(template);
}

From source file:WeeklyReport.WeeklyPDF.java

public static void writeChartToPDF(JFreeChart chart, int width, int height, String fileName) {
    PdfWriter writer = null;/* ww w  .j  av  a 2s. com*/
    Document document = new Document();
    try {
        writer = PdfWriter.getInstance(document, new FileOutputStream(fileName));
        document.open();
        document.add(new Paragraph("Here is the recommendation"));
        PdfContentByte contentByte = writer.getDirectContent();
        PdfTemplate template = contentByte.createTemplate(width, height);
        Graphics2D graphics2d = template.createGraphics(width, height, new DefaultFontMapper());
        Rectangle2D rectangle2d = new Rectangle2D.Double(5, -100, width, height);
        chart.draw(graphics2d, rectangle2d);
        graphics2d.dispose();
        contentByte.addTemplate(template, 0, 0);
    } catch (FileNotFoundException | DocumentException ex) {
        ex.getMessage();
    }
    document.close();
}