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:de.chott.jfreechartsample.service.ChartService.java

/**
 * Schreibt mehrere JFreeCharts in ein PDF. Fr jedes Chart wird hierbei eine halbe PDF-Seite verwendet.
 * /*  w  ww  .j a v a  2s  .c o  m*/
 * @param charts
 * @return Das PDF als ByteArray
 */
private byte[] writeChartsToDocument(JFreeChart... charts) {
    try {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();

        float width = PageSize.A4.getWidth();
        float height = PageSize.A4.getHeight() / 2;
        int index = 0;

        Document document = new Document();
        PdfWriter writer = PdfWriter.getInstance(document, baos);
        document.open();
        PdfContentByte contentByte = writer.getDirectContent();

        for (JFreeChart chart : charts) {

            PdfTemplate template = contentByte.createTemplate(width, height);
            Graphics2D graphics2D = template.createGraphics(width, height);
            Rectangle2D rectangle2D = new Rectangle2D.Double(0, 0, width, height);

            chart.draw(graphics2D, rectangle2D);

            graphics2D.dispose();
            contentByte.addTemplate(template, 0, height - (height * index));
            index++;
        }

        writer.flush();
        document.close();

        return baos.toByteArray();
    } catch (Exception ex) {
        Logger.getLogger(ChartService.class.getName()).log(Level.SEVERE, null, ex);
    }
    return null;
}

From source file:WeeklyReport.Sections.Bookings.java

public Image bookingsByPODImage(PdfWriter writer) throws BadElementException {
    JFreeChart chart = new Bookings().bookingsByPODChart();
    PdfContentByte contentByte = writer.getDirectContent();
    PdfTemplate template = contentByte.createTemplate(600f, 400f);
    Graphics2D graphics2d = template.createGraphics(600f, 400f, new DefaultFontMapper());
    Rectangle2D rectangle2d = new Rectangle2D.Float(10f, 0, 500f, 400f);
    chart.draw(graphics2d, rectangle2d);
    graphics2d.dispose();/*  w  w  w .  j  a  v a2s  .c om*/

    Image chartImage = Image.getInstance(template);
    return chartImage;
}

From source file:WeeklyReport.Sections.Bookings.java

public Image bookingsByTradelaneImage(PdfWriter writer) throws BadElementException {
    JFreeChart chart = new Bookings().bookingsByTradelaneChart();
    PdfContentByte contentByte = writer.getDirectContent();
    PdfTemplate template = contentByte.createTemplate(600f, 400f);
    Graphics2D graphics2d = template.createGraphics(600f, 400f, new DefaultFontMapper());
    Rectangle2D rectangle2d = new Rectangle2D.Float(10f, 0, 500f, 400f);
    chart.draw(graphics2d, rectangle2d);
    graphics2d.dispose();/*w  w w .j a v a2s  .  com*/

    Image chartImage = Image.getInstance(template);
    return chartImage;
}

From source file:WeeklyReport.Sections.Commodities.java

public Image commodityChartImage(PdfWriter writer) throws BadElementException {
    JFreeChart chart = commodityChart();
    PdfContentByte contentByte = writer.getDirectContent();
    PdfTemplate template = contentByte.createTemplate(525f, 475f);
    Graphics2D graphics2d = template.createGraphics(525f, 475f, new DefaultFontMapper());
    Rectangle2D rectangle2d = new Rectangle2D.Float(0, 0, 525f, 475f);
    chart.draw(graphics2d, rectangle2d);
    graphics2d.dispose();/*w ww .  ja  v  a2 s.  co m*/

    Image chartImage = Image.getInstance(template);
    return chartImage;
}

From source file:WeeklyReport.Sections.Declines.java

public Image declinesByReasonImage(PdfWriter writer) throws BadElementException {
    JFreeChart chart = declinesByReasonChart();
    PdfContentByte contentByte = writer.getDirectContent();
    PdfTemplate template = contentByte.createTemplate(600f, 400f);
    Graphics2D graphics2d = template.createGraphics(600f, 400f, new DefaultFontMapper());
    Rectangle2D rectangle2d = new Rectangle2D.Float(10f, 0, 500f, 400f);
    chart.draw(graphics2d, rectangle2d);
    graphics2d.dispose();// ww w.  jav  a 2 s .c  om

    Image chartImage = Image.getInstance(template);
    return chartImage;
}

From source file:WeeklyReport.Sections.Declines.java

public Image declinesByCommodityImage(PdfWriter writer) throws BadElementException {
    JFreeChart chart = new Declines().declinesByCommodityClassChart();
    PdfContentByte contentByte = writer.getDirectContent();
    PdfTemplate template = contentByte.createAppearance(600f, 400f);
    Graphics2D graphics2d = template.createGraphics(600f, 400f, new DefaultFontMapper());
    Rectangle2D rectangle2d = new Rectangle2D.Float(10f, 0, 500f, 400f);
    chart.draw(graphics2d, rectangle2d);
    graphics2d.dispose();/*from w  w w.j a v  a2s.co  m*/

    Image chartImage = Image.getInstance(template);
    return chartImage;
}

