From d0442932ae75efcedcda07907e0da369a852ef39 Mon Sep 17 00:00:00 2001 From: Giambo Date: Fri, 26 Apr 2024 20:37:56 +0200 Subject: [PATCH 1/2] Give the possibility to supply a custom label generator for pie charts --- .../xchart/demo/charts/pie/PieChart02.java | 31 +++++++++++++------ .../internal/chartpart/PlotContent_Pie.java | 2 ++ .../org/knowm/xchart/style/PieStyler.java | 19 ++++++++++++ 3 files changed, 42 insertions(+), 10 deletions(-) diff --git a/xchart-demo/src/main/java/org/knowm/xchart/demo/charts/pie/PieChart02.java b/xchart-demo/src/main/java/org/knowm/xchart/demo/charts/pie/PieChart02.java index c742b77bc..5ef25bb45 100644 --- a/xchart-demo/src/main/java/org/knowm/xchart/demo/charts/pie/PieChart02.java +++ b/xchart-demo/src/main/java/org/knowm/xchart/demo/charts/pie/PieChart02.java @@ -1,11 +1,14 @@ package org.knowm.xchart.demo.charts.pie; import java.awt.Color; +import java.util.Collection; +import java.util.function.Function; + import org.knowm.xchart.PieChart; import org.knowm.xchart.PieChartBuilder; +import org.knowm.xchart.PieSeries; import org.knowm.xchart.SwingWrapper; import org.knowm.xchart.demo.charts.ExampleChart; -import org.knowm.xchart.style.PieStyler.LabelType; /** * Pie Chart Custom Color Palette @@ -35,6 +38,13 @@ public PieChart getChart() { PieChart chart = new PieChartBuilder().width(800).height(600).title(getClass().getSimpleName()).build(); + // Series + chart.addSeries("Gold", 24); + chart.addSeries("Silver", 21); + chart.addSeries("Platinum", 39); + chart.addSeries("Copper", 17); + chart.addSeries("Zinc", 40); + // Customize Chart Color[] sliceColors = new Color[] { @@ -45,18 +55,11 @@ public PieChart getChart() { new Color(246, 199, 182) }; chart.getStyler().setSeriesColors(sliceColors); - chart.getStyler().setLabelType(LabelType.Value); + chart.getStyler().setCustomSeriesLabelFunction(generateSeriesLabel(chart.getSeriesMap().values())); // chart.getStyler().setDecimalPattern("#0.000"); chart.getStyler().setToolTipsEnabled(true); // chart.getStyler().setToolTipsAlwaysVisible(true); - // Series - chart.addSeries("Gold", 24); - chart.addSeries("Silver", 21); - chart.addSeries("Platinum", 39); - chart.addSeries("Copper", 17); - chart.addSeries("Zinc", 40); - return chart; } @@ -65,4 +68,12 @@ public String getExampleChartName() { return getClass().getSimpleName() + " - Pie Chart Custom Color Palette"; } -} + + private Function generateSeriesLabel(Collection values) { + double total = values.stream().map(PieSeries::getValue).mapToDouble(Number::doubleValue).sum(); + return pieSeries -> { + double percent = (pieSeries.getValue().doubleValue() / total) * 100; + return String.format("%s (%.2f%%)", pieSeries.getValue(), percent); + }; + } +} \ No newline at end of file diff --git a/xchart/src/main/java/org/knowm/xchart/internal/chartpart/PlotContent_Pie.java b/xchart/src/main/java/org/knowm/xchart/internal/chartpart/PlotContent_Pie.java index 94d6f52dd..5fdb50355 100644 --- a/xchart/src/main/java/org/knowm/xchart/internal/chartpart/PlotContent_Pie.java +++ b/xchart/src/main/java/org/knowm/xchart/internal/chartpart/PlotContent_Pie.java @@ -290,6 +290,8 @@ private void paintLabels(Graphics2D g, Rectangle2D pieBounds, double total, doub } else { label = series.getName() + " (" + y.toString() + ")"; } + } else if (pieStyler.getCustomSeriesLabelFunction() != null) { + label = pieStyler.getCustomSeriesLabelFunction().apply(series); } TextLayout textLayout = diff --git a/xchart/src/main/java/org/knowm/xchart/style/PieStyler.java b/xchart/src/main/java/org/knowm/xchart/style/PieStyler.java index 00919166f..aba15c6df 100644 --- a/xchart/src/main/java/org/knowm/xchart/style/PieStyler.java +++ b/xchart/src/main/java/org/knowm/xchart/style/PieStyler.java @@ -2,6 +2,9 @@ import java.awt.Color; import java.awt.Font; +import java.util.function.Function; + +import org.knowm.xchart.PieSeries; import org.knowm.xchart.PieSeries.PieSeriesRenderStyle; import org.knowm.xchart.style.colors.FontColorDetector; import org.knowm.xchart.style.theme.Theme; @@ -25,6 +28,7 @@ public class PieStyler extends Styler { private Color labelsFontColor; private double labelsDistance; private LabelType labelType; + private Function customSeriesLabelFunction; private boolean isForceAllLabelsVisible; private boolean isLabelsFontColorAutomaticEnabled; private Color labelsFontColorAutomaticLight; @@ -143,6 +147,21 @@ public PieStyler setLabelType(LabelType labelType) { return this; } + public Function getCustomSeriesLabelFunction() { + return this.customSeriesLabelFunction; + } + + /** + * Sets the Pie custom label generator + * + * @param customSeriesLabelFunction + */ + public PieStyler setCustomSeriesLabelFunction(Function customSeriesLabelFunction) { + this.customSeriesLabelFunction = customSeriesLabelFunction; + this.setLabelType(null); + return this; + } + public boolean isForceAllLabelsVisible() { return isForceAllLabelsVisible; From 44f6a189d4e5f22beb4d5a4598203e703038eb29 Mon Sep 17 00:00:00 2001 From: Giambo Date: Fri, 7 Jun 2024 10:57:30 +0200 Subject: [PATCH 2/2] Avoid call to to Chart#getSeriesMap (Marked for removal) --- .../xchart/demo/charts/pie/PieChart02.java | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/xchart-demo/src/main/java/org/knowm/xchart/demo/charts/pie/PieChart02.java b/xchart-demo/src/main/java/org/knowm/xchart/demo/charts/pie/PieChart02.java index 5ef25bb45..ba7e1e56f 100644 --- a/xchart-demo/src/main/java/org/knowm/xchart/demo/charts/pie/PieChart02.java +++ b/xchart-demo/src/main/java/org/knowm/xchart/demo/charts/pie/PieChart02.java @@ -3,6 +3,7 @@ import java.awt.Color; import java.util.Collection; import java.util.function.Function; +import java.util.stream.Stream; import org.knowm.xchart.PieChart; import org.knowm.xchart.PieChartBuilder; @@ -39,11 +40,13 @@ public PieChart getChart() { new PieChartBuilder().width(800).height(600).title(getClass().getSimpleName()).build(); // Series - chart.addSeries("Gold", 24); - chart.addSeries("Silver", 21); - chart.addSeries("Platinum", 39); - chart.addSeries("Copper", 17); - chart.addSeries("Zinc", 40); + int total = Stream.of( + chart.addSeries("Gold", 24), + chart.addSeries("Silver", 21), + chart.addSeries("Platinum", 39), + chart.addSeries("Copper", 17), + chart.addSeries("Zinc", 40) + ).map(PieSeries::getValue).mapToInt(Number::intValue).sum(); // Customize Chart Color[] sliceColors = @@ -55,7 +58,7 @@ public PieChart getChart() { new Color(246, 199, 182) }; chart.getStyler().setSeriesColors(sliceColors); - chart.getStyler().setCustomSeriesLabelFunction(generateSeriesLabel(chart.getSeriesMap().values())); + chart.getStyler().setCustomSeriesLabelFunction(generateSeriesLabel(total)); // chart.getStyler().setDecimalPattern("#0.000"); chart.getStyler().setToolTipsEnabled(true); // chart.getStyler().setToolTipsAlwaysVisible(true); @@ -69,8 +72,7 @@ public String getExampleChartName() { return getClass().getSimpleName() + " - Pie Chart Custom Color Palette"; } - private Function generateSeriesLabel(Collection values) { - double total = values.stream().map(PieSeries::getValue).mapToDouble(Number::doubleValue).sum(); + private Function generateSeriesLabel(int total) { return pieSeries -> { double percent = (pieSeries.getValue().doubleValue() / total) * 100; return String.format("%s (%.2f%%)", pieSeries.getValue(), percent);