List of usage examples for org.json JSONObject getJSONArray
public JSONArray getJSONArray(String key) throws JSONException
From source file:at.zone.madeleine.ir.PixlinqSearch.java
private String parsePixlinqResponse(String response) { JSONObject responseObject; String result = null;/*from www . ja v a 2 s.c om*/ if (response != null) { try { responseObject = (JSONObject) new JSONTokener(response).nextValue(); JSONArray matches = responseObject.getJSONArray("matches"); JSONArray errors = responseObject.getJSONArray("errors"); if (matches.length() > 0) { JSONObject match = matches.getJSONObject(0); String title = match.getString("title"); String message = match.getString("message"); String sanatizedMessage = message.split("\\.")[0]; result = sanatizedMessage; } } catch (JSONException e) { e.printStackTrace(); } } else { result = null; } return result; }
From source file:com.google.cast.samples.tictactoe.GameChannel.java
/** * Processes all Text messages received from the receiver device and performs the appropriate * action for the message. Recognizable messages are of the form: * * <ul>//from ww w.ja va 2 s. c om * <li> KEY_JOINED: a player joined the current game * <li> KEY_MOVED: a player made a move * <li> KEY_ENDGAME: the game has ended in one of the END_STATE_* states * <li> KEY_ERROR: a game error has occurred * <li> KEY_BOARD_LAYOUT_RESPONSE: the board has been laid out in some new configuration * </ul> * * <p>No other messages are recognized. */ @Override public void onMessageReceived(CastDevice castDevice, String namespace, String message) { try { Log.d(TAG, "onTextMessageReceived: " + message); JSONObject payload = new JSONObject(message); Log.d(TAG, "payload: " + payload); if (payload.has(KEY_EVENT)) { String event = payload.getString(KEY_EVENT); if (KEY_JOINED.equals(event)) { Log.d(TAG, "JOINED"); try { String player = payload.getString(KEY_PLAYER); String opponentName = payload.getString(KEY_OPPONENT); onGameJoined(player, opponentName); } catch (JSONException e) { e.printStackTrace(); } } else if (KEY_MOVED.equals(event)) { Log.d(TAG, "MOVED"); try { String player = payload.getString(KEY_PLAYER); int row = payload.getInt(KEY_ROW); int column = payload.getInt(KEY_COLUMN); boolean isGameOver = payload.getBoolean(KEY_GAME_OVER); onGameMove(player, row, column, isGameOver); } catch (JSONException e) { e.printStackTrace(); } } else if (KEY_ENDGAME.equals(event)) { Log.d(TAG, "ENDGAME"); try { String endState = payload.getString(KEY_END_STATE); int winningLocation = -1; if (END_STATE_ABANDONED.equals(endState) == false) { winningLocation = payload.getInt(KEY_WINNING_LOCATION); } onGameEnd(endState, winningLocation); } catch (JSONException e) { e.printStackTrace(); } } else if (KEY_ERROR.equals(event)) { Log.d(TAG, "ERROR"); try { String errorMessage = payload.getString(KEY_MESSAGE); onGameError(errorMessage); } catch (JSONException e) { e.printStackTrace(); } } else if (KEY_BOARD_LAYOUT_RESPONSE.equals(event)) { Log.d(TAG, "Board Layout"); int[][] boardLayout = new int[3][3]; try { JSONArray boardJSONArray = payload.getJSONArray(KEY_BOARD); for (int i = 0; i < 3; ++i) { for (int j = 0; j < 3; ++j) { boardLayout[i][j] = boardJSONArray.getInt(i * 3 + j); } } onGameBoardLayout(boardLayout); } catch (JSONException e) { e.printStackTrace(); } } } else { Log.w(TAG, "Unknown payload: " + payload); } } catch (JSONException e) { Log.w(TAG, "Message doesn't contain an expected key.", e); } }
From source file:com.jvoid.quote.controller.JVoidQuoteController.java
public ProductsMaster getJVoidProduct(int productId) { RestTemplate restTemplate = new RestTemplate(); HttpHeaders headers = new HttpHeaders(); headers.set("Accept", MediaType.APPLICATION_JSON_VALUE); JSONObject jsonObj = new JSONObject(); try {/*from w w w. j a va 2s . com*/ jsonObj.put("id", productId); } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println("param jsonObj=>" + jsonObj.toString()); UriComponentsBuilder builder = UriComponentsBuilder .fromHttpUrl(PRODUCT_SERVER_URI + URIConstants.GET_PRODUCT).queryParam("params", jsonObj); HttpEntity<?> entity = new HttpEntity<>(headers); HttpEntity<String> returnString = restTemplate.exchange(builder.build().toUri(), HttpMethod.GET, entity, String.class); System.out.println("returnString=>" + returnString); JSONObject returnJsonObj = null; try { returnJsonObj = new JSONObject(returnString.getBody()); } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); } JSONArray productsArr = null; ProductsMaster productsMaster = null; try { productsArr = returnJsonObj.getJSONArray("products"); } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); } productsMaster = new ProductsMaster(); try { ObjectMapper mapper = new ObjectMapper(); try { productsMaster = mapper.readValue(productsArr.getJSONObject(0).toString(), ProductsMaster.class); } catch (JsonParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (JsonMappingException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); } return productsMaster; }
From source file:com.cssweb.android.view.KlineView.java
public void initData(JSONObject quoteData) throws JSONException { if (!quoteData.isNull("K")) { this.quoteData = quoteData; this.actualDataLen = quoteData.getJSONArray("K").length(); if (quoteData.isNull("joTMP") && actualDataLen > 1) {//temp?? this.actualDataLen = quoteData.getJSONArray("K").length() - 1; } else {/*w w w. j av a2 s . co m*/ makeTodayData(); } } }
From source file:com.cssweb.android.view.KlineView.java
public void drawQuoteWin(Canvas canvas, JSONObject quoteData, int idx) throws JSONException { Paint mPaint = new Paint(); mPaint.setTextAlign(Paint.Align.LEFT); mPaint.setStyle(Paint.Style.STROKE); mPaint.setTypeface(Typeface.DEFAULT_BOLD); mPaint.setAntiAlias(true);/* w w w. j a va 2 s. c om*/ mPaint.setColor(GlobalColor.colorKlinePopub); mPaint.setTextSize(sTextSize); canvas.drawText(":", 0, axisLabelHeight, mPaint); canvas.drawText(":", 0, axisLabelHeight * 3, mPaint); canvas.drawText(":", 0, axisLabelHeight * 5, mPaint); canvas.drawText(":", 0, axisLabelHeight * 7, mPaint); canvas.drawText(":", 0, axisLabelHeight * 9, mPaint); canvas.drawText(":", 0, axisLabelHeight * 11, mPaint); canvas.drawText("??:", 0, axisLabelHeight * 13, mPaint); canvas.drawText("??:", 0, axisLabelHeight * 15, mPaint); mPaint.setTextAlign(Paint.Align.RIGHT); String qt = quoteData.getJSONArray("K").getJSONArray(idx).getString(0); double jrkp = quoteData.getJSONArray("K").getJSONArray(idx).getDouble(1); double zg = quoteData.getJSONArray("K").getJSONArray(idx).getDouble(2); double zd = quoteData.getJSONArray("K").getJSONArray(idx).getDouble(3); double sp = quoteData.getJSONArray("K").getJSONArray(idx).getDouble(4); double preclose; if ("cf".equals(exchange) || "dc".equals(exchange) || "sf".equals(exchange) || "cz".equals(exchange)) { switch (idx) { case 0: preclose = quoteData.getJSONArray("K").getJSONArray(idx).getDouble(7); break; default: preclose = quoteData.getJSONArray("K").getJSONArray(idx - 1).getDouble(7); break; } if (quoteData.getJSONArray("K").length() == 1) { preclose = quoteData.getJSONArray("K").getJSONArray(0).getDouble(7); } } else { switch (idx) { case 0: preclose = quoteData.getJSONArray("K").getJSONArray(idx).getDouble(4); break; default: preclose = quoteData.getJSONArray("K").getJSONArray(idx - 1).getDouble(4); break; } if (quoteData.getJSONArray("K").length() == 1) { preclose = quoteData.getDouble("zrsp");//quoteData.getJSONArray("K").getJSONArray(0).getDouble(4); } } mPaint.setColor(getcolor(jrkp, preclose)); canvas.drawText(Utils.dataFormation(jrkp, stockdigit), klineX, axisLabelHeight * 2, mPaint); mPaint.setColor(getcolor(zg, preclose)); canvas.drawText(Utils.dataFormation(zg, stockdigit), klineX, axisLabelHeight * 4, mPaint); mPaint.setColor(getcolor(zd, preclose)); canvas.drawText(Utils.dataFormation(zd, stockdigit), klineX, axisLabelHeight * 6, mPaint); mPaint.setColor(getcolor(sp, preclose)); canvas.drawText(Utils.dataFormation(sp, stockdigit), klineX, axisLabelHeight * 8, mPaint); double zhangdie = sp - preclose; if (zhangdie > 0) mPaint.setColor(GlobalColor.colorpriceUp); else if (zhangdie < 0) mPaint.setColor(GlobalColor.colorPriceDown); else mPaint.setColor(GlobalColor.colorPriceEqual); canvas.drawText(Utils.dataFormation(zhangdie, stockdigit), klineX, axisLabelHeight * 10, mPaint); // if(quoteData.getString("period").equals("min5") || // quoteData.getString("period").equals("min15") || // quoteData.getString("period").equals("min30") || // quoteData.getString("period").equals("min60")){ // qt = qt.substring(4,6)+'/'+qt.substring(6,8)+' '+qt.substring(8); // }else{ // qt = qt.substring(2,4)+qt.substring(4,6)+qt.substring(6,8); // } qt = qt.substring(2, 4) + qt.substring(4, 6) + qt.substring(6, 8); mPaint.setColor(GlobalColor.colorLabelName); canvas.drawText(qt, klineX, axisLabelHeight * 12, mPaint); mPaint.setColor(GlobalColor.colorStockName); canvas.drawText(Utils.getAmountFormat(quoteData.getJSONArray("K").getJSONArray(idx).getDouble(5), false), klineX, axisLabelHeight * 14, mPaint); mPaint.setColor(GlobalColor.colorStockName); canvas.drawText(Utils.getAmountFormat(quoteData.getJSONArray("K").getJSONArray(idx).getDouble(6), false), klineX, axisLabelHeight * 16, mPaint); }
From source file:com.cssweb.android.view.KlineView.java
private void drawMA(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();//from w w w. j a va 2s . c om return; } int startX = klineX; int startY = klineY; double ma5Height = 0; double ma5x = 0; double ma5y = 0; double ma5 = 0; paint.setColor(GlobalColor.colorM5); for (int i = begin; i < (begin + count); i++) { //klineX = 0; ma5 = quoteData.getJSONArray("MA").getJSONArray(i).getDouble(1); if ((i - begin) == 0) { ma5x = startX + spaceWidth + shapeWidth / 2; ma5Height = (int) ((ma5 - lowPrice) * scale); ma5y = axisLabelHeight + klineHeight - ma5Height; } else { if (quoteData.getJSONArray("MA").getJSONArray(i - 1).getDouble(1) == 0) { ma5x = ma5x + spaceWidth + shapeWidth; ma5Height = (ma5 - lowPrice) * scale; ma5y = startY + klineHeight - ma5Height; } else { canvas.drawLine((float) ma5x, (float) ma5y, (float) (ma5x + spaceWidth + shapeWidth), (float) (axisLabelHeight + klineHeight - (ma5 - lowPrice) * scale), paint); ma5x = ma5x + spaceWidth + shapeWidth; ma5Height = (ma5 - lowPrice) * scale; ma5y = startY + klineHeight - ma5Height; } } } // end for double ma10x = 0; double ma10y = 0; double ma10Height = 0; double ma10 = 0; paint.setColor(GlobalColor.colorM10); for (int i = begin; i < (begin + count); i++) { //klineX = 0; ma10 = quoteData.getJSONArray("MA").getJSONArray(i).getDouble(2); if ((i - begin) == 0) { ma10x = startX + spaceWidth + shapeWidth / 2; ma10Height = (ma10 - lowPrice) * scale; ma10y = startY + klineHeight - ma10Height; } else { if (quoteData.getJSONArray("MA").getJSONArray(i - 1).getDouble(2) == 0) { ma10x = ma10x + spaceWidth + shapeWidth; ma10Height = (ma10 - lowPrice) * scale; ma10y = startY + klineHeight - ma10Height; } else { canvas.drawLine((float) ma10x, (float) ma10y, (float) (ma10x + spaceWidth + shapeWidth), (float) (axisLabelHeight + klineHeight - (ma10 - lowPrice) * scale), paint); ma10x = ma10x + spaceWidth + shapeWidth; ma10Height = (ma10 - lowPrice) * scale; ma10y = startY + klineHeight - ma10Height; } } } // end for double ma20x = 0; double ma20y = 0; double ma20Height = 0; double ma20 = 0; //paint.setColor(GlobalColor.colorM20); paint.setARGB(255, 255, 0, 255); for (int i = begin; i < (begin + count); i++) { //klineX = 0; ma20 = quoteData.getJSONArray("MA").getJSONArray(i).getDouble(3); if ((i - begin) == 0) { ma20x = startX + spaceWidth + shapeWidth / 2; ma20Height = (ma20 - lowPrice) * scale; ma20y = startY + klineHeight - ma20Height; } else { if (quoteData.getJSONArray("MA").getJSONArray(i - 1).getDouble(3) == 0) { ma20x = ma20x + spaceWidth + shapeWidth; ma20Height = (ma20 - lowPrice) * scale; ma20y = startY + klineHeight - ma20Height; } else { canvas.drawLine((float) ma20x, (float) ma20y, (float) (ma20x + spaceWidth + shapeWidth), (float) (axisLabelHeight + klineHeight - (ma20 - lowPrice) * scale), paint); ma20x = ma20x + spaceWidth + shapeWidth; ma20Height = (ma20 - lowPrice) * scale; ma20y = startY + klineHeight - ma20Height; } } } // end for double ma60x = 0; double ma60y = 0; double ma60Height = 0; double ma60 = 0; paint.setColor(GlobalColor.colorM60); for (int i = begin; i < (begin + count); i++) { //klineX = 0; ma60 = quoteData.getJSONArray("MA").getJSONArray(i).getDouble(4); if ((i - begin) == 0) { ma60x = startX + spaceWidth + shapeWidth / 2; ma60Height = (ma60 - lowPrice) * scale; ma60y = startY + klineHeight - ma60Height; } else { if (quoteData.getJSONArray("MA").getJSONArray(i - 1).getDouble(4) == 0) { ma60x = ma60x + spaceWidth + shapeWidth; ma60Height = (ma60 - lowPrice) * scale; ma60y = startY + klineHeight - ma60Height; } else { canvas.drawLine((float) ma60x, (float) ma60y, (float) (ma60x + spaceWidth + shapeWidth), (float) (axisLabelHeight + klineHeight - (ma60 - lowPrice) * scale), paint); ma60x = ma60x + spaceWidth + shapeWidth; ma60Height = (ma60 - lowPrice) * scale; ma60y = startY + klineHeight - ma60Height; } } } // end for }
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 ww w . j a v a2 s. com 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:com.cssweb.android.view.KlineView.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 ww. j a v a2 s .c om 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:com.cssweb.android.view.KlineView.java
private void drawVolumeMACD(Canvas canvas, JSONObject quoteData, int begin, int count, double shapeWidth, double spaceWidth, double highPrice, double lowPrice, double highVolume) throws JSONException { if (quoteData == null || quoteData.isNull("MA") || highVolume == 0) { //isNetworkErrorDisplay(); return;/* w ww . j a va 2 s .co m*/ } double avgPrice, x1 = 0, x2 = 0; int y1 = 0, y2 = 0; paint.setColor(GlobalColor.colorM5); for (int i = begin; i < begin + count; i++) { if (quoteData.getJSONArray("MA").length() < i) break; avgPrice = quoteData.getJSONArray("MA").getJSONArray(i).getDouble(5); y2 = (int) (avgPrice * scale); y2 = klineY + volumeHeight - y2; if (i < 4) { if (x2 == 0) { x2 = klineX + spaceWidth + shapeWidth / 2; x2 += (spaceWidth + shapeWidth) * (4 - i); } continue; } if (i == 4 && begin != 4) { x1 = x2; y1 = y2; continue; } if (i == begin) x2 = klineX + spaceWidth + shapeWidth / 2; else x2 = spaceWidth + shapeWidth + x2; if (i == begin) { x1 = x2; y1 = y2; } else { canvas.drawLine((int) Math.round(x1), y1, (int) Math.round(x2), y2, paint); x1 = x2; y1 = y2; } if (i == (begin + count - 1)) { // Util.drawString(g2, labelMaVol5, "MA5:"+avgPrice, cssMa5); } } paint.setColor(GlobalColor.colorM10); x2 = 0; for (int i = begin; i < begin + count; i++) { if (quoteData.getJSONArray("MA").length() < i) break; avgPrice = quoteData.getJSONArray("MA").getJSONArray(i).getDouble(6); y2 = (int) (avgPrice * scale); y2 = klineY + volumeHeight - y2; if (i < 9) { if (x2 == 0) { x2 = klineX + spaceWidth + shapeWidth / 2; x2 += (spaceWidth + shapeWidth) * (9 - i); } continue; } if (i == 9 && begin != 9) { x1 = x2; y1 = y2; continue; } if (i == begin) x2 = klineX + spaceWidth + shapeWidth / 2; else x2 = spaceWidth + shapeWidth + x2; if (i == begin) { x1 = x2; y1 = y2; } else { canvas.drawLine((int) Math.round(x1), y1, (int) Math.round(x2), y2, paint); x1 = x2; y1 = y2; } if (i == (begin + count - 1)) { // Util.drawString(g2, labelMaVol10, "MA10:"+avgPrice, cssMa10); } } // end for }
From source file:com.cssweb.android.view.KlineView.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 ww w . j a v a2s . c om*/ 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; } } max = max + (max - min) * 0.1; min = min - (max - min) * 0.1; if (max <= 0) { max = -min / 5; } max = Math.max(max, 0); double scale = this.volumeHeight / (max - min); double lbl1value = max - (max - min) / 3; double lbl2value = max - (max - min) * 2 / 3; if (Math.abs(lbl1value) < Math.abs(lbl2value)) { lbl1value = 0; } else { lbl2value = 0; } int startX = klineX; int startY = klineY; int axisY10 = (int) (startY + volumeHeight - (max - min) * scale); paint.setColor(GlobalColor.clrLine); Graphics.drawDashline(canvas, startX, axisY10, width, axisY10, paint); int axisY0 = (int) (startY + volumeHeight - (lbl1value - min) * scale); Graphics.drawDashline(canvas, startX, axisY0, width, axisY0, paint); int axisYM10 = (int) (startY + volumeHeight - (lbl2value - min) * scale); Graphics.drawDashline(canvas, startX, axisYM10, width, axisYM10, paint); if (isTrackStatus == false) { tPaint.setColor(GlobalColor.colorKlabel); canvas.drawText(Utils.dataFormation(max, stockdigit), startX - tips / 4, axisY10 + axisLabelHeight / 2, tPaint); canvas.drawText(Utils.dataFormation(lbl1value, stockdigit), startX - tips / 4, axisY0 + axisLabelHeight / 2, tPaint); canvas.drawText(Utils.dataFormation(lbl2value, stockdigit), startX - tips / 4, axisYM10 + axisLabelHeight / 2, tPaint); } canvas.drawLine(startX, 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); if ((i - begin) == 0) axisX = axisX + spaceWidth; else axisX = axisX + spaceWidth + shapeWidth; if ((i - begin) == 0) { difx2 = difx2 + klineX + spaceWidth + shapeWidth / 2; dify2 = (int) (startY + volumeHeight - (dif - min) * scale); } else { difx1 = difx2; dify1 = dify2; difx2 = difx2 + spaceWidth + shapeWidth; dify2 = (int) (startY + volumeHeight - (dif - min) * scale); 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; deay2 = (int) (startY + volumeHeight - (dea - min) * scale); } else { deax1 = deax2; deay1 = deay2; deax2 = deax2 + spaceWidth + shapeWidth; deay2 = (int) (startY + volumeHeight - (dea - min) * scale); paint.setColor(GlobalColor.colorM10); canvas.drawLine((float) deax1, (float) deay1, (float) deax2, (float) deay2, paint); } if (macd >= 0) { paint.setColor(GlobalColor.colorKdown); canvas.drawLine((int) (axisX + shapeWidth / 2), (int) (startY + volumeHeight - (0 - min) * scale), (int) (axisX + shapeWidth / 2), (int) (startY + volumeHeight - (macd - min) * scale), paint); } else { paint.setARGB(255, 84, 255, 255); canvas.drawLine((int) (axisX + shapeWidth / 2), (int) (startY + volumeHeight - (0 - min) * scale), (int) (axisX + shapeWidth / 2), (int) (startY + volumeHeight - (macd - min) * scale), paint); } if (i == (begin + count - 1)) { // } } // end for } // end if }