Graph.java
Go to the documentation of this file.
00001 /*
00002 copyright 2014 UNL Nimbus Lab 
00003 
00004   Licensed under the Apache License, Version 2.0 (the "License");
00005      you may not use this file except in compliance with the License.
00006     You may obtain a copy of the License at
00007   
00008         http://www.apache.org/licenses/LICENSE-2.0
00009   
00010     Unless required by applicable law or agreed to in writing, software
00011   distributed under the License is distributed on an "AS IS" BASIS,
00012    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013    See the License for the specific language governing permissions and
00014   limitations under the License.
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                         //get out the value
00067                         double val = Double.parseDouble(tm.getField(field));
00068                         pastValues.add(val);
00069                         
00070                         //we are at a point that we can update the max and min values
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                         //Prune data.
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                                 // For efficiency, we don't draw all of the samples in the buffer,
00117                                 // but only the ones that align with pixel boundaries.
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                         //can't graph it.
00137                 }
00138         }
00139 
00140 
00141 }


ros_glass_tools
Author(s):
autogenerated on Thu Aug 27 2015 14:47:21