From source file:net.atomique.ksar.export.FilePDF.java

private void addchart(PdfWriter writer, Graph graph) {
    JFreeChart chart = graph.getgraph(mysar.myparser.getStartOfGraph(), mysar.myparser.getEndOfGraph());
    PdfTemplate pdftpl = pdfcb.createTemplate(pagewidth, pageheight);
    Graphics2D g2d = new PdfGraphics2D(pdftpl, pagewidth, pageheight);
    Double r2d = new Rectangle2D.Double(0, 0, pagewidth, pageheight);
    chart.draw(g2d, r2d);
    g2d.dispose();//from   w ww . j av  a 2s .c o m
    pdfcb.addTemplate(pdftpl, pdfmargins, pdfmargins);
    try {
        writer.releaseTemplate(pdftpl);
    } catch (IOException ioe) {
        log.error("Unable to write to : {}", pdffilename);
    }
}

From source file:org.n52.server.io.DiagramGenerator.java

/**
 * Creates a time series chart diagram and writes it to the OutputStream.
 *///from  ww w .ja va 2s  . com
public void producePresentation(Map<String, OXFFeatureCollection> entireCollMap, DesignOptions options,
        FileOutputStream out, boolean compress) throws OXFException, IOException {

    // render features:
    int width = options.getWidth();
    int height = options.getHeight();
    Calendar begin = Calendar.getInstance();
    begin.setTimeInMillis(options.getBegin());
    Calendar end = Calendar.getInstance();
    end.setTimeInMillis(options.getEnd());

    DiagramRenderer renderer = new DiagramRenderer(false);

    JFreeChart diagramChart = renderer.renderChart(entireCollMap, options, begin, end, compress);
    diagramChart.removeLegend();

    // draw chart into image:
    BufferedImage diagramImage = new BufferedImage(width, height, TYPE_INT_RGB);
    Graphics2D chartGraphics = diagramImage.createGraphics();
    chartGraphics.setColor(Color.white);
    chartGraphics.fillRect(0, 0, width, height);

    diagramChart.draw(chartGraphics, new Rectangle2D.Float(0, 0, width, height));

    JPEGImageWriteParam p = new JPEGImageWriteParam(null);
    p.setCompressionMode(JPEGImageWriteParam.MODE_DEFAULT);
    write(diagramImage, FORMAT, out);
}

From source file:org.n52.server.sos.generator.DiagramGenerator.java

/**
 * Creates a time series chart diagram and writes it to the OutputStream.
 * /*from   ww  w .  j av  a  2  s .co  m*/
 * @param entireCollMap
 * @param options
 * @param out
 * @throws OXFException
 * @throws IOException
 */
public void producePresentation(Map<String, OXFFeatureCollection> entireCollMap, DesignOptions options,
        FileOutputStream out, boolean compress) throws OXFException, IOException {

    // render features:
    int width = options.getWidth();
    int height = options.getHeight();
    Calendar begin = Calendar.getInstance();
    begin.setTimeInMillis(options.getBegin());
    Calendar end = Calendar.getInstance();
    end.setTimeInMillis(options.getEnd());

    DiagramRenderer renderer = new DiagramRenderer(false);

    JFreeChart diagramChart = renderer.renderChart(entireCollMap, options, begin, end, compress);
    diagramChart.removeLegend();

    // draw chart into image:
    BufferedImage diagramImage = new BufferedImage(width, height, TYPE_INT_RGB);
    Graphics2D chartGraphics = diagramImage.createGraphics();
    chartGraphics.setColor(Color.white);
    chartGraphics.fillRect(0, 0, width, height);

    diagramChart.draw(chartGraphics, new Rectangle2D.Float(0, 0, width, height));

    JPEGEncodeParam p = new JPEGEncodeParam();
    p.setQuality(1f);
    ImageEncoder encoder = ImageCodec.createImageEncoder("jpeg", out, p);

    encoder.encode(diagramImage);
}

From source file:org.n52.server.sos.generator.Generator.java

protected String createAndSaveImage(DesignOptions options, JFreeChart chart, ChartRenderingInfo renderingInfo)
        throws Exception {
    int width = options.getWidth();
    int height = options.getHeight();
    BufferedImage image = chart.createBufferedImage(width, height, renderingInfo);
    Graphics2D chartGraphics = image.createGraphics();
    chartGraphics.setColor(Color.white);
    chartGraphics.fillRect(0, 0, width, height);
    chart.draw(chartGraphics, new Rectangle2D.Float(0, 0, width, height));

    try {/*from  w  ww .  ja va2 s.c  o  m*/
        return ServletUtilities.saveChartAsPNG(chart, width, height, renderingInfo, null);
    } catch (IOException e) {
        throw new Exception("Could not save PNG!", e);
    }
}