List of usage examples for org.jfree.chart JFreeChart draw
@Override public void draw(Graphics2D g2, Rectangle2D area)
From source file:de.xirp.chart.ChartUtil.java
/** * Exports the given chart as PDF in the specified size. The * export is written to the given path.// w ww .ja v a 2 s . c o m * * @param chart * The chart to export. * @param width * The desired width of the PDF. * @param height * The desired height of the PDF. * @param path * The path to write the PDF to. * @see org.jfree.chart.JFreeChart */ private static void exportPDF(JFreeChart chart, int width, int height, String path) { Document document = new Document(new Rectangle(width, height)); try { PdfWriter writer; writer = PdfWriter.getInstance(document, new FileOutputStream(path)); 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); } catch (Exception e) { logClass.error("Error: " + e.getMessage() //$NON-NLS-1$ + Constants.LINE_SEPARATOR, e); } document.close(); }
From source file:ec.util.chart.swing.Charts.java
private static String generateSVG(JFreeChart chart, int width, int height) throws IOException { try {//from ww w .j a v a2s . co m Class<?> svgGraphics2d = Class.forName("org.jfree.graphics2d.svg.SVGGraphics2D"); Graphics2D g2 = (Graphics2D) svgGraphics2d.getConstructor(int.class, int.class).newInstance(width, height); // we suppress shadow generation, because SVG is a vector format and // the shadow effect is applied via bitmap effects... g2.setRenderingHint(JFreeChart.KEY_SUPPRESS_SHADOW_GENERATION, true); chart.draw(g2, new Rectangle2D.Double(0, 0, width, height)); return (String) g2.getClass().getMethod("getSVGElement").invoke(g2); } catch (ClassNotFoundException | NoSuchMethodException | SecurityException | InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) { throw new IOException("Cannot generate SVG", ex); } }
From source file:org.objectweb.proactive.extensions.timitspmd.util.charts.Utilities.java
/** * Exports a JFreeChart to a SVG file.//from www. ja va2 s . c o m * * @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 saveChartAsSVG(JFreeChart chart, Rectangle bounds, File svgFile) throws IOException { try { Class<?> GDI = Class.forName("org.apache.batik.dom.GenericDOMImplementation"); // Get a DOMImplementation and create an XML document Method getDOMImplementation = GDI.getMethod("getDOMImplementation", new Class<?>[0]); DOMImplementation domImpl = (DOMImplementation) getDOMImplementation.invoke(null, new Object[0]); org.w3c.dom.Document document = domImpl.createDocument(null, "svg", null); // Create an instance of the SVG Generator Class<?> SG2D = Class.forName("org.apache.batik.svggen.SVGGraphics2D"); Method streamMethod = SG2D.getMethod("stream", new Class<?>[] { Writer.class, boolean.class }); Constructor<?> SG2DConstr = SG2D.getConstructor(new Class<?>[] { org.w3c.dom.Document.class }); Object svgGenerator = SG2DConstr.newInstance(document); // draw the chart in the SVG generator chart.draw((Graphics2D) svgGenerator, bounds); // Write svg file OutputStream outputStream = new FileOutputStream(svgFile); Writer out = new OutputStreamWriter(outputStream, "UTF-8"); streamMethod.invoke(svgGenerator, new Object[] { out, true /* use css */ }); outputStream.flush(); outputStream.close(); out.close(); } catch (Exception e) { e.printStackTrace(); } }
From source file:by.bsuir.group172301.matskevich.tour.util.PDFCreator.java
public static void writeChartToPDF(JFreeChart chart, int width, int height, String fileName, int col1, int col2, double perc) { PdfWriter writer = null;// w w w . jav a 2 s .c o m Document document = new Document(); try { writer = PdfWriter.getInstance(document, new FileOutputStream(fileName)); document.open(); Paragraph paragraph = new Paragraph(); // We add one empty line paragraph = new Paragraph("Results:"); paragraph.setAlignment(Element.ALIGN_CENTER); document.add(paragraph); paragraph = new Paragraph("Test before: " + col1); document.add(paragraph); paragraph = new Paragraph("Test after: " + col2); document.add(paragraph); paragraph = new Paragraph("Percentage: " + perc); document.add(paragraph); 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, 150, 250); } catch (Exception e) { e.printStackTrace(); } document.close(); }
From source file:org.geotools.renderer.chart.ChartGraphicFactory.java
BufferedImage drawChart(JFreeChart chart, int w, int h) { BufferedImage bi = new BufferedImage(w, h, BufferedImage.TYPE_4BYTE_ABGR); Graphics2D gr = bi.createGraphics(); try {//from w w w .jav a2 s. c o m chart.draw(gr, new Rectangle2D.Double(0, 0, w, h)); } finally { gr.dispose(); } return bi; }
From source file:com.athena.chameleon.engine.utils.PDFWriterUtil.java
/** * /*from ww w . j a v a 2 s .c o m*/ * chart * * @param section chart section ? * @param e chart element * @throws Exception */ public static void setChart(PdfWriter writer, Section section, Element e) throws Exception { DefaultCategoryDataset dataset = new DefaultCategoryDataset(); for (Element e1 : e.getChildren()) { if (!e1.getChild("column").getText().equals(FileType.DIRECTORY.toString()) && !e1.getChild("column").getText().equals(FileType.SUM.toString())) { dataset.setValue(Integer.parseInt(e1.getChild("value").getText()), e.getAttributeValue("title"), e1.getChild("column").getText()); } } JFreeChart chart = ChartFactory.createBarChart3D(e.getAttributeValue("title"), "", "", dataset, PlotOrientation.VERTICAL, false, true, false); CategoryPlot plot = chart.getCategoryPlot(); java.awt.Font labelFont = chart.getCategoryPlot().getDomainAxis().getLabelFont(); plot.getDomainAxis().setLabelFont(new java.awt.Font(labelFont.getName(), Font.NORMAL, 6)); plot.getDomainAxis().setTickLabelFont(new java.awt.Font(labelFont.getName(), Font.NORMAL, 6)); PdfContentByte cb = writer.getDirectContent(); PdfTemplate bar = cb.createTemplate(500, 150); Graphics2D g2d2 = new PdfGraphics2D(bar, 500, 150); Rectangle2D r2d2 = new Rectangle2D.Double(0, 0, 500, 150); chart.draw(g2d2, r2d2); g2d2.dispose(); Image image = Image.getInstance(bar); image.setAlignment(com.itextpdf.text.Element.ALIGN_CENTER); section.add(image); }
From source file:canreg.client.analysis.Tools.java
public static void exportChartAsPDF(JFreeChart chart, Rectangle bounds, File file) throws IOException, DocumentException { System.out.println(file.getPath()); PdfWriter writer = null;//from w w w .j ava 2 s . c om com.itextpdf.text.Document document = new com.itextpdf.text.Document(); document.addCreator("CanReg5"); document.addCreationDate(); writer = PdfWriter.getInstance(document, new FileOutputStream(file)); document.open(); PdfContentByte contentByte = writer.getDirectContent(); PdfTemplate template = contentByte.createTemplate(bounds.width, bounds.height); Graphics2D graphics2d = template.createGraphics(bounds.width, bounds.height, new DefaultFontMapper()); Rectangle2D rectangle2d = new Rectangle2D.Double(0, 0, bounds.width, bounds.height); chart.draw(graphics2d, rectangle2d); graphics2d.dispose(); contentByte.addTemplate(template, 0, 0); document.close(); }
From source file:com.anevis.jfreechartsamplespring.chart.ChartServiceImpl.java
private byte[] writeChartsToDocument(JFreeChart... charts) { try {/* ww w .jav a2s.com*/ 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 (DocumentException ex) { Logger.getLogger(ChartService.class.getName()).log(Level.SEVERE, null, ex); } return null; }
From source file:io.github.mzmine.util.jfreechart.JFreeChartUtils.java
public static void exportToImageFile(ChartViewer chartNode, File file, ImgFileType fileType) { final JFreeChart chart = chartNode.getChart(); final int width = (int) chartNode.getWidth(); final int height = (int) chartNode.getHeight(); try {/*from w ww . j a va 2 s .c o m*/ switch (fileType) { case JPG: ExportUtils.writeAsJPEG(chart, width, height, file); break; case PNG: ExportUtils.writeAsPNG(chart, width, height, file); break; case SVG: setDrawSeriesLineAsPath(chart, true); ExportUtils.writeAsSVG(chart, width, height, file); setDrawSeriesLineAsPath(chart, false); break; case PDF: setDrawSeriesLineAsPath(chart, true); ExportUtils.writeAsPDF(chart, width, height, file); setDrawSeriesLineAsPath(chart, false); break; case EMF: FileOutputStream out2 = new FileOutputStream(file); setDrawSeriesLineAsPath(chart, true); EMFGraphics2D g2d2 = new EMFGraphics2D(out2, new Dimension(width, height)); g2d2.startExport(); chart.draw(g2d2, new Rectangle(width, height)); g2d2.endExport(); setDrawSeriesLineAsPath(chart, false); break; case EPS: FileOutputStream out = new FileOutputStream(file); setDrawSeriesLineAsPath(chart, true); EPSDocumentGraphics2D g2d = new EPSDocumentGraphics2D(false); g2d.setGraphicContext(new GraphicContext()); g2d.setupDocument(out, width, height); chart.draw(g2d, new Rectangle(width, height)); g2d.finish(); setDrawSeriesLineAsPath(chart, false); out.close(); break; } } catch (IOException e) { MZmineGUI.displayMessage("Unable to save image: " + e.getMessage()); e.printStackTrace(); } }
From source file:delphsim.model.Resultado.java
/** * Mtodo esttico que exporta una grfica <CODE>JFreeChart</CODE> al * formato de imagen vectorial SVG./* w ww.j a va2 s . c om*/ * @param destino El fichero de destino. * @param chart La grfica a exportar. * @param anchura Anchura de la imagen final. * @param altura Altura de la imagen final. * @throws java.io.IOException Si hubiera algn problema al crear el archivo en disco. */ public static void exportarComoSVG(File destino, JFreeChart chart, int anchura, int altura) throws IOException { // Para SVG utilizamos la librera Batik // Obtener un DOMImplementation DOMImplementation domImpl = GenericDOMImplementation.getDOMImplementation(); // Crear una instancia de org.w3c.dom.Document String svgNS = "http://www.w3.org/2000/svg"; Document document = domImpl.createDocument(svgNS, "svg", null); // Crear una instancia del Generador de SVGs SVGGraphics2D svgGenerator = new SVGGraphics2D(document); // Dibujar la grfica en el Generador de SVGs chart.draw(svgGenerator, new Rectangle(anchura, altura)); // Escribir el archivo SVG OutputStream outputStream = new FileOutputStream(destino); Writer out = new OutputStreamWriter(outputStream, "UTF-8"); svgGenerator.stream(out, true /* utilizar CSS */); outputStream.flush(); outputStream.close(); }