/** draw graph without animation */ private void drawGraphWithoutAnimation(GraphCanvasWrapper graphCanvas) { for (int i = 0; i < mLineGraphVO.getArrGraph().size(); i++) { GraphPath linePath = new GraphPath( width, height, mLineGraphVO.getPaddingLeft(), mLineGraphVO.getPaddingBottom()); GraphPath regionPath = new GraphPath( width, height, mLineGraphVO.getPaddingLeft(), mLineGraphVO.getPaddingBottom()); boolean firstSet = false; float x = 0; float y = 0; p.setColor(mLineGraphVO.getArrGraph().get(i).getColor()); pCircle.setColor(mLineGraphVO.getArrGraph().get(i).getColor()); float xGap = xLength / (mLineGraphVO.getArrGraph().get(i).getCoordinateArr().length - 1); Bitmap icon = arrIcon.get(i); for (int j = 0; j < mLineGraphVO.getArrGraph().get(i).getCoordinateArr().length; j++) { if (j < mLineGraphVO.getArrGraph().get(i).getCoordinateArr().length) { if (!firstSet) { x = xGap * j; y = yLength * mLineGraphVO.getArrGraph().get(i).getCoordinateArr()[j] / mLineGraphVO.getMaxValue(); linePath.moveTo(x, y); firstSet = true; } else { x = xGap * j; y = yLength * mLineGraphVO.getArrGraph().get(i).getCoordinateArr()[j] / mLineGraphVO.getMaxValue(); linePath.lineTo(x, y); } if (icon == null) { graphCanvas.drawCircle(x, y, 4, pCircle); } else { graphCanvas.drawBitmapIcon(icon, x, y, null); } } } graphCanvas.getCanvas().drawPath(linePath, p); } }
/** draw Y Mark */ private void drawYMark(GraphCanvasWrapper canvas) { for (int i = 0; mLineGraphVO.getIncrement() * i <= mLineGraphVO.getMaxValue(); i++) { float y = yLength * mLineGraphVO.getIncrement() * i / mLineGraphVO.getMaxValue(); canvas.drawLine(0, y, -10, y, pBaseLine); } }
private void drawBaseLine(GraphCanvasWrapper graphCanvas) { for (int i = 1; mLineGraphVO.getIncrement() * i <= mLineGraphVO.getMaxValue(); i++) { float y = yLength * mLineGraphVO.getIncrement() * i / mLineGraphVO.getMaxValue(); graphCanvas.drawLine(0, y, chartXLength, y, pBaseLineX); } }
/** draw graph without animation */ private void drawGraphRegionWithoutAnimation(GraphCanvasWrapper graphCanvas) { boolean isDrawRegion = mLineGraphVO.isDrawRegion(); for (int i = 0; i < mLineGraphVO.getArrGraph().size(); i++) { GraphPath regionPath = new GraphPath( width, height, mLineGraphVO.getPaddingLeft(), mLineGraphVO.getPaddingBottom()); boolean firstSet = false; float x = 0; float y = 0; p.setColor(mLineGraphVO.getArrGraph().get(i).getColor()); pCircle.setColor(mLineGraphVO.getArrGraph().get(i).getColor()); float xGap = xLength / (mLineGraphVO.getArrGraph().get(i).getCoordinateArr().length - 1); for (int j = 0; j < mLineGraphVO.getArrGraph().get(i).getCoordinateArr().length; j++) { if (j < mLineGraphVO.getArrGraph().get(i).getCoordinateArr().length) { if (!firstSet) { x = xGap * j; y = yLength * mLineGraphVO.getArrGraph().get(i).getCoordinateArr()[j] / mLineGraphVO.getMaxValue(); regionPath.moveTo(x, 0); regionPath.lineTo(x, y); firstSet = true; } else { x = xGap * j; y = yLength * mLineGraphVO.getArrGraph().get(i).getCoordinateArr()[j] / mLineGraphVO.getMaxValue(); regionPath.lineTo(x, y); } } } if (isDrawRegion) { regionPath.lineTo(x, 0); regionPath.lineTo(0, 0); Paint pBg = new Paint(); pBg.setFlags(Paint.ANTI_ALIAS_FLAG); pBg.setAntiAlias(true); // text anti alias pBg.setFilterBitmap(true); // bitmap anti alias pBg.setStyle(Style.FILL); pBg.setColor(mLineGraphVO.getArrGraph().get(i).getColor()); graphCanvas.getCanvas().drawPath(regionPath, pBg); } } }
/** draw Y Text */ private void drawYText(GraphCanvasWrapper graphCanvas) { for (int i = 0; mLineGraphVO.getIncrement() * i <= mLineGraphVO.getMaxValue(); i++) { String mark = Float.toString(mLineGraphVO.getIncrement() * i); float y = yLength * mLineGraphVO.getIncrement() * i / mLineGraphVO.getMaxValue(); pMarkText.measureText(mark); pMarkText.setTextSize(20); Rect rect = new Rect(); pMarkText.getTextBounds(mark, 0, mark.length(), rect); // Log.e(TAG, "rect = height()" + rect.height()); // Log.e(TAG, "rect = width()" + rect.width()); graphCanvas.drawText(mark, -(rect.width() + 20), y - rect.height() / 2, pMarkText); } }
/** draw X Mark */ private void drawXMark(GraphCanvasWrapper graphCanvas) { float x = 0; float y = 0; float xGap = xLength / (mLineGraphVO.getArrGraph().get(0).getCoordinateArr().length - 1); for (int i = 0; i < mLineGraphVO.getArrGraph().get(0).getCoordinateArr().length; i++) { x = xGap * i; y = yLength * mLineGraphVO.getArrGraph().get(0).getCoordinateArr()[i] / mLineGraphVO.getMaxValue(); graphCanvas.drawLine(x, 0, x, -10, pBaseLine); } }
/** draw X Text */ private void drawXText(GraphCanvasWrapper graphCanvas) { float x = 0; float y = 0; float xGap = xLength / (mLineGraphVO.getArrGraph().get(0).getCoordinateArr().length - 1); for (int i = 0; i < mLineGraphVO.getLegendArr().length; i++) { x = xGap * i; String text = mLineGraphVO.getLegendArr()[i]; pMarkText.measureText(text); pMarkText.setTextSize(20); Rect rect = new Rect(); pMarkText.getTextBounds(text, 0, text.length(), rect); graphCanvas.drawText(text, x - (rect.width() / 2), -(20 + rect.height()), pMarkText); } }
/** draw graph with animation */ private void drawGraphWithAnimation(GraphCanvasWrapper graphCanvas) { // for draw animation float prev_x = 0; float prev_y = 0; float next_x = 0; float next_y = 0; float value = 0; float mode = 0; for (int i = 0; i < mLineGraphVO.getArrGraph().size(); i++) { GraphPath linePath = new GraphPath( width, height, mLineGraphVO.getPaddingLeft(), mLineGraphVO.getPaddingBottom()); GraphPath regionPath = new GraphPath( width, height, mLineGraphVO.getPaddingLeft(), mLineGraphVO.getPaddingBottom()); boolean firstSet = false; float x = 0; float y = 0; p.setColor(mLineGraphVO.getArrGraph().get(i).getColor()); pCircle.setColor(mLineGraphVO.getArrGraph().get(i).getColor()); float xGap = xLength / (mLineGraphVO.getArrGraph().get(i).getCoordinateArr().length - 1); Bitmap icon = arrIcon.get(i); value = anim / 1; mode = anim % 1; for (int j = 0; j < value + 1; j++) { if (j < mLineGraphVO.getArrGraph().get(i).getCoordinateArr().length) { if (!firstSet) { x = xGap * j; y = yLength * mLineGraphVO.getArrGraph().get(i).getCoordinateArr()[j] / mLineGraphVO.getMaxValue(); linePath.moveTo(x, y); firstSet = true; } else { x = xGap * j; y = yLength * mLineGraphVO.getArrGraph().get(i).getCoordinateArr()[j] / mLineGraphVO.getMaxValue(); if (j > value && mode != 0) { next_x = x - prev_x; next_y = y - prev_y; linePath.lineTo(prev_x + next_x * mode, prev_y + next_y * mode); } else { linePath.lineTo(x, y); } } if (icon == null) { graphCanvas.drawCircle(x, y, 4, pCircle); } else { graphCanvas.drawBitmapIcon(icon, x, y, null); } prev_x = x; prev_y = y; } } graphCanvas.getCanvas().drawPath(linePath, p); } }
/** draw graph with animation */ private void drawGraphRegionWithAnimation(GraphCanvasWrapper graphCanvas) { // for draw animation float prev_x = 0; float prev_y = 0; float next_x = 0; float next_y = 0; int value = 0; float mode = 0; boolean isDrawRegion = mLineGraphVO.isDrawRegion(); for (int i = 0; i < mLineGraphVO.getArrGraph().size(); i++) { GraphPath regionPath = new GraphPath( width, height, mLineGraphVO.getPaddingLeft(), mLineGraphVO.getPaddingBottom()); boolean firstSet = false; float x = 0; float y = 0; p.setColor(mLineGraphVO.getArrGraph().get(i).getColor()); pCircle.setColor(mLineGraphVO.getArrGraph().get(i).getColor()); float xGap = xLength / (mLineGraphVO.getArrGraph().get(i).getCoordinateArr().length - 1); value = (int) (anim / 1); mode = anim % 1; boolean isFinish = false; for (int j = 0; j <= value + 1; j++) { if (j < mLineGraphVO.getArrGraph().get(i).getCoordinateArr().length) { if (!firstSet) { x = xGap * j; y = yLength * mLineGraphVO.getArrGraph().get(i).getCoordinateArr()[j] / mLineGraphVO.getMaxValue(); regionPath.moveTo(x, 0); regionPath.lineTo(x, y); firstSet = true; } else { x = xGap * j; y = yLength * mLineGraphVO.getArrGraph().get(i).getCoordinateArr()[j] / mLineGraphVO.getMaxValue(); if (j > value) { next_x = x - prev_x; next_y = y - prev_y; regionPath.lineTo(prev_x + next_x * mode, prev_y + next_y * mode); } else { regionPath.lineTo(x, y); } } prev_x = x; prev_y = y; } } isFinish = true; if (isDrawRegion) { float x_bg = prev_x + next_x * mode; if (x_bg >= xLength) { x_bg = xLength; } regionPath.lineTo(x_bg, 0); regionPath.lineTo(0, 0); Paint pBg = new Paint(); pBg.setFlags(Paint.ANTI_ALIAS_FLAG); pBg.setAntiAlias(true); // text anti alias pBg.setFilterBitmap(true); // bitmap anti alias pBg.setStyle(Style.FILL); pBg.setColor(mLineGraphVO.getArrGraph().get(i).getColor()); graphCanvas.getCanvas().drawPath(regionPath, pBg); } } }
@Override public void run() { Canvas canvas = null; GraphCanvasWrapper graphCanvasWrapper = null; Log.e(TAG, "height = " + height); Log.e(TAG, "width = " + width); setPaint(); isAnimation(); isDrawRegion(); animStartTime = System.currentTimeMillis(); while (isRun) { // draw only on dirty mode if (!isDirty) { try { Thread.sleep(100); } catch (InterruptedException e1) { e1.printStackTrace(); } continue; } canvas = mHolder.lockCanvas(); graphCanvasWrapper = new GraphCanvasWrapper( canvas, width, height, mLineGraphVO.getPaddingLeft(), mLineGraphVO.getPaddingBottom()); try { Thread.sleep(0000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } calcTimePass(); synchronized (mHolder) { synchronized (touchLock) { try { // bg color canvas.drawColor(Color.WHITE); if (bg != null) { canvas.drawBitmap(bg, 0, 0, null); } // x coord dot line drawBaseLine(graphCanvasWrapper); // y coord graphCanvasWrapper.drawLine(0, 0, 0, chartYLength, pBaseLine); // x coord graphCanvasWrapper.drawLine(0, 0, chartXLength, 0, pBaseLine); // x, y coord mark drawXMark(graphCanvasWrapper); drawYMark(graphCanvasWrapper); // x, y coord text drawXText(graphCanvasWrapper); drawYText(graphCanvasWrapper); // Graph drawGraphRegion(graphCanvasWrapper); drawGraph(graphCanvasWrapper); drawGraphName(canvas); // isDirty = false; } catch (Exception e) { e.printStackTrace(); } finally { if (graphCanvasWrapper.getCanvas() != null) { mHolder.unlockCanvasAndPost(graphCanvasWrapper.getCanvas()); } } } } } }