@Override public void drawData(Canvas c) { CandleData candleData = mChart.getCandleData(); for (CandleDataSet set : candleData.getDataSets()) { if (set.isVisible()) drawDataSet(c, set); } }
@Override public void initBuffers() { CandleData candleData = mChart.getCandleData(); mShadowBuffers = new CandleShadowBuffer[candleData.getDataSetCount()]; mBodyBuffers = new CandleBodyBuffer[candleData.getDataSetCount()]; for (int i = 0; i < mShadowBuffers.length; i++) { CandleDataSet set = candleData.getDataSetByIndex(i); mShadowBuffers[i] = new CandleShadowBuffer(set.getValueCount() * 4); mBodyBuffers[i] = new CandleBodyBuffer(set.getValueCount() * 4); } }
@Override public void drawHighlighted(Canvas c, Highlight[] indices) { for (int i = 0; i < indices.length; i++) { int xIndex = indices[i].getXIndex(); // get the // x-position CandleDataSet set = mChart.getCandleData().getDataSetByIndex(indices[i].getDataSetIndex()); if (set == null || !set.isHighlightEnabled()) continue; mHighlightPaint.setColor(set.getHighLightColor()); CandleEntry e = set.getEntryForXIndex(xIndex); if (e == null || e.getXIndex() != xIndex) continue; float low = e.getLow() * mAnimator.getPhaseY(); float high = e.getHigh() * mAnimator.getPhaseY(); float min = mChart.getYChartMin(); float max = mChart.getYChartMax(); float[] vertPts = new float[] { xIndex - 0.5f, max, xIndex - 0.5f, min, xIndex + 0.5f, max, xIndex + 0.5f, min }; float[] horPts = new float[] { mChart.getXChartMin(), low, mChart.getXChartMax(), low, mChart.getXChartMin(), high, mChart.getXChartMax(), high }; mChart.getTransformer(set.getAxisDependency()).pointValuesToPixel(vertPts); mChart.getTransformer(set.getAxisDependency()).pointValuesToPixel(horPts); // draw the vertical highlight lines c.drawLines(vertPts, mHighlightPaint); // draw the horizontal highlight lines c.drawLines(horPts, mHighlightPaint); } }
@Override public void drawValues(Canvas c) { // if values are drawn if (mChart.getCandleData().getYValCount() < mChart.getMaxVisibleCount() * mViewPortHandler.getScaleX()) { List<CandleDataSet> dataSets = mChart.getCandleData().getDataSets(); for (int i = 0; i < dataSets.size(); i++) { CandleDataSet dataSet = dataSets.get(i); if (!dataSet.isDrawValuesEnabled()) continue; // apply the text-styling defined by the DataSet applyValueTextStyle(dataSet); Transformer trans = mChart.getTransformer(dataSet.getAxisDependency()); List<CandleEntry> entries = dataSet.getYVals(); Entry entryFrom = dataSet.getEntryForXIndex(mMinX); Entry entryTo = dataSet.getEntryForXIndex(mMaxX); int minx = Math.max(dataSet.getEntryPosition(entryFrom), 0); int maxx = Math.min(dataSet.getEntryPosition(entryTo) + 1, entries.size()); float[] positions = trans.generateTransformedValuesCandle( entries, mAnimator.getPhaseX(), mAnimator.getPhaseY(), minx, maxx); float yOffset = Utils.convertDpToPixel(5f); for (int j = 0; j < positions.length; j += 2) { float x = positions[j]; float y = positions[j + 1]; if (!mViewPortHandler.isInBoundsRight(x)) break; if (!mViewPortHandler.isInBoundsLeft(x) || !mViewPortHandler.isInBoundsY(y)) continue; float val = entries.get(j / 2 + minx).getHigh(); c.drawText( dataSet.getValueFormatter().getFormattedValue(val), x, y - yOffset, mValuePaint); } } } }
private void setData(DataParse mData) { kLineDatas = mData.getKLineDatas(); // axisLeftBar.setAxisMaxValue(mData.getVolmax()); String unit = MyUtils.getVolUnit(mData.getVolmax()); int u = 1; if ("万手".equals(unit)) { u = 4; } else if ("亿手".equals(unit)) { u = 8; } axisLeftBar.setValueFormatter(new VolFormatter((int) Math.pow(10, u))); // axisRightBar.setAxisMaxValue(mData.getVolmax()); Log.e("@@@", mData.getVolmax() + "da"); ArrayList<String> xVals = new ArrayList<>(); ArrayList<BarEntry> barEntries = new ArrayList<>(); ArrayList<CandleEntry> candleEntries = new ArrayList<>(); ArrayList<Entry> line5Entries = new ArrayList<>(); ArrayList<Entry> line10Entries = new ArrayList<>(); ArrayList<Entry> line30Entries = new ArrayList<>(); for (int i = 0, j = 0; i < mData.getKLineDatas().size(); i++, j++) { xVals.add(mData.getKLineDatas().get(i).date + ""); barEntries.add(new BarEntry(mData.getKLineDatas().get(i).vol, i)); candleEntries.add( new CandleEntry( i, mData.getKLineDatas().get(i).high, mData.getKLineDatas().get(i).low, mData.getKLineDatas().get(i).open, mData.getKLineDatas().get(i).close)); if (i >= 4) { sum = 0; line5Entries.add(new Entry(getSum(i - 4, i) / 5, i)); } if (i >= 9) { sum = 0; line10Entries.add(new Entry(getSum(i - 9, i) / 10, i)); } if (i >= 29) { sum = 0; line30Entries.add(new Entry(getSum(i - 29, i) / 30, i)); } } barDataSet = new BarDataSet(barEntries, "成交量"); barDataSet.setBarSpacePercent(50); // bar空隙 barDataSet.setHighlightEnabled(true); barDataSet.setHighLightAlpha(255); barDataSet.setHighLightColor(Color.WHITE); barDataSet.setDrawValues(false); barDataSet.setColor(Color.RED); BarData barData = new BarData(xVals, barDataSet); barChart.setData(barData); final ViewPortHandler viewPortHandlerBar = barChart.getViewPortHandler(); viewPortHandlerBar.setMaximumScaleX(culcMaxscale(xVals.size())); Matrix touchmatrix = viewPortHandlerBar.getMatrixTouch(); final float xscale = 3; touchmatrix.postScale(xscale, 1f); CandleDataSet candleDataSet = new CandleDataSet(candleEntries, "KLine"); candleDataSet.setDrawHorizontalHighlightIndicator(false); candleDataSet.setHighlightEnabled(true); candleDataSet.setHighLightColor(Color.WHITE); candleDataSet.setValueTextSize(10f); candleDataSet.setDrawValues(false); candleDataSet.setColor(Color.RED); candleDataSet.setShadowWidth(1f); candleDataSet.setAxisDependency(YAxis.AxisDependency.LEFT); CandleData candleData = new CandleData(xVals, candleDataSet); ArrayList<ILineDataSet> sets = new ArrayList<>(); sets.add(setMaLine(5, xVals, line5Entries)); sets.add(setMaLine(10, xVals, line10Entries)); sets.add(setMaLine(30, xVals, line30Entries)); CombinedData combinedData = new CombinedData(xVals); LineData lineData = new LineData(xVals, sets); combinedData.setData(candleData); combinedData.setData(lineData); combinedchart.setData(combinedData); combinedchart.moveViewToX(mData.getKLineDatas().size() - 1); final ViewPortHandler viewPortHandlerCombin = combinedchart.getViewPortHandler(); viewPortHandlerCombin.setMaximumScaleX(culcMaxscale(xVals.size())); Matrix matrixCombin = viewPortHandlerCombin.getMatrixTouch(); final float xscaleCombin = 3; matrixCombin.postScale(xscaleCombin, 1f); combinedchart.moveViewToX(mData.getKLineDatas().size() - 1); barChart.moveViewToX(mData.getKLineDatas().size() - 1); setOffset(); /** * ************************************************************************************** * 此处解决方法来源于CombinedChartDemo,k线图y轴显示问题,图表滑动后才能对齐的bug,希望有人给出解决方法 (注:此bug现已修复,感谢和chenguang79一起研究) * ************************************************************************************** */ handler.sendEmptyMessageDelayed(0, 300); }
protected void drawDataSet(Canvas c, CandleDataSet dataSet) { Transformer trans = mChart.getTransformer(dataSet.getAxisDependency()); float phaseX = mAnimator.getPhaseX(); float phaseY = mAnimator.getPhaseY(); int dataSetIndex = mChart.getCandleData().getIndexOfDataSet(dataSet); List<CandleEntry> entries = dataSet.getYVals(); Entry entryFrom = dataSet.getEntryForXIndex(mMinX); Entry entryTo = dataSet.getEntryForXIndex(mMaxX); int minx = Math.max(dataSet.getEntryPosition(entryFrom), 0); int maxx = Math.min(dataSet.getEntryPosition(entryTo) + 1, entries.size()); int range = (maxx - minx) * 4; int to = (int) Math.ceil((maxx - minx) * phaseX + minx); CandleShadowBuffer shadowBuffer = mShadowBuffers[dataSetIndex]; shadowBuffer.setPhases(phaseX, phaseY); shadowBuffer.limitFrom(minx); shadowBuffer.limitTo(maxx); shadowBuffer.feed(entries); trans.pointValuesToPixel(shadowBuffer.buffer); mRenderPaint.setStyle(Paint.Style.STROKE); // If not set, use default functionality for backward compatibility if (dataSet.getShadowColor() == ColorTemplate.COLOR_NONE) { mRenderPaint.setColor(dataSet.getColor()); } else { mRenderPaint.setColor(dataSet.getShadowColor()); } mRenderPaint.setStrokeWidth(dataSet.getShadowWidth()); // draw the shadow c.drawLines(shadowBuffer.buffer, 0, range, mRenderPaint); CandleBodyBuffer bodyBuffer = mBodyBuffers[dataSetIndex]; bodyBuffer.setBodySpace(dataSet.getBodySpace()); bodyBuffer.setPhases(phaseX, phaseY); bodyBuffer.limitFrom(minx); bodyBuffer.limitTo(maxx); bodyBuffer.feed(entries); trans.pointValuesToPixel(bodyBuffer.buffer); // draw the body for (int j = 0; j < range; j += 4) { // get the entry CandleEntry e = entries.get(j / 4 + minx); if (!fitsBounds(e.getXIndex(), mMinX, to)) continue; float leftBody = bodyBuffer.buffer[j]; float open = bodyBuffer.buffer[j + 1]; float rightBody = bodyBuffer.buffer[j + 2]; float close = bodyBuffer.buffer[j + 3]; // draw body differently for increasing and decreasing entry if (open > close) { // decreasing if (dataSet.getDecreasingColor() == ColorTemplate.COLOR_NONE) { mRenderPaint.setColor(dataSet.getColor(j / 4 + minx)); } else { mRenderPaint.setColor(dataSet.getDecreasingColor()); } mRenderPaint.setStyle(dataSet.getDecreasingPaintStyle()); // draw the body c.drawRect(leftBody, close, rightBody, open, mRenderPaint); } else if (open < close) { if (dataSet.getIncreasingColor() == ColorTemplate.COLOR_NONE) { mRenderPaint.setColor(dataSet.getColor(j / 4 + minx)); } else { mRenderPaint.setColor(dataSet.getIncreasingColor()); } mRenderPaint.setStyle(dataSet.getIncreasingPaintStyle()); // draw the body c.drawRect(leftBody, open, rightBody, close, mRenderPaint); } else { // equal values mRenderPaint.setColor(Color.BLACK); mRenderPaint.setStyle(Paint.Style.STROKE); c.drawLine(leftBody, open, rightBody, close, mRenderPaint); } } }