Переглянути джерело

fixed label alignment in the line bar

Julien Clauter 10 роки тому
батько
коміт
aa2cce4289
1 змінених файлів з 31 додано та 23 видалено
  1. 31 23
      src/com/echo/holographlibrary/LineGraph.java

+ 31 - 23
src/com/echo/holographlibrary/LineGraph.java

@@ -48,7 +48,7 @@ public class LineGraph extends View {
         public String convertDataForY_Position(double y);
     }
 
-    private final static int AXIS_LABEL_FONT_SIZE = 10;
+    private final static int AXIS_LABEL_FONT_SIZE = 8;
 
     private ArrayList<Line> lines = new ArrayList<Line>();
 	Paint paint = new Paint();
@@ -64,8 +64,8 @@ public class LineGraph extends View {
 	private Bitmap fullImage;
 	private boolean shouldUpdate = false;
 
-    static final float bottomPadding = 50, topPadding = 10;
-    static final float leftPadding = 50, rightPadding = 10;
+    static final float bottomPadding = 40, topPadding = 16;
+    static final float leftPadding = 50, rightPadding = 16;
     static final float sidePadding = rightPadding + leftPadding;
 
     private float xAxisStep = 4;
@@ -361,13 +361,12 @@ public class LineGraph extends View {
 	        // DRAW THE BACKGROUND
             //this.drawBackground(canvas);
 
+            paint.setAntiAlias(true);
+
             // DRAW THE AXIS
             this.drawAxis(canvas);
 
 
-            paint.reset();
-	        paint.setAntiAlias(true);
-
             // DRAW LINES
 			for (Line line : lines){
 				int count = 0;
@@ -445,20 +444,16 @@ public class LineGraph extends View {
 		}
 		
 		ca.drawBitmap(fullImage, 0, 0, null);
-		
-		
+
 	}
 
     private void drawAxis(Canvas canvas){
-        paint.reset();
+        //double maxX = getMaxLimX();
+        //double minX = getMinLimX();
+        //float usableWidth = getWidth() - 2*sidePadding;
+        //float usableHeight = getHeight() - bottomPadding - topPadding;
 
-        double maxX = getMaxLimX();
-        double minX = getMinLimX();
-
-        float usableWidth = getWidth() - 2*sidePadding;
-        float usableHeight = getHeight() - bottomPadding - topPadding;
-
-        float yPixels = getHeight() - (bottomPadding*(4.f/5.f));
+        float yPixels = getHeight() - (bottomPadding - (10));
 
         // DRAW SEPERATOR
         paint.setColor(Color.BLACK);
@@ -470,8 +465,22 @@ public class LineGraph extends View {
         canvas.drawLine(leftPadding ,topPadding, leftPadding, yPixels, paint);
 
         paint.setAlpha(255);
+
+        this.drawAxisLabel(canvas);
+    }
+
+
+    private void drawAxisLabel(Canvas canvas){
         this.paint.setTextSize(AXIS_LABEL_FONT_SIZE * mContext.getResources().getDisplayMetrics().scaledDensity);
 
+        double maxX = getMaxLimX();
+        double minX = getMinLimX();
+
+        float usableWidth = getWidth() - 2*sidePadding;
+        float usableHeight = getHeight() - bottomPadding - topPadding;
+        float yPixels = getHeight() - (bottomPadding - (10));
+
+
         // Draw y-axis label text
         double step = Math.max(1., (maxY - minY) / (Math.max(1., yAxisStep)));
         double v = 0;
@@ -485,12 +494,11 @@ public class LineGraph extends View {
             canvas.drawText(title, 5.f ,(float)newYPixels + (textwidth/2), this.paint);
 
             v+=step;
-            //value+=skippedValue;
         }
 
+        this.paint.setTextSize(AXIS_LABEL_FONT_SIZE * mContext.getResources().getDisplayMetrics().scaledDensity);
+
         // Draw x-axis label text
-        //value = minX;
-        //skippedValue = (maxX - minX ) / (Math.max(1,xAxisStep));
         step = Math.max(1, (maxX - minX) / (Math.max(1, xAxisStep)));
 
         for (double x = minX; x <= maxX; x+=step){
@@ -500,12 +508,12 @@ public class LineGraph extends View {
             canvas.drawLine((float)newXPixels,(float) yPixels + 5,(float) newXPixels,(float) yPixels, paint);
             String title = this.getX_AxisTitle(x);
             float textwidth = (this.paint.measureText(title));
-            //this.paint.setTextSize(AXIS_LABEL_FONT_SIZE * mContext.getResources().getDisplayMetrics().scaledDensity);
-            canvas.drawText(title,(float) newXPixels - (textwidth/2),(float) yPixels + (bottomPadding / 2), this.paint);
 
-            //value+=skippedValue;
+            float x_Coord = Math.max(0.f, (float) (newXPixels - (textwidth / 2)));
+            x_Coord = Math.min(x_Coord, getWidth() - rightPadding);
+            float y_Coord =  (float) (yPixels + ((bottomPadding - 10) / 1));
+            canvas.drawText(title, x_Coord , y_Coord, this.paint);
         }
-        paint.reset();
     }
 
     private void drawBackground(Canvas canvas){