From d816bcb2d22517da46158ca50549712ecbaaa680 Mon Sep 17 00:00:00 2001 From: giambo Date: Sat, 3 Sep 2022 17:31:04 +0200 Subject: [PATCH 1/2] Give the possibility to supply a custom label generator for pie charts --- .../internal/chartpart/PlotContent_Pie.java | 2 ++ .../knowm/xchart/style/LabelGenerator.java | 7 +++++++ .../org/knowm/xchart/style/PieStyler.java | 21 ++++++++++++++++++- 3 files changed, 29 insertions(+), 1 deletion(-) create mode 100644 xchart/src/main/java/org/knowm/xchart/style/LabelGenerator.java 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 3b4dfa3f0..99f72b6cf 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.getLabelType() == LabelType.Custom) { + label = pieStyler.getLabelGenerator().generateSeriesLabel(series); } TextLayout textLayout = diff --git a/xchart/src/main/java/org/knowm/xchart/style/LabelGenerator.java b/xchart/src/main/java/org/knowm/xchart/style/LabelGenerator.java new file mode 100644 index 000000000..0dfc50fd9 --- /dev/null +++ b/xchart/src/main/java/org/knowm/xchart/style/LabelGenerator.java @@ -0,0 +1,7 @@ +package org.knowm.xchart.style; + +import org.knowm.xchart.internal.series.Series; + +public interface LabelGenerator { + String generateSeriesLabel(Series series); +} 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 8c36a8d1b..e4f11844b 100644 --- a/xchart/src/main/java/org/knowm/xchart/style/PieStyler.java +++ b/xchart/src/main/java/org/knowm/xchart/style/PieStyler.java @@ -26,6 +26,7 @@ public class PieStyler extends Styler { private Color labelsFontColor; private double labelsDistance; private LabelType labelType; + private LabelGenerator labelGenerator; private boolean isForceAllLabelsVisible; private boolean isLabelsFontColorAutomaticEnabled; private Color labelsFontColorAutomaticLight; @@ -143,6 +144,23 @@ public PieStyler setLabelType(LabelType labelType) { return this; } + public LabelGenerator getLabelGenerator() { + + return labelGenerator; + } + + /** + * Sets the Pie custom label generator + * + * @param labelGenerator + */ + public PieStyler setLabelGenerator(LabelGenerator labelGenerator) { + + this.labelType = LabelType.Custom; + this.labelGenerator = labelGenerator; + return this; + } + public boolean isForceAllLabelsVisible() { return isForceAllLabelsVisible; @@ -361,7 +379,8 @@ public enum LabelType { Percentage, Name, NameAndPercentage, - NameAndValue + NameAndValue, + Custom } public enum ClockwiseDirectionType { From 44e8618cfc6e3a91c67cc6df0b2a137ebf7420e0 Mon Sep 17 00:00:00 2001 From: giambo Date: Tue, 1 Aug 2023 15:30:00 +0200 Subject: [PATCH 2/2] Modified PieChart02 to show how to use LabelGenerator --- .../xchart/demo/charts/pie/PieChart02.java | 39 +++++++++++++++---- .../knowm/xchart/style/LabelGenerator.java | 4 +- 2 files changed, 33 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..b1f141f32 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,10 +1,14 @@ package org.knowm.xchart.demo.charts.pie; import java.awt.Color; +import java.util.Collection; + 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.LabelGenerator; import org.knowm.xchart.style.PieStyler.LabelType; /** @@ -16,6 +20,7 @@ *
  • Pie Chart *
  • PieChartBuilder *
  • Custom series palette + *
  • Custom label type *
  • Value Annotations *
  • Tooltips */ @@ -35,6 +40,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 +57,12 @@ public PieChart getChart() { new Color(246, 199, 182) }; chart.getStyler().setSeriesColors(sliceColors); - chart.getStyler().setLabelType(LabelType.Value); + chart.getStyler().setLabelType(LabelType.Custom); + chart.getStyler().setLabelGenerator(new CustomLabelGenerator(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 +71,21 @@ public String getExampleChartName() { return getClass().getSimpleName() + " - Pie Chart Custom Color Palette"; } + + private static class CustomLabelGenerator implements LabelGenerator { + + private final double total; + + public CustomLabelGenerator(Collection values) { + + this.total = values.stream().map(PieSeries::getValue).mapToDouble(Number::doubleValue).sum(); + } + + @Override + public String generateSeriesLabel(PieSeries series) { + + double percent = (series.getValue().doubleValue() / total) * 100; + return String.format("%s (%.2f%%)", series.getValue(), percent); + } + } } diff --git a/xchart/src/main/java/org/knowm/xchart/style/LabelGenerator.java b/xchart/src/main/java/org/knowm/xchart/style/LabelGenerator.java index 0dfc50fd9..31969dedd 100644 --- a/xchart/src/main/java/org/knowm/xchart/style/LabelGenerator.java +++ b/xchart/src/main/java/org/knowm/xchart/style/LabelGenerator.java @@ -1,7 +1,7 @@ package org.knowm.xchart.style; -import org.knowm.xchart.internal.series.Series; +import org.knowm.xchart.PieSeries; public interface LabelGenerator { - String generateSeriesLabel(Series series); + String generateSeriesLabel(PieSeries series); }