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..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 @@ -1,11 +1,15 @@ package org.knowm.xchart.demo.charts.pie; 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; +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 +39,15 @@ public PieChart getChart() { PieChart chart = new PieChartBuilder().width(800).height(600).title(getClass().getSimpleName()).build(); + // Series + 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 = new Color[] { @@ -45,18 +58,11 @@ public PieChart getChart() { new Color(246, 199, 182) }; chart.getStyler().setSeriesColors(sliceColors); - chart.getStyler().setLabelType(LabelType.Value); + chart.getStyler().setCustomSeriesLabelFunction(generateSeriesLabel(total)); // 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,11 @@ public String getExampleChartName() { return getClass().getSimpleName() + " - Pie Chart Custom Color Palette"; } -} + + private Function generateSeriesLabel(int total) { + 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;