-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPieChart.pde
More file actions
193 lines (144 loc) · 4.34 KB
/
Copy pathPieChart.pde
File metadata and controls
193 lines (144 loc) · 4.34 KB
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
186
187
188
189
190
191
//import java.lang.Math;
public class PieChart{
ArrayList<Arc> arcs = new ArrayList<Arc>(7);
// data[0] is total, data[1] is alone, etc.
float xD;
float yD;
State state;
float[] data;
int display;
public PieChart(int x, int y, int w, int h){
this.state = state;
xD = (float)x;
yD = (float)y;
}
public void setState(State state){
if(state!=null && !state.equals(this.state)){
arcs = new ArrayList<Arc>(6);
this.state = state;
data = state.getStateData().getPercArray();
float angleSum = 0;
//for(int i = data.length-1; i>0; i--){
for(int i=0; i<data.length; i++){
float rad = data[i]/100 * 2*PI;
Arc arc = new Arc(i, xD, yD, 300.0f, 300.0f, angleSum, angleSum+rad, PIE);
arcs.add(arc);
angleSum+=rad;
}
}
}
int oldDisplay = -1;
String overlay ="";
void drawPie(){
ellipseMode(CORNER);
for (int i=0; i<arcs.size();i++){
arcs.get(i).draw();
}
if(display!=-1 ){
int type = 1;
if(numOrPerc){
type = state.getWorkers();
}
if(oldDisplay!=display)
overlay =typeName[display]+" : "+ (int)(Math.ceil((double)type*data[display]/100));
draw.overlayText(overlay);
oldDisplay = display;
}
ellipseMode(CENTER);
}
public void mouseMoved(){
display = -1;
pieReset();
for(int i=0; i<arcs.size(); i++){
if(arcs.get(i).contains()){
arcs.get(i).setHighlight(true);
display = i;
//--Set Overlay to Brush--
draw.setOverlayState(state, i);
break;
}
}
draw.draw();
}
//When brushed in the other view
void pieHighlight(int dataType){
pieReset();
for(Arc arc: arcs){
if(arc.dataType==dataType){
arc.setHighlight(true);
break;
}
}
}
void pieReset(){
display = -1;
for(Arc arc: arcs){
arc.setHighlight(false);
}
draw.overlayText(null);
}
private class Arc{
int dataType;
float x;
float y;
float widthPie;
float heightPie;
float start;
float stop;
int fill;
boolean highlight;
color col;
public Arc(int dataType, float x, float y, float widthPie, float heightPie, float start , float stop , int fill){
this.widthPie = widthPie;
this.heightPie = heightPie;
this.dataType = dataType;
this.x = x;
this.y = y;
this.col = typeColor[dataType];
this.start = start;
this.stop = stop;
this.fill = fill;
this.highlight = false;
}
void draw() {
if(highlight){
fill(col,50);
stroke(col);
strokeWeight(3);
}
else{
fill(col);
noStroke();
}
arc(this.x, this.y, widthPie, heightPie, start, stop, fill);
}
void setHighlight(boolean tf){
highlight = tf;
}
boolean contains(){
double angle = getAngle();
if (angle>=start && angle<=stop){
return true;
}
return false;
}
/* Fetches angle relative to screen centre point
* where 3 O'Clock is 0 and 12 O'Clock is 270 degrees
* From StackOverflow
* @param screenPoint
* @return angle in degress from 0-360.
*/
public double getAngle(){
double dx = mouseX - (this.x+widthPie/2);
// Minus to correct for coord re-mapping
double dy = (double)-1*(mouseY - (this.y+heightPie/2));
double inRads = Math.atan2(dy,dx);
// We need to map to coord system when 0 degree is at 3 O'clock, 270 at 12 O'clock
if (inRads < 0)
inRads = Math.abs(inRads);
else
inRads = 2*Math.PI - inRads;
return inRads;
}
}//end arc
}//end pie