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:org.exist.xquery.modules.jfreechart.render.SVGrenderer.java

@Override
public void render(JFreeChart chart, Configuration config, OutputStream os) throws IOException {

    Rectangle bounds = new Rectangle(config.getImageWidth(), config.getImageHeight());

    // 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);

    Writer out = new OutputStreamWriter(os, "UTF-8");
    svgGenerator.stream(out, true /* use css */);
    os.flush();//from  w w  w.  j  a va  2 s.c  o m
    os.close();
}

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

protected String createAndSaveImage(DesignOptions options, JFreeChart chart, ChartRenderingInfo renderingInfo)
        throws GeneratorException {
    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  w  w.ja va 2s  .c  om*/
        return ServletUtilities.saveChartAsPNG(chart, width, height, renderingInfo, null);
    } catch (IOException e) {
        throw new GeneratorException("Could not save PNG!", e);
    }
}

From source file:org.openscience.cdk.applications.taverna.basicutilities.ChartTool.java

/**
 * Adds a chart to the pdf file.//from ww w  .  ja  va  2 s.  c o m
 * 
 * @param chart
 * @param cb
 */
private void addChartPageToPDF(JFreeChart chart, PdfContentByte cb) {
    PdfTemplate tp = cb.createTemplate(this.width, this.height);
    Graphics2D g2 = tp.createGraphics(this.width, this.height, new DefaultFontMapper());
    Rectangle2D r2D = new Rectangle2D.Double(0, 0, this.width, this.height);
    chart.draw(g2, r2D);
    g2.dispose();
    cb.addTemplate(tp, 0, 0);
}

From source file:net.sf.jasperreports.charts.util.SvgChartRendererFactory.java

@Override
public Renderable getRenderable(JasperReportsContext jasperReportsContext, JFreeChart chart,
        ChartHyperlinkProvider chartHyperlinkProvider, Rectangle2D rectangle) {
    DOMImplementation domImpl = GenericDOMImplementation.getDOMImplementation();
    Document document = domImpl.createDocument(null, "svg", null);
    SVGGraphics2D grx = new SVGGraphics2D(document);

    grx.setSVGCanvasSize(rectangle.getBounds().getSize());

    List<JRPrintImageAreaHyperlink> areaHyperlinks = null;

    if (chartHyperlinkProvider != null && chartHyperlinkProvider.hasHyperlinks()) {
        areaHyperlinks = ChartUtil.getImageAreaHyperlinks(chart, chartHyperlinkProvider, grx, rectangle);
    } else {/*  w w  w  .  j  a va2 s . c o  m*/
        chart.draw(grx, rectangle);
    }

    try {
        StringWriter swriter = new StringWriter();
        grx.stream(swriter);
        byte[] svgData = null;
        try {
            svgData = swriter.getBuffer().toString().getBytes("UTF-8");
        } catch (UnsupportedEncodingException e) {
            throw new JRRuntimeException(e);
        }
        return new SimpleRenderToImageAwareDataRenderer(svgData, areaHyperlinks);
    } catch (SVGGraphics2DIOException e) {
        throw new JRRuntimeException(e);
    }
}

From source file:no.met.jtimeseries.meteogram.SvgChartSaver.java

@Override
public void save(File file, JFreeChart chart, int width, int height) throws IOException {

    if (file == null || chart == null)
        throw new IllegalArgumentException("Null 'file' or 'chart' argument.");
    //get the genric dom imp
    DOMImplementation dom = GenericDOMImplementation.getDOMImplementation();
    //create document
    org.w3c.dom.Document document = dom.createDocument(null, "svg", null);
    //create svg 2d graphics
    SVGGraphics2D svgGenerator = new SVGGraphics2D(document);
    svgGenerator.getGeneratorContext().setPrecision(6);
    svgGenerator.setSVGCanvasSize(new Dimension(width, height));
    //render chart with svg 2D graphics        
    chart.draw(svgGenerator, new Rectangle2D.Double(0, 0, width, height));
    Element svgRoot = svgGenerator.getRoot();
    /// set SVG Canvas size (For auto resizing)
    svgRoot.setAttributeNS(null, SVGGraphics2D.SVG_VIEW_BOX_ATTRIBUTE,
            String.format("0 0 %d %d", width, height));
    svgRoot.setAttributeNS(null, "preserveAspectRatio", "xMidYMid meet");
    svgRoot.removeAttribute("width");
    svgRoot.removeAttribute("height");
    OutputStream out = new BufferedOutputStream(new FileOutputStream(file));
    Writer writer = new OutputStreamWriter(out, "UTF-8");
    svgGenerator.stream(svgRoot, writer, false, true);
}

