Commits

Daniel Nadeau committed de4cea9

Some optimizations to the line and bar graphs

Comments (0)

Files changed (8)

HoloGraphLibrary/bin/classes/com/echo/holographlibrary/BarGraph$OnBarClickedListener.class

Binary file modified.

HoloGraphLibrary/bin/classes/com/echo/holographlibrary/BarGraph.class

Binary file modified.

HoloGraphLibrary/bin/classes/com/echo/holographlibrary/LineGraph$OnPointClickedListener.class

Binary file modified.

HoloGraphLibrary/bin/classes/com/echo/holographlibrary/LineGraph.class

Binary file modified.

HoloGraphLibrary/bin/holographlibrary.jar

Binary file modified.

HoloGraphLibrary/libs/android-support-v4.jar

Binary file modified.

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

 import com.echo.holographlibrary.PieGraph.OnSliceClickedListener;
 
 import android.content.Context;
+import android.graphics.Bitmap;
+import android.graphics.Bitmap.Config;
 import android.graphics.Canvas;
 import android.graphics.Color;
 import android.graphics.Paint;
 	private boolean showBarText = true;
 	private int indexSelected = -1;
 	private OnBarClickedListener listener;
+	private Bitmap fullImage;
+	private boolean shouldUpdate = false;
 	
 	public BarGraph(Context context) {
 		super(context);
 		return this.points;
 	}
 
-	public void onDraw(Canvas canvas) {
+	public void onDraw(Canvas ca) {
 		
-		canvas.drawColor(Color.TRANSPARENT);
-		NinePatchDrawable popup = (NinePatchDrawable)this.getResources().getDrawable(R.drawable.popup_nocolor);
-		
-		float maxValue = 0;
-		float padding = 7;
-		int selectPadding = 4;
-		float bottomPadding = 40;
-		
-		if (p == null) p = new Paint();
-		
-		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));
+		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_nocolor);
+			
+			float maxValue = 0;
+			float padding = 7;
+			int selectPadding = 4;
+			float bottomPadding = 40;
 			
-        	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){
+			if (p == null) p = new Paint();
+			
+			float usableHeight;
+			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);
+				Rect r3 = new Rect();
+				this.p.getTextBounds("$", 0, 1, r3);
+				usableHeight = getHeight()-bottomPadding-Math.abs(r3.top-r3.bottom)-26;
+			} else {
+				usableHeight = getHeight()-bottomPadding;
 			}
-        	count++;
-        }
+			 
+			
+			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
 	    }
 	    
 	    if (event.getAction() == MotionEvent.ACTION_DOWN || event.getAction() == MotionEvent.ACTION_UP){
+	    	shouldUpdate = true;
 	    	postInvalidate();
 	    }
 	    

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

 	private int lineToFill = -1;
 	private int indexSelected = -1;
 	private OnPointClickedListener listener;
