List of usage examples for org.jfree.chart JFreeChart draw
@Override public void draw(Graphics2D g2, Rectangle2D area)
From source file:peakml.graphics.JFreeChartTools.java
/** * This method writes the given graph to the output stream in the PNG format. * // w w w . ja v a 2s . com * @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 writeAsPNG(OutputStream out, JFreeChart chart, int width, int height) throws IOException { BufferedImage graph_img = new BufferedImage(800, 500, BufferedImage.TYPE_INT_ARGB); chart.draw(graph_img.createGraphics(), new java.awt.Rectangle(0, 0, 800, 500)); javax.imageio.ImageIO.write(graph_img, "png", out); }
From source file:jmbench.plots.UtilPlotPdf.java
protected static BufferedImage draw(JFreeChart chart, int width, int height) { BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); Graphics2D g2 = img.createGraphics(); chart.draw(g2, new Rectangle2D.Double(0, 0, width, height)); g2.dispose();//from w ww .j a va 2s.co m return img; }
From source file:de.bund.bfr.knime.chart.ChartUtils.java
public static ImagePortObject getImage(JFreeChart chart, boolean asSvg, int width, int height) { if (asSvg) {//from ww w .j a v a 2 s . co m SVGDocument document = (SVGDocument) new SVGDOMImplementation().createDocument(null, "svg", null); SVGGraphics2D g = new SVGGraphics2D(document); g.setSVGCanvasSize(new Dimension(width, height)); if (chart != null) { chart.draw(g, new Rectangle2D.Double(0, 0, width, height)); } g.dispose(); document.replaceChild(g.getRoot(), document.getDocumentElement()); return new ImagePortObject(new SvgImageContent(document), new ImagePortObjectSpec(SvgCell.TYPE)); } else { try { BufferedImage img = chart != null ? chart.createBufferedImage(width, height) : new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); return new ImagePortObject(new PNGImageContent(ChartUtilities.encodeAsPNG(img)), new ImagePortObjectSpec(PNGImageContent.TYPE)); } catch (IOException e) { e.printStackTrace(); return null; } } }
From source file:de.bund.bfr.knime.pmm.common.chart.ChartUtilities.java
public static SvgImageContent convertToSVGImageContent(JFreeChart chart, int width, int height) { SVGDOMImplementation domImpl = new SVGDOMImplementation(); Document document = domImpl.createDocument(null, "svg", null); SVGGraphics2D svgGenerator = new SVGGraphics2D(document); svgGenerator.setSVGCanvasSize(new Dimension(width, height)); if (chart != null) { chart.draw(svgGenerator, new Rectangle2D.Double(0, 0, width, height)); }/*from ww w .ja v a 2 s . com*/ svgGenerator.finalize(); document.replaceChild(svgGenerator.getRoot(), document.getDocumentElement()); return new SvgImageContent((SVGDocument) document, true); }
From source file:org.jfreechart.SVGExporter.java
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();// www. j a va2 s .c o m outputStream.close(); }
From source file:com.redhat.thermostat.byteman.plot.impl.TestPlotRenderer.java
private static void chartToSvg(JFreeChart chart, int width, int height, String filename) throws Exception { Writer writer = null;//from w ww. j av a2 s . c o m try { DOMImplementation domImpl = GenericDOMImplementation.getDOMImplementation(); Document document = domImpl.createDocument(null, "svg", null); SVGGraphics2D svgGenerator = new SVGGraphics2D(document); svgGenerator.setSVGCanvasSize(new Dimension(width, height)); chart.draw(svgGenerator, new Rectangle(width, height)); OutputStream os = new FileOutputStream(filename + ".svg"); writer = new BufferedWriter(new OutputStreamWriter(os, Charset.forName("UTF-8"))); svgGenerator.stream(writer); } finally { closeQuietly(writer); } }
From source file:org.dkpro.lab.reporting.ChartUtil.java
/** * Exports a JFreeChart to a SVG file./*from w ww .j av a 2s .c o m*/ * * @param chart JFreeChart to export * @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. * @see <a href="http://dolf.trieschnigg.nl/jfreechart/">Saving JFreeChart as SVG vector images * using Batik</a> */ public static void writeChartAsSVG(OutputStream aOS, JFreeChart chart, int aWidth, int aHeight) 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, new Rectangle(aWidth, aHeight)); // Write svg file Writer out = new OutputStreamWriter(aOS, "UTF-8"); svgGenerator.stream(out, true /* use css */); out.flush(); out.close(); }
From source file:com.unicornlabs.kabouter.reporting.PowerReport.java
public static void GeneratePowerReport(Date startDate, Date endDate) { try {/*from w w w .j a va 2 s . c om*/ Historian theHistorian = (Historian) BusinessObjectManager.getBusinessObject(Historian.class.getName()); ArrayList<String> powerLogDeviceIds = theHistorian.getPowerLogDeviceIds(); Document document = new Document(PageSize.A4, 50, 50, 50, 50); File outputFile = new File("PowerReport.pdf"); outputFile.createNewFile(); PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(outputFile)); document.open(); document.add(new Paragraph("Power Report for " + startDate.toString() + " to " + endDate.toString())); document.newPage(); DecimalFormat df = new DecimalFormat("#.###"); for (String deviceId : powerLogDeviceIds) { ArrayList<Powerlog> powerlogs = theHistorian.getPowerlogs(deviceId, startDate, endDate); double total = 0; double max = 0; Date maxTime = startDate; double average = 0; XYSeries series = new XYSeries(deviceId); XYDataset dataset = new XYSeriesCollection(series); for (Powerlog log : powerlogs) { total += log.getPower(); if (log.getPower() > max) { max = log.getPower(); maxTime = log.getId().getLogtime(); } series.add(log.getId().getLogtime().getTime(), log.getPower()); } average = total / powerlogs.size(); document.add(new Paragraph("\nDevice: " + deviceId)); document.add(new Paragraph("Average Power Usage: " + df.format(average))); document.add(new Paragraph("Maximum Power Usage: " + df.format(max) + " at " + maxTime.toString())); document.add(new Paragraph("Total Power Usage: " + df.format(total))); //Create a custom date axis to display dates on the X axis DateAxis dateAxis = new DateAxis("Date"); //Make the labels vertical dateAxis.setVerticalTickLabels(true); //Create the power axis NumberAxis powerAxis = new NumberAxis("Power"); //Set both axes to auto range for their values powerAxis.setAutoRange(true); dateAxis.setAutoRange(true); //Create the tooltip generator StandardXYToolTipGenerator ttg = new StandardXYToolTipGenerator("{0}: {2}", new SimpleDateFormat("yyyy/MM/dd HH:mm"), NumberFormat.getInstance()); //Set the renderer StandardXYItemRenderer renderer = new StandardXYItemRenderer(StandardXYItemRenderer.LINES, ttg, null); //Create the plot XYPlot plot = new XYPlot(dataset, dateAxis, powerAxis, renderer); //Create the chart JFreeChart myChart = new JFreeChart(deviceId, JFreeChart.DEFAULT_TITLE_FONT, plot, true); PdfContentByte pcb = writer.getDirectContent(); PdfTemplate tp = pcb.createTemplate(480, 360); Graphics2D g2d = tp.createGraphics(480, 360, new DefaultFontMapper()); Rectangle2D r2d = new Rectangle2D.Double(0, 0, 480, 360); myChart.draw(g2d, r2d); g2d.dispose(); pcb.addTemplate(tp, 0, 0); document.newPage(); } document.close(); JOptionPane.showMessageDialog(null, "Report Generated."); Desktop.getDesktop().open(outputFile); } catch (FileNotFoundException fnfe) { JOptionPane.showMessageDialog(null, "Unable To Open File For Writing, Make Sure It Is Not Currently Open"); } catch (IOException ex) { Logger.getLogger(PowerReport.class.getName()).log(Level.SEVERE, null, ex); } catch (DocumentException ex) { Logger.getLogger(PowerReport.class.getName()).log(Level.SEVERE, null, ex); } }
From source file:net.pickapack.chart.ChartPdfExporter.java
/** * Export the specified chart to the specified PDF file. * * @param chart the chart//w w w. j a va 2 s .c om * @param width the width of the PDF file * @param height the height of the PDF file * @param fileName the PDF file name */ public static void exportPdf(JFreeChart chart, int width, int height, String fileName) { try { Document document = new Document(new Rectangle(width, height)); PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(fileName)); 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(); } catch (DocumentException e) { throw new RuntimeException(e); } catch (FileNotFoundException e) { throw new RuntimeException(e); } }
From source file:org.encog.workbench.util.graph.DocumentPDF.java
public static void savePDF(File filename, JFreeChart chart, int width, int height) { try {/*from w w w. j ava 2 s . c o m*/ // step 1 Document document = new Document(new Rectangle(width, height)); // step 2 PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(filename)); // step 3 document.open(); // step 4 PdfContentByte canvas = writer.getDirectContent(); Graphics2D g2 = canvas.createGraphics(width, height); Rectangle2D area = new Rectangle2D.Double(0, 0, width, height); chart.draw(g2, area); g2.dispose(); // step 5 document.close(); } catch (DocumentException ex) { throw new WorkBenchError(ex); } catch (IOException ex) { throw new WorkBenchError(ex); } }