From e18cdbb455a65f678629474c1704a0173bff8bf0 Mon Sep 17 00:00:00 2001 From: Gerrit Grunwald Date: Tue, 5 Dec 2023 08:52:23 +0100 Subject: [PATCH] Fix for issue #118 and #119 Added another method that returns a BufferedImage renderToImage(w,h) as requested in issue #118 and (hopefully) fixed the signing issue #119 --- build.gradle | 6 ++-- .../java/eu/hansolo/fx/charts/ArcChart.java | 11 ++++++ .../java/eu/hansolo/fx/charts/BarChart.java | 11 ++++++ .../java/eu/hansolo/fx/charts/BoxPlot.java | 12 +++++++ .../java/eu/hansolo/fx/charts/BoxPlots.java | 11 ++++++ .../eu/hansolo/fx/charts/BubbleChart.java | 11 ++++++ .../eu/hansolo/fx/charts/BubbleGridChart.java | 11 ++++++ .../eu/hansolo/fx/charts/CandleChart.java | 11 ++++++ .../eu/hansolo/fx/charts/CircularPlot.java | 11 ++++++ .../hansolo/fx/charts/ComparisonBarChart.java | 11 ++++++ .../fx/charts/ComparisonRingChart.java | 11 ++++++ .../fx/charts/ConcentricRingChart.java | 11 ++++++ .../eu/hansolo/fx/charts/CoxcombChart.java | 11 ++++++ .../java/eu/hansolo/fx/charts/CubeChart.java | 11 ++++++ .../java/eu/hansolo/fx/charts/MatrixPane.java | 12 +++++++ .../eu/hansolo/fx/charts/NestedBarChart.java | 11 ++++++ .../eu/hansolo/fx/charts/PanelBarChart.java | 11 ++++++ .../fx/charts/ParallelCoordinatesChart.java | 11 ++++++ .../eu/hansolo/fx/charts/PixelMatrix.java | 11 ++++++ .../java/eu/hansolo/fx/charts/PolarChart.java | 11 ++++++ .../eu/hansolo/fx/charts/RadialTidyTree.java | 11 ++++++ .../java/eu/hansolo/fx/charts/SankeyPlot.java | 11 ++++++ .../eu/hansolo/fx/charts/SectorChart.java | 11 ++++++ .../eu/hansolo/fx/charts/StreamChart.java | 11 ++++++ .../eu/hansolo/fx/charts/SunburstChart.java | 11 ++++++ .../java/eu/hansolo/fx/charts/XYChart.java | 11 ++++++ .../java/eu/hansolo/fx/charts/YChart.java | 11 ++++++ .../fx/charts/areaheatmap/AreaHeatMap.java | 11 ++++++ .../hansolo/fx/charts/pareto/ParetoPanel.java | 11 ++++++ .../eu/hansolo/fx/charts/tools/Helper.java | 35 +++++++++++++++++-- .../fx/charts/voronoi/VoronoiChart.java | 11 ++++++ .../eu/hansolo/fx/charts/wafermap/DieMap.java | 12 +++++++ .../hansolo/fx/charts/wafermap/WaferMap.java | 11 ++++++ 33 files changed, 379 insertions(+), 6 deletions(-) diff --git a/build.gradle b/build.gradle index c7143c8..a1445ea 100644 --- a/build.gradle +++ b/build.gradle @@ -127,10 +127,10 @@ artifacts { signing { if (gpgkey && gpgpassphrase) { useInMemoryPgpKeys(gpgkey, gpgpassphrase) - } - sign configurations.archives - sign publishing.publications + sign configurations.archives + sign publishing.publications + } } // Remove 'thirdPartyCompatibility' from JavaFX runtimeElements variant diff --git a/src/main/java/eu/hansolo/fx/charts/ArcChart.java b/src/main/java/eu/hansolo/fx/charts/ArcChart.java index be49c10..4563729 100644 --- a/src/main/java/eu/hansolo/fx/charts/ArcChart.java +++ b/src/main/java/eu/hansolo/fx/charts/ArcChart.java @@ -48,6 +48,7 @@ import javafx.scene.shape.StrokeLineCap; import javafx.scene.text.TextAlignment; +import java.awt.image.BufferedImage; import java.util.Arrays; import java.util.Collections; import java.util.Comparator; @@ -549,6 +550,16 @@ public boolean renderToImage(final String filename, final int width, final int h return Helper.renderToImage(ArcChart.this, width, height, filename); } + /** + * Calling this method will render this chart/plot to a png given of the given width and height + * @param width The width of the final image in pixels (if < 0 then 400 and if > 4096 then 4096) + * @param height The height of the final image in pixels (if < 0 then 400 and if > 4096 then 4096) + * @return A BufferedImage of this chart in the given dimension + */ + public BufferedImage renderToImage(final int width, final int height) { + return Helper.renderToImage(ArcChart.this, width, height); + } + private void validateData() { connections.clear(); Map incoming = new HashMap<>(getItems().size()); diff --git a/src/main/java/eu/hansolo/fx/charts/BarChart.java b/src/main/java/eu/hansolo/fx/charts/BarChart.java index 28d8ff3..238937a 100644 --- a/src/main/java/eu/hansolo/fx/charts/BarChart.java +++ b/src/main/java/eu/hansolo/fx/charts/BarChart.java @@ -63,6 +63,7 @@ import javafx.scene.text.Font; import javafx.scene.text.TextAlignment; +import java.awt.image.BufferedImage; import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; @@ -757,6 +758,16 @@ public boolean renderToImage(final String filename, final int width, final int h return Helper.renderToImage(BarChart.this, width, height, filename); } + /** + * Calling this method will render this chart/plot to a png given of the given width and height + * @param width The width of the final image in pixels (if < 0 then 400 and if > 4096 then 4096) + * @param height The height of the final image in pixels (if < 0 then 400 and if > 4096 then 4096) + * @return A BufferedImage of this chart in the given dimension + */ + public BufferedImage renderToImage(final int width, final int height) { + return Helper.renderToImage(BarChart.this, width, height); + } + private void handleMouseEvents(final MouseEvent evt) { final double x = evt.getX(); final double y = evt.getY(); diff --git a/src/main/java/eu/hansolo/fx/charts/BoxPlot.java b/src/main/java/eu/hansolo/fx/charts/BoxPlot.java index 5719505..fc96059 100644 --- a/src/main/java/eu/hansolo/fx/charts/BoxPlot.java +++ b/src/main/java/eu/hansolo/fx/charts/BoxPlot.java @@ -47,6 +47,7 @@ import javafx.scene.paint.Color; import javafx.scene.text.TextAlignment; +import java.awt.image.BufferedImage; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; @@ -599,6 +600,17 @@ public boolean renderToImage(final String filename, final int width, final int h return Helper.renderToImage(BoxPlot.this, width, height, filename); } + /** + * Calling this method will render this chart/plot to a png given of the given width and height + * @param width The width of the final image in pixels (if < 0 then 400 and if > 4096 then 4096) + * @param height The height of the final image in pixels (if < 0 then 400 and if > 4096 then 4096) + * @return A BufferedImage of this chart in the given dimension + */ + public BufferedImage renderToImage(final int width, final int height) { + return Helper.renderToImage(BoxPlot.this, width, height); + } + + // ******************** Layout ******************************************** @Override public String getUserAgentStylesheet() { if (null == userAgentStyleSheet) { userAgentStyleSheet = BoxPlot.class.getResource("chart.css").toExternalForm(); } diff --git a/src/main/java/eu/hansolo/fx/charts/BoxPlots.java b/src/main/java/eu/hansolo/fx/charts/BoxPlots.java index 996c45c..0802fbb 100644 --- a/src/main/java/eu/hansolo/fx/charts/BoxPlots.java +++ b/src/main/java/eu/hansolo/fx/charts/BoxPlots.java @@ -48,6 +48,7 @@ import javafx.scene.paint.Color; import javafx.scene.text.TextAlignment; +import java.awt.image.BufferedImage; import java.util.ArrayList; import java.util.Comparator; import java.util.LinkedHashMap; @@ -619,6 +620,16 @@ public boolean renderToImage(final String filename, final int width, final int h return Helper.renderToImage(BoxPlots.this, width, height, filename); } + /** + * Calling this method will render this chart/plot to a png given of the given width and height + * @param width The width of the final image in pixels (if < 0 then 400 and if > 4096 then 4096) + * @param height The height of the final image in pixels (if < 0 then 400 and if > 4096 then 4096) + * @return A BufferedImage of this chart in the given dimension + */ + public BufferedImage renderToImage(final int width, final int height) { + return Helper.renderToImage(BoxPlots.this, width, height); + } + // ******************** Layout ******************************************** private void resize() { diff --git a/src/main/java/eu/hansolo/fx/charts/BubbleChart.java b/src/main/java/eu/hansolo/fx/charts/BubbleChart.java index 006cc34..9f8d12d 100644 --- a/src/main/java/eu/hansolo/fx/charts/BubbleChart.java +++ b/src/main/java/eu/hansolo/fx/charts/BubbleChart.java @@ -37,6 +37,7 @@ import javafx.scene.layout.Region; import javafx.scene.paint.Color; +import java.awt.image.BufferedImage; import java.util.ArrayList; import java.util.Arrays; import java.util.Comparator; @@ -205,6 +206,16 @@ public boolean renderToImage(final String filename, final int width, final int h return Helper.renderToImage(BubbleChart.this, width, height, filename); } + /** + * Calling this method will render this chart/plot to a png given of the given width and height + * @param width The width of the final image in pixels (if < 0 then 400 and if > 4096 then 4096) + * @param height The height of the final image in pixels (if < 0 then 400 and if > 4096 then 4096) + * @return A BufferedImage of this chart in the given dimension + */ + public BufferedImage renderToImage(final int width, final int height) { + return Helper.renderToImage(BubbleChart.this, width, height); + } + // ******************** Layout ******************************************** private void update() { diff --git a/src/main/java/eu/hansolo/fx/charts/BubbleGridChart.java b/src/main/java/eu/hansolo/fx/charts/BubbleGridChart.java index 1572eff..6b18dff 100644 --- a/src/main/java/eu/hansolo/fx/charts/BubbleGridChart.java +++ b/src/main/java/eu/hansolo/fx/charts/BubbleGridChart.java @@ -51,6 +51,7 @@ import javafx.scene.text.Font; import javafx.scene.text.TextAlignment; +import java.awt.image.BufferedImage; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; @@ -633,6 +634,16 @@ public boolean renderToImage(final String filename, final int width, final int h return Helper.renderToImage(BubbleGridChart.this, width, height, filename); } + /** + * Calling this method will render this chart/plot to a png given of the given width and height + * @param width The width of the final image in pixels (if < 0 then 400 and if > 4096 then 4096) + * @param height The height of the final image in pixels (if < 0 then 400 and if > 4096 then 4096) + * @return A BufferedImage of this chart in the given dimension + */ + public BufferedImage renderToImage(final int width, final int height) { + return Helper.renderToImage(BubbleGridChart.this, width, height); + } + private void sort() { sortCategoryX(getSortTopicX(), getSortOrderX()); sortCategoryY(getSortTopicY(), getSortOrderY()); diff --git a/src/main/java/eu/hansolo/fx/charts/CandleChart.java b/src/main/java/eu/hansolo/fx/charts/CandleChart.java index 800b710..571f20e 100644 --- a/src/main/java/eu/hansolo/fx/charts/CandleChart.java +++ b/src/main/java/eu/hansolo/fx/charts/CandleChart.java @@ -42,6 +42,7 @@ import javafx.scene.layout.Region; import javafx.scene.paint.Color; +import java.awt.image.BufferedImage; import java.time.LocalDateTime; import java.time.ZoneId; import java.time.format.DateTimeFormatter; @@ -481,6 +482,16 @@ public boolean renderToImage(final String filename, final int width, final int h return Helper.renderToImage(CandleChart.this, width, height, filename); } + /** + * Calling this method will render this chart/plot to a png given of the given width and height + * @param width The width of the final image in pixels (if < 0 then 400 and if > 4096 then 4096) + * @param height The height of the final image in pixels (if < 0 then 400 and if > 4096 then 4096) + * @return A BufferedImage of this chart in the given dimension + */ + public BufferedImage renderToImage(final int width, final int height) { + return Helper.renderToImage(CandleChart.this, width, height); + } + // ******************** Layout ******************************************** @Override public String getUserAgentStylesheet() { diff --git a/src/main/java/eu/hansolo/fx/charts/CircularPlot.java b/src/main/java/eu/hansolo/fx/charts/CircularPlot.java index fe6e25e..1b9051e 100644 --- a/src/main/java/eu/hansolo/fx/charts/CircularPlot.java +++ b/src/main/java/eu/hansolo/fx/charts/CircularPlot.java @@ -16,6 +16,7 @@ package eu.hansolo.fx.charts; +import java.awt.image.BufferedImage; import java.math.BigDecimal; import java.math.RoundingMode; import java.util.Arrays; @@ -475,6 +476,16 @@ public boolean renderToImage(final String filename, final int width, final int h return Helper.renderToImage(CircularPlot.this, width, height, filename); } + /** + * Calling this method will render this chart/plot to a png given of the given width and height + * @param width The width of the final image in pixels (if < 0 then 400 and if > 4096 then 4096) + * @param height The height of the final image in pixels (if < 0 then 400 and if > 4096 then 4096) + * @return A BufferedImage of this chart in the given dimension + */ + public BufferedImage renderToImage(final int width, final int height) { + return Helper.renderToImage(CircularPlot.this, width, height); + } + protected void validateData() { connections.clear(); Map incoming = new HashMap<>(getItems().size()); diff --git a/src/main/java/eu/hansolo/fx/charts/ComparisonBarChart.java b/src/main/java/eu/hansolo/fx/charts/ComparisonBarChart.java index cdb6491..abbeefb 100644 --- a/src/main/java/eu/hansolo/fx/charts/ComparisonBarChart.java +++ b/src/main/java/eu/hansolo/fx/charts/ComparisonBarChart.java @@ -58,6 +58,7 @@ import javafx.scene.text.Font; import javafx.scene.text.TextAlignment; + import java.awt.image.BufferedImage; import java.util.Collections; import java.util.Comparator; import java.util.HashMap; @@ -719,6 +720,16 @@ public boolean renderToImage(final String filename, final int width, final int h return Helper.renderToImage(ComparisonBarChart.this, width, height, filename); } + /** + * Calling this method will render this chart/plot to a png given of the given width and height + * @param width The width of the final image in pixels (if < 0 then 400 and if > 4096 then 4096) + * @param height The height of the final image in pixels (if < 0 then 400 and if > 4096 then 4096) + * @return A BufferedImage of this chart in the given dimension + */ + public BufferedImage renderToImage(final int width, final int height) { + return Helper.renderToImage(ComparisonBarChart.this, width, height); + } + // ******************** Event Handling ************************************ public void addChartEvtObserver(final EvtType type, final EvtObserver observer) { diff --git a/src/main/java/eu/hansolo/fx/charts/ComparisonRingChart.java b/src/main/java/eu/hansolo/fx/charts/ComparisonRingChart.java index ef17550..e3d4f70 100644 --- a/src/main/java/eu/hansolo/fx/charts/ComparisonRingChart.java +++ b/src/main/java/eu/hansolo/fx/charts/ComparisonRingChart.java @@ -50,6 +50,7 @@ import javafx.scene.shape.StrokeLineCap; import javafx.scene.text.TextAlignment; +import java.awt.image.BufferedImage; import java.util.Comparator; import java.util.List; import java.util.Locale; @@ -361,6 +362,16 @@ public boolean renderToImage(final String filename, final int width, final int h return Helper.renderToImage(ComparisonRingChart.this, width, height, filename); } + /** + * Calling this method will render this chart/plot to a png given of the given width and height + * @param width The width of the final image in pixels (if < 0 then 400 and if > 4096 then 4096) + * @param height The height of the final image in pixels (if < 0 then 400 and if > 4096 then 4096) + * @return A BufferedImage of this chart in the given dimension + */ + public BufferedImage renderToImage(final int width, final int height) { + return Helper.renderToImage(ComparisonRingChart.this, width, height); + } + // ******************** Event Handling ************************************ public void addChartEvtObserver(final EvtType type, final EvtObserver observer) { diff --git a/src/main/java/eu/hansolo/fx/charts/ConcentricRingChart.java b/src/main/java/eu/hansolo/fx/charts/ConcentricRingChart.java index 8b21d79..11761b6 100644 --- a/src/main/java/eu/hansolo/fx/charts/ConcentricRingChart.java +++ b/src/main/java/eu/hansolo/fx/charts/ConcentricRingChart.java @@ -50,6 +50,7 @@ import javafx.scene.shape.StrokeLineCap; import javafx.scene.text.TextAlignment; +import java.awt.image.BufferedImage; import java.util.ArrayList; import java.util.Arrays; import java.util.Comparator; @@ -452,6 +453,16 @@ public boolean renderToImage(final String filename, final int width, final int h return Helper.renderToImage(ConcentricRingChart.this, width, height, filename); } + /** + * Calling this method will render this chart/plot to a png given of the given width and height + * @param width The width of the final image in pixels (if < 0 then 400 and if > 4096 then 4096) + * @param height The height of the final image in pixels (if < 0 then 400 and if > 4096 then 4096) + * @return A BufferedImage of this chart in the given dimension + */ + public BufferedImage renderToImage(final int width, final int height) { + return Helper.renderToImage(ConcentricRingChart.this, width, height); + } + // ******************** Event Handling ************************************ public void addChartEvtObserver(final EvtType type, final EvtObserver observer) { diff --git a/src/main/java/eu/hansolo/fx/charts/CoxcombChart.java b/src/main/java/eu/hansolo/fx/charts/CoxcombChart.java index b2c6226..948ec2a 100644 --- a/src/main/java/eu/hansolo/fx/charts/CoxcombChart.java +++ b/src/main/java/eu/hansolo/fx/charts/CoxcombChart.java @@ -51,6 +51,7 @@ import javafx.scene.shape.StrokeLineCap; import javafx.scene.text.TextAlignment; +import java.awt.image.BufferedImage; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; @@ -600,6 +601,16 @@ public boolean renderToImage(final String filename, final int width, final int h return Helper.renderToImage(CoxcombChart.this, width, height, filename); } + /** + * Calling this method will render this chart/plot to a png given of the given width and height + * @param width The width of the final image in pixels (if < 0 then 400 and if > 4096 then 4096) + * @param height The height of the final image in pixels (if < 0 then 400 and if > 4096 then 4096) + * @return A BufferedImage of this chart in the given dimension + */ + public BufferedImage renderToImage(final int width, final int height) { + return Helper.renderToImage(CoxcombChart.this, width, height); + } + private void reorder(final Order ORDER) { if (ORDER == Order.ASCENDING) { sortItemsAscending(); diff --git a/src/main/java/eu/hansolo/fx/charts/CubeChart.java b/src/main/java/eu/hansolo/fx/charts/CubeChart.java index 57a3889..444c95e 100644 --- a/src/main/java/eu/hansolo/fx/charts/CubeChart.java +++ b/src/main/java/eu/hansolo/fx/charts/CubeChart.java @@ -47,6 +47,7 @@ import javafx.scene.text.Font; import javafx.scene.text.TextAlignment; +import java.awt.image.BufferedImage; import java.util.ArrayList; import java.util.List; import java.util.Locale; @@ -455,6 +456,16 @@ public boolean renderToImage(final String filename, final int width, final int h return Helper.renderToImage(CubeChart.this, width, height, filename); } + /** + * Calling this method will render this chart/plot to a png given of the given width and height + * @param width The width of the final image in pixels (if < 0 then 400 and if > 4096 then 4096) + * @param height The height of the final image in pixels (if < 0 then 400 and if > 4096 then 4096) + * @return A BufferedImage of this chart in the given dimension + */ + public BufferedImage renderToImage(final int width, final int height) { + return Helper.renderToImage(CubeChart.this, width, height); + } + private void wrapText(final GraphicsContext ctx, final String text, final double x, final double y, final double maxWidth, final double lineHeight, final int maxNoOfLines) { double tx = x; double ty = y; diff --git a/src/main/java/eu/hansolo/fx/charts/MatrixPane.java b/src/main/java/eu/hansolo/fx/charts/MatrixPane.java index 4842949..98ae54f 100644 --- a/src/main/java/eu/hansolo/fx/charts/MatrixPane.java +++ b/src/main/java/eu/hansolo/fx/charts/MatrixPane.java @@ -35,6 +35,8 @@ import javafx.scene.paint.LinearGradient; import javafx.scene.paint.Paint; +import java.awt.image.BufferedImage; + public class MatrixPane extends Region implements ChartArea { private static final double PREFERRED_WIDTH = 250; @@ -337,6 +339,16 @@ public boolean renderToImage(final String filename, final int width, final int h return Helper.renderToImage(MatrixPane.this, width, height, filename); } + /** + * Calling this method will render this chart/plot to a png given of the given width and height + * @param width The width of the final image in pixels (if < 0 then 400 and if > 4096 then 4096) + * @param height The height of the final image in pixels (if < 0 then 400 and if > 4096 then 4096) + * @return A BufferedImage of this chart in the given dimension + */ + public BufferedImage renderToImage(final int width, final int height) { + return Helper.renderToImage(MatrixPane.this, width, height); + } + // ******************** Draw Chart **************************************** private void drawChart() { diff --git a/src/main/java/eu/hansolo/fx/charts/NestedBarChart.java b/src/main/java/eu/hansolo/fx/charts/NestedBarChart.java index 0785c9a..cd7f74a 100644 --- a/src/main/java/eu/hansolo/fx/charts/NestedBarChart.java +++ b/src/main/java/eu/hansolo/fx/charts/NestedBarChart.java @@ -46,6 +46,7 @@ import javafx.scene.text.Font; import javafx.scene.text.TextAlignment; +import java.awt.image.BufferedImage; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; @@ -345,6 +346,16 @@ public boolean renderToImage(final String filename, final int width, final int h return Helper.renderToImage(NestedBarChart.this, width, height, filename); } + /** + * Calling this method will render this chart/plot to a png given of the given width and height + * @param width The width of the final image in pixels (if < 0 then 400 and if > 4096 then 4096) + * @param height The height of the final image in pixels (if < 0 then 400 and if > 4096 then 4096) + * @return A BufferedImage of this chart in the given dimension + */ + public BufferedImage renderToImage(final int width, final int height) { + return Helper.renderToImage(NestedBarChart.this, width, height); + } + private void sortItems(final List ITEMS, final Order ORDER) { if (Order.ASCENDING == ORDER) { Collections.sort(ITEMS, Comparator.comparingDouble(ChartItem::getValue)); diff --git a/src/main/java/eu/hansolo/fx/charts/PanelBarChart.java b/src/main/java/eu/hansolo/fx/charts/PanelBarChart.java index b19ca9f..223fdb2 100644 --- a/src/main/java/eu/hansolo/fx/charts/PanelBarChart.java +++ b/src/main/java/eu/hansolo/fx/charts/PanelBarChart.java @@ -52,6 +52,7 @@ import javafx.scene.paint.Paint; import javafx.scene.text.TextAlignment; +import java.awt.image.BufferedImage; import java.time.format.TextStyle; import java.util.ArrayList; import java.util.Arrays; @@ -596,6 +597,16 @@ public boolean renderToImage(final String filename, final int width, final int h return Helper.renderToImage(PanelBarChart.this, width, height, filename); } + /** + * Calling this method will render this chart/plot to a png given of the given width and height + * @param width The width of the final image in pixels (if < 0 then 400 and if > 4096 then 4096) + * @param height The height of the final image in pixels (if < 0 then 400 and if > 4096 then 4096) + * @return A BufferedImage of this chart in the given dimension + */ + public BufferedImage renderToImage(final int width, final int height) { + return Helper.renderToImage(PanelBarChart.this, width, height); + } + public void removeAllData() { listOfSeries.clear(); comparisonListOfSeries.clear(); diff --git a/src/main/java/eu/hansolo/fx/charts/ParallelCoordinatesChart.java b/src/main/java/eu/hansolo/fx/charts/ParallelCoordinatesChart.java index 8b2b61f..1468eb8 100644 --- a/src/main/java/eu/hansolo/fx/charts/ParallelCoordinatesChart.java +++ b/src/main/java/eu/hansolo/fx/charts/ParallelCoordinatesChart.java @@ -50,6 +50,7 @@ import javafx.scene.text.Text; import javafx.scene.text.TextAlignment; +import java.awt.image.BufferedImage; import java.math.BigDecimal; import java.math.RoundingMode; import java.util.ArrayList; @@ -516,6 +517,16 @@ public boolean renderToImage(final String filename, final int width, final int h return Helper.renderToImage(ParallelCoordinatesChart.this, width, height, filename); } + /** + * Calling this method will render this chart/plot to a png given of the given width and height + * @param width The width of the final image in pixels (if < 0 then 400 and if > 4096 then 4096) + * @param height The height of the final image in pixels (if < 0 then 400 and if > 4096 then 4096) + * @return A BufferedImage of this chart in the given dimension + */ + public BufferedImage renderToImage(final int width, final int height) { + return Helper.renderToImage(ParallelCoordinatesChart.this, width, height); + } + private double[] getMinMax(final String CATEGORY) { double min = categoryObjectMap.get(CATEGORY).stream().mapToDouble(obj -> obj.getProperties().get(CATEGORY).getValue()).min().getAsDouble(); diff --git a/src/main/java/eu/hansolo/fx/charts/PixelMatrix.java b/src/main/java/eu/hansolo/fx/charts/PixelMatrix.java index b041618..18b8048 100644 --- a/src/main/java/eu/hansolo/fx/charts/PixelMatrix.java +++ b/src/main/java/eu/hansolo/fx/charts/PixelMatrix.java @@ -33,6 +33,7 @@ import javafx.scene.layout.Region; import javafx.scene.paint.Color; +import java.awt.image.BufferedImage; import java.util.concurrent.CopyOnWriteArrayList; @@ -318,6 +319,16 @@ public boolean renderToImage(final String filename, final int width, final int h return Helper.renderToImage(PixelMatrix.this, width, height, filename); } + /** + * Calling this method will render this chart/plot to a png given of the given width and height + * @param width The width of the final image in pixels (if < 0 then 400 and if > 4096 then 4096) + * @param height The height of the final image in pixels (if < 0 then 400 and if > 4096 then 4096) + * @return A BufferedImage of this chart in the given dimension + */ + public BufferedImage renderToImage(final int width, final int height) { + return Helper.renderToImage(PixelMatrix.this, width, height); + } + public void drawMatrix() { ctx.clearRect(0, 0, width, height); switch(pixelShape) { diff --git a/src/main/java/eu/hansolo/fx/charts/PolarChart.java b/src/main/java/eu/hansolo/fx/charts/PolarChart.java index 9f6ed2b..9135cc9 100644 --- a/src/main/java/eu/hansolo/fx/charts/PolarChart.java +++ b/src/main/java/eu/hansolo/fx/charts/PolarChart.java @@ -26,6 +26,7 @@ import javafx.scene.layout.AnchorPane; import javafx.scene.layout.Region; +import java.awt.image.BufferedImage; import java.util.Arrays; import java.util.List; @@ -145,6 +146,16 @@ public boolean renderToImage(final String filename, final int width, final int h return Helper.renderToImage(PolarChart.this, width, height, filename); } + /** + * Calling this method will render this chart/plot to a png given of the given width and height + * @param width The width of the final image in pixels (if < 0 then 400 and if > 4096 then 4096) + * @param height The height of the final image in pixels (if < 0 then 400 and if > 4096 then 4096) + * @return A BufferedImage of this chart in the given dimension + */ + public BufferedImage renderToImage(final int width, final int height) { + return Helper.renderToImage(PolarChart.this, width, height); + } + public XYPane getXYPane() { return xyPane; } public void refresh() { xyPane.redraw(); } diff --git a/src/main/java/eu/hansolo/fx/charts/RadialTidyTree.java b/src/main/java/eu/hansolo/fx/charts/RadialTidyTree.java index 9f1a7bd..efe660f 100644 --- a/src/main/java/eu/hansolo/fx/charts/RadialTidyTree.java +++ b/src/main/java/eu/hansolo/fx/charts/RadialTidyTree.java @@ -59,6 +59,7 @@ import javafx.scene.text.Font; import javafx.scene.text.TextAlignment; +import java.awt.image.BufferedImage; import java.util.ArrayList; import java.util.HashMap; import java.util.List; @@ -549,6 +550,16 @@ public boolean renderToImage(final String filename, final int width, final int h return Helper.renderToImage(RadialTidyTree.this, width, height, filename); } + /** + * Calling this method will render this chart/plot to a png given of the given width and height + * @param width The width of the final image in pixels (if < 0 then 400 and if > 4096 then 4096) + * @param height The height of the final image in pixels (if < 0 then 400 and if > 4096 then 4096) + * @return A BufferedImage of this chart in the given dimension + */ + public BufferedImage renderToImage(final int width, final int height) { + return Helper.renderToImage(RadialTidyTree.this, width, height); + } + private void adjustTextColors() { Color brightColor = getBrightTextColor(); Color darkColor = getDarkTextColor(); diff --git a/src/main/java/eu/hansolo/fx/charts/SankeyPlot.java b/src/main/java/eu/hansolo/fx/charts/SankeyPlot.java index d736147..dee8a7f 100644 --- a/src/main/java/eu/hansolo/fx/charts/SankeyPlot.java +++ b/src/main/java/eu/hansolo/fx/charts/SankeyPlot.java @@ -58,6 +58,7 @@ import javafx.scene.text.TextAlignment; import javafx.stage.Stage; +import java.awt.image.BufferedImage; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; @@ -740,6 +741,16 @@ public boolean renderToImage(final String filename, final int width, final int h return Helper.renderToImage(SankeyPlot.this, width, height, filename); } + /** + * Calling this method will render this chart/plot to a png given of the given width and height + * @param width The width of the final image in pixels (if < 0 then 400 and if > 4096 then 4096) + * @param height The height of the final image in pixels (if < 0 then 400 and if > 4096 then 4096) + * @return A BufferedImage of this chart in the given dimension + */ + public BufferedImage renderToImage(final int width, final int height) { + return Helper.renderToImage(SankeyPlot.this, width, height); + } + private void sortIncomingByOutgoingOrder(final List INCOMING, final List OUTGOING) { Collections.sort(INCOMING, Comparator.comparing(item -> OUTGOING.indexOf(item))); } diff --git a/src/main/java/eu/hansolo/fx/charts/SectorChart.java b/src/main/java/eu/hansolo/fx/charts/SectorChart.java index 2198ecb..2bbc7a8 100644 --- a/src/main/java/eu/hansolo/fx/charts/SectorChart.java +++ b/src/main/java/eu/hansolo/fx/charts/SectorChart.java @@ -51,6 +51,7 @@ import javafx.scene.paint.Color; import javafx.scene.text.TextAlignment; +import java.awt.image.BufferedImage; import java.util.Comparator; import java.util.HashMap; import java.util.List; @@ -529,6 +530,16 @@ public boolean renderToImage(final String filename, final int width, final int h return Helper.renderToImage(SectorChart.this, width, height, filename); } + /** + * Calling this method will render this chart/plot to a png given of the given width and height + * @param width The width of the final image in pixels (if < 0 then 400 and if > 4096 then 4096) + * @param height The height of the final image in pixels (if < 0 then 400 and if > 4096 then 4096) + * @return A BufferedImage of this chart in the given dimension + */ + public BufferedImage renderToImage(final int width, final int height) { + return Helper.renderToImage(SectorChart.this, width, height); + } + // ******************** Style related ************************************* @Override public String getUserAgentStylesheet() { diff --git a/src/main/java/eu/hansolo/fx/charts/StreamChart.java b/src/main/java/eu/hansolo/fx/charts/StreamChart.java index 797017f..3b6b0d5 100644 --- a/src/main/java/eu/hansolo/fx/charts/StreamChart.java +++ b/src/main/java/eu/hansolo/fx/charts/StreamChart.java @@ -48,6 +48,7 @@ import javafx.scene.text.Font; import javafx.scene.text.TextAlignment; +import java.awt.image.BufferedImage; import java.time.DayOfWeek; import java.time.LocalDate; import java.time.ZoneId; @@ -744,6 +745,16 @@ public boolean renderToImage(final String filename, final int width, final int h return Helper.renderToImage(StreamChart.this, width, height, filename); } + /** + * Calling this method will render this chart/plot to a png given of the given width and height + * @param width The width of the final image in pixels (if < 0 then 400 and if > 4096 then 4096) + * @param height The height of the final image in pixels (if < 0 then 400 and if > 4096 then 4096) + * @return A BufferedImage of this chart in the given dimension + */ + public BufferedImage renderToImage(final int width, final int height) { + return Helper.renderToImage(StreamChart.this, width, height); + } + private void prepareData() { if (chartItems.isEmpty()) { return; } diff --git a/src/main/java/eu/hansolo/fx/charts/SunburstChart.java b/src/main/java/eu/hansolo/fx/charts/SunburstChart.java index 623c63d..3f448b1 100644 --- a/src/main/java/eu/hansolo/fx/charts/SunburstChart.java +++ b/src/main/java/eu/hansolo/fx/charts/SunburstChart.java @@ -59,6 +59,7 @@ import javafx.scene.shape.StrokeLineCap; import javafx.scene.text.TextAlignment; +import java.awt.image.BufferedImage; import java.util.ArrayList; import java.util.HashMap; import java.util.List; @@ -579,6 +580,16 @@ public boolean renderToImage(final String filename, final int width, final int h return Helper.renderToImage(SunburstChart.this, width, height, filename); } + /** + * Calling this method will render this chart/plot to a png given of the given width and height + * @param width The width of the final image in pixels (if < 0 then 400 and if > 4096 then 4096) + * @param height The height of the final image in pixels (if < 0 then 400 and if > 4096 then 4096) + * @return A BufferedImage of this chart in the given dimension + */ + public BufferedImage renderToImage(final int width, final int height) { + return Helper.renderToImage(SunburstChart.this, width, height); + } + private void adjustTextColors() { Color brightColor = getBrightTextColor(); Color darkColor = getDarkTextColor(); diff --git a/src/main/java/eu/hansolo/fx/charts/XYChart.java b/src/main/java/eu/hansolo/fx/charts/XYChart.java index 7b60711..8a5686a 100644 --- a/src/main/java/eu/hansolo/fx/charts/XYChart.java +++ b/src/main/java/eu/hansolo/fx/charts/XYChart.java @@ -45,6 +45,7 @@ import javafx.scene.text.Font; import javafx.scene.text.TextAlignment; +import java.awt.image.BufferedImage; import java.lang.System.Logger; import java.lang.System.Logger.Level; import java.util.ArrayList; @@ -375,6 +376,16 @@ public boolean renderToImage(final String filename, final int width, final int h return Helper.renderToImage(XYChart.this, width, height, filename); } + /** + * Calling this method will render this chart/plot to a png given of the given width and height + * @param width The width of the final image in pixels (if < 0 then 400 and if > 4096 then 4096) + * @param height The height of the final image in pixels (if < 0 then 400 and if > 4096 then 4096) + * @return A BufferedImage of this chart in the given dimension + */ + public BufferedImage renderToImage(final int width, final int height) { + return Helper.renderToImage(XYChart.this, width, height); + } + private void drawMarkerCanvas() { if (null == markerCanvas) { return; } markerCtx.clearRect(0, 0, width, height); diff --git a/src/main/java/eu/hansolo/fx/charts/YChart.java b/src/main/java/eu/hansolo/fx/charts/YChart.java index 831f40c..110e168 100644 --- a/src/main/java/eu/hansolo/fx/charts/YChart.java +++ b/src/main/java/eu/hansolo/fx/charts/YChart.java @@ -26,6 +26,7 @@ import javafx.scene.layout.AnchorPane; import javafx.scene.layout.Region; +import java.awt.image.BufferedImage; import java.util.List; @@ -145,6 +146,16 @@ public boolean renderToImage(final String filename, final int width, final int h return Helper.renderToImage(YChart.this, width, height, filename); } + /** + * Calling this method will render this chart/plot to a png given of the given width and height + * @param width The width of the final image in pixels (if < 0 then 400 and if > 4096 then 4096) + * @param height The height of the final image in pixels (if < 0 then 400 and if > 4096 then 4096) + * @return A BufferedImage of this chart in the given dimension + */ + public BufferedImage renderToImage(final int width, final int height) { + return Helper.renderToImage(YChart.this, width, height); + } + public void refresh() { yPane.redraw(); } private void validateSeries() { diff --git a/src/main/java/eu/hansolo/fx/charts/areaheatmap/AreaHeatMap.java b/src/main/java/eu/hansolo/fx/charts/areaheatmap/AreaHeatMap.java index b36158f..3e439e5 100644 --- a/src/main/java/eu/hansolo/fx/charts/areaheatmap/AreaHeatMap.java +++ b/src/main/java/eu/hansolo/fx/charts/areaheatmap/AreaHeatMap.java @@ -44,6 +44,7 @@ import javafx.scene.paint.Stop; import javafx.scene.text.TextAlignment; +import java.awt.image.BufferedImage; import java.util.ArrayList; import java.util.Arrays; import java.util.Comparator; @@ -363,6 +364,16 @@ public boolean renderToImage(final String filename, final int width, final int h return Helper.renderToImage(AreaHeatMap.this, width, height, filename); } + /** + * Calling this method will render this chart/plot to a png given of the given width and height + * @param width The width of the final image in pixels (if < 0 then 400 and if > 4096 then 4096) + * @param height The height of the final image in pixels (if < 0 then 400 and if > 4096 then 4096) + * @return A BufferedImage of this chart in the given dimension + */ + public BufferedImage renderToImage(final int width, final int height) { + return Helper.renderToImage(AreaHeatMap.this, width, height); + } + private Color getColorForValue(final double VALUE, final boolean LEVELS) { double limit = 0.55; double min = -30; diff --git a/src/main/java/eu/hansolo/fx/charts/pareto/ParetoPanel.java b/src/main/java/eu/hansolo/fx/charts/pareto/ParetoPanel.java index d801a2f..bf82fdc 100644 --- a/src/main/java/eu/hansolo/fx/charts/pareto/ParetoPanel.java +++ b/src/main/java/eu/hansolo/fx/charts/pareto/ParetoPanel.java @@ -47,6 +47,7 @@ import javafx.scene.text.Font; import javafx.scene.text.TextAlignment; +import java.awt.image.BufferedImage; import java.util.ArrayList; import java.util.HashMap; import java.util.Iterator; @@ -928,6 +929,16 @@ public boolean renderToImage(final String filename, final int width, final int h return Helper.renderToImage(ParetoPanel.this, width, height, filename); } + /** + * Calling this method will render this chart/plot to a png given of the given width and height + * @param width The width of the final image in pixels (if < 0 then 400 and if > 4096 then 4096) + * @param height The height of the final image in pixels (if < 0 then 400 and if > 4096 then 4096) + * @return A BufferedImage of this chart in the given dimension + */ + public BufferedImage renderToImage(final int width, final int height) { + return Helper.renderToImage(ParetoPanel.this, width, height); + } + // ******************** Resizing ****************************************** private void resize() { diff --git a/src/main/java/eu/hansolo/fx/charts/tools/Helper.java b/src/main/java/eu/hansolo/fx/charts/tools/Helper.java index 7f6e767..de98aa5 100644 --- a/src/main/java/eu/hansolo/fx/charts/tools/Helper.java +++ b/src/main/java/eu/hansolo/fx/charts/tools/Helper.java @@ -62,6 +62,7 @@ import javafx.stage.StageStyle; import javax.imageio.ImageIO; +import java.awt.image.BufferedImage; import java.awt.image.RenderedImage; import java.io.File; import java.io.IOException; @@ -1428,7 +1429,7 @@ public static final Set getKeysByValue(final Map map, final E va * @param width The width of the final image in pixels (if < 0 then 400 and if > 4096 then 4096) * @param height The height of the final image in pixels (if < 0 then 400 and if > 4096 then 4096) * @param filename The path and name of the file e.g. /Users/hansolo/Desktop/sankeyplot.png - * @return + * @return true if the image was successfully saved */ public static final boolean renderToImage(final Node node, final int width, final int height, final String filename) { final int w; @@ -1441,8 +1442,8 @@ public static final boolean renderToImage(final Node node, final int width, fina final StackPane pane = new StackPane(node); pane.setPadding(new Insets(5)); pane.setBackground(new Background(new BackgroundFill(Color.TRANSPARENT, javafx.scene.layout.CornerRadii.EMPTY, Insets.EMPTY))); - Scene scene = new Scene(pane, w, h, Color.TRANSPARENT); - Stage stage = new Stage(); + final Scene scene = new Scene(pane, w, h, Color.TRANSPARENT); + final Stage stage = new Stage(); stage.centerOnScreen(); stage.setScene(scene); stage.initStyle(StageStyle.TRANSPARENT); @@ -1458,4 +1459,32 @@ public static final boolean renderToImage(final Node node, final int width, fina return false; } } + + /** + * A method to save a given node to an image in PNG format. The given JavaFX node will be added to a StackPane that + * comes with a padding of 5px on each side and a transparent background. + * @param node The JavaFX node that should be rendered to an image + * @param width The width of the final image in pixels (if < 0 then 400 and if > 4096 then 4096) + * @param height The height of the final image in pixels (if < 0 then 400 and if > 4096 then 4096) + * @return a buffered image of the given node with the given dimensions + */ + public static final BufferedImage renderToImage(final Node node, final int width, final int height) { + final int w; + final int h; + if (width < 0) { w = 400; } else if (width > 4096) { w = 4096; } else { w = width; } + if (height < 0) { h = 400; } else if (height > 4096) { h = 4096; } else { h = height; } + final WritableImage writableImage = new WritableImage(w, h); + final StackPane pane = new StackPane(node); + pane.setPadding(new Insets(5)); + pane.setBackground(new Background(new BackgroundFill(Color.TRANSPARENT, javafx.scene.layout.CornerRadii.EMPTY, Insets.EMPTY))); + final Scene scene = new Scene(pane, w, h, Color.TRANSPARENT); + final Stage stage = new Stage(); + stage.centerOnScreen(); + stage.setScene(scene); + stage.initStyle(StageStyle.TRANSPARENT); + + pane.snapshot(null, writableImage); + final BufferedImage bufferedImage = SwingFXUtils.fromFXImage(writableImage, null); + return bufferedImage; + } } diff --git a/src/main/java/eu/hansolo/fx/charts/voronoi/VoronoiChart.java b/src/main/java/eu/hansolo/fx/charts/voronoi/VoronoiChart.java index 7bee762..0c65cf8 100644 --- a/src/main/java/eu/hansolo/fx/charts/voronoi/VoronoiChart.java +++ b/src/main/java/eu/hansolo/fx/charts/voronoi/VoronoiChart.java @@ -39,6 +39,7 @@ import javafx.scene.paint.Color; import javafx.scene.paint.Paint; +import java.awt.image.BufferedImage; import java.util.ArrayList; import java.util.HashMap; import java.util.HashSet; @@ -394,6 +395,16 @@ public boolean renderToImage(final String filename, final int width, final int h return Helper.renderToImage(VoronoiChart.this, width, height, filename); } + /** + * Calling this method will render this chart/plot to a png given of the given width and height + * @param width The width of the final image in pixels (if < 0 then 400 and if > 4096 then 4096) + * @param height The height of the final image in pixels (if < 0 then 400 and if > 4096 then 4096) + * @return A BufferedImage of this chart in the given dimension + */ + public BufferedImage renderToImage(final int width, final int height) { + return Helper.renderToImage(VoronoiChart.this, width, height); + } + // ******************** Layout ******************************************* @Override public String getUserAgentStylesheet() { diff --git a/src/main/java/eu/hansolo/fx/charts/wafermap/DieMap.java b/src/main/java/eu/hansolo/fx/charts/wafermap/DieMap.java index 10e31a8..afb1cf3 100644 --- a/src/main/java/eu/hansolo/fx/charts/wafermap/DieMap.java +++ b/src/main/java/eu/hansolo/fx/charts/wafermap/DieMap.java @@ -19,6 +19,7 @@ package eu.hansolo.fx.charts.wafermap; import eu.hansolo.fx.charts.SankeyPlot; +import eu.hansolo.fx.charts.XYChart; import eu.hansolo.fx.charts.tools.Helper; import javafx.beans.DefaultProperty; import javafx.beans.property.BooleanProperty; @@ -37,6 +38,7 @@ import javafx.scene.text.Font; import javafx.scene.text.TextAlignment; +import java.awt.image.BufferedImage; import java.util.List; import java.util.Map; @@ -309,6 +311,16 @@ public boolean renderToImage(final String filename, final int width, final int h return Helper.renderToImage(DieMap.this, width, height, filename); } + /** + * Calling this method will render this chart/plot to a png given of the given width and height + * @param width The width of the final image in pixels (if < 0 then 400 and if > 4096 then 4096) + * @param height The height of the final image in pixels (if < 0 then 400 and if > 4096 then 4096) + * @return A BufferedImage of this chart in the given dimension + */ + public BufferedImage renderToImage(final int width, final int height) { + return Helper.renderToImage(DieMap.this, width, height); + } + // ******************** Layout ******************************************* @Override public void layoutChildren() { diff --git a/src/main/java/eu/hansolo/fx/charts/wafermap/WaferMap.java b/src/main/java/eu/hansolo/fx/charts/wafermap/WaferMap.java index cd6e27f..1b11ab8 100644 --- a/src/main/java/eu/hansolo/fx/charts/wafermap/WaferMap.java +++ b/src/main/java/eu/hansolo/fx/charts/wafermap/WaferMap.java @@ -54,6 +54,7 @@ import javafx.scene.text.Font; import javafx.scene.text.TextAlignment; + import java.awt.image.BufferedImage; import java.util.ArrayList; import java.util.HashMap; import java.util.List; @@ -538,6 +539,16 @@ public boolean renderToImage(final String filename, final int width, final int h return Helper.renderToImage(WaferMap.this, width, height, filename); } + /** + * Calling this method will render this chart/plot to a png given of the given width and height + * @param width The width of the final image in pixels (if < 0 then 400 and if > 4096 then 4096) + * @param height The height of the final image in pixels (if < 0 then 400 and if > 4096 then 4096) + * @return A BufferedImage of this chart in the given dimension + */ + public BufferedImage renderToImage(final int width, final int height) { + return Helper.renderToImage(WaferMap.this, width, height); + } + public void dispose() { canvas.removeEventFilter(MouseEvent.MOUSE_PRESSED, mouseHandler); }