1 |
|
package co.harlequinmettle.healthfoodconcepts; |
2 |
|
|
3 |
|
import java.util.ArrayList; |
4 |
|
import java.util.Arrays; |
5 |
|
import java.util.Collections; |
6 |
|
import java.util.TreeMap; |
7 |
|
|
8 |
|
import android.content.Context; |
9 |
|
import android.graphics.Canvas; |
10 |
|
import android.graphics.Paint; |
11 |
|
import android.graphics.RectF; |
12 |
|
import android.widget.FrameLayout; |
13 |
|
|
|
|
| 58,2% |
Uncovered Elements: 56 (134) |
Complexity: 30 |
Complexity Density: 0,29 |
|
14 |
|
public class HistogramView extends FrameLayout { |
15 |
|
|
16 |
|
int _nPaints = 10; |
17 |
|
private Paint[] _paints = new Paint[_nPaints]; |
18 |
|
private double sum = 0, sumOfSquares = 0, mean, median, min, max, n, range; |
19 |
|
private int size; |
20 |
|
private float upperLim; |
21 |
|
private int meanBar, pointBar; |
22 |
|
public double standardDeviation; |
23 |
|
|
24 |
|
ArrayList<Float> _data = new ArrayList<Float>(); |
25 |
|
private int[] histogram; |
26 |
|
|
27 |
|
private int BAR_BUFFER = 2; |
28 |
|
|
29 |
|
private RectF[] histoBars; |
30 |
|
int frameW, frameH, barMax; |
31 |
|
double barwidth; |
32 |
|
double eMin_0, eMax_3rd; |
33 |
|
String title, title2, title3, title4; |
34 |
|
float nutrientPt; |
35 |
|
float scale; |
36 |
|
StatTool boiledDown; |
37 |
|
int barcolor = 0xffcccccc; |
38 |
|
|
|
|
| 58,4% |
Uncovered Elements: 32 (77) |
Complexity: 11 |
Complexity Density: 0,16 |
|
39 |
10
|
public HistogramView(Context context, StatTool blDn, int sw, int sh,int bgcolor) {... |
40 |
10
|
super(context); |
41 |
10
|
this.setWillNotDraw(false); |
42 |
10
|
this.barcolor = bgcolor; |
43 |
10
|
this.boiledDown = blDn; |
44 |
10
|
this.frameW = (int) (sw * 2.3f); |
45 |
10
|
this.frameH = sh; |
46 |
|
|
47 |
10
|
this.nutrientPt = WhatYouEat.db[SubScroll.currentNutrientID][SubScroll.currentFoodID]; |
48 |
|
|
49 |
10
|
boolean useServingOK = true; |
50 |
|
|
51 |
10
|
switch (WhatYouEat.Nutrient_Measure) { |
52 |
0
|
case WhatYouEat.USING_KCAL: |
53 |
0
|
this.nutrientPt = WhatYouEat.getPer100KcalDataPoint( |
54 |
|
SubScroll.currentFoodID, this.nutrientPt); |
55 |
|
|
56 |
0
|
break; |
57 |
0
|
case WhatYouEat.USING_SERVING: |
58 |
0
|
this.nutrientPt = WhatYouEat.getPerServingDataPoint( |
59 |
|
SubScroll.currentFoodID, this.nutrientPt); |
60 |
0
|
if (this.nutrientPt < 0) { |
61 |
0
|
useServingOK = false; |
62 |
0
|
this.nutrientPt =WhatYouEat.db[SubScroll.currentNutrientID][SubScroll.currentFoodID]; |
63 |
|
} |
64 |
0
|
break; |
65 |
10
|
case WhatYouEat.USING_GRAMS: |
66 |
|
|
67 |
10
|
break; |
68 |
|
|
69 |
0
|
default: |
70 |
|
|
71 |
0
|
break; |
72 |
|
} |
73 |
|
|
74 |
10
|
this.title = "Distribution of " |
75 |
|
+ WhatYouEat.nutrients[SubScroll.currentNutrientID] |
76 |
|
+ " in all foods in: "+WhatYouEat.getFoodsSearchDescription(); |
77 |
|
|
78 |
10
|
this.title2 = "["+WhatYouEat.foods[SubScroll.currentFoodID]+"]" + " contains "; |
79 |
10
|
this.title3 = this.nutrientPt + " " |
80 |
|
+ WhatYouEat.units[SubScroll.currentNutrientID]+" of ["+ WhatYouEat.nutrients[SubScroll.currentNutrientID]+"]"; |
81 |
|
|
82 |
10
|
switch (WhatYouEat.Nutrient_Measure) { |
83 |
0
|
case WhatYouEat.USING_KCAL: |
84 |
0
|
this.title3 += " per 100 kilocalories"; |
85 |
0
|
break; |
86 |
10
|
case WhatYouEat.USING_GRAMS: |
87 |
10
|
this.title3 += " per 100 grams"; |
88 |
10
|
break; |
89 |
0
|
case WhatYouEat.USING_SERVING: |
90 |
|
|
91 |
0
|
if (useServingOK) { |
92 |
0
|
this.title3 += " in " |
93 |
|
+ WhatYouEat.quantityFactor[SubScroll.currentFoodID][WhatYouEat.optimalServingId[SubScroll.currentFoodID]] |
94 |
|
+ " " |
95 |
|
+ WhatYouEat.oddUnits[SubScroll.currentFoodID][WhatYouEat.optimalServingId[SubScroll.currentFoodID]] |
96 |
|
+ " of " + WhatYouEat.foods[SubScroll.currentFoodID]; |
97 |
|
} else { |
98 |
0
|
this.title3 += " per 100 gram serving"; |
99 |
|
} |
100 |
0
|
break; |
101 |
|
|
102 |
0
|
default: |
103 |
|
|
104 |
0
|
break; |
105 |
|
} |
106 |
|
|
107 |
|
|
108 |
|
|
109 |
|
|
110 |
10
|
histogram = boiledDown.histogram; |
111 |
10
|
sum = boiledDown.sum; |
112 |
10
|
sumOfSquares = boiledDown.sumOfSquares; |
113 |
10
|
mean = boiledDown.mean; |
114 |
10
|
median = boiledDown.median; |
115 |
10
|
min = boiledDown.min; |
116 |
10
|
max = boiledDown.max; |
117 |
10
|
n = boiledDown.n; |
118 |
10
|
range = boiledDown.range; |
119 |
10
|
standardDeviation = boiledDown.standardDeviation; |
120 |
10
|
barwidth = (frameW - 30 - BAR_BUFFER * StatTool.nbars) / StatTool.nbars; |
121 |
10
|
upperLim = (float) boiledDown.cutOffPoint; |
122 |
|
|
123 |
10
|
meanBar = (int) ((mean) / boiledDown.interval); |
124 |
10
|
pointBar = (int) ((nutrientPt) / boiledDown.interval); |
125 |
|
|
126 |
10
|
if (false) { |
127 |
0
|
int[] histoTemp = (int[]) histogram.clone(); |
128 |
|
|
129 |
0
|
Arrays.sort(histoTemp); |
130 |
0
|
if (histoTemp.length > 3) |
131 |
0
|
barMax = histoTemp[histoTemp.length - 4] + 1; |
132 |
|
else |
133 |
0
|
barMax = 1; |
134 |
|
} |
135 |
10
|
setUpBars(StatTool.nbars); |
136 |
110
|
for (int i = 0; i < _nPaints; i++) |
137 |
100
|
_paints[i] = new Paint(); |
138 |
|
|
139 |
10
|
_paints[0].setARGB(255, 70, 120, 140); |
140 |
10
|
_paints[1].setARGB(180, 230, 40, 50); |
141 |
10
|
_paints[2].setColor(0xff000000 | barcolor); |
142 |
|
|
143 |
10
|
_paints[3].setARGB(255, 200,200,200); |
144 |
10
|
_paints[3].setTextSize(22); |
145 |
10
|
_paints[4].setARGB(200, 250, 70, 170); |
146 |
10
|
_paints[5].setARGB(200, 250, 70, 170); |
147 |
|
|
148 |
|
} |
149 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (10) |
Complexity: 2 |
Complexity Density: 0,25 |
|
150 |
10
|
private void setUpBars(int nBars) {... |
151 |
|
|
152 |
10
|
histoBars = new RectF[nBars]; |
153 |
10
|
float scaleFactor = 2; |
154 |
|
|
155 |
1010
|
for (int i = 0; i < nBars; i++) { |
156 |
1000
|
int top = frameH - 50 - (int) (scaleFactor*histogram[i]); |
157 |
1000
|
int left = 10 + BAR_BUFFER * i + ((int) barwidth) * i; |
158 |
1000
|
int width = (int) (barwidth); |
159 |
1000
|
int height = (int) (scaleFactor*histogram[i]); |
160 |
1000
|
histoBars[i] = new RectF(left, top, left + width, top + height); |
161 |
|
|
162 |
|
} |
163 |
|
|
164 |
|
} |
165 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0,5 |
|
166 |
10
|
@Override... |
167 |
|
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { |
168 |
|
|
169 |
10
|
super.onMeasure(frameW, frameH); |
170 |
10
|
setMeasuredDimension(frameW, frameH); |
171 |
|
} |
172 |
|
|
173 |
|
|
|
|
| 89,5% |
Uncovered Elements: 2 (19) |
Complexity: 5 |
Complexity Density: 0,38 |
|
174 |
1199
|
protected void onDraw(Canvas c) {... |
175 |
1199
|
c.drawColor(0x00000000); |
176 |
|
|
177 |
|
|
178 |
121099
|
for (int i = 0; i < histoBars.length; i++) { |
179 |
119900
|
if (i == pointBar) |
180 |
738
|
c.drawRect(histoBars[i], _paints[3]); |
181 |
119162
|
else if (false && i == meanBar) |
182 |
0
|
c.drawRect(histoBars[i], _paints[4]); |
183 |
|
else |
184 |
|
|
185 |
119162
|
c.drawRect(histoBars[i], _paints[2]); |
186 |
|
} |
187 |
|
|
188 |
1199
|
c.drawText(title, 50, 100, _paints[3]); |
189 |
1199
|
c.drawText(title2, 50, 150, _paints[3]); |
190 |
1199
|
c.drawText(title3, 50, 200, _paints[3]); |
191 |
1199
|
c.drawText("0.0", 15, frameH - 20, _paints[3]); |
192 |
1199
|
c.drawText( |
193 |
|
" " |
194 |
|
+ Double.toString((double) ((int) (10000.0 * (upperLim))) / 10000.0), |
195 |
|
frameW - 120, frameH - 20, _paints[3]); |
196 |
1199
|
invalidate(); |
197 |
|
} |
198 |
|
|
199 |
|
|
200 |
|
@return |
201 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
202 |
0
|
public double getRange() {... |
203 |
0
|
return range; |
204 |
|
} |
205 |
|
|
206 |
|
|
207 |
|
|
208 |
|
|
209 |
|
@see |
210 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
211 |
0
|
@Override... |
212 |
|
public String toString() { |
213 |
|
|
214 |
0
|
return "\nactual size: " + n + "\nmin: " + min + "\nmax: " + max |
215 |
|
+ "\nmean: " + mean + "\nmedian: " + median |
216 |
|
|
217 |
|
+ "\nstandard deviation: " + standardDeviation; |
218 |
|
} |
219 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
220 |
0
|
public double getEffectiveRange() {... |
221 |
0
|
return 4 * standardDeviation; |
222 |
|
} |
223 |
|
|
224 |
|
|
225 |
|
@return |
226 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
227 |
0
|
public double getSum() {... |
228 |
0
|
return sum; |
229 |
|
} |
230 |
|
|
231 |
|
|
232 |
|
@return |
233 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
234 |
0
|
public double getSumOfSquares() {... |
235 |
0
|
return sumOfSquares; |
236 |
|
} |
237 |
|
|
238 |
|
|
239 |
|
@return |
240 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
241 |
0
|
public double getMean() {... |
242 |
0
|
return mean; |
243 |
|
} |
244 |
|
|
245 |
|
|
246 |
|
@return |
247 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
248 |
0
|
public double getMedian() {... |
249 |
0
|
return median; |
250 |
|
} |
251 |
|
|
252 |
|
|
253 |
|
@return |
254 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
255 |
0
|
public double getMin() {... |
256 |
0
|
return min; |
257 |
|
} |
258 |
|
|
259 |
|
|
260 |
|
@return |
261 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
262 |
0
|
public double getMax() {... |
263 |
0
|
return max; |
264 |
|
} |
265 |
|
|
266 |
|
|
267 |
|
@return |
268 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
269 |
0
|
public double getN() {... |
270 |
0
|
return n; |
271 |
|
} |
272 |
|
|
273 |
|
|
274 |
|
@return |
275 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
276 |
0
|
public double getStandardDeviation() {... |
277 |
0
|
return standardDeviation; |
278 |
|
} |
279 |
|
|
280 |
|
} |