MPAndroidChart实现区间内最高点最低点绘制

MPAndroidChart实现区间内最高点最低点绘制_第1张图片
image
image.gif

com.github.mikephil.charting.renderer.CandleStickChartRenderer

  @Override
    public void drawValues(Canvas c) {
        float currPrice = 0.0f;
        List dataSets = mChart.getCandleData().getDataSets();

        for (int i = 0; i < dataSets.size(); i++) {

            ICandleDataSet dataSet = dataSets.get(i);

            if (!dataSet.isDrawValuesEnabled() || dataSet.getEntryCount() == 0) {
                continue;
            }

            // apply the text-styling defined by the DataSet
            applyValueTextStyle(dataSet);

            Transformer trans = mChart.getTransformer(dataSet.getAxisDependency());

            int minx = (int) Math.max(dataSet.getXMin(), 0);
            int maxx = (int) Math.min(dataSet.getXMax(), dataSet.getEntryCount() - 1);

            float[] positions = trans.generateTransformedValuesCandle(
                    dataSet, mAnimator.getPhaseX(), mAnimator.getPhaseY(), minx, maxx);

            //计算最大值和最小值
            float maxValue = 0, minValue = 0;
            int maxIndex = 0, minIndex = 0;
            CandleEntry maxEntry = null;
            boolean firstInit = true;
            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;
                }

                CandleEntry entry = dataSet.getEntryForIndex(j / 2 + minx);

                try {
                    currPrice = dataSet.getEntryForIndex(dataSet.getEntryCount() - 1).getY();
                } catch (Exception e) {

                }

                if (firstInit) {
                    maxValue = entry.getHigh();
                    minValue = entry.getLow();
                    firstInit = false;
                    maxEntry = entry;
                    minIndex = j;
                } else {
                    if (entry.getHigh() > maxValue) {
                        maxValue = entry.getHigh();
                        maxIndex = j;
                        maxEntry = entry;
                    }

                    if (entry.getLow() < minValue) {
                        minValue = entry.getLow();
                        minIndex = j;
                    }

                }
            }
            Chart chart = (Chart) mChart;
            boolean isSuppFiatMoney = chart.isSuppFiatMoney;
            //绘制最大值和最小值
            float x = positions[minIndex];
            if (maxIndex > minIndex) {
                //画最小右边
                String highStringMin = "← " + NumberFormatUtil.coinString(minValue, isSuppFiatMoney);

                drawRightMin(c, dataSet, trans, minValue, x, highStringMin);
                //画最大左边
                String highStringMax = NumberFormatUtil.coinString(maxValue, isSuppFiatMoney) + " →";
                drawLeftMax(c, dataSet, trans, maxIndex, maxEntry, highStringMax);
            } else {
                //画最小左边
                String highStringMin = NumberFormatUtil.coinString(minValue, isSuppFiatMoney) + " →";
                drawLeftMin(c, dataSet, trans, minValue, x, highStringMin);

                //画最大右边
                String highStringMax = "← " + NumberFormatUtil.coinString(maxValue, isSuppFiatMoney);
                drawRightMax(c, dataSet, trans, maxIndex, maxEntry, highStringMax);

            }
            //画其他操作
            //drawDes(c, dataSet, trans, maxIndex, maxEntry, ("← " + "买入操作"));

        }
    }

    private void drawRightMax(Canvas c, ICandleDataSet dataSet, Transformer trans, int maxIndex, CandleEntry maxEntry, String highString) {
        //计算显示位置
        int highStringWidth = Utils.calcTextWidth(mValuePaint, highString);

        float[] tPosition = new float[2];
        tPosition[0] = maxEntry == null ? 0f : maxEntry.getX();
        tPosition[1] = maxEntry == null ? 0f : maxEntry.getHigh();
        trans.pointValuesToPixel(tPosition);

        mValuePaint.setColor(dataSet.getValueTextColor(maxIndex / 2));
        c.drawText(highString, tPosition[0] + highStringWidth / 2, tPosition[1], mValuePaint);
    }

    private void drawLeftMin(Canvas c, ICandleDataSet dataSet, Transformer trans, float minValue, float x, String highString) {
        //计算显示位置
        int highStringWidth = Utils.calcTextWidth(mValuePaint, highString);
        float[] tPosition = new float[2];
        tPosition[1] = minValue;
        trans.pointValuesToPixel(tPosition);
        mValuePaint.setColor(dataSet.getValueTextColor());
        c.drawText(highString, x - highStringWidth / 2, tPosition[1] + highStringWidth / 8, mValuePaint);
    }

    private void drawLeftMax(Canvas c, ICandleDataSet dataSet, Transformer trans, int maxIndex, CandleEntry maxEntry, String highString) {
        int highStringWidth = Utils.calcTextWidth(mValuePaint, highString);

        float[] tPosition = new float[2];
        tPosition[0] = maxEntry == null ? 0f : maxEntry.getX();
        tPosition[1] = maxEntry == null ? 0f : maxEntry.getHigh();
        trans.pointValuesToPixel(tPosition);

        mValuePaint.setColor(dataSet.getValueTextColor(maxIndex / 2));
        c.drawText(highString, tPosition[0] - highStringWidth / 2, tPosition[1], mValuePaint);
    }

    private void drawRightMin(Canvas c, ICandleDataSet dataSet, Transformer trans, float minValue, float x, String highString) {
        int highStringWidth = Utils.calcTextWidth(mValuePaint, highString);

        float[] tPosition = new float[2];
        tPosition[1] = minValue;
        trans.pointValuesToPixel(tPosition);
        mValuePaint.setColor(dataSet.getValueTextColor());
        c.drawText(highString, x + highStringWidth / 2, tPosition[1] + highStringWidth / 8, mValuePaint);
    }
image.gif

你可能感兴趣的:(MPAndroidChart实现区间内最高点最低点绘制)