Skip to content

Commit

Permalink
Merge pull request #698 from ggiambo/pieChartLabelGenerator
Browse files Browse the repository at this point in the history
Give the possibility to supply a custom label generator for pie charts
  • Loading branch information
timmolter authored Apr 25, 2024
2 parents 071d7f1 + 44e8618 commit 9ceaa50
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -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;

/**
Expand All @@ -16,6 +20,7 @@
* <li>Pie Chart
* <li>PieChartBuilder
* <li>Custom series palette
* <li>Custom label type
* <li>Value Annotations
* <li>Tooltips
*/
Expand All @@ -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[] {
Expand All @@ -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;
}

Expand All @@ -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<PieSeries> 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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package org.knowm.xchart.style;

import org.knowm.xchart.PieSeries;

public interface LabelGenerator {
String generateSeriesLabel(PieSeries series);
}
21 changes: 20 additions & 1 deletion xchart/src/main/java/org/knowm/xchart/style/PieStyler.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -362,7 +380,8 @@ public enum LabelType {
Percentage,
Name,
NameAndPercentage,
NameAndValue
NameAndValue,
Custom
}

public enum ClockwiseDirectionType {
Expand Down

0 comments on commit 9ceaa50

Please sign in to comment.