Skip to content

Commit

Permalink
Revert "Give the possibility to supply a custom label generator for p…
Browse files Browse the repository at this point in the history
…ie charts"
  • Loading branch information
timmolter authored Apr 25, 2024
1 parent 9ceaa50 commit 7917f9c
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 60 deletions.
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
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 @@ -20,7 +16,6 @@
* <li>Pie Chart
* <li>PieChartBuilder
* <li>Custom series palette
* <li>Custom label type
* <li>Value Annotations
* <li>Tooltips
*/
Expand All @@ -40,13 +35,6 @@ 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 @@ -57,12 +45,18 @@ public PieChart getChart() {
new Color(246, 199, 182)
};
chart.getStyler().setSeriesColors(sliceColors);
chart.getStyler().setLabelType(LabelType.Custom);
chart.getStyler().setLabelGenerator(new CustomLabelGenerator(chart.getSeriesMap().values()));
chart.getStyler().setLabelType(LabelType.Value);
// 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 @@ -71,21 +65,4 @@ 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,8 +290,6 @@ 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

This file was deleted.

21 changes: 1 addition & 20 deletions xchart/src/main/java/org/knowm/xchart/style/PieStyler.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ 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,23 +142,6 @@ 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 @@ -380,8 +362,7 @@ public enum LabelType {
Percentage,
Name,
NameAndPercentage,
NameAndValue,
Custom
NameAndValue
}

public enum ClockwiseDirectionType {
Expand Down

0 comments on commit 7917f9c

Please sign in to comment.