From source file:com.rapidminer.gui.viewer.ROCChartPlotter.java

public void paintDeviationChart(Graphics graphics, int width, int height) {
    prepareData();/*from w w  w . ja  v  a2  s . c om*/

    JFreeChart chart = createChart(this.dataset);

    // set the background color for the chart...
    chart.setBackgroundPaint(Color.white);

    // legend settings
    LegendTitle legend = chart.getLegend();
    if (legend != null) {
        legend.setPosition(RectangleEdge.TOP);
        legend.setFrame(BlockBorder.NONE);
        legend.setHorizontalAlignment(HorizontalAlignment.LEFT);
    }

    Rectangle2D drawRect = new Rectangle2D.Double(0, 0, width, height);
    chart.draw((Graphics2D) graphics, drawRect);
}

From source file:de.fischer.thotti.reportgen.diagram.ChartGenerator.java

private void saveChartAsSVG(JFreeChart chartToSave, String svgFilename)
        throws IOException, FileNotFoundException {

    File chartFile = new File(baseDir, svgFilename);

    DOMImplementation domImpl = GenericDOMImplementation.getDOMImplementation();
    final Document document = domImpl.createDocument(null, "svg", null);

    SVGGraphics2D svgGenerator = new SVGGraphics2D(document);
    ///*  w w w  .  j  av a2  s.  c  o m*/
    Rectangle2D.Double area = new Rectangle2D.Double(0.0, 0.0, DEFAULT_CHAR_WIDTH, DEFAULT_CHAR_HEIGHT);
    chartToSave.draw(svgGenerator, area);
    //
    //            // Write svg file
    OutputStream outputStream = new FileOutputStream(chartFile);
    Writer out = new FileWriter(chartFile);// new OutputStreamWriter(System.out, "UTF-8");
    //            ChartUtilities.writeChartAsPNG(outputStream, chart, 800, 400);
    svgGenerator.stream(out, true /* use css */);

}

From source file:org.deegree.graphics.charts.ChartsBuilder.java

/**
 * Creates a BufferedImage instance from a given chart, according to the given additional parameters
 *
 * @param chart//from   w  ww  .  jav  a  2 s .c o m
 * @param width
 *            of the generated image
 * @param height
 *            of the generated image
 * @param imageType
 *            ex image/png, image/jpg
 * @return BufferedImage
 */
protected BufferedImage createBufferedImage(JFreeChart chart, int width, int height, String imageType) {

    chart.setTextAntiAlias(true);
    chart.setAntiAlias(true);
    BufferedImage image = new BufferedImage(width, height, mapImageformat(imageType));
    Graphics2D g2 = image.createGraphics();
    chart.draw(g2, new Rectangle(new Dimension(width, height)));
    return image;
}

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

public static void writeChartAsPDF(OutputStream outputstream, 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 {/*from  www .java  2s  .  co m*/
        PdfWriter pdfwriter = PdfWriter.getInstance(document, outputstream);
        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:peakml.math.Signal.java

public BufferedImage createGraphImage(String name, String xlabel, String ylabel, int width, int height) {
    JFreeChart linechart = createGraph(name, xlabel, ylabel);

    // create the image
    BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
    // create the graphics context
    Graphics2D g = img.createGraphics();

    // draw the line chart
    linechart.draw(g, new Rectangle(0, 0, width, height));

    // return the result
    return img;//w  w w .j  a  va  2 s . co m
}