List of usage examples for android.graphics Canvas drawLine
public void drawLine(float startX, float startY, float stopX, float stopY, @NonNull Paint paint)
From source file:com.cssweb.android.view.KlineViewSingle.java
public void drawVOLUME(Canvas canvas, JSONObject quoteData, int begin, int count, double shapeWidth, double spaceWidth, double highPrice, double lowPrice, double highVolume, int actualDataLen) throws JSONException { if (quoteData == null || quoteData.isNull("MA")) { isNetworkErrorDisplay();/* w w w.j a va 2s . com*/ return; } axisX = klineX; for (int i = begin; i < (begin + count); i++) { highVolume = Arith.max(highVolume, quoteData.getJSONArray("MA").getJSONArray(i).getDouble(5), quoteData.getJSONArray("MA").getJSONArray(i).getDouble(6)); } if (highVolume > 0) { scale = volumeHeight / highVolume; } else { scale = 999999; } double ratio = highVolume / rowNum; ratio = Math.round(ratio); String lblvalue = "12345", labelRatio = ""; String lbhighVolume = String.valueOf(Math.round(highVolume)); int ratiolen = lbhighVolume.length() - lblvalue.length(); double scaleVol = 1; switch (ratiolen) { case 1: labelRatio = "x10"; scaleVol = 10; break; case 2: labelRatio = "x100"; scaleVol = 100; break; case 3: labelRatio = "x1000"; scaleVol = 1000; break; case 4: labelRatio = "x1"; scaleVol = 10000; break; case 5: labelRatio = "x1"; scaleVol = 1000000; break; case 6: labelRatio = "x1"; scaleVol = 1000000; break; default: labelRatio = "x1"; scaleVol = 1; break; } double AxisLabelVolume = 0; paint.setColor(GlobalColor.clrLine); tPaint.setTextAlign(Paint.Align.RIGHT); for (int i = 0; i <= rowNum; i++) { if (i == rowNum || i == 0) { canvas.drawLine(klineX, klineY + rowHeight * i, width, klineY + rowHeight * i, paint); } else { Graphics.drawDashline(canvas, klineX, klineY + rowHeight * i, width, klineY + rowHeight * i, paint); } if (i != rowNum && isTrackStatus == false) { AxisLabelVolume = Math.round(highVolume - ratio * i); if (i == 0) { tPaint.setColor(GlobalColor.colorTicklabel); canvas.drawText(Utils.dataFormation(Math.round(AxisLabelVolume / scaleVol), 0), klineX - tips / 4, klineY + rowHeight * i + axisLabelHeight / 2, tPaint); } else { tPaint.setColor(GlobalColor.colorTicklabel); canvas.drawText(Utils.dataFormation(Math.round(AxisLabelVolume / scaleVol), 0), klineX - tips / 4, klineY + rowHeight * i + axisLabelHeight / 2, tPaint); } } } if (isTrackStatus == false) { tPaint.setColor(GlobalColor.colorTicklabel); canvas.drawText(labelRatio, klineX - tips / 4, height - axisLabelHeight, tPaint); } // K if (quoteData != null) { for (int i = begin; i < (begin + count); i++) { if (i == 0) { drawVolumeKLine(canvas, i - begin, quoteData.getJSONArray("K").getJSONArray(i).getDouble(1), quoteData.getJSONArray("K").getJSONArray(i).getDouble(4), quoteData.getJSONArray("K").getJSONArray(i).getDouble(5)); } else { drawVolumeKLine(canvas, i - begin, quoteData.getJSONArray("K").getJSONArray(i).getDouble(1), quoteData.getJSONArray("K").getJSONArray(i).getDouble(4), quoteData.getJSONArray("K").getJSONArray(i).getDouble(5)); } if (i == (begin + count - 1)) { // Util.drawString(g2, labelVolume, "VOLUME:", css); } } drawVolumeMACD(canvas, quoteData, begin, count, shapeWidth, spaceWidth, highPrice, lowPrice, highVolume); } }
From source file:net.droidsolutions.droidcharts.core.plot.CategoryPlot.java
/** * Draws a range crosshair./*from w w w.j a v a2 s . c o m*/ * * @param g2 * the graphics target. * @param dataArea * the data area. * @param orientation * the plot orientation. * @param value * the crosshair value. * @param axis * the axis against which the value is measured. * @param stroke * the stroke used to draw the crosshair line. * @param paint * the paint used to draw the crosshair line. * * @see #drawDomainCrosshair(Graphics2D, Rectangle2D, PlotOrientation, int, * Comparable, Comparable, Stroke, Paint) * * @since 1.0.5 */ protected void drawRangeCrosshair(Canvas g2, Rectangle2D dataArea, PlotOrientation orientation, double value, ValueAxis axis, float stroke, Paint paint) { if (!axis.getRange().contains(value)) { return; } Line2D line = null; if (orientation == PlotOrientation.HORIZONTAL) { double xx = axis.valueToJava2D(value, dataArea, RectangleEdge.BOTTOM); line = new Line2D.Double(xx, dataArea.getMinY(), xx, dataArea.getMaxY()); } else { double yy = axis.valueToJava2D(value, dataArea, RectangleEdge.LEFT); line = new Line2D.Double(dataArea.getMinX(), yy, dataArea.getMaxX(), yy); } paint.setStrokeWidth(stroke); g2.drawLine((float) line.getX1(), (float) line.getY1(), (float) line.getX2(), (float) line.getY2(), paint); }
From source file:com.cssweb.android.view.KlineView.java
private void drawBoll(Canvas canvas, JSONObject quoteData, int begin, int count, double shapeWidth, double spaceWidth, double highPrice, double lowPrice, double highVolume, int actualDataLen) throws JSONException { if (quoteData == null || quoteData.isNull("BOLL")) { isNetworkErrorDisplay();/*from www. j a v a 2 s .c om*/ return; } int startX = klineX; int startY = klineY; //double axisX = 0; double upperx = 0; double uppery = 0; double upperHeight = 0; double upper = 0; double lowerx = 0; double lowery = 0; double lowerHeight = 0; double lower = 0; double midx = 0; double midy = 0; double midHeight = 0; double mid = 0; int len = quoteData.getJSONArray("BOLL").length(); for (int i = begin; i < (begin + count) && i < len; i++) { //klineX = 0; if (quoteData.getJSONArray("BOLL").length() < i - 1) break; upper = quoteData.getJSONArray("BOLL").getJSONArray(i).getDouble(2); lower = quoteData.getJSONArray("BOLL").getJSONArray(i).getDouble(3); mid = quoteData.getJSONArray("BOLL").getJSONArray(i).getDouble(1); if ((i - begin) == 0) { upperx = startX + spaceWidth + shapeWidth / 2; upperHeight = (upper - lowPrice) * scale; uppery = startY + klineHeight - upperHeight; } else { if (quoteData.getJSONArray("BOLL").getJSONArray(i - 1).getDouble(2) == 0) { upperx = upperx + spaceWidth + shapeWidth; upperHeight = (upper - lowPrice) * scale; uppery = startY + klineHeight - upperHeight; } else { double x1 = upperx; double y1 = uppery; upperx = upperx + spaceWidth + shapeWidth; upperHeight = (upper - lowPrice) * scale; uppery = startY + klineHeight - upperHeight; paint.setColor(GlobalColor.colorM10); canvas.drawLine((float) x1, (float) y1, (float) upperx, (float) uppery, paint); } } if ((i - begin) == 0) { lowerx = startX + spaceWidth + shapeWidth / 2; lowerHeight = (lower - lowPrice) * scale; lowery = startY + klineHeight - lowerHeight; } else { if (quoteData.getJSONArray("BOLL").getJSONArray(i - 1).getDouble(3) == 0) { lowerx = lowerx + spaceWidth + shapeWidth; lowerHeight = (lower - lowPrice) * scale; lowery = startY + klineHeight - lowerHeight; } else { double x1 = lowerx; double y1 = lowery; lowerx = lowerx + spaceWidth + shapeWidth; lowerHeight = (lower - lowPrice) * scale; lowery = startY + klineHeight - lowerHeight; //paint.setColor(GlobalColor.colorM20); paint.setARGB(255, 255, 0, 255); canvas.drawLine((float) x1, (float) y1, (float) lowerx, (float) lowery, paint); } } if ((i - begin) == 0) { midx = startX + spaceWidth + shapeWidth / 2; midHeight = (mid - lowPrice) * scale; midy = startY + klineHeight - midHeight; } else { if (quoteData.getJSONArray("BOLL").getJSONArray(i - 1).getDouble(1) == 0) { midx = midx + spaceWidth + shapeWidth; midHeight = (mid - lowPrice) * scale; midy = startY + klineHeight - midHeight; } else { double x1 = midx; double y1 = midy; midx = midx + spaceWidth + shapeWidth; midHeight = (mid - lowPrice) * scale; midy = startY + klineHeight - midHeight; paint.setColor(GlobalColor.colorM5); canvas.drawLine((float) x1, (float) y1, (float) midx, (float) midy, paint); } } } }
From source file:net.droidsolutions.droidcharts.core.plot.CategoryPlot.java
/** * Draws a domain crosshair./* w w w. ja v a 2 s. com*/ * * @param g2 * the graphics target. * @param dataArea * the data area. * @param orientation * the plot orientation. * @param datasetIndex * the dataset index. * @param rowKey * the row key. * @param columnKey * the column key. * @param stroke * the stroke used to draw the crosshair line. * @param paint * the paint used to draw the crosshair line. * * @see #drawRangeCrosshair(Graphics2D, Rectangle2D, PlotOrientation, * double, ValueAxis, Stroke, Paint) * * @since 1.0.11 */ protected void drawDomainCrosshair(Canvas g2, Rectangle2D dataArea, PlotOrientation orientation, int datasetIndex, Comparable rowKey, Comparable columnKey, float stroke, Paint paint) { CategoryDataset dataset = getDataset(datasetIndex); CategoryAxis axis = getDomainAxisForDataset(datasetIndex); CategoryItemRenderer renderer = getRenderer(datasetIndex); Line2D line = null; if (orientation == PlotOrientation.VERTICAL) { double xx = renderer.getItemMiddle(rowKey, columnKey, dataset, axis, dataArea, RectangleEdge.BOTTOM); line = new Line2D.Double(xx, dataArea.getMinY(), xx, dataArea.getMaxY()); } else { double yy = renderer.getItemMiddle(rowKey, columnKey, dataset, axis, dataArea, RectangleEdge.LEFT); line = new Line2D.Double(dataArea.getMinX(), yy, dataArea.getMaxX(), yy); } paint.setStrokeWidth(stroke); g2.drawLine((float) line.getX1(), (float) line.getY1(), (float) line.getX2(), (float) line.getY2(), paint); }
From source file:com.cssweb.android.view.KlineViewSingle.java
public void onDraw(Canvas canvas) { paint = new Paint(Paint.ANTI_ALIAS_FLAG); paint.setStyle(Paint.Style.STROKE); paint.setStrokeWidth(1);//from ww w. ja va 2 s.c om tPaint = new Paint(); tPaint.setStyle(Paint.Style.STROKE); tPaint.setTypeface(Typeface.DEFAULT_BOLD); tPaint.setAntiAlias(true); tPaint.setTextSize(dTextSize); // tips = (int) tPaint.measureText("0"); try { if (actualDataLen == 0) { return; } if (zs) axisLabelWidth = (int) Math.max(tPaint.measureText("99999.99"), tPaint.measureText("11000")); else axisLabelWidth = (int) tPaint.measureText("11000"); klineWidth = width - axisLabelWidth; //?spaceWidth??? visualKLineCount = (int) ((klineWidth - spaceWidth) / (spaceWidth + shapeWidth)); if (isSingleMoved == false && isTrackStatus == false) { if (actualDataLen > visualKLineCount) { actualPos = actualDataLen - visualKLineCount; count = visualKLineCount; } else { actualPos = 0; count = actualDataLen; } } calcMaxMin(actualPos); // axisLabelHeight = Font.getFontHeight(dTextSize); klineX = axisLabelWidth; klineY = axisLabelHeight; klineHeight = (int) ((height - axisLabelHeight) * 0.6); volumeHeight = (int) ((height - axisLabelHeight) * 0.4); axisX = klineX; if (!isTrackStatus) { moveQuote(actualDataLen - 1); } else { if (trackLineV == 0) { trackLineV = (int) (visualPos * (spaceWidth + shapeWidth) - shapeWidth / 2); isTrackNumber = actualPos + visualPos - 1; } if (isTrackNumber < 0) { isTrackNumber = 0; } else if (isTrackNumber > actualDataLen - 1) { isTrackNumber = actualDataLen - 1; } paint.setColor(GlobalColor.clrGrayLine); canvas.drawLine(klineX + trackLineV, axisLabelHeight, klineX + trackLineV, height - axisLabelHeight, paint); moveQuote(isTrackNumber); drawQuoteWin(canvas, quoteData, isTrackNumber); } // tPaint.setTextAlign(Paint.Align.LEFT); tPaint.setColor(GlobalColor.colorM5); canvas.drawText(lblIndicatorName, (float) (klineX + tips), axisLabelHeight - 5, tPaint); float size = tPaint.measureText(lblIndicatorName) + tips * 2; tPaint.setColor(GlobalColor.colorM10); canvas.drawText(lblIndicatorT1, (float) (klineX + size), axisLabelHeight - 5, tPaint); size += tPaint.measureText(lblIndicatorT1) + tips; if (size <= (klineWidth - tPaint.measureText(lblIndicatorT2))) { //tPaint.setColor(GlobalColor.colorM20); tPaint.setARGB(255, 255, 0, 255); canvas.drawText(lblIndicatorT2, (float) (klineX + size), axisLabelHeight - 5, tPaint); } size += tPaint.measureText(lblIndicatorT2) + tips; if (size <= (klineWidth - tPaint.measureText(lblIndicatorT3))) { tPaint.setColor(GlobalColor.colorM60); canvas.drawText(lblIndicatorT3, (float) (klineX + size), axisLabelHeight - 5, tPaint); } //? tPaint.setColor(GlobalColor.colorLabelName); canvas.drawText(lblmainIndicatorT1, (float) (klineX + tips), klineY + klineHeight + axisLabelHeight - 5, tPaint); size = tPaint.measureText(lblmainIndicatorT1) + tips * 2; tPaint.setColor(GlobalColor.colorM5); canvas.drawText(lblmainIndicatorT2, (float) (klineX + size), klineY + klineHeight + axisLabelHeight - 5, tPaint); size += tPaint.measureText(lblmainIndicatorT2) + tips; if (size <= (klineWidth - tPaint.measureText(lblmainIndicatorT3))) { tPaint.setColor(GlobalColor.colorM10); canvas.drawText(lblmainIndicatorT3, (float) (klineX + size), klineY + klineHeight + axisLabelHeight - 5, tPaint); } size += tPaint.measureText(lblmainIndicatorT3) + tips; if (size <= (klineWidth - tPaint.measureText(lblmainIndicatorT4))) { tPaint.setARGB(255, 255, 0, 255); canvas.drawText(lblmainIndicatorT4, (float) (klineX + size), klineY + klineHeight + axisLabelHeight - 5, tPaint); } //??? rowHeight = klineHeight / rowNum; scale = klineHeight / (highPrice - lowPrice); double ratio = (highPrice - lowPrice) / rowNum; paint.setColor(GlobalColor.clrLine); tPaint.setColor(GlobalColor.colorTicklabel); tPaint.setTextAlign(Paint.Align.RIGHT); for (int i = 0; i <= rowNum; i++) { if (i == rowNum || i == 0) { canvas.drawLine(klineX, klineY + rowHeight * i, width, klineY + rowHeight * i, paint); } else { Graphics.drawDashline(canvas, klineX, klineY + rowHeight * i, width, klineY + rowHeight * i, paint); } if (i != rowNum && isTrackStatus == false) { double AxisLabelPrice = highPrice - ratio * i; String AxisLabelPriceText; AxisLabelPriceText = Utils.dataFormation(AxisLabelPrice, stockdigit); canvas.drawText(AxisLabelPriceText, klineX - tips / 4, klineY + rowHeight * i + axisLabelHeight / 2, tPaint); } } // if (quoteData != null) { //axisX = 0; for (int i = actualPos; i < (actualPos + count); i++) { if (i == 0) drawKLine(canvas, i - actualPos, quoteData.getJSONArray("K").getJSONArray(i).getDouble(1), quoteData.getJSONArray("K").getJSONArray(i).getDouble(2), quoteData.getJSONArray("K").getJSONArray(i).getDouble(3), quoteData.getJSONArray("K").getJSONArray(i).getDouble(4), quoteData.getJSONArray("K").getJSONArray(i).getDouble(4)); else drawKLine(canvas, i - actualPos, quoteData.getJSONArray("K").getJSONArray(i).getDouble(1), quoteData.getJSONArray("K").getJSONArray(i).getDouble(2), quoteData.getJSONArray("K").getJSONArray(i).getDouble(3), quoteData.getJSONArray("K").getJSONArray(i).getDouble(4), quoteData.getJSONArray("K").getJSONArray(i - 1).getDouble(4)); } if (mainIndicatorType.toUpperCase().equals("MA")) drawMA(canvas, quoteData, actualPos, count, shapeWidth, spaceWidth, highPrice, lowPrice, highVolume, actualDataLen); else if (mainIndicatorType.toUpperCase().equals("BOLL")) drawBoll(canvas, quoteData, actualPos, count, shapeWidth, spaceWidth, highPrice, lowPrice, highVolume, actualDataLen); rowHeight = (volumeHeight - axisLabelHeight * 2) / rowNum; klineY = klineHeight + axisLabelHeight * 2; volumeHeight = rowNum * rowHeight; if (indicatorType.toUpperCase().equals("VOLUME")) drawVOLUME(canvas, quoteData, actualPos, count, shapeWidth, spaceWidth, highPrice, lowPrice, highVolume, actualDataLen); else if (indicatorType.toUpperCase().equals("MACD")) drawMACD(canvas, quoteData, actualPos, count, shapeWidth, spaceWidth, highPrice, lowPrice, highVolume, actualDataLen); else if (indicatorType.toUpperCase().equals("CCI")) drawCCI(canvas, quoteData, actualPos, count, shapeWidth, spaceWidth, highPrice, lowPrice, highVolume, actualDataLen); else if (indicatorType.toUpperCase().equals("BIAS")) drawBIAS(canvas, quoteData, actualPos, count, shapeWidth, spaceWidth, highPrice, lowPrice, highVolume, actualDataLen); else if (indicatorType.toUpperCase().equals("KDJ")) drawKDJ(canvas, quoteData, actualPos, count, shapeWidth, spaceWidth, highPrice, lowPrice, highVolume, actualDataLen); else if (indicatorType.toUpperCase().equals("RSI")) drawRSI(canvas, quoteData, actualPos, count, shapeWidth, spaceWidth, highPrice, lowPrice, highVolume, actualDataLen); else if (indicatorType.toUpperCase().equals("OBV")) drawOBV(canvas, quoteData, actualPos, count, shapeWidth, spaceWidth, highPrice, lowPrice, highVolume, actualDataLen); else if (indicatorType.toUpperCase().equals("PSY")) drawOther(canvas, quoteData, actualPos, count, shapeWidth, spaceWidth, highPrice, lowPrice, highVolume, actualDataLen, "PSY"); else if (indicatorType.toUpperCase().equals("ROC")) drawROC(canvas, quoteData, actualPos, count, shapeWidth, spaceWidth, highPrice, lowPrice, highVolume, actualDataLen, "ROC"); else if (indicatorType.toUpperCase().equals("VR")) drawOther(canvas, quoteData, actualPos, count, shapeWidth, spaceWidth, highPrice, lowPrice, highVolume, actualDataLen, "VR"); else if (indicatorType.toUpperCase().equals("WR")) drawOther(canvas, quoteData, actualPos, count, shapeWidth, spaceWidth, highPrice, lowPrice, highVolume, actualDataLen, "WR"); drawTimeAix(canvas, quoteData, actualPos, count, shapeWidth, spaceWidth, highPrice, lowPrice, highVolume, actualDataLen); } //????? paint.setColor(GlobalColor.clrLine); canvas.drawLine(klineX, 0, width, 0, paint); canvas.drawLine(klineX, 0, klineX, height - axisLabelHeight, paint); canvas.drawLine(width, 0, width, height - axisLabelHeight, paint); //canvas.drawLine(klineX, height - axisLabelHeight, width, height - axisLabelHeight, paint); } catch (JSONException e) { e.printStackTrace(); } }
From source file:net.droidsolutions.droidcharts.core.plot.XYPlot.java
/** * Utility method for drawing a horizontal line across the data area of the * plot./*w ww . j a v a 2s . com*/ * * @param g2 * the graphics device. * @param dataArea * the data area. * @param value * the coordinate, where to draw the line. * @param stroke * the stroke to use. * @param paint * the paint to use. */ protected void drawHorizontalLine(Canvas g2, Rectangle2D dataArea, double value, Float stroke, Paint paint) { ValueAxis axis = getRangeAxis(); if (getOrientation() == PlotOrientation.HORIZONTAL) { axis = getDomainAxis(); } if (axis.getRange().contains(value)) { double yy = axis.valueToJava2D(value, dataArea, RectangleEdge.LEFT); Line2D line = new Line2D.Double(dataArea.getMinX(), yy, dataArea.getMaxX(), yy); paint.setStyle(Paint.Style.STROKE); paint.setStrokeWidth(stroke); g2.drawLine((float) line.getX1(), (float) line.getY1(), (float) line.getX2(), (float) line.getY2(), paint); } }
From source file:net.droidsolutions.droidcharts.core.plot.XYPlot.java
/** * Utility method for drawing a vertical line on the data area of the plot. * //from www . j av a 2s . com * @param g2 * the graphics device. * @param dataArea * the data area. * @param value * the coordinate, where to draw the line. * @param stroke * the stroke to use. * @param paint * the paint to use. */ protected void drawVerticalLine(Canvas g2, Rectangle2D dataArea, double value, Float stroke, Paint paint) { ValueAxis axis = getDomainAxis(); if (getOrientation() == PlotOrientation.HORIZONTAL) { axis = getRangeAxis(); } if (axis.getRange().contains(value)) { double xx = axis.valueToJava2D(value, dataArea, RectangleEdge.BOTTOM); Line2D line = new Line2D.Double(xx, dataArea.getMinY(), xx, dataArea.getMaxY()); paint.setStyle(Paint.Style.STROKE); paint.setStrokeWidth(stroke); g2.drawLine((float) line.getX1(), (float) line.getY1(), (float) line.getX2(), (float) line.getY2(), paint); } }
From source file:com.cssweb.android.view.KlineViewSingle.java
private void drawMACD(Canvas canvas, JSONObject quoteData, int begin, int count, double shapeWidth, double spaceWidth, double highPrice, double lowPrice, double highVolume, int actualDataLen) throws JSONException { if (quoteData == null || quoteData.isNull("MACD")) { isNetworkErrorDisplay();//from w w w .j av a 2 s. co m return; } double max = 0.001; double min = -0.001; int len = quoteData.getJSONArray("MACD").length(); for (int i = begin; i < (begin + count) && i < len; i++) { double dif = quoteData.getJSONArray("MACD").getJSONArray(i).getDouble(1); double dea = quoteData.getJSONArray("MACD").getJSONArray(i).getDouble(2); double macd = quoteData.getJSONArray("MACD").getJSONArray(i).getDouble(3); if (dif > max) max = dif; if (dea > max) max = dea; if (macd > max) max = macd; if (dif < 0) { if (dif < min) min = dif; } if (dea < 0) { if (dea < min) min = dea; } if (macd < 0) { if (macd < min) min = macd; } } double upHeight = 0; double downHeight = 0; upHeight = volumeHeight * (max / (max + Math.abs(min))); downHeight = volumeHeight * (Math.abs(min) / (max + Math.abs(min))); if (isTrackStatus == false) { String axisLabel1 = ""; if (zs) axisLabel1 = Utils.dataFormation(max, 0); else axisLabel1 = Utils.dataFormation(max, stockdigit); tPaint.setColor(GlobalColor.colorKlabel); canvas.drawText(axisLabel1, klineX - tips / 4, klineY, tPaint); canvas.drawText(Utils.dataFormation(0, stockdigit), klineX - tips / 4, (int) (klineY + upHeight), tPaint); String axisLabel3 = ""; if (zs) axisLabel3 = Utils.dataFormation(min, 0); else axisLabel3 = Utils.dataFormation(min, stockdigit); canvas.drawText(axisLabel3, klineX - tips / 4, (klineY + volumeHeight), tPaint); } paint.setColor(GlobalColor.clrLine); Graphics.drawDashline(canvas, klineX, klineY, width, klineY, paint); Graphics.drawDashline(canvas, klineX, (int) (klineY + upHeight), width, (int) (klineY + upHeight), paint); canvas.drawLine(klineX, height - axisLabelHeight, width, height - axisLabelHeight, paint); if (quoteData != null) { double axisX = klineX; double dif = 0, dea = 0, macd = 0, difx1 = 0, difx2 = 0, deax1 = 0, deax2 = 0; int dify1 = 0, dify2 = 0, deay1 = 0, deay2 = 0; for (int i = begin; i < (begin + count) && i < len; i++) { dif = quoteData.getJSONArray("MACD").getJSONArray(i).getDouble(1); dea = quoteData.getJSONArray("MACD").getJSONArray(i).getDouble(2); macd = quoteData.getJSONArray("MACD").getJSONArray(i).getDouble(3); double difScale = 0; double deaScale = 0; double macdScale = 0; double difHeight = 0; double deaHeight = 0; double macdHeight = 0; if (dif >= 0) { difScale = upHeight / max; } else { difScale = downHeight / Math.abs(min); } if (dea >= 0) { deaScale = upHeight / max; } else { deaScale = downHeight / Math.abs(min); } if (macd >= 0) { macdScale = upHeight / max; } else { macdScale = downHeight / Math.abs(min); } if ((i - begin) == 0) axisX = axisX + spaceWidth; else axisX = axisX + spaceWidth + shapeWidth; if ((i - begin) == 0) { difx2 = difx2 + klineX + spaceWidth + shapeWidth / 2; difHeight = Math.abs(dif) * difScale; if (dif >= 0) { dify2 = (int) (klineY + upHeight - difHeight); } else { dify2 = (int) (klineY + upHeight + difHeight); } } else { difx1 = difx2; dify1 = dify2; difx2 = difx2 + spaceWidth + shapeWidth; difHeight = Math.abs(dif) * difScale; if (dif >= 0) { dify2 = (int) (klineY + upHeight - difHeight); } else { dify2 = (int) (klineY + upHeight + difHeight); } paint.setColor(GlobalColor.colorM5); canvas.drawLine((float) difx1, (float) dify1, (float) difx2, (float) dify2, paint); } if ((i - begin) == 0) { deax2 = deax2 + klineX + spaceWidth + shapeWidth / 2; deaHeight = Math.abs(dea) * deaScale; if (dea >= 0) { deay2 = (int) (klineY + upHeight - deaHeight); } else { deay2 = (int) (klineY + upHeight + deaHeight); } } else { deax1 = deax2; deay1 = deay2; deax2 = deax2 + spaceWidth + shapeWidth; deaHeight = Math.abs(dea) * deaScale; if (dea >= 0) { deay2 = (int) (klineY + upHeight - deaHeight); } else { deay2 = (int) (klineY + upHeight + deaHeight); } paint.setColor(GlobalColor.colorM10); canvas.drawLine((float) deax1, (float) deay1, (float) deax2, (float) deay2, paint); } if (macd >= 0) { macdHeight = Math.abs(macd) * macdScale; paint.setColor(GlobalColor.colorKdown); canvas.drawLine((int) (axisX + shapeWidth / 2), (int) (klineY + upHeight - macdHeight), (int) (axisX + shapeWidth / 2), (int) (klineY + upHeight), paint); } else { macdHeight = Math.abs(macd) * macdScale; paint.setARGB(255, 84, 255, 255); canvas.drawLine((int) (axisX + shapeWidth / 2), (int) (klineY + upHeight), (int) (axisX + shapeWidth / 2), (int) (klineY + upHeight + macdHeight), paint); } if (i == (begin + count - 1)) { // } } // end for } // end if }
From source file:com.cssweb.android.view.KlineView.java
public void onDraw(Canvas canvas) { paint = new Paint(Paint.ANTI_ALIAS_FLAG); paint.setStyle(Paint.Style.STROKE); paint.setStrokeWidth(1);/*from w ww . j a v a 2 s.c o m*/ tPaint = new Paint(); tPaint.setStyle(Paint.Style.STROKE); tPaint.setTypeface(Typeface.DEFAULT_BOLD); tPaint.setAntiAlias(true); tPaint.setTextSize(dTextSize); // tips = (int) tPaint.measureText("0"); try { if (actualDataLen == 0) { return; } if (zs) axisLabelWidth = (int) Math.max(tPaint.measureText("99999.99"), tPaint.measureText("11000")); else axisLabelWidth = (int) tPaint.measureText("11000"); klineWidth = width - axisLabelWidth; //?spaceWidth??? visualKLineCount = (int) ((klineWidth - spaceWidth) / (spaceWidth + shapeWidth)); if (isSingleMoved == false && isTrackStatus == false) { if (actualDataLen > visualKLineCount) { actualPos = actualDataLen - visualKLineCount; count = visualKLineCount; } else { actualPos = 0; count = actualDataLen; } } //Log.i("@@@@@@@@isTrackStatus@@@@@@@@@", isTrackStatus+">>>>>>"+actualDataLen+">>>>>>"+isTrackNumber + ">>>>>>>>>" + actualPos); calcMaxMin(actualPos); // axisLabelHeight = Font.getFontHeight(dTextSize); klineX = axisLabelWidth; klineY = axisLabelHeight; klineHeight = (int) ((height - axisLabelHeight) * 0.6); volumeHeight = (int) ((height - axisLabelHeight) * 0.4); axisX = klineX; if (!isTrackStatus) { moveQuote(actualDataLen - 1); } else { //isTrackNumber = (isTrackNumber>actualDataLen-1)?actualDataLen-1:isTrackNumber; if (trackLineV == 0) { trackLineV = (int) (visualPos * (spaceWidth + shapeWidth) - shapeWidth / 2); isTrackNumber = actualPos + visualPos - 1; } if (isTrackNumber < 0) { isTrackNumber = 0; } else if (isTrackNumber > actualDataLen - 1) { isTrackNumber = actualDataLen - 1; } paint.setColor(GlobalColor.clrGrayLine); canvas.drawLine(klineX + trackLineV, axisLabelHeight, klineX + trackLineV, height - axisLabelHeight, paint); moveQuote(isTrackNumber); drawQuoteWin(canvas, quoteData, isTrackNumber); } // tPaint.setTextAlign(Paint.Align.LEFT); tPaint.setColor(GlobalColor.colorM5); canvas.drawText(lblIndicatorName, (float) (klineX + tips), axisLabelHeight - 5, tPaint); float size = tPaint.measureText(lblIndicatorName) + tips * 2; tPaint.setColor(GlobalColor.colorM10); canvas.drawText(lblIndicatorT1, (float) (klineX + size), axisLabelHeight - 5, tPaint); size += tPaint.measureText(lblIndicatorT1) + tips; if (size <= (klineWidth - tPaint.measureText(lblIndicatorT2))) { //tPaint.setColor(GlobalColor.colorM20); tPaint.setARGB(255, 255, 0, 255); canvas.drawText(lblIndicatorT2, (float) (klineX + size), axisLabelHeight - 5, tPaint); } size += tPaint.measureText(lblIndicatorT2) + tips; if (size <= (klineWidth - tPaint.measureText(lblIndicatorT3))) { tPaint.setColor(GlobalColor.colorM60); canvas.drawText(lblIndicatorT3, (float) (klineX + size), axisLabelHeight - 5, tPaint); } //? tPaint.setColor(GlobalColor.colorLabelName); canvas.drawText(lblmainIndicatorT1, (float) (klineX + tips), klineY + klineHeight + axisLabelHeight - 5, tPaint); size = tPaint.measureText(lblmainIndicatorT1) + tips * 2; tPaint.setColor(GlobalColor.colorM5); canvas.drawText(lblmainIndicatorT2, (float) (klineX + size), klineY + klineHeight + axisLabelHeight - 5, tPaint); size += tPaint.measureText(lblmainIndicatorT2) + tips; if (size <= (klineWidth - tPaint.measureText(lblmainIndicatorT3))) { tPaint.setColor(GlobalColor.colorM10); canvas.drawText(lblmainIndicatorT3, (float) (klineX + size), klineY + klineHeight + axisLabelHeight - 5, tPaint); } size += tPaint.measureText(lblmainIndicatorT3) + tips; if (size <= (klineWidth - tPaint.measureText(lblmainIndicatorT4))) { tPaint.setARGB(255, 255, 0, 255); canvas.drawText(lblmainIndicatorT4, (float) (klineX + size), klineY + klineHeight + axisLabelHeight - 5, tPaint); } //??? rowHeight = klineHeight / rowNum; scale = klineHeight / (highPrice - lowPrice); double ratio = (highPrice - lowPrice) / rowNum; paint.setColor(GlobalColor.clrLine); tPaint.setColor(GlobalColor.colorTicklabel); tPaint.setTextAlign(Paint.Align.RIGHT); for (int i = 0; i <= rowNum; i++) { if (i == rowNum || i == 0) { canvas.drawLine(klineX, klineY + rowHeight * i, width, klineY + rowHeight * i, paint); } else { Graphics.drawDashline(canvas, klineX, klineY + rowHeight * i, width, klineY + rowHeight * i, paint); } if (i != rowNum && isTrackStatus == false) { double AxisLabelPrice = highPrice - ratio * i; String AxisLabelPriceText; AxisLabelPriceText = Utils.dataFormation(AxisLabelPrice, stockdigit); canvas.drawText(AxisLabelPriceText, klineX - tips / 4, klineY + rowHeight * i + axisLabelHeight / 2, tPaint); } } // if (quoteData != null) { //axisX = 0; for (int i = actualPos; i < (actualPos + count); i++) { if (i == 0) drawKLine(canvas, i - actualPos, quoteData.getJSONArray("K").getJSONArray(i).getDouble(1), quoteData.getJSONArray("K").getJSONArray(i).getDouble(2), quoteData.getJSONArray("K").getJSONArray(i).getDouble(3), quoteData.getJSONArray("K").getJSONArray(i).getDouble(4), quoteData.getJSONArray("K").getJSONArray(i).getDouble(4)); else drawKLine(canvas, i - actualPos, quoteData.getJSONArray("K").getJSONArray(i).getDouble(1), quoteData.getJSONArray("K").getJSONArray(i).getDouble(2), quoteData.getJSONArray("K").getJSONArray(i).getDouble(3), quoteData.getJSONArray("K").getJSONArray(i).getDouble(4), quoteData.getJSONArray("K").getJSONArray(i - 1).getDouble(4)); } if (mainIndicatorType.toUpperCase().equals("MA")) drawMA(canvas, quoteData, actualPos, count, shapeWidth, spaceWidth, highPrice, lowPrice, highVolume, actualDataLen); else if (mainIndicatorType.toUpperCase().equals("BOLL")) drawBoll(canvas, quoteData, actualPos, count, shapeWidth, spaceWidth, highPrice, lowPrice, highVolume, actualDataLen); rowHeight = (volumeHeight - axisLabelHeight * 2) / rowNum; klineY = klineHeight + axisLabelHeight * 2; volumeHeight = rowNum * rowHeight; if (indicatorType.toUpperCase().equals("VOLUME")) drawVOLUME(canvas, quoteData, actualPos, count, shapeWidth, spaceWidth, highPrice, lowPrice, highVolume, actualDataLen); else if (indicatorType.toUpperCase().equals("MACD")) drawMACD(canvas, quoteData, actualPos, count, shapeWidth, spaceWidth, highPrice, lowPrice, highVolume, actualDataLen); else if (indicatorType.toUpperCase().equals("CCI")) drawCCI(canvas, quoteData, actualPos, count, shapeWidth, spaceWidth, highPrice, lowPrice, highVolume, actualDataLen); else if (indicatorType.toUpperCase().equals("BIAS")) drawBIAS(canvas, quoteData, actualPos, count, shapeWidth, spaceWidth, highPrice, lowPrice, highVolume, actualDataLen); else if (indicatorType.toUpperCase().equals("KDJ")) drawKDJ(canvas, quoteData, actualPos, count, shapeWidth, spaceWidth, highPrice, lowPrice, highVolume, actualDataLen); else if (indicatorType.toUpperCase().equals("RSI")) drawRSI(canvas, quoteData, actualPos, count, shapeWidth, spaceWidth, highPrice, lowPrice, highVolume, actualDataLen); else if (indicatorType.toUpperCase().equals("OBV")) drawOBV(canvas, quoteData, actualPos, count, shapeWidth, spaceWidth, highPrice, lowPrice, highVolume, actualDataLen); else if (indicatorType.toUpperCase().equals("PSY")) drawOther(canvas, quoteData, actualPos, count, shapeWidth, spaceWidth, highPrice, lowPrice, highVolume, actualDataLen, "PSY"); else if (indicatorType.toUpperCase().equals("ROC")) drawROC(canvas, quoteData, actualPos, count, shapeWidth, spaceWidth, highPrice, lowPrice, highVolume, actualDataLen, "ROC"); else if (indicatorType.toUpperCase().equals("VR")) drawOther(canvas, quoteData, actualPos, count, shapeWidth, spaceWidth, highPrice, lowPrice, highVolume, actualDataLen, "VR"); else if (indicatorType.toUpperCase().equals("WR")) drawOther(canvas, quoteData, actualPos, count, shapeWidth, spaceWidth, highPrice, lowPrice, highVolume, actualDataLen, "WR"); drawTimeAix(canvas, quoteData, actualPos, count, shapeWidth, spaceWidth, highPrice, lowPrice, highVolume, actualDataLen); } //????? paint.setColor(GlobalColor.clrLine); canvas.drawLine(klineX, 0, width, 0, paint); canvas.drawLine(klineX, 0, klineX, height - axisLabelHeight, paint); canvas.drawLine(width, 0, width, height - axisLabelHeight, paint); //canvas.drawLine(klineX, height - axisLabelHeight, width, height - axisLabelHeight, paint); } catch (JSONException e) { e.printStackTrace(); } }
From source file:net.droidsolutions.droidcharts.core.plot.XYPlot.java
/** * Draws a domain crosshair.//from w ww . java 2s .com * * @param g2 * the graphics target. * @param dataArea * the data area. * @param orientation * the plot orientation. * @param value * the crosshair value. * @param axis * the axis against which the value is measured. * @param stroke * the stroke used to draw the crosshair line. * @param paint * the paint used to draw the crosshair line. * * @since 1.0.4 */ protected void drawDomainCrosshair(Canvas g2, Rectangle2D dataArea, PlotOrientation orientation, double value, ValueAxis axis, Float stroke, Paint paint) { if (axis.getRange().contains(value)) { Line2D line = null; if (orientation == PlotOrientation.VERTICAL) { double xx = axis.valueToJava2D(value, dataArea, RectangleEdge.BOTTOM); line = new Line2D.Double(xx, dataArea.getMinY(), xx, dataArea.getMaxY()); } else { double yy = axis.valueToJava2D(value, dataArea, RectangleEdge.LEFT); line = new Line2D.Double(dataArea.getMinX(), yy, dataArea.getMaxX(), yy); } paint.setStyle(Paint.Style.STROKE); paint.setStrokeWidth(stroke); g2.drawLine((float) line.getX1(), (float) line.getY1(), (float) line.getX2(), (float) line.getY2(), paint); } }