Commits

Daniel Nadeau committed a7d07d5 Merge

Merged in alexfu/holographlibrary (pull request #4)

Improve readability -- BarGraph

THANKS! :)

Comments (0)

Files changed (2)

 *.iws
 .idea/
 
+# Gradle stuff
+out/

HoloGraphLibrary/src/com/echo/holographlibrary/BarGraph.java

 /*
- * 	   Created by Daniel Nadeau
- * 	   daniel.nadeau01@gmail.com
- * 	   danielnadeau.blogspot.com
+ *     Created by Daniel Nadeau
+ *     daniel.nadeau01@gmail.com
+ *     danielnadeau.blogspot.com
  * 
- * 	   Licensed to the Apache Software Foundation (ASF) under one
+ *     Licensed to the Apache Software Foundation (ASF) under one
        or more contributor license agreements.  See the NOTICE file
        distributed with this work for additional information
        regarding copyright ownership.  The ASF licenses this file
 
 public class BarGraph extends View {
 
-	private ArrayList<Bar> points = new ArrayList<Bar>();
-	private Paint p = new Paint();
-	private Rect r;
-	private boolean showBarText = true;
-	private int indexSelected = -1;
-	private OnBarClickedListener listener;
-	private Bitmap fullImage;
-	private boolean shouldUpdate = false;
-	
-	public BarGraph(Context context) {
-		super(context);
-	}
-	
-	public BarGraph(Context context, AttributeSet attrs) {
-		super(context, attrs);
-	}
-	
-	public void setShowBarText(boolean show){
-		showBarText = show;
-	}
-	
-	public void setBars(ArrayList<Bar> points){
-		this.points = points;
-		postInvalidate();
-	}
-	
-	public ArrayList<Bar> getBars(){
-		return this.points;
-	}
-
-	public void onDraw(Canvas ca) {
-		
-		if (fullImage == null || shouldUpdate) {
-			fullImage = Bitmap.createBitmap(getWidth(), getHeight(), Config.ARGB_8888);
-			Canvas canvas = new Canvas(fullImage);
-			canvas.drawColor(Color.TRANSPARENT);
-			NinePatchDrawable popup = (NinePatchDrawable)this.getResources().getDrawable(R.drawable.popup_black);
-			
-			float maxValue = 0;
-			float padding = 7;
-			int selectPadding = 4;
-			float bottomPadding = 40;
-			
-			float usableHeight;
-			if (showBarText) {
-				this.p.setTextSize(40);
-				Rect r3 = new Rect();
-				this.p.getTextBounds("$", 0, 1, r3);
-				usableHeight = getHeight()-bottomPadding-Math.abs(r3.top-r3.bottom)-26;
-			} else {
-				usableHeight = getHeight()-bottomPadding;
-			}
-			 
-			
-			p.setColor(Color.BLACK);
-			p.setStrokeWidth(2);
-			p.setAlpha(50);
-			p.setAntiAlias(true);
-			
-			canvas.drawLine(0, getHeight()-bottomPadding+10, getWidth(), getHeight()-bottomPadding+10, p);
-			
-			float barWidth = (getWidth() - (padding*2)*points.size())/points.size();
-			
-			for (Bar p : points) {
-	        	maxValue += p.getValue();
-	        }
-			
-			r = new Rect();
-			
-			int count = 0;
-			for (Bar p : points) {
-				r.set((int)((padding*2)*count + padding + barWidth*count), (int)(getHeight()-bottomPadding-(usableHeight*(p.getValue()/maxValue))), (int)((padding*2)*count + padding + barWidth*(count+1)), (int)(getHeight()-bottomPadding));
-	        	
-				Path path = new Path();
-				path.addRect(new RectF(r.left-selectPadding, r.top-selectPadding, r.right+selectPadding, r.bottom+selectPadding), Path.Direction.CW);
-				p.setPath(path);
-				p.setRegion(new Region(r.left-selectPadding, r.top-selectPadding, r.right+selectPadding, r.bottom+selectPadding));
-				
-				this.p.setColor(p.getColor());
-				this.p.setAlpha(255);
-				canvas.drawRect(r, this.p);
-				this.p.setTextSize(20);
-				canvas.drawText(p.getName(), (int)(((r.left+r.right)/2)-(this.p.measureText(p.getName())/2)), getHeight()-5, this.p);
-				if (showBarText){
-					this.p.setTextSize(40);
-					this.p.setColor(Color.WHITE);
-					Rect r2 = new Rect();
-					this.p.getTextBounds("$"+p.getValue(), 0, 1, r2);
-					popup.setBounds((int)(((r.left+r.right)/2)-(this.p.measureText("$"+p.getValue())/2))-14, r.top+(r2.top-r2.bottom)-26, (int)(((r.left+r.right)/2)+(this.p.measureText("$"+p.getValue())/2))+14, r.top);
-					popup.draw(canvas);
-					canvas.drawText("$"+p.getValue(), (int)(((r.left+r.right)/2)-(this.p.measureText("$"+p.getValue())/2)), r.top-20, this.p);
-				}
-				if (indexSelected == count && listener != null) {
-					this.p.setColor(Color.parseColor("#33B5E5"));
-					this.p.setAlpha(100);
-					canvas.drawPath(p.getPath(), this.p);
-					this.p.setAlpha(255);
-				}
-	        	count++;
-	        }
-			shouldUpdate = false;
-		}
-		
-		ca.drawBitmap(fullImage, 0, 0, null);
-		
-	}
-
-	@Override
-	public boolean onTouchEvent(MotionEvent event) {
-
-	    Point point = new Point();
-	    point.x = (int) event.getX();
-	    point.y = (int) event.getY();
-	    
-	    int count = 0;
-	    for (Bar bar : points){
-	    	Region r = new Region();
-	    	r.setPath(bar.getPath(), bar.getRegion());
-	    	if (r.contains((int)point.x,(int) point.y) && event.getAction() == MotionEvent.ACTION_DOWN){
-	    		indexSelected = count;
-	    	} else if (event.getAction() == MotionEvent.ACTION_UP){
-	    		if (r.contains((int)point.x,(int) point.y) && listener != null){
-	    			listener.onClick(indexSelected);
-	    		}
-	    		indexSelected = -1;
-	    	}
-		    count++;
-	    }
-	    
-	    if (event.getAction() == MotionEvent.ACTION_DOWN || event.getAction() == MotionEvent.ACTION_UP){
-	    	shouldUpdate = true;
-	    	postInvalidate();
-	    }
-	    
-	    
-
-	    return true;
-	}
-	
-	public void setOnBarClickedListener(OnBarClickedListener listener) {
-		this.listener = listener;
-	}
-	
-	public interface OnBarClickedListener {
-		abstract void onClick(int index);
-	}
+    private ArrayList<Bar> points = new ArrayList<Bar>();
+    private Paint p = new Paint();
+    private Rect r;
+    private boolean showBarText = true;
+    private int indexSelected = -1;
+    private OnBarClickedListener listener;
+    private Bitmap fullImage;
+    private boolean shouldUpdate = false;
+    
+    public BarGraph(Context context) {
+        super(context);
+    }
+    
+    public BarGraph(Context context, AttributeSet attrs) {
+        super(context, attrs);
+    }
+    
+    public void setShowBarText(boolean show){
+        showBarText = show;
+    }
+    
+    public void setBars(ArrayList<Bar> points){
+        this.points = points;
+        postInvalidate();
+    }
+    
+    public ArrayList<Bar> getBars(){
+        return this.points;
+    }
+
+    public void onDraw(Canvas ca) {
+        
+        if (fullImage == null || shouldUpdate) {
+            fullImage = Bitmap.createBitmap(getWidth(), getHeight(), Config.ARGB_8888);
+            Canvas canvas = new Canvas(fullImage);
+            canvas.drawColor(Color.TRANSPARENT);
+            NinePatchDrawable popup = (NinePatchDrawable)this.getResources().getDrawable(R.drawable.popup_black);
+            
+            float maxValue = 0;
+            float padding = 7;
+            int selectPadding = 4;
+            float bottomPadding = 40;
+            
+            float usableHeight;
+            if (showBarText) {
+                this.p.setTextSize(40);
+                Rect r3 = new Rect();
+                this.p.getTextBounds("$", 0, 1, r3);
+                usableHeight = getHeight()-bottomPadding-Math.abs(r3.top-r3.bottom)-26;
+            } else {
+                usableHeight = getHeight()-bottomPadding;
+            }
+             
+            // Draw x-axis line
+            p.setColor(Color.BLACK);
+            p.setStrokeWidth(2);
+            p.setAlpha(50);
+            p.setAntiAlias(true);
+            canvas.drawLine(0, getHeight()-bottomPadding+10, getWidth(), getHeight()-bottomPadding+10, p);
+            
+            float barWidth = (getWidth() - (padding*2)*points.size())/points.size();
+
+            // Maximum y value = sum of all values.
+            for (Bar p : points) {
+                maxValue += p.getValue();
+            }
+            
+            r = new Rect();
+            
+            int count = 0;
+            for (Bar p : points) {
+                // Set bar bounds
+                int left = (int)((padding*2)*count + padding + barWidth*count);
+                int top = (int)(getHeight()-bottomPadding-(usableHeight*(p.getValue()/maxValue)));
+                int right = (int)((padding*2)*count + padding + barWidth*(count+1));
+                int bottom = (int)(getHeight()-bottomPadding);
+                r.set(left, top, right, bottom);
+
+                // Draw bar
+                this.p.setColor(p.getColor());
+                this.p.setAlpha(255);
+                canvas.drawRect(r, this.p);
+
+                // Create selection region
+                Path path = new Path();
+                path.addRect(new RectF(r.left-selectPadding, r.top-selectPadding, r.right+selectPadding, r.bottom+selectPadding), Path.Direction.CW);
+                p.setPath(path);
+                p.setRegion(new Region(r.left-selectPadding, r.top-selectPadding, r.right+selectPadding, r.bottom+selectPadding));
+
+                // Draw x-axis label text
+                this.p.setTextSize(20);
+                int x = (int)(((r.left+r.right)/2)-(this.p.measureText(p.getName())/2));
+                int y = getHeight()-5;
+                canvas.drawText(p.getName(), x, y, this.p);
+
+                // Draw value text
+                if (showBarText){
+                    this.p.setTextSize(40);
+                    this.p.setColor(Color.WHITE);
+                    Rect r2 = new Rect();
+                    this.p.getTextBounds("$"+p.getValue(), 0, 1, r2);
+                    popup.setBounds((int)(((r.left+r.right)/2)-(this.p.measureText("$"+p.getValue())/2))-14, r.top+(r2.top-r2.bottom)-26, (int)(((r.left+r.right)/2)+(this.p.measureText("$"+p.getValue())/2))+14, r.top);
+                    popup.draw(canvas);
+                    canvas.drawText("$"+p.getValue(), (int)(((r.left+r.right)/2)-(this.p.measureText("$"+p.getValue())/2)), r.top-20, this.p);
+                }
+                if (indexSelected == count && listener != null) {
+                    this.p.setColor(Color.parseColor("#33B5E5"));
+                    this.p.setAlpha(100);
+                    canvas.drawPath(p.getPath(), this.p);
+                    this.p.setAlpha(255);
+                }
+                count++;
+            }
+            shouldUpdate = false;
+        }
+        
+        ca.drawBitmap(fullImage, 0, 0, null);
+        
+    }
+
+    @Override
+    public boolean onTouchEvent(MotionEvent event) {
+
+        Point point = new Point();
+        point.x = (int) event.getX();
+        point.y = (int) event.getY();
+        
+        int count = 0;
+        for (Bar bar : points){
+            Region r = new Region();
+            r.setPath(bar.getPath(), bar.getRegion());
+            if (r.contains((int)point.x,(int) point.y) && event.getAction() == MotionEvent.ACTION_DOWN){
+                indexSelected = count;
+            } else if (event.getAction() == MotionEvent.ACTION_UP){
+                if (r.contains((int)point.x,(int) point.y) && listener != null){
+                    listener.onClick(indexSelected);
+                }
+                indexSelected = -1;
+            }
+            count++;
+        }
+        
+        if (event.getAction() == MotionEvent.ACTION_DOWN || event.getAction() == MotionEvent.ACTION_UP){
+            shouldUpdate = true;
+            postInvalidate();
+        }
+        
+        
+
+        return true;
+    }
+    
+    public void setOnBarClickedListener(OnBarClickedListener listener) {
+        this.listener = listener;
+    }
+    
+    public interface OnBarClickedListener {
+        abstract void onClick(int index);
+    }
 }