Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016 package edu.nimbus.glass.screens;
00017
00018 import java.util.LinkedList;
00019 import java.util.List;
00020 import java.util.Random;
00021
00022 import android.graphics.Canvas;
00023 import android.graphics.Color;
00024 import android.text.TextPaint;
00025 import android.util.DisplayMetrics;
00026 import android.util.Log;
00027 import edu.nimbus.glass.TopicManager;
00028
00029
00037 public class Graph implements Screen {
00038
00039 private static final int MAX_POINTS = 100;
00040 private static final int MILLISECONDS_REDRAW_GRAPH = 5000;
00041
00042 private String field;
00043 private List<Double> pastValues;
00044
00045 private double recentMax;
00046 private double recentMin;
00047 private double min;
00048 private double max;
00049 private long counter;
00050
00051 public Graph(String field){
00052 this.field = field;
00053 this.pastValues = new LinkedList<Double>();
00054
00055 recentMax = Double.MIN_VALUE;
00056 recentMin = Double.MAX_VALUE;
00057 min = 0;
00058 max = 1;
00059 counter = 0;
00060 }
00061
00062 @Override
00063 public void draw(TopicManager tm, TextPaint mPaint, Canvas canvas, int width, int height) {
00064 canvas.drawColor(Color.BLACK);
00065 try{
00066
00067 double val = Double.parseDouble(tm.getField(field));
00068 pastValues.add(val);
00069
00070
00071 if(System.currentTimeMillis() - counter > MILLISECONDS_REDRAW_GRAPH){
00072 counter = System.currentTimeMillis();
00073
00074 min = recentMin;
00075 max = recentMax;
00076
00077 recentMin = Float.MAX_VALUE;
00078 recentMax = Float.MIN_VALUE;
00079 }
00080
00081 if(val < min){
00082 min = val;
00083 }
00084
00085 if(val > max){
00086 max = val;
00087 }
00088
00089 if(val < recentMin){
00090 recentMin = val;
00091 }
00092
00093 if(val > recentMax){
00094 recentMax = val;
00095 }
00096
00097
00098
00099 if(pastValues.size() > MAX_POINTS){
00100 pastValues.remove(0);
00101 }
00102
00103 double vertdataRange = max - min;
00104 double hrange = pastValues.size();
00105 double horRatio = width / hrange;
00106 double vertRatio = height / vertdataRange;
00107
00108
00109 int brightness = 10;
00110
00111 float lastX = 0;
00112 float lastY = (float) (vertRatio * pastValues.get(0) );
00113 for(int i=0; i<pastValues.size(); i++){
00114 mPaint.setColor(Color.argb(brightness, 128, 255, 192));
00115
00116
00117
00118 float x = (float) (i*(horRatio));
00119 float y = height - (float) ((pastValues.get(i).floatValue() - min) * vertRatio);
00120
00121 canvas.drawLine(lastX, lastY, x, y, mPaint);
00122
00123 lastX = x;
00124 lastY = y;
00125
00126 brightness += 3;
00127 }
00128
00129 mPaint.setTextSize((float)50);
00130
00131 canvas.drawText(String.format("%.4f", max), 0, 40, mPaint);
00132 canvas.drawText(String.format("%.4f", min), 0, height, mPaint);
00133
00134 counter ++;
00135 }catch(java.lang.Exception e){
00136
00137 }
00138 }
00139
00140
00141 }