1818import android .widget .ImageButton ;
1919import android .widget .Spinner ;
2020import android .widget .TextView ;
21+ import android .widget .Toast ;
2122
2223import com .github .mikephil .charting .animation .Easing ;
2324import com .github .mikephil .charting .charts .LineChart ;
@@ -233,15 +234,17 @@ private void exportGraphToGallery() {
233234 }
234235
235236 private void setData () {
237+ // TODO: Refactor all this mess ASAP! -- @paolorotolo
236238 // int metricSpinnerPosition = graphSpinnerMetric.getSelectedItemPosition();
237239 // if (metricSpinnerPosition == 0) {
238240 ArrayList <String > xVals = new ArrayList <String >();
239241
242+
240243 if (graphSpinnerRange .getSelectedItemPosition () == 0 ) {
241244 // Day view
242245 for (int i = 0 ; i < presenter .getGlucoseDatetime ().size (); i ++) {
243246 String date = presenter .convertDate (presenter .getGlucoseDatetime ().get (i ));
244- xVals .add (date + "" );
247+ xVals .add (date + "" );
245248 }
246249 } else if (graphSpinnerRange .getSelectedItemPosition () == 1 ){
247250 // Week view
@@ -258,50 +261,116 @@ private void setData() {
258261 }
259262
260263 GlucoseConverter converter = new GlucoseConverter ();
264+ GlucoseRanges ranges = new GlucoseRanges (getActivity ().getApplicationContext ());
265+
266+
267+ ArrayList <Entry > yValsLow = new ArrayList <Entry >();
268+ ArrayList <Entry > yValsNormal = new ArrayList <Entry >();
269+ ArrayList <Entry > yValsHigh = new ArrayList <Entry >();
261270
262- ArrayList <Entry > yVals = new ArrayList <Entry >();
263- ArrayList <Integer > colors = new ArrayList <>();
264271
265- if (graphSpinnerRange .getSelectedItemPosition () == 0 ) {
272+ if (graphSpinnerRange .getSelectedItemPosition () == 0 ) {
266273 // Day view
267274 for (int i = 0 ; i < presenter .getGlucoseReading ().size (); i ++) {
268275 if (presenter .getUnitMeasuerement ().equals ("mg/dL" )) {
269276 float val = Float .parseFloat (presenter .getGlucoseReading ().get (i ).toString ());
270- yVals .add (new Entry (val , i ));
277+ String range = ranges .colorFromReading (presenter .getGlucoseReading ().get (i ));
278+
279+ if (range .equals ("purple" ) || range .equals ("blue" )){
280+ // low
281+ yValsLow .add (new Entry (val , i ));
282+ } else if (range .equals ("red" ) || range .equals ("orange" )){
283+ // high
284+ yValsHigh .add (new Entry (val , i ));
285+ } else {
286+ // normal
287+ yValsNormal .add (new Entry (val , i ));
288+ }
271289 } else {
272290 double val = converter .glucoseToMmolL (Double .parseDouble (presenter .getGlucoseReading ().get (i ).toString ()));
273291 float converted = (float ) val ;
274- yVals .add (new Entry (converted , i ));
292+ String range = ranges .colorFromReading (presenter .getGlucoseReading ().get (i ));
293+
294+ if (range .equals ("purple" ) || range .equals ("blue" )){
295+ // low
296+ yValsLow .add (new Entry (converted , i ));
297+ } else if (range .equals ("red" ) || range .equals ("orange" )){
298+ // high
299+ yValsHigh .add (new Entry (converted , i ));
300+ } else {
301+ // normal
302+ yValsNormal .add (new Entry (converted , i ));
303+ }
275304 }
276- GlucoseRanges ranges = new GlucoseRanges (getActivity ().getApplicationContext ());
277- colors .add (ranges .stringToColor (ranges .colorFromReading (presenter .getGlucoseReading ().get (i ))));
278305 }
279306 } else if (graphSpinnerRange .getSelectedItemPosition () == 1 ){
280307 // Week view
281308 for (int i = 0 ; i < presenter .getGlucoseReadingsWeek ().size (); i ++) {
282309 if (presenter .getUnitMeasuerement ().equals ("mg/dL" )) {
283310 float val = Float .parseFloat (presenter .getGlucoseReadingsWeek ().get (i )+"" );
284- yVals .add (new Entry (val , i ));
311+ String range = ranges .colorFromReading (presenter .getGlucoseReadingsWeek ().get (i ));
312+
313+ if (range .equals ("purple" ) || range .equals ("blue" )){
314+ // low
315+ yValsLow .add (new Entry (val , i ));
316+ } else if (range .equals ("red" ) || range .equals ("orange" )){
317+ // high
318+ yValsHigh .add (new Entry (val , i ));
319+ } else {
320+ // normal
321+ yValsNormal .add (new Entry (val , i ));
322+ }
323+
285324 } else {
286325 double val = converter .glucoseToMmolL (Double .parseDouble (presenter .getGlucoseReadingsWeek ().get (i )+"" ));
287326 float converted = (float ) val ;
288- yVals .add (new Entry (converted , i ));
289- }
327+ String range = ranges .colorFromReading (presenter .getGlucoseReadingsWeek ().get (i ));
328+
329+ if (range .equals ("purple" ) && range .equals ("blue" )){
330+ // low
331+ yValsLow .add (new Entry (converted , i ));
332+ } else if (range .equals ("red" ) && range .equals ("orange" )){
333+ // high
334+ yValsHigh .add (new Entry (converted , i ));
335+ } else {
336+ // normal
337+ yValsNormal .add (new Entry (converted , i ));
338+ } }
290339 }
291- colors .add (getResources ().getColor (R .color .glucosio_pink ));
292340 } else {
293341 // Month view
294342 for (int i = 0 ; i < presenter .getGlucoseReadingsMonth ().size (); i ++) {
295343 if (presenter .getUnitMeasuerement ().equals ("mg/dL" )) {
296344 float val = Float .parseFloat (presenter .getGlucoseReadingsMonth ().get (i )+"" );
297- yVals .add (new Entry (val , i ));
345+ String range = ranges .colorFromReading (presenter .getGlucoseReadingsMonth ().get (i ));
346+
347+ if (range .equals ("purple" ) && range .equals ("blue" )){
348+ // low
349+ yValsLow .add (new Entry (val , i ));
350+ } else if (range .equals ("red" ) && range .equals ("orange" )){
351+ // high
352+ yValsHigh .add (new Entry (val , i ));
353+ } else {
354+ // normal
355+ yValsNormal .add (new Entry (val , i ));
356+ }
298357 } else {
299358 double val = converter .glucoseToMmolL (Double .parseDouble (presenter .getGlucoseReadingsMonth ().get (i )+"" ));
300359 float converted = (float ) val ;
301- yVals .add (new Entry (converted , i ));
360+ String range = ranges .colorFromReading (presenter .getGlucoseReadingsWeek ().get (i ));
361+
362+ if (range .equals ("purple" ) && range .equals ("blue" )){
363+ // low
364+ yValsLow .add (new Entry (converted , i ));
365+ } else if (range .equals ("red" ) && range .equals ("orange" )){
366+ // high
367+ yValsHigh .add (new Entry (converted , i ));
368+ } else {
369+ // normal
370+ yValsNormal .add (new Entry (converted , i ));
371+ }
302372 }
303373 }
304- colors .add (getResources ().getColor (R .color .glucosio_pink ));
305374 }
306375 /*} else if (metricSpinnerPosition == 1){
307376 // A1C
@@ -333,36 +402,20 @@ private void setData() {
333402 // Weight
334403 }*/
335404
336- // create a dataset and give it a type
337- LineDataSet set1 = new LineDataSet (yVals , "" );
338- set1 .setColor (getResources ().getColor (R .color .glucosio_pink ));
339- set1 .setLineWidth (2.5f );
340- set1 .setCircleColor (getResources ().getColor (R .color .glucosio_pink ));
341- set1 .setCircleSize (4f );
342- set1 .setDrawCircleHole (true );
343- set1 .disableDashedLine ();
344- set1 .setFillAlpha (255 );
345- set1 .setDrawFilled (true );
346- set1 .setValueTextSize (0 );
347- set1 .setValueTextColor (Color .parseColor ("#FFFFFF" ));
348- set1 .setFillColor (Color .parseColor ("#FCE2EA" ));
349-
350- if (Build .VERSION .SDK_INT <= Build .VERSION_CODES .JELLY_BEAN_MR2 ){
351- set1 .setDrawFilled (false );
352- set1 .setLineWidth (3f );
353- set1 .setCircleSize (4.5f );
354- set1 .setDrawCircleHole (true );
355- }
356-
357405// set1.setDrawFilled(true);
358406 // set1.setShader(new LinearGradient(0, 0, 0, mChart.getHeight(),
359407 // Color.BLACK, Color.WHITE, Shader.TileMode.MIRROR));I
360408
361- ArrayList <ILineDataSet > dataSets = new ArrayList <ILineDataSet >();
362- dataSets .add (set1 ); // add the datasets
409+ // add the datasets
410+ ArrayList <ILineDataSet > dataSet = new ArrayList <ILineDataSet >();
411+
412+ dataSet .add (createLineDataSet (yValsLow , getResources ().getColor (R .color .glucosio_reading_low ), "low" ));
413+ dataSet .add (createLineDataSet (yValsNormal , getResources ().getColor (R .color .glucosio_reading_ok ), "normal" ));
414+ dataSet .add (createLineDataSet (yValsHigh , getResources ().getColor (R .color .glucosio_reading_high ), "high" ));
415+
363416
364417 // create a data object with the datasets
365- LineData data = new LineData (xVals , dataSets );
418+ LineData data = new LineData (xVals , dataSet );
366419
367420 // set data
368421 chart .setData (data );
@@ -376,6 +429,24 @@ private void setData() {
376429 chart .moveViewToX (data .getXValCount ());
377430 }
378431
432+ private LineDataSet createLineDataSet (ArrayList <Entry > yVals , int color , String label ) {
433+ // create a dataset and give it a type
434+ LineDataSet set1 = new LineDataSet (yVals , label );
435+ set1 .setColor (color );
436+ set1 .setDrawFilled (false );
437+ set1 .setLineWidth (1f );
438+ set1 .setCircleColor (color );
439+ set1 .setCircleSize (4f );
440+ set1 .setDrawCircleHole (true );
441+ set1 .disableDashedLine ();
442+ set1 .setDrawCubic (true );
443+ set1 .setCubicIntensity (0.2f );
444+ set1 .setValueTextSize (0 );
445+ set1 .setValueTextColor (Color .parseColor ("#FFFFFF" ));
446+
447+ return set1 ;
448+ }
449+
379450 private void loadHB1AC (){
380451 if (!presenter .isdbEmpty ()){
381452 HB1ACTextView .setText (presenter .getHB1AC ());
0 commit comments