finance-analyzer/src/main/java/de/arminwolf/financeanalyzer/dao/charts/factories/BubbleChartFactory.java

61 lines
2.4 KiB
Java

package de.arminwolf.financeanalyzer.dao.charts.factories;
import de.arminwolf.financeanalyzer.dao.TransactionDAO;
import de.arminwolf.financeanalyzer.dao.charts.model.Chart;
import de.arminwolf.financeanalyzer.dao.charts.model.HighChartDAO;
import de.arminwolf.financeanalyzer.dao.charts.model.Packedbubble;
import de.arminwolf.financeanalyzer.dao.charts.model.PlotOptions;
import de.arminwolf.financeanalyzer.dao.charts.model.Series;
import de.arminwolf.financeanalyzer.dao.charts.model.SeriesElement;
import de.arminwolf.financeanalyzer.dao.charts.model.Title;
import de.arminwolf.financeanalyzer.dao.charts.model.ToolTip;
import de.arminwolf.financeanalyzer.util.RandomColorUtil;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;
public class BubbleChartFactory {
public static HighChartDAO createBubbleChart(
final String pTitle,
final List<TransactionDAO> transactions) {
HighChartDAO highChartDAO = new HighChartDAO();
highChartDAO.setTitle(new Title(pTitle));
highChartDAO.setChart(new Chart("packedbubble", "75%"));
List<Series> series = new ArrayList<>();
List<String> colors = new ArrayList<>();
AtomicInteger min = new AtomicInteger(0);
AtomicInteger max = new AtomicInteger(0);
transactions.stream()
.collect(Collectors.groupingBy(TransactionDAO::getAnalyseHauptkategorie))
.forEach((key, value) -> {
Series data = new Series();
data.setName(key);
Float reduce = value.stream().reduce(0f, (a, b) -> a + (-1.0f * b.getBetragAsFloat()), Float::sum);
min.set(Math.min(min.get(), reduce.intValue()));
max.set(Math.max(max.get(), reduce.intValue()));
data.setComplexData(value.stream().map(b -> new SeriesElement(b.getBeguenstigterAuftraggeber(), -1.0f * b.getBetragAsFloat())).collect(Collectors.toList()));
series.add(data);
colors.add(RandomColorUtil.getRandomColor());
});
PlotOptions plotOptions = new PlotOptions();
plotOptions.setPackedbubble(new Packedbubble());
plotOptions.getPackedbubble().setzMax(max.get());
plotOptions.getPackedbubble().setzMin(min.get());
plotOptions.getPackedbubble().getDataLabels().getFilter().setValue(Double.valueOf(0.2 * max.get()).intValue());
highChartDAO.setPlotOptions(plotOptions);
highChartDAO.setTooltip(new ToolTip("<b>{point.name}:</b> <u>{point.value} €</u>", true));
highChartDAO.setColors(colors);
highChartDAO.setSeries(series);
return highChartDAO;
}
}