Skip to content

Commit

Permalink
Avoid call to to Chart#getSeriesMap (Marked for removal)
Browse files Browse the repository at this point in the history
  • Loading branch information
ggiambo committed Jun 7, 2024
1 parent d044293 commit 44f6a18
Showing 1 changed file with 10 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
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;
Expand Down Expand Up @@ -39,11 +40,13 @@ public PieChart getChart() {
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);
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 =
Expand All @@ -55,7 +58,7 @@ public PieChart getChart() {
new Color(246, 199, 182)
};
chart.getStyler().setSeriesColors(sliceColors);
chart.getStyler().setCustomSeriesLabelFunction(generateSeriesLabel(chart.getSeriesMap().values()));
chart.getStyler().setCustomSeriesLabelFunction(generateSeriesLabel(total));
// chart.getStyler().setDecimalPattern("#0.000");
chart.getStyler().setToolTipsEnabled(true);
// chart.getStyler().setToolTipsAlwaysVisible(true);
Expand All @@ -69,8 +72,7 @@ public String getExampleChartName() {
return getClass().getSimpleName() + " - Pie Chart Custom Color Palette";
}

private Function<PieSeries, String> generateSeriesLabel(Collection<PieSeries> values) {
double total = values.stream().map(PieSeries::getValue).mapToDouble(Number::doubleValue).sum();
private Function<PieSeries, String> generateSeriesLabel(int total) {
return pieSeries -> {
double percent = (pieSeries.getValue().doubleValue() / total) * 100;
return String.format("%s (%.2f%%)", pieSeries.getValue(), percent);
Expand Down

0 comments on commit 44f6a18

Please sign in to comment.