Custome Graph View
custome line chart
//
add class to project
custome line chart
//
add class to project
public class CustomeGraphview extends View { private Paint centerLinePaint; private GestureDetector gestureDetector; private Scroller scroller; private float nextX; private int maxX; private float currentX; private Paint tickPaint; private Paint tickTextPaint; private int startDistance; private int bitweenDistance; private int totalDistance; private int noOfMonth; ArrayList<Integer> arrListRates; ArrayList<String> arrListMonths; private int Line0, Line1, Line2, Line3, Line4, Line5, LineForText; private int lastX, lastY; boolean iScall = true; private int circleRadius = 3; @Override protected void onSizeChanged(int w, int h, int oldw, int oldh) { super.onSizeChanged(w, h, oldw, oldh); Log.d("TAG", "onsize"); bitweenDistance = (getWidth() / 6); startDistance = (getWidth() / 12); totalDistance = (bitweenDistance * (noOfMonth - 1)) + (startDistance * 2); maxX = totalDistance - getWidth(); Line5 = getHeight() / 7; Line4 = (getHeight() / 7) * 2; Line3 = (getHeight() / 7) * 3; Line2 = (getHeight() / 7) * 4; Line1 = (getHeight() / 7) * 5; Line0 = (getHeight() / 7) * 6; LineForText = Line0 + ((getHeight() / 7) / 2); } public void setDetail(ArrayList<Integer> arrListRates, ArrayList<String> arrListMonths, int noOfMonth) { this.arrListMonths = arrListMonths; this.arrListRates = arrListRates; this.noOfMonth = noOfMonth; } private Runnable requestLayoutRunnable = new Runnable() { public void run() { requestLayout(); } }; public CustomeGraphview(Context context, ArrayList<Integer> arrListRates, ArrayList<String> arrListMonths, int noOfMonth) { super(context); Log.d("TAG", "contstructor"); this.arrListMonths = arrListMonths; this.arrListRates = arrListRates; if (arrListMonths.size() < 6) { this.noOfMonth = 6; } else { this.noOfMonth = arrListMonths.size(); } initView(); } private void initView() { Log.d("TAG", "init view"); if (centerLinePaint != null) { return; } centerLinePaint = new Paint(Paint.ANTI_ALIAS_FLAG); centerLinePaint.setStrokeWidth(5.0f); centerLinePaint.setColor(Color.argb(255, 38, 189, 190)); tickPaint = new Paint(Paint.ANTI_ALIAS_FLAG); tickPaint.setStrokeWidth(1.0f); tickPaint.setColor(Color.argb(200, 181, 181, 181)); tickTextPaint = new Paint(Paint.ANTI_ALIAS_FLAG); tickTextPaint.setStrokeWidth(1.0f); tickTextPaint.setColor(Color.argb(255, 181, 181, 181)); nextX = getWidth(); currentX = 0; scroller = new Scroller(getContext()); gestureDetector = new GestureDetector(getContext(), new GestureDetector.SimpleOnGestureListener() { @Override public boolean onDown(MotionEvent e) { return CustomeGraphview.this.onDown(e); } @Override public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { return true; } @Override public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) { synchronized (CustomeGraphview.this) { nextX += (int) distanceX; } requestLayout(); return true; } }); } @Override public boolean dispatchTouchEvent(MotionEvent event) { boolean handled = super.dispatchTouchEvent(event); handled |= gestureDetector.onTouchEvent(event); return handled; } protected boolean onDown(MotionEvent e) { scroller.forceFinished(true); return true; } @Override protected void onLayout(boolean changed, int left, int top, int right, int bottom) { super.onLayout(changed, left, top, right, bottom); if (scroller.computeScrollOffset()) { int scrollx = scroller.getCurrX(); nextX = scrollx; } if (nextX <= 0) { nextX = 0; scroller.forceFinished(true); } if (nextX >= maxX) { nextX = maxX; scroller.forceFinished(true); } currentX = nextX; if (!scroller.isFinished()) { post(requestLayoutRunnable); } else { postInvalidate(); } } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); canvas.drawLine(0, Line0, getWidth(), Line0, tickPaint); canvas.drawLine(0, Line1, getWidth(), Line1, tickPaint); canvas.drawLine(0, Line2, getWidth(), Line2, tickPaint); canvas.drawLine(0, Line3, getWidth(), Line3, tickPaint); canvas.drawLine(0, Line4, getWidth(), Line4, tickPaint); canvas.drawLine(0, Line5, getWidth(), Line5, tickPaint); if (arrListMonths.size() < 6) { for (float mm = 0.0f; mm < 6; mm++) { int x = getXFromMillimeters(mm); // canvas.drawLine(x, centerY - 100, x, centerY, tickPaint); try { if (mm == 0.0) { if (arrListRates.get((int) mm) == 0) { lastY = Line0; } else if (arrListRates.get((int) mm) == 1) { lastY = Line1; } else if (arrListRates.get((int) mm) == 2) { lastY = Line2; } else if (arrListRates.get((int) mm) == 3) { lastY = Line3; } else if (arrListRates.get((int) mm) == 4) { lastY = Line4; } else if (arrListRates.get((int) mm) == 5) { lastY = Line5; } lastX = x; } else { if (arrListRates.get((int) mm) == 0) { canvas.drawLine(lastX, lastY, x, Line0, centerLinePaint); lastY = Line0; } else if (arrListRates.get((int) mm) == 1) { canvas.drawLine(lastX, lastY, x, Line1, centerLinePaint); lastY = Line1; } else if (arrListRates.get((int) mm) == 2) { canvas.drawLine(lastX, lastY, x, Line2, centerLinePaint); lastY = Line2; } else if (arrListRates.get((int) mm) == 3) { canvas.drawLine(lastX, lastY, x, Line3, centerLinePaint); lastY = Line3; } else if (arrListRates.get((int) mm) == 4) { canvas.drawLine(lastX, lastY, x, Line4, centerLinePaint); lastY = Line4; } else if (arrListRates.get((int) mm) == 5) { canvas.drawLine(lastX, lastY, x, Line5, centerLinePaint); lastY = Line5; } lastX = x; } if (arrListRates.get((int) mm) == 0) { RectF rectF = new RectF(x - circleRadius, Line0 - circleRadius, x + circleRadius, Line0 + circleRadius); canvas.drawOval(rectF, centerLinePaint); } else if (arrListRates.get((int) mm) == 1) { RectF rectF = new RectF(x - circleRadius, Line1 - circleRadius, x + circleRadius, Line1 + circleRadius); canvas.drawOval(rectF, centerLinePaint); } else if (arrListRates.get((int) mm) == 2) { RectF rectF = new RectF(x - circleRadius, Line2 - circleRadius, x + circleRadius, Line2 + circleRadius); canvas.drawOval(rectF, centerLinePaint); } else if (arrListRates.get((int) mm) == 3) { RectF rectF = new RectF(x - circleRadius, Line3 - circleRadius, x + circleRadius, Line3 + circleRadius); canvas.drawOval(rectF, centerLinePaint); } else if (arrListRates.get((int) mm) == 4) { RectF rectF = new RectF(x - circleRadius, Line4 - circleRadius, x + circleRadius, Line4 + circleRadius); canvas.drawOval(rectF, centerLinePaint); } else if (arrListRates.get((int) mm) == 5) { RectF rectF = new RectF(x - circleRadius, Line5 - circleRadius, x + circleRadius, Line5 + circleRadius); canvas.drawOval(rectF, centerLinePaint); } // for text drawHvAlignedText(canvas, x, LineForText, arrListMonths.get((int) mm), tickTextPaint, Paint.Align.CENTER, TextVertAlign.Middle); } catch (Exception e) { } } } else { for (float mm = 0.0f; mm < arrListMonths.size(); mm++) { int x = getXFromMillimeters(mm); // canvas.drawLine(x, centerY - 100, x, centerY, tickPaint); try { if (mm == 0.0) { if (arrListRates.get((int) mm) == 0) { lastY = Line0; } else if (arrListRates.get((int) mm) == 1) { lastY = Line1; } else if (arrListRates.get((int) mm) == 2) { lastY = Line2; } else if (arrListRates.get((int) mm) == 3) { lastY = Line3; } else if (arrListRates.get((int) mm) == 4) { lastY = Line4; } else if (arrListRates.get((int) mm) == 5) { lastY = Line5; } lastX = x; } else { if (arrListRates.get((int) mm) == 0) { canvas.drawLine(lastX, lastY, x, Line0, centerLinePaint); lastY = Line0; } else if (arrListRates.get((int) mm) == 1) { canvas.drawLine(lastX, lastY, x, Line1, centerLinePaint); lastY = Line1; } else if (arrListRates.get((int) mm) == 2) { canvas.drawLine(lastX, lastY, x, Line2, centerLinePaint); lastY = Line2; } else if (arrListRates.get((int) mm) == 3) { canvas.drawLine(lastX, lastY, x, Line3, centerLinePaint); lastY = Line3; } else if (arrListRates.get((int) mm) == 4) { canvas.drawLine(lastX, lastY, x, Line4, centerLinePaint); lastY = Line4; } else if (arrListRates.get((int) mm) == 5) { canvas.drawLine(lastX, lastY, x, Line5, centerLinePaint); lastY = Line5; } lastX = x; } if (arrListRates.get((int) mm) == 0) { RectF rectF = new RectF(x - circleRadius, Line0 - circleRadius, x + circleRadius, Line0 + circleRadius); canvas.drawOval(rectF, centerLinePaint); } else if (arrListRates.get((int) mm) == 1) { RectF rectF = new RectF(x - circleRadius, Line1 - circleRadius, x + circleRadius, Line1 + circleRadius); canvas.drawOval(rectF, centerLinePaint); } else if (arrListRates.get((int) mm) == 2) { RectF rectF = new RectF(x - circleRadius, Line2 - circleRadius, x + circleRadius, Line2 + circleRadius); canvas.drawOval(rectF, centerLinePaint); } else if (arrListRates.get((int) mm) == 3) { RectF rectF = new RectF(x - circleRadius, Line3 - circleRadius, x + circleRadius, Line3 + circleRadius); canvas.drawOval(rectF, centerLinePaint); } else if (arrListRates.get((int) mm) == 4) { RectF rectF = new RectF(x - circleRadius, Line4 - circleRadius, x + circleRadius, Line4 + circleRadius); canvas.drawOval(rectF, centerLinePaint); } else if (arrListRates.get((int) mm) == 5) { RectF rectF = new RectF(x - circleRadius, Line5 - circleRadius, x + circleRadius, Line5 + circleRadius); canvas.drawOval(rectF, centerLinePaint); } // for text drawHvAlignedText(canvas, x, LineForText, arrListMonths.get((int) mm), tickTextPaint, Paint.Align.CENTER, TextVertAlign.Middle); } catch (Exception e) { } } } } private int getXFromMillimeters(float mm) { int centerX = startDistance; int scrollLeftX = (int) (currentX - centerX); int temp = (int) (mm * bitweenDistance) - scrollLeftX; return temp; } public enum TextVertAlign { Top, Middle, Baseline, Bottom }; public static void drawHvAlignedText(Canvas canvas, float x, float y, String s, Paint p, Paint.Align horizAlign, TextVertAlign vertAlign) { float testTextSize = canvas.getWidth() / 30; p.setTextSize(testTextSize); p.setTypeface(Typeface.create(Typeface.DEFAULT, Typeface.NORMAL)); // Set horizontal alignment p.setTextAlign(horizAlign); // Get bounding rectangle - we need its attribute and method values Rect r = new Rect(); p.getTextBounds(s, 0, s.length(), r); // Note: r.top will be negative // Compute y-coordinate we'll need for drawing text for specified // vertical alignment float textX = x; float textY = y; switch (vertAlign) { case Top: textY = y - r.top; break; case Middle: textY = y - r.top - r.height() / 2; break; case Baseline: // Default behavior - do nothing break; case Bottom: textY = y - (r.height() + r.top); break; } canvas.drawText(s, textX, textY, p); } }
// call from activiy
LinearLayout linear_graphlayout = (LinearLayout) findViewById(R.id.linear_graphlayout);
CustomeGraphview cgv = new CustomeGraphview(getParent(), arrListRates, arrListMonths, arrListRates.size()); linear_graphlayout.addView(cgv);