Skip to content
Browse files

view with children views for future features

  • Loading branch information...
1 parent 134fa19 commit 71a2b5277623415a7657accefc57c6599455aa3c Jonas Gehring committed Jul 5, 2011
Showing with 164 additions and 126 deletions.
  1. +164 −126 src/com/jjoe64/graphview/GraphView.java
View
290 src/com/jjoe64/graphview/GraphView.java
@@ -6,18 +6,145 @@
import android.graphics.Paint;
import android.graphics.Paint.Align;
import android.view.View;
+import android.widget.LinearLayout;
/**
* GraphView creates a scaled line or bar graph with x and y axis labels.
* @author originally: Arno den Hond
*
*/
-public class GraphView extends View {
+public class GraphView extends LinearLayout {
static final private class GraphViewConfig {
static final float BORDER = 20;
static final float VERTICAL_LABEL_WIDTH = 100;
static final float HORIZONTAL_LABEL_HEIGHT = 80;
}
+ private class GraphViewContentView extends View {
+ public GraphViewContentView(Context context) {
+ super(context);
+ setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT, 1));
+ }
+
+ @Override
+ protected void onDraw(Canvas canvas) {
+ // normal
+ paint.setStrokeWidth(0);
+
+ float border = GraphViewConfig.BORDER;
+ float horstart = 0;
+ float height = getHeight();
+ float width = getWidth() - 1;
+ double maxY = getMaxY();
+ double minY = getMinY();
+ double diffY = maxY - minY;
+ double maxX = getMaxX();
+ double minX = getMinX();
+ double diffX = maxX - minX;
+ float graphheight = height - (2 * border);
+ float graphwidth = width;
+
+ if (horlabels == null) {
+ horlabels = generateHorlabels(graphwidth);
+ }
+
+ // vertical lines
+ paint.setTextAlign(Align.LEFT);
+ int vers = verlabels.length - 1;
+ for (int i = 0; i < verlabels.length; i++) {
+ paint.setColor(Color.DKGRAY);
+ float y = ((graphheight / vers) * i) + border;
+ canvas.drawLine(horstart, y, width, y, paint);
+ }
+
+ // horizontal labels + lines
+ int hors = horlabels.length - 1;
+ for (int i = 0; i < horlabels.length; i++) {
+ paint.setColor(Color.DKGRAY);
+ float x = ((graphwidth / hors) * i) + horstart;
+ canvas.drawLine(x, height - border, x, border, paint);
+ paint.setTextAlign(Align.CENTER);
+ if (i==horlabels.length-1)
+ paint.setTextAlign(Align.RIGHT);
+ if (i==0)
+ paint.setTextAlign(Align.LEFT);
+ paint.setColor(Color.WHITE);
+ canvas.drawText(horlabels[i], x, height - 4, paint);
+ }
+
+ paint.setTextAlign(Align.CENTER);
+ canvas.drawText(title, (graphwidth / 2) + horstart, border - 4, paint);
+
+ if (maxY != minY) {
+ // blue version
+ paint.setARGB(255, 0, 119, 204);
+ paint.setStrokeCap(Paint.Cap.ROUND);
+ paint.setStrokeWidth(3);
+
+ // draw background
+ double lastEndY = 0;
+ double lastEndX = 0;
+ if (drawBackground) {
+ float startY = graphheight + border;
+ for (int i = 0; i < values.length; i++) {
+ double valY = values[i].valueY - minY;
+ double ratY = valY / diffY;
+ double y = graphheight * ratY;
+
+ double valX = values[i].valueX - minX;
+ double ratX = valX / diffX;
+ double x = graphwidth * ratX;
+
+ float endX = (float) x + (horstart + 1);
+ float endY = (float) (border - y) + graphheight +2;
+
+ if (i > 0) {
+ // fill space between last and current point
+ int numSpace = (int) ((endX - lastEndX) / 3f) +1;
+ for (int xi=0; xi<numSpace; xi++) {
+ float spaceX = (float) (lastEndX + ((endX-lastEndX)*xi/(numSpace-1)));
+ float spaceY = (float) (lastEndY + ((endY-lastEndY)*xi/(numSpace-1)));
+
+ // start => bottom edge
+ float startX = spaceX;
+
+ // do not draw over the left edge
+ if (startX-horstart > 1) {
+ canvas.drawLine(startX, startY, spaceX, spaceY, paintBackground);
+ }
+ }
+ }
+
+ lastEndY = endY;
+ lastEndX = endX;
+ }
+ }
+
+ // draw data
+ lastEndY = 0;
+ lastEndX = 0;
+ for (int i = 0; i < values.length; i++) {
+ double valY = values[i].valueY - minY;
+ double ratY = valY / diffY;
+ double y = graphheight * ratY;
+
+ double valX = values[i].valueX - minX;
+ double ratX = valX / diffX;
+ double x = graphwidth * ratX;
+
+ if (i > 0) {
+ float startX = (float) lastEndX + (horstart + 1);
+ float startY = (float) (border - lastEndY) + graphheight;
+ float endX = (float) x + (horstart + 1);
+ float endY = (float) (border - y) + graphheight;
+
+ canvas.drawLine(startX, startY, endX, endY, paint);
+ }
+ lastEndY = y;
+ lastEndX = x;
+ }
+ }
+ }
+ }
static public class GraphViewData {
double valueX;
double valueY;
@@ -27,6 +154,35 @@ public GraphViewData(double valueX, double valueY) {
this.valueY = valueY;
}
}
+ private class VerLabelsView extends View {
+ public VerLabelsView(Context context) {
+ super(context);
+ setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT, 10));
+ }
+
+ @Override
+ protected void onDraw(Canvas canvas) {
+ // normal
+ paint.setStrokeWidth(0);
+
+ float border = GraphViewConfig.BORDER;
+ float height = getHeight();
+ float graphheight = height - (2 * border);
+
+ if (verlabels == null) {
+ verlabels = generateVerlabels(graphheight);
+ }
+
+ // vertical labels
+ paint.setTextAlign(Align.LEFT);
+ int vers = verlabels.length - 1;
+ for (int i = 0; i < verlabels.length; i++) {
+ float y = ((graphheight / vers) * i) + border;
+ paint.setColor(Color.WHITE);
+ canvas.drawText(verlabels[i], 0, y, paint);
+ }
+ }
+ }
private final Paint paint;
private final Paint paintBackground;
@@ -46,6 +202,8 @@ public GraphViewData(double valueX, double valueY) {
*/
public GraphView(Context context, GraphViewData[] values, String title, String[] horlabels, String[] verlabels) {
super(context);
+ setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
+
if (values == null)
values = new GraphViewData[0];
else
@@ -62,6 +220,11 @@ public GraphView(Context context, GraphViewData[] values, String title, String[]
paintBackground = new Paint();
paintBackground.setARGB(255, 20, 40, 60);
paintBackground.setStrokeWidth(4);
+
+ View viewVerLabels = new VerLabelsView(context);
+ addView(viewVerLabels);
+ View viewContent = new GraphViewContentView(context);
+ addView(viewContent);
}
private String[] generateHorlabels(float graphwidth) {
@@ -116,131 +279,6 @@ public boolean isDrawBackground() {
return drawBackground;
}
- @Override
- protected void onDraw(Canvas canvas) {
- // normal
- paint.setStrokeWidth(0);
-
- float border = GraphViewConfig.BORDER;
- float horstart = border * 2;
- float height = getHeight();
- float width = getWidth() - 1;
- double maxY = getMaxY();
- double minY = getMinY();
- double diffY = maxY - minY;
- double maxX = getMaxX();
- double minX = getMinX();
- double diffX = maxX - minX;
- float graphheight = height - (2 * border);
- float graphwidth = width - (2 * border);
-
- if (horlabels == null) {
- horlabels = generateHorlabels(graphwidth);
- }
- if (verlabels == null) {
- verlabels = generateVerlabels(graphheight);
- }
-
- // vertical labels + lines
- paint.setTextAlign(Align.LEFT);
- int vers = verlabels.length - 1;
- for (int i = 0; i < verlabels.length; i++) {
- paint.setColor(Color.DKGRAY);
- float y = ((graphheight / vers) * i) + border;
- canvas.drawLine(horstart, y, width, y, paint);
- paint.setColor(Color.WHITE);
- canvas.drawText(verlabels[i], 0, y, paint);
- }
-
- // horizontal labels + lines
- int hors = horlabels.length - 1;
- for (int i = 0; i < horlabels.length; i++) {
- paint.setColor(Color.DKGRAY);
- float x = ((graphwidth / hors) * i) + horstart;
- canvas.drawLine(x, height - border, x, border, paint);
- paint.setTextAlign(Align.CENTER);
- if (i==horlabels.length-1)
- paint.setTextAlign(Align.RIGHT);
- if (i==0)
- paint.setTextAlign(Align.LEFT);
- paint.setColor(Color.WHITE);
- canvas.drawText(horlabels[i], x, height - 4, paint);
- }
-
- paint.setTextAlign(Align.CENTER);
- canvas.drawText(title, (graphwidth / 2) + horstart, border - 4, paint);
-
- if (maxY != minY) {
- // blue version
- paint.setARGB(255, 0, 119, 204);
- paint.setStrokeCap(Paint.Cap.ROUND);
- paint.setStrokeWidth(3);
-
- // draw background
- double lastEndY = 0;
- double lastEndX = 0;
- if (drawBackground) {
- float startY = graphheight + border;
- for (int i = 0; i < values.length; i++) {
- double valY = values[i].valueY - minY;
- double ratY = valY / diffY;
- double y = graphheight * ratY;
-
- double valX = values[i].valueX - minX;
- double ratX = valX / diffX;
- double x = graphwidth * ratX;
-
- float endX = (float) x + (horstart + 1);
- float endY = (float) (border - y) + graphheight +2;
-
- if (i > 0) {
- // fill space between last and current point
- int numSpace = (int) ((endX - lastEndX) / 3f) +1;
- for (int xi=0; xi<numSpace; xi++) {
- float spaceX = (float) (lastEndX + ((endX-lastEndX)*xi/(numSpace-1)));
- float spaceY = (float) (lastEndY + ((endY-lastEndY)*xi/(numSpace-1)));
-
- // start => bottom edge
- float startX = spaceX;
-
- // do not draw over the left edge
- if (startX-horstart > 1) {
- canvas.drawLine(startX, startY, spaceX, spaceY, paintBackground);
- }
- }
- }
-
- lastEndY = endY;
- lastEndX = endX;
- }
- }
-
- // draw data
- lastEndY = 0;
- lastEndX = 0;
- for (int i = 0; i < values.length; i++) {
- double valY = values[i].valueY - minY;
- double ratY = valY / diffY;
- double y = graphheight * ratY;
-
- double valX = values[i].valueX - minX;
- double ratX = valX / diffX;
- double x = graphwidth * ratX;
-
- if (i > 0) {
- float startX = (float) lastEndX + (horstart + 1);
- float startY = (float) (border - lastEndY) + graphheight;
- float endX = (float) x + (horstart + 1);
- float endY = (float) (border - y) + graphheight;
-
- canvas.drawLine(startX, startY, endX, endY, paint);
- }
- lastEndY = y;
- lastEndX = x;
- }
- }
- }
-
public void setDrawBackground(boolean drawBackground) {
this.drawBackground = drawBackground;
}

0 comments on commit 71a2b52

Please sign in to comment.
Something went wrong with that request. Please try again.