Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Give the possibility to supply a custom label generator for pie charts #698

Merged
merged 2 commits into from
Apr 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -361,7 +379,8 @@ public enum LabelType {
Percentage,
Name,
NameAndPercentage,
NameAndValue
NameAndValue,
Custom
}

public enum ClockwiseDirectionType {
Expand Down