forked from FionaWright/High5ive
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathW_PieChart.pde
185 lines (160 loc) · 4.97 KB
/
W_PieChart.pde
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/**
* F. Wright
*
* Represents a Pie Chart user interface widget.
*
* @param <T> The type of data for the chart.
* @param <TData> The type of the data key.
* @extends Widget
* @implements IChart<T, TData>
*/
class PieChartUI<T, TData> extends Widget implements IChart<T, TData> {
private TreeMap<TData, Integer> m_map = new TreeMap<TData, Integer>();
private ArrayList<Float> m_arcSizes = new ArrayList<Float>();
private boolean m_dataLoaded = false;
private QueryType m_translationField = null;
private QueryManagerClass m_queryManager = null;
/**
* F. Wright
*
* Initializes a new instance of the PieChartUI class.
*
* @param posX The x-coordinate of the top-left corner of the widget.
* @param posY The y-coordinate of the top-left corner of the widget.
* @param diameter The diameter of the pie chart.
*/
public PieChartUI(int posX, int posY, int diameter) {
super(posX, posY, diameter, diameter);
}
/**
* F. Wright
*
* Adds data to the pie chart.
*
* @param data The array of data to be added.
* @param getKey The function to extract the key from each data element.
*/
public void addData(T[] data, Function<T, TData> getKey) {
for (var value : data) {
TData k = getKey.apply(value);
Integer entryValue = m_map.get(k);
if (entryValue == null)
m_map.put(k, 1);
else
m_map.replace(k, entryValue + 1);
m_arcSizes.add(1.0f);
}
m_dataLoaded = true;
}
/**
* F. Wright
*
* Adds data to the pie chart from an iterable collection.
*
* @param <I> The type of the iterable collection.
* @param data The iterable collection of data to be added.
* @param getKey The function to extract the key from each data element.
*/
public <I extends Iterable<T>> void addData(I data, Function<T, TData> getKey) {
for (var value : data) {
TData k = getKey.apply(value);
Integer entryValue = m_map.get(k);
if (entryValue == null)
m_map.put(k, 1);
else
m_map.replace(k, entryValue + 1);
m_arcSizes.add(1.0f);
}
m_dataLoaded = true;
}
/**
* F. Wright
*
* Removes all data from the pie chart.
*/
public void removeData() {
m_map = new TreeMap<TData, Integer>();
m_arcSizes.clear();
m_dataLoaded = false;
}
/**
* F. Wright
*
* Draws the pie chart on the screen.
*/
@ Override
public void draw() {
super.draw();
if (!m_dataLoaded)
return;
int total = 0;
for (var val : m_map.values()) {
total += val;
}
float lastAngle = 0;
int i = 0;
for (Map.Entry<TData, Integer> entry : m_map.entrySet()) {
float arcSize = 2 * PI * (entry.getValue() / (float)total);
fill(getColor(i));
float diameterOfArc = m_scale.x * m_arcSizes.get(i);
boolean isHovered = false;
if (pointWithinSector(m_pos, mouseX, mouseY, diameterOfArc, lastAngle, lastAngle + arcSize))
isHovered = true;
float growTarget = isHovered ? 1.1f : 1.0f;
m_arcSizes.set(i, lerp(m_arcSizes.get(i), growTarget, 0.2f));
arc(m_pos.x, m_pos.y, diameterOfArc, diameterOfArc, lastAngle, lastAngle + arcSize, PIE);
if (isHovered) {
float middleAngle = lastAngle + (arcSize * 0.5f);
float textPosX = m_pos.x + (cos(middleAngle) * diameterOfArc * 1.3f);
float textPosY = m_pos.y + (sin(middleAngle) * diameterOfArc * 1.3f);
float textScale = (m_arcSizes.get(i) - 1) / 0.1f;
fill(255);
textAlign(CENTER);
textSize(max(textScale * 30, 1));
text(translateXValues(entry.getKey().toString()), textPosX, textPosY);
}
lastAngle += arcSize;
i++;
}
}
/**
* F. Wright
*
* Sets the translation field and query manager for translating X values of the pie chart.
*
* @param query The query type for translation.
* @param queryManager The query manager for fetching translation data.
*/
public void setTranslationField(QueryType query, QueryManagerClass queryManager) {
m_translationField = query;
m_queryManager = queryManager;
}
/**
* F. Wright
*
* Translates X-axis values of the pie chart based on the set translation field and query manager.
*
* @param val The value to translate.
* @return The translated value.
*/
public String translateXValues(String val) {
if (m_translationField == null)
return val;
switch (m_translationField) {
case CANCELLED:
return val.equals("0") ? "None" : "Cancelled";
case DIVERTED:
return val.equals("0") ? "None" : "Diverted";
case CARRIER_CODE_INDEX:
return m_queryManager.getAirlineName(Integer.parseInt(val));
case AIRPORT_ORIGIN_INDEX:
case AIRPORT_DEST_INDEX:
return m_queryManager.getCode(Integer.parseInt(val));
default:
return val;
}
}
}
// Descending code authorship changes:
// F. Wright, Created pie chart class, 5pm 19/03/24
// F. Wright, Implemented juiciness to pie chart, 1pm, 20/03/24