/** * Save a Chart as an image file * * @param chart * @param fileName * @param bitmapFormat * @throws IOException */ public static void saveBitmap(Chart chart, String fileName, BitmapFormat bitmapFormat) throws IOException { OutputStream out = new FileOutputStream(addFileExtension(fileName, bitmapFormat)); try { saveBitmap(chart, out, bitmapFormat); } finally { out.close(); } }
try { if (fileChooser.getFileFilter() == null) { BitmapEncoder.saveBitmap(chart, theFileToSave.getCanonicalPath(), BitmapFormat.PNG); } else if (fileChooser.getFileFilter().getDescription().equals("*.jpg,*.JPG")) { BitmapEncoder.saveJPGWithQuality( chart, BitmapEncoder.addFileExtension(theFileToSave.getCanonicalPath(), BitmapFormat.JPG), 1.0f); } else if (fileChooser.getFileFilter().getDescription().equals("*.png,*.PNG")) { BitmapEncoder.saveBitmap(chart, theFileToSave.getCanonicalPath(), BitmapFormat.PNG); } else if (fileChooser.getFileFilter().getDescription().equals("*.bmp,*.BMP")) { BitmapEncoder.saveBitmap(chart, theFileToSave.getCanonicalPath(), BitmapFormat.BMP); } else if (fileChooser.getFileFilter().getDescription().equals("*.gif,*.GIF")) { BitmapEncoder.saveBitmap(chart, theFileToSave.getCanonicalPath(), BitmapFormat.GIF); } else if (fileChooser.getFileFilter().getDescription().equals("*.svg,*.SVG")) { VectorGraphicsEncoder.saveVectorGraphic(
/** * Save list of Charts into a given stream. Does not close the target stream automatically at the * end of the operation. Function assumes that all charts are the same size (width, height). * Number of charts should equal rows multiplied by cols. * * @param charts * @param rows number of rows * @param cols number of columns * @param targetStream * @param bitmapFormat * @throws IOException */ public static void saveBitmap( List<Chart> charts, Integer rows, Integer cols, OutputStream targetStream, BitmapEncoder.BitmapFormat bitmapFormat) throws IOException { List<BufferedImage> chartImages = new LinkedList<BufferedImage>(); for (Chart c : charts) chartImages.add(getBufferedImage(c)); BufferedImage bufferedImage = mergeImages(chartImages, rows, cols); ImageIO.write(bufferedImage, bitmapFormat.toString().toLowerCase(), targetStream); }
public static void main(String[] args) throws IOException { double[] xData = new double[] {0.0, 1.0, 2.0}; double[] yData = new double[] {2.0, 1.0, 0.0}; // Create Chart XYChart chart = QuickChart.getChart("Sample Chart", "X", "Y", "y(x)", xData, yData); // Show it new SwingWrapper<XYChart>(chart).displayChart(); // Save it BitmapEncoder.saveBitmap(chart, "./Sample_Chart", BitmapFormat.PNG); // or save it in high-res BitmapEncoder.saveBitmapWithDPI(chart, "./Sample_Chart_300_DPI", BitmapFormat.PNG, 300); } }
public static void main(String[] args) throws Exception { List<Chart> charts = Arrays.asList( new Chart[] { createChart("chart1", new double[] {2.0, 1.0, 0.0}), createChart("chart2", new double[] {3.0, 4.0, 0.0}), createChart("chart3", new double[] {4.0, 1.5, 0.0}), createChart("chart4", new double[] {2.0, 3.0, 0.0}), createChart("chart5", new double[] {4.0, 1.0, 0.0}), createChart("chart6", new double[] {5.0, 2.0, 0.0}) }); BitmapEncoder.saveBitmap(charts, 2, 3, "./Sample_Charts", BitmapFormat.PNG); }
public static void main(String[] args) throws Exception { double[] yData = new double[] {2.0, 1.0, 0.0}; // Create Chart XYChart chart = new XYChart(500, 400); chart.setTitle("Sample Chart"); chart.setXAxisTitle("X"); chart.setXAxisTitle("Y"); XYSeries series = chart.addSeries("y(x)", null, yData); series.setMarker(SeriesMarkers.CIRCLE); BitmapEncoder.saveBitmap(chart, "./Sample_Chart", BitmapFormat.PNG); BitmapEncoder.saveBitmap(chart, "./Sample_Chart", BitmapFormat.JPG); BitmapEncoder.saveJPGWithQuality(chart, "./Sample_Chart_With_Quality.jpg", 0.95f); BitmapEncoder.saveBitmap(chart, "./Sample_Chart", BitmapFormat.BMP); BitmapEncoder.saveBitmap(chart, "./Sample_Chart", BitmapFormat.GIF); BitmapEncoder.saveBitmapWithDPI(chart, "./Sample_Chart_300_DPI", BitmapFormat.PNG, 300); BitmapEncoder.saveBitmapWithDPI(chart, "./Sample_Chart_300_DPI", BitmapFormat.JPG, 300); BitmapEncoder.saveBitmapWithDPI(chart, "./Sample_Chart_300_DPI", BitmapFormat.GIF, 300); VectorGraphicsEncoder.saveVectorGraphic(chart, "./Sample_Chart", VectorGraphicsFormat.EPS); VectorGraphicsEncoder.saveVectorGraphic(chart, "./Sample_Chart", VectorGraphicsFormat.PDF); VectorGraphicsEncoder.saveVectorGraphic(chart, "./Sample_Chart", VectorGraphicsFormat.SVG); } }
setDPI(metadata, DPI); File file = new File(addFileExtension(fileName, bitmapFormat)); FileImageOutputStream output = new FileImageOutputStream(file); writer.setOutput(output);
/** * Write a Chart into a given stream. Does not close the target stream automatically at the end of * the operation * * @param chart * @param targetStream * @param bitmapFormat * @throws IOException */ public static void saveBitmap(Chart chart, OutputStream targetStream, BitmapFormat bitmapFormat) throws IOException { BufferedImage bufferedImage = getBufferedImage(chart); ImageIO.write(bufferedImage, bitmapFormat.toString().toLowerCase(), targetStream); }
public static void saveBitmap(List<Chart> charts, Integer rows, Integer cols, String fileName, BitmapEncoder.BitmapFormat bitmapFormat) throws IOException { BufferedImage bufferedImage = mergeImages( charts.stream().map(BitmapEncoder::getBufferedImage).collect(Collectors.toList()), rows, cols ); OutputStream out = new FileOutputStream(BitmapEncoder.addFileExtension(fileName, bitmapFormat)); try { ImageIO.write(bufferedImage, bitmapFormat.toString().toLowerCase(), out); } finally { out.close(); } }
@Override public PNGImageNotebookOutput convert(Object object) { Chart chart = (Chart) object; byte[] bt; try { bt = BitmapEncoder.getBitmapBytes(chart, BitmapEncoder.BitmapFormat.PNG); Base64.getEncoder().encodeToString(bt); return new PNGImageNotebookOutput(Base64.getEncoder().encodeToString(bt)); } catch (IOException ex) { Logger.getLogger(ChartToPNGNotebookConverter.class.getName()).log(Level.SEVERE, null, ex); } return null; }
public static void main(String[] args) throws IOException { int numCharts = 4; List<Chart> charts = new ArrayList<Chart>(); for (int i = 0; i < numCharts; i++) { XYChart chart = new XYChartBuilder().xAxisTitle("X").yAxisTitle("Y").width(600).height(400).build(); chart.getStyler().setYAxisMin(-10.0); chart.getStyler().setYAxisMax(10.0); XYSeries series = chart.addSeries("" + i, null, getRandomWalk(200)); series.setMarker(SeriesMarkers.NONE); charts.add(chart); } new SwingWrapper<Chart>(charts).displayChartMatrix(); BitmapEncoder.saveBitmap(charts, 2, 2, "./Sample_Chart_Matrix", BitmapEncoder.BitmapFormat.PNG); }
setDPI(metadata, DPI); File file = new File(addFileExtension(fileName, bitmapFormat)); FileImageOutputStream output = new FileImageOutputStream(file); writer.setOutput(output);
/** * Generates a byte[] for a given chart * * @param chart * @return a byte[] for a given chart * @throws IOException */ public static byte[] getBitmapBytes(Chart chart, BitmapFormat bitmapFormat) throws IOException { BufferedImage bufferedImage = getBufferedImage(chart); byte[] imageInBytes; ByteArrayOutputStream baos = new ByteArrayOutputStream(); ImageIO.write(bufferedImage, bitmapFormat.toString().toLowerCase(), baos); try { baos.flush(); imageInBytes = baos.toByteArray(); } finally { baos.close(); } return imageInBytes; }
/** * Save list of Charts as an image file. Function assumes that all charts are the same size * (width, height). Number of charts should equal rows multiplied by cols. * * @param charts * @param rows number of rows * @param cols number of columns * @param fileName * @param bitmapFormat * @throws IOException */ public static void saveBitmap( List<Chart> charts, Integer rows, Integer cols, String fileName, BitmapEncoder.BitmapFormat bitmapFormat) throws IOException { OutputStream out = new FileOutputStream(addFileExtension(fileName, bitmapFormat)); try { saveBitmap(charts, rows, cols, out, bitmapFormat); } finally { out.close(); } }
try { if (fileChooser.getFileFilter() == null) { BitmapEncoder.saveBitmap(chart, theFileToSave.getCanonicalPath(), BitmapFormat.PNG); } else if (fileChooser.getFileFilter().getDescription().equals("*.jpg,*.JPG")) { BitmapEncoder.saveJPGWithQuality( chart, BitmapEncoder.addFileExtension(theFileToSave.getCanonicalPath(), BitmapFormat.JPG), 1.0f); } else if (fileChooser.getFileFilter().getDescription().equals("*.png,*.PNG")) { BitmapEncoder.saveBitmap(chart, theFileToSave.getCanonicalPath(), BitmapFormat.PNG); } else if (fileChooser.getFileFilter().getDescription().equals("*.bmp,*.BMP")) { BitmapEncoder.saveBitmap(chart, theFileToSave.getCanonicalPath(), BitmapFormat.BMP); } else if (fileChooser.getFileFilter().getDescription().equals("*.gif,*.GIF")) { BitmapEncoder.saveBitmap(chart, theFileToSave.getCanonicalPath(), BitmapFormat.GIF); } else if (fileChooser.getFileFilter().getDescription().equals("*.svg,*.SVG")) { VectorGraphicsEncoder.saveVectorGraphic(
public void saveChart(String fileName, BitmapFormat format) throws IOException { for (String chart : this.charts.keySet()) { BitmapEncoder.saveBitmap(this.charts.get(chart), fileName + "_" + chart, format); } }
/** * Save list of Charts into a given stream. Does not close the target stream automatically at the * end of the operation. Function assumes that all charts are the same size (width, height). * Number of charts should equal rows multiplied by cols. * * @param charts * @param rows number of rows * @param cols number of columns * @param targetStream * @param bitmapFormat * @throws IOException */ public static void saveBitmap( List<Chart> charts, Integer rows, Integer cols, OutputStream targetStream, BitmapEncoder.BitmapFormat bitmapFormat) throws IOException { List<BufferedImage> chartImages = new LinkedList<BufferedImage>(); for (Chart c : charts) chartImages.add(getBufferedImage(c)); BufferedImage bufferedImage = mergeImages(chartImages, rows, cols); ImageIO.write(bufferedImage, bitmapFormat.toString().toLowerCase(), targetStream); }
/** * Save a Chart as a JPEG file * * @param chart * @param fileName * @param quality - a float between 0 and 1 (1 = maximum quality) * @throws IOException */ public static void saveJPGWithQuality(Chart chart, String fileName, float quality) throws IOException { BufferedImage bufferedImage = getBufferedImage(chart); Iterator<ImageWriter> iter = ImageIO.getImageWritersByFormatName("jpeg"); ImageWriter writer = iter.next(); // instantiate an ImageWriteParam object with default compression options ImageWriteParam iwp = writer.getDefaultWriteParam(); iwp.setCompressionMode(ImageWriteParam.MODE_EXPLICIT); iwp.setCompressionQuality(quality); File file = new File(fileName); FileImageOutputStream output = new FileImageOutputStream(file); try { writer.setOutput(output); IIOImage image = new IIOImage(bufferedImage, null, null); writer.write(null, image, iwp); writer.dispose(); } finally { output.close(); } }
/** * Save a Chart as an image file * * @param chart * @param fileName * @param bitmapFormat * @throws IOException */ public static void saveBitmap(Chart chart, String fileName, BitmapFormat bitmapFormat) throws IOException { OutputStream out = new FileOutputStream(addFileExtension(fileName, bitmapFormat)); try { saveBitmap(chart, out, bitmapFormat); } finally { out.close(); } }
public void saveChart(String fileName, BitmapFormat format) throws IOException { for (String chart : this.charts.keySet()) { BitmapEncoder.saveBitmap(this.charts.get(chart), fileName + "_" + chart, format); } }