1 | package de.uka.ipd.sdq.codegen.simudatavisualisation.datatypes; |
2 | |
3 | import java.text.DecimalFormat; |
4 | import java.text.DecimalFormatSymbols; |
5 | import java.util.ArrayList; |
6 | import java.util.Collection; |
7 | import java.util.Collections; |
8 | import java.util.Locale; |
9 | |
10 | public abstract class AbstractPie { |
11 | protected ArrayList<PieEntity> entities = new ArrayList<PieEntity>(); |
12 | private String label; |
13 | public AbstractPie(String string){ |
14 | this.label = string; |
15 | } |
16 | |
17 | public void addEntity(PieEntity e){ |
18 | entities.add(e); |
19 | } |
20 | |
21 | public Collection<PieEntity> getEntities(int maxCount){ |
22 | Collections.sort(entities); |
23 | if (entities.size() > maxCount && maxCount > 0) { |
24 | return getAggregatedPie(maxCount); |
25 | } |
26 | return entities; |
27 | } |
28 | |
29 | private Collection<PieEntity> getAggregatedPie(int maxCount) { |
30 | ArrayList<PieEntity> newEntities = new ArrayList<PieEntity>(); |
31 | int i; double sum = 0; |
32 | for (i=entities.size()-1; i >= entities.size() - maxCount; i--) { |
33 | newEntities.add(entities.get(i)); |
34 | sum += entities.get(i).getValue(); |
35 | } |
36 | double remaining = 0; |
37 | for (; i >= 0; i--) |
38 | remaining += entities.get(i).getValue(); |
39 | sum += remaining; |
40 | DecimalFormat df = new DecimalFormat("#0.0", new DecimalFormatSymbols(Locale.US)); |
41 | newEntities.add(new PieEntity(remaining, "Other ("+df.format(remaining * 100.0 / sum)+"%)")); |
42 | Collections.sort(newEntities); |
43 | return newEntities; |
44 | } |
45 | |
46 | public String getLabel() { |
47 | return label; |
48 | } |
49 | } |