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/internal/chartpart/PlotContent_Pie.java b/xchart/src/main/java/org/knowm/xchart/internal/chartpart/PlotContent_Pie.java
index 94d6f52dd..f87f11a6f 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..31969dedd
--- /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.PieSeries;
+
+public interface LabelGenerator {
+ String generateSeriesLabel(PieSeries 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 6df15afa6..0c7fcefd4 100644
--- a/xchart/src/main/java/org/knowm/xchart/style/PieStyler.java
+++ b/xchart/src/main/java/org/knowm/xchart/style/PieStyler.java
@@ -25,6 +25,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;
@@ -142,6 +143,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;
@@ -362,7 +380,8 @@ public enum LabelType {
Percentage,
Name,
NameAndPercentage,
- NameAndValue
+ NameAndValue,
+ Custom
}
public enum ClockwiseDirectionType {