Skip to content

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

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

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
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,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<PieSeries, String> generateSeriesLabel(int total) {
return pieSeries -> {
double percent = (pieSeries.getValue().doubleValue() / total) * 100;
return String.format("%s (%.2f%%)", pieSeries.getValue(), percent);
};
}
}
Original file line number Diff line number Diff line change
@@ -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 =
19 changes: 19 additions & 0 deletions xchart/src/main/java/org/knowm/xchart/style/PieStyler.java
Original file line number Diff line number Diff line change
@@ -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<PieSeries, String> customSeriesLabelFunction;
private boolean isForceAllLabelsVisible;
private boolean isLabelsFontColorAutomaticEnabled;
private Color labelsFontColorAutomaticLight;
@@ -143,6 +147,21 @@ public PieStyler setLabelType(LabelType labelType) {
return this;
}

public Function<PieSeries, String> getCustomSeriesLabelFunction() {
return this.customSeriesLabelFunction;
}

/**
* Sets the Pie custom label generator
*
* @param customSeriesLabelFunction
*/
public PieStyler setCustomSeriesLabelFunction(Function<PieSeries, String> customSeriesLabelFunction) {
this.customSeriesLabelFunction = customSeriesLabelFunction;
this.setLabelType(null);
return this;
}

public boolean isForceAllLabelsVisible() {

return isForceAllLabelsVisible;