+	private Bitmap fullImage;
+	private boolean shouldUpdate = false;
 	
 	public LineGraph(Context context){
 		super(context);
 	@Override
 	public void surfaceDestroyed(SurfaceHolder holder) {}
 	
-	public void onDraw(Canvas canvas) {
-		Paint paint = new Paint();
-		Path path = new Path();
-		float bottomPadding = 2, topPadding = 10;
-		float sidePadding = 10;
-		float usableHeight = getHeight() - bottomPadding - topPadding;
-		float usableWidth = getWidth() - 2*sidePadding;
-		
-		int lineCount = 0;
-		for (Line line : lines){
-			int count = 0;
-			float firstXPixels = 0, lastXPixels = 0, newYPixels = 0;
-			float lastYPixels = 0, newXPixels = 0;
-			float maxY = getMaxY();
-			float minY = getMinY();
-			float maxX = getMaxX();
-			float minX = getMinX();
+	public void onDraw(Canvas ca) {
+		if (fullImage == null || shouldUpdate) {
+			fullImage = Bitmap.createBitmap(getWidth(), getHeight(), Config.ARGB_8888);
+			Canvas canvas = new Canvas(fullImage);
+			Paint paint = new Paint();
+			Path path = new Path();
+			float bottomPadding = 2, topPadding = 10;
+			float sidePadding = 10;
+			float usableHeight = getHeight() - bottomPadding - topPadding;
+			float usableWidth = getWidth() - 2*sidePadding;
+			
+			int lineCount = 0;
+			for (Line line : lines){
+				int count = 0;
+				float firstXPixels = 0, lastXPixels = 0, newYPixels = 0;
+				float lastYPixels = 0, newXPixels = 0;
+				float maxY = getMaxY();
+				float minY = getMinY();
+				float maxX = getMaxX();
+				float minX = getMinX();
+				
+				if (lineCount == lineToFill){
+					
+					Bitmap b = Bitmap.createBitmap(getWidth(), getHeight(), Config.ARGB_8888);
+					Canvas c = new Canvas(b);
+					
+					for (LinePoint p : line.getPoints()){
+						float yPercent = (p.getY()-minY)/(maxY - minY);
+						float xPercent = (p.getX()-minX)/(maxX - minX);
+						if (count == 0){
+							lastXPixels = sidePadding + (xPercent*usableWidth);
+							lastYPixels = getHeight() - bottomPadding - (usableHeight*yPercent);
+							firstXPixels = lastXPixels;
+							path.moveTo(lastXPixels, lastYPixels);
+						} else {
+							newXPixels = sidePadding + (xPercent*usableWidth);
+							newYPixels = getHeight() - bottomPadding - (usableHeight*yPercent);
+							path.lineTo(newXPixels, newYPixels);
+							lastXPixels = newXPixels;
+							lastYPixels = newYPixels;
+						}
+						count++;
+					}
+					path.lineTo(newXPixels, getHeight()-bottomPadding);
+					path.lineTo(getWidth(), getHeight()-bottomPadding);
+					path.lineTo(getWidth(), 0);
+					path.lineTo(0, 0);
+					path.lineTo(0, getHeight()-bottomPadding);
+					path.lineTo(firstXPixels, getHeight()-bottomPadding);
+					path.close();
+						
+					paint.setColor(Color.BLACK);
+					paint.setAlpha(30);
+					paint.setStrokeWidth(2);
+					for (int i = 10; i-getWidth() < getHeight(); i = i+20){
+						c.drawLine(i, getHeight()-bottomPadding, 0, getHeight()-bottomPadding-i, paint);
+					}
+					paint.setColor(Color.RED);
+					paint.setAlpha(255);
+					c.drawPath(path, paint);
+	
+					Paint pa = new Paint();
+					pa.setColor(Color.RED); // ARGB for the color, in this case red
+					int removeColor = pa.getColor(); // store this color's int for later use
+					pa.setAlpha(0);
+					pa.setXfermode(new AvoidXfermode(removeColor, 0, AvoidXfermode.Mode.TARGET));
+					// draw transparent on the "brown" pixels
+					c.drawPaint(pa);
+					
+					canvas.drawBitmap(b, 0, 0, null);
+				}
+				
+				lineCount++;
+			}
+			
+			paint.setColor(Color.BLACK);
+			paint.setAlpha(50);
+			paint.setAntiAlias(true);
+			canvas.drawLine(sidePadding, getHeight() - bottomPadding, getWidth()-sidePadding, getHeight()-bottomPadding, paint);
+			paint.setAlpha(255);
 			
-			if (lineCount == lineToFill){
+			
+			for (Line line : lines){
+				int count = 0;
+				float lastXPixels = 0, newYPixels = 0;
+				float lastYPixels = 0, newXPixels = 0;
+				float maxY = getMaxY();
+				float minY = getMinY();
+				float maxX = getMaxX();
+				float minX = getMinX();
 				
-				Bitmap b = Bitmap.createBitmap(getWidth(), getHeight(), Config.ARGB_8888);
-				Canvas c = new Canvas(b);
+				paint.setColor(line.getColor());
+				paint.setStrokeWidth(6);
 				
 				for (LinePoint p : line.getPoints()){
 					float yPercent = (p.getY()-minY)/(maxY - minY);
 					if (count == 0){
 						lastXPixels = sidePadding + (xPercent*usableWidth);
 						lastYPixels = getHeight() - bottomPadding - (usableHeight*yPercent);
-						firstXPixels = lastXPixels;
-						path.moveTo(lastXPixels, lastYPixels);
 					} else {
 						newXPixels = sidePadding + (xPercent*usableWidth);
 						newYPixels = getHeight() - bottomPadding - (usableHeight*yPercent);
-						path.lineTo(newXPixels, newYPixels);
+						canvas.drawLine(lastXPixels, lastYPixels, newXPixels, newYPixels, paint);
 						lastXPixels = newXPixels;
 						lastYPixels = newYPixels;
 					}
 					count++;
 				}
-				path.lineTo(newXPixels, getHeight()-bottomPadding);
-				path.lineTo(getWidth(), getHeight()-bottomPadding);
-				path.lineTo(getWidth(), 0);
-				path.lineTo(0, 0);
-				path.lineTo(0, getHeight()-bottomPadding);
-				path.lineTo(firstXPixels, getHeight()-bottomPadding);
-				path.close();
-					
-				paint.setColor(Color.BLACK);
-				paint.setAlpha(30);
-				paint.setStrokeWidth(2);
-				for (int i = 10; i-getWidth() < getHeight(); i = i+20){
-					c.drawLine(i, getHeight()-bottomPadding, 0, getHeight()-bottomPadding-i, paint);
-				}
-				paint.setColor(Color.RED);
-				paint.setAlpha(255);
-				c.drawPath(path, paint);
-
-				Paint pa = new Paint();
-				pa.setColor(Color.RED); // ARGB for the color, in this case red
-				int removeColor = pa.getColor(); // store this color's int for later use
-				pa.setAlpha(0);
-				pa.setXfermode(new AvoidXfermode(removeColor, 0, AvoidXfermode.Mode.TARGET));
-				// draw transparent on the "brown" pixels
-				c.drawPaint(pa);
-				
-				canvas.drawBitmap(b, 0, 0, null);
 			}
 			
-			lineCount++;
-		}
-		
-		paint.setColor(Color.BLACK);
-		paint.setAlpha(50);
-		paint.setAntiAlias(true);
-		canvas.drawLine(sidePadding, getHeight() - bottomPadding, getWidth()-sidePadding, getHeight()-bottomPadding, paint);
-		paint.setAlpha(255);
-		
-		
-		for (Line line : lines){
-			int count = 0;
-			float lastXPixels = 0, newYPixels = 0;
-			float lastYPixels = 0, newXPixels = 0;
-			float maxY = getMaxY();
-			float minY = getMinY();
-			float maxX = getMaxX();
-			float minX = getMinX();
-			
-			paint.setColor(line.getColor());
-			paint.setStrokeWidth(6);
-			
-			for (LinePoint p : line.getPoints()){
-				float yPercent = (p.getY()-minY)/(maxY - minY);
-				float xPercent = (p.getX()-minX)/(maxX - minX);
-				if (count == 0){
-					lastXPixels = sidePadding + (xPercent*usableWidth);
-					lastYPixels = getHeight() - bottomPadding - (usableHeight*yPercent);
-				} else {
-					newXPixels = sidePadding + (xPercent*usableWidth);
-					newYPixels = getHeight() - bottomPadding - (usableHeight*yPercent);
-					canvas.drawLine(lastXPixels, lastYPixels, newXPixels, newYPixels, paint);
-					lastXPixels = newXPixels;
-					lastYPixels = newYPixels;
-				}
-				count++;
-			}
-		}
-		
-		
-		int pointCount = 0;
-		
-		for (Line line : lines){
-			float maxY = getMaxY();
-			float minY = getMinY();
-			float maxX = getMaxX();
-			float minX = getMinX();
 			
-			paint.setColor(line.getColor());
-			paint.setStrokeWidth(6);
-			paint.setStrokeCap(Paint.Cap.ROUND);
+			int pointCount = 0;
 			
-			if (line.isShowingPoints()){
-				for (LinePoint p : line.getPoints()){
-					float yPercent = (p.getY()-minY)/(maxY - minY);
-					float xPercent = (p.getX()-minX)/(maxX - minX);
-					float xPixels = sidePadding + (xPercent*usableWidth);
-					float yPixels = getHeight() - bottomPadding - (usableHeight*yPercent);
-					
-					paint.setColor(Color.GRAY);
-					canvas.drawCircle(xPixels, yPixels, 10, paint);
-					paint.setColor(Color.WHITE);
-					canvas.drawCircle(xPixels, yPixels, 5, paint);
-					
-					Path path2 = new Path();
-					path2.addCircle(xPixels, yPixels, 30, Direction.CW);
-					p.setPath(path2);
-					p.setRegion(new Region((int)(xPixels-30), (int)(yPixels-30), (int)(xPixels+30), (int)(yPixels+30)));
-					
-					if (indexSelected == pointCount){
-						paint.setColor(Color.parseColor("#33B5E5"));
-						paint.setAlpha(100);
-						canvas.drawPath(p.getPath(), paint);
-						paint.setAlpha(255);
+			for (Line line : lines){
+				float maxY = getMaxY();
+				float minY = getMinY();
+				float maxX = getMaxX();
+				float minX = getMinX();
+				
+				paint.setColor(line.getColor());
+				paint.setStrokeWidth(6);
+				paint.setStrokeCap(Paint.Cap.ROUND);
+				
+				if (line.isShowingPoints()){
+					for (LinePoint p : line.getPoints()){
+						float yPercent = (p.getY()-minY)/(maxY - minY);
+						float xPercent = (p.getX()-minX)/(maxX - minX);
+						float xPixels = sidePadding + (xPercent*usableWidth);
+						float yPixels = getHeight() - bottomPadding - (usableHeight*yPercent);
+						
+						paint.setColor(Color.GRAY);
+						canvas.drawCircle(xPixels, yPixels, 10, paint);
+						paint.setColor(Color.WHITE);
+						canvas.drawCircle(xPixels, yPixels, 5, paint);
+						
+						Path path2 = new Path();
+						path2.addCircle(xPixels, yPixels, 30, Direction.CW);
+						p.setPath(path2);
+						p.setRegion(new Region((int)(xPixels-30), (int)(yPixels-30), (int)(xPixels+30), (int)(yPixels+30)));
+						
+						if (indexSelected == pointCount && listener != null){
+							paint.setColor(Color.parseColor("#33B5E5"));
+							paint.setAlpha(100);
+							canvas.drawPath(p.getPath(), paint);
+							paint.setAlpha(255);
+						}
+						
+						pointCount++;
 					}
-					
-					pointCount++;
 				}
 			}
+			
+			shouldUpdate = false;
 		}
 		
+		ca.drawBitmap(fullImage, 0, 0, null);
+		
 		
 	}
 	
 	    }
 	    
 	    if (event.getAction() == MotionEvent.ACTION_DOWN || event.getAction() == MotionEvent.ACTION_UP){
+	    	shouldUpdate = true;
 	    	postInvalidate();
 	    }