Java Code Examples for com.github.mikephil.charting.utils.Transformer#pathValueToPixel()

The following examples show how to use com.github.mikephil.charting.utils.Transformer#pathValueToPixel() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: LineChartRenderer.java    From StockChart-MPAndroidChart with MIT License 6 votes vote down vote up
protected void drawCubicFill(Canvas c, ILineDataSet dataSet, Path spline, Transformer trans, XBounds bounds) {

        float fillMin = dataSet.getFillFormatter()
                .getFillLinePosition(dataSet, mChart);

        spline.lineTo(dataSet.getEntryForIndex(bounds.min + bounds.range).getX(), fillMin);
        spline.lineTo(dataSet.getEntryForIndex(bounds.min).getX(), fillMin);
        spline.close();

        trans.pathValueToPixel(spline);

        final Drawable drawable = dataSet.getFillDrawable();
        if (drawable != null) {

            drawFilledPath(c, spline, drawable);
        } else {

            drawFilledPath(c, spline, dataSet.getFillColor(), dataSet.getFillAlpha());
        }
    }
 
Example 2
Source File: LineChartRenderer.java    From iMoney with Apache License 2.0 6 votes vote down vote up
protected void drawLinearFill(Canvas c, LineDataSet dataSet, List<Entry> entries, int minx,
        int maxx,
        Transformer trans) {

    mRenderPaint.setStyle(Paint.Style.FILL);

    mRenderPaint.setColor(dataSet.getFillColor());
    // filled is drawn with less alpha
    mRenderPaint.setAlpha(dataSet.getFillAlpha());

    Path filled = generateFilledPath(
            entries,
            dataSet.getFillFormatter().getFillLinePosition(dataSet, mChart), minx, maxx);

    trans.pathValueToPixel(filled);

    c.drawPath(filled, mRenderPaint);

    // restore alpha
    mRenderPaint.setAlpha(255);
}
 
Example 3
Source File: LineChartRenderer.java    From Stayfit with Apache License 2.0 6 votes vote down vote up
protected void drawLinearFill(Canvas c, ILineDataSet dataSet, int minx,
                              int maxx,
                              Transformer trans) {

    Path filled = generateFilledPath(
            dataSet, minx, maxx);

    trans.pathValueToPixel(filled);

    final Drawable drawable = dataSet.getFillDrawable();
    if (drawable != null) {

        drawFilledPath(c, filled, drawable);
    } else {

        drawFilledPath(c, filled, dataSet.getFillColor(), dataSet.getFillAlpha());
    }
}
 
Example 4
Source File: LineChartRenderer.java    From NetKnight with Apache License 2.0 6 votes vote down vote up
protected void drawLinearFill(Canvas c, ILineDataSet dataSet, int minx,
                              int maxx,
                              Transformer trans) {

    Path filled = generateFilledPath(
            dataSet, minx, maxx);

    trans.pathValueToPixel(filled);

    final Drawable drawable = dataSet.getFillDrawable();
    if (drawable != null) {

        drawFilledPath(c, filled, drawable);
    } else {

        drawFilledPath(c, filled, dataSet.getFillColor(), dataSet.getFillAlpha());
    }
}
 
Example 5
Source File: LineChartRenderer.java    From Stayfit with Apache License 2.0 5 votes vote down vote up
protected void drawCubicFill(Canvas c, ILineDataSet dataSet, Path spline, Transformer trans,
                             int from, int to) {

    if (to - from <= 1)
        return;

    float fillMin = dataSet.getFillFormatter()
            .getFillLinePosition(dataSet, mChart);
    
    // Take the from/to xIndex from the entries themselves,
    // so missing entries won't screw up the filling.
    // What we need to draw is line from points of the xIndexes - not arbitrary entry indexes!

    final Entry toEntry = dataSet.getEntryForIndex(to - 1);
    final Entry fromEntry = dataSet.getEntryForIndex(from);
    final float xTo = toEntry == null ? 0 : toEntry.getXIndex();
    final float xFrom = fromEntry == null ? 0 : fromEntry.getXIndex();

    spline.lineTo(xTo, fillMin);
    spline.lineTo(xFrom, fillMin);
    spline.close();

    trans.pathValueToPixel(spline);

    final Drawable drawable = dataSet.getFillDrawable();
    if (drawable != null) {

        drawFilledPath(c, spline, drawable);
    } else {

        drawFilledPath(c, spline, dataSet.getFillColor(), dataSet.getFillAlpha());
    }
}
 
Example 6
Source File: LineChartRenderer.java    From android-kline with Apache License 2.0 5 votes vote down vote up
/**
 * Draws a filled linear path on the canvas.
 *
 * @param c
 * @param dataSet
 * @param trans
 * @param bounds
 */
protected void drawLinearFill(Canvas c, ILineDataSet dataSet, Transformer trans, XBounds bounds) {

    final Path filled = mGenerateFilledPathBuffer;

    final int startingIndex = bounds.min;
    final int endingIndex = bounds.range + bounds.min;
    final int indexInterval = 128;

    int currentStartIndex = 0;
    int currentEndIndex = indexInterval;
    int iterations = 0;

    // Doing this iteratively in order to avoid OutOfMemory errors that can happen on large bounds sets.
    do {
        currentStartIndex = startingIndex + (iterations * indexInterval);
        currentEndIndex = currentStartIndex + indexInterval;
        currentEndIndex = currentEndIndex > endingIndex ? endingIndex : currentEndIndex;

        if (currentStartIndex <= currentEndIndex) {
            generateFilledPath(dataSet, currentStartIndex, currentEndIndex, filled);

            trans.pathValueToPixel(filled);

            final Drawable drawable = dataSet.getFillDrawable();
            if (drawable != null) {

                drawFilledPath(c, filled, drawable);
            } else {

                drawFilledPath(c, filled, dataSet.getFillColor(), dataSet.getFillAlpha());
            }
        }

        iterations++;

    } while (currentStartIndex <= currentEndIndex);

}
 
Example 7
Source File: LineChartRenderer.java    From Ticket-Analysis with MIT License 5 votes vote down vote up
/**
 * Draws a filled linear path on the canvas.
 *
 * @param c
 * @param dataSet
 * @param trans
 * @param bounds
 */
protected void drawLinearFill(Canvas c, ILineDataSet dataSet, Transformer trans, XBounds bounds) {

    final Path filled = mGenerateFilledPathBuffer;

    final int startingIndex = bounds.min;
    final int endingIndex = bounds.range + bounds.min;
    final int indexInterval = 128;

    int currentStartIndex = 0;
    int currentEndIndex = indexInterval;
    int iterations = 0;

    // Doing this iteratively in order to avoid OutOfMemory errors that can happen on large bounds sets.
    do {
        currentStartIndex = startingIndex + (iterations * indexInterval);
        currentEndIndex = currentStartIndex + indexInterval;
        currentEndIndex = currentEndIndex > endingIndex ? endingIndex : currentEndIndex;

        if (currentStartIndex <= currentEndIndex) {
            generateFilledPath(dataSet, currentStartIndex, currentEndIndex, filled);

            trans.pathValueToPixel(filled);

            final Drawable drawable = dataSet.getFillDrawable();
            if (drawable != null) {

                drawFilledPath(c, filled, drawable);
            } else {

                drawFilledPath(c, filled, dataSet.getFillColor(), dataSet.getFillAlpha());
            }
        }

        iterations++;

    } while (currentStartIndex <= currentEndIndex);

}
 
Example 8
Source File: LineChartRenderer.java    From android-kline with Apache License 2.0 5 votes vote down vote up
/**
 * Draws a filled linear path on the canvas.
 *
 * @param c
 * @param dataSet
 * @param trans
 * @param bounds
 */
protected void drawLinearFill(Canvas c, ILineDataSet dataSet, Transformer trans, XBounds bounds) {

    final Path filled = mGenerateFilledPathBuffer;

    final int startingIndex = bounds.min;
    final int endingIndex = bounds.range + bounds.min;
    final int indexInterval = 128;

    int currentStartIndex = 0;
    int currentEndIndex = indexInterval;
    int iterations = 0;

    // Doing this iteratively in order to avoid OutOfMemory errors that can happen on large bounds sets.
    do {
        currentStartIndex = startingIndex + (iterations * indexInterval);
        currentEndIndex = currentStartIndex + indexInterval;
        currentEndIndex = currentEndIndex > endingIndex ? endingIndex : currentEndIndex;

        if (currentStartIndex <= currentEndIndex) {
            generateFilledPath(dataSet, currentStartIndex, currentEndIndex, filled);

            trans.pathValueToPixel(filled);

            final Drawable drawable = dataSet.getFillDrawable();
            if (drawable != null) {

                drawFilledPath(c, filled, drawable);
            } else {

                drawFilledPath(c, filled, dataSet.getFillColor(), dataSet.getFillAlpha());
            }
        }

        iterations++;

    } while (currentStartIndex <= currentEndIndex);

}
 
Example 9
Source File: BarChartRenderer.java    From NetKnight with Apache License 2.0 4 votes vote down vote up
@Override
public void drawHighlighted(Canvas c, Highlight[] indices) {

    BarData barData = mChart.getBarData();
    int setCount = barData.getDataSetCount();

    for (Highlight high : indices) {

        final int minDataSetIndex = high.getDataSetIndex() == -1
                ? 0
                : high.getDataSetIndex();
        final int maxDataSetIndex = high.getDataSetIndex() == -1
                ? barData.getDataSetCount()
                : (high.getDataSetIndex() + 1);
        if (maxDataSetIndex - minDataSetIndex < 1) continue;

        for (int dataSetIndex = minDataSetIndex;
                dataSetIndex < maxDataSetIndex;
                dataSetIndex++) {

            IBarDataSet set = barData.getDataSetByIndex(dataSetIndex);

            if (set == null || !set.isHighlightEnabled())
                continue;

            float barspaceHalf = set.getBarSpace() / 2f;

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

            mHighlightPaint.setColor(set.getHighLightColor());
            mHighlightPaint.setAlpha(set.getHighLightAlpha());

            int index = high.getXIndex();

            // check outofbounds
            if (index >= 0
                    && index < (mChart.getXChartMax() * mAnimator.getPhaseX()) / setCount) {

                BarEntry e = set.getEntryForXIndex(index);

                if (e == null || e.getXIndex() != index)
                    continue;

                float groupspace = barData.getGroupSpace();
                boolean isStack = high.getStackIndex() < 0 ? false : true;

                // calculate the correct x-position
                float x = index * setCount + dataSetIndex + groupspace / 2f
                        + groupspace * index;

                final float y1;
                final float y2;

                if (isStack) {
                    y1 = high.getRange().from;
                    y2 = high.getRange().to;
                } else {
                    y1 = e.getVal();
                    y2 = 0.f;
                }

                prepareBarHighlight(x, y1, y2, barspaceHalf, trans);

                c.drawRect(mBarRect, mHighlightPaint);

                if (mChart.isDrawHighlightArrowEnabled()) {

                    mHighlightPaint.setAlpha(255);

                    // distance between highlight arrow and bar
                    float offsetY = mAnimator.getPhaseY() * 0.07f;

                    float[] values = new float[9];
                    trans.getPixelToValueMatrix().getValues(values);
                    final float xToYRel = Math.abs(
                            values[Matrix.MSCALE_Y] / values[Matrix.MSCALE_X]);

                    final float arrowWidth = set.getBarSpace() / 2.f;
                    final float arrowHeight = arrowWidth * xToYRel;

                    final float yArrow = (y1 > -y2 ? y1 : y1) * mAnimator.getPhaseY();

                    Path arrow = new Path();
                    arrow.moveTo(x + 0.4f, yArrow + offsetY);
                    arrow.lineTo(x + 0.4f + arrowWidth, yArrow + offsetY - arrowHeight);
                    arrow.lineTo(x + 0.4f + arrowWidth, yArrow + offsetY + arrowHeight);

                    trans.pathValueToPixel(arrow);
                    c.drawPath(arrow, mHighlightPaint);
                }
            }
        }
    }
}
 
Example 10
Source File: BarChartRenderer.java    From iMoney with Apache License 2.0 4 votes vote down vote up
@Override
public void drawHighlighted(Canvas c, Highlight[] indices) {

    int setCount = mChart.getBarData().getDataSetCount();

    for (int i = 0; i < indices.length; i++) {

        Highlight h = indices[i];
        int index = h.getXIndex();

        int dataSetIndex = h.getDataSetIndex();
        BarDataSet set = mChart.getBarData().getDataSetByIndex(dataSetIndex);

        if (set == null || !set.isHighlightEnabled())
            continue;

        float barspaceHalf = set.getBarSpace() / 2f;
        
        Transformer trans = mChart.getTransformer(set.getAxisDependency());

        mHighlightPaint.setColor(set.getHighLightColor());
        mHighlightPaint.setAlpha(set.getHighLightAlpha());

        // check outofbounds
        if (index >= 0
                && index < (mChart.getXChartMax() * mAnimator.getPhaseX()) / setCount) {

            BarEntry e = set.getEntryForXIndex(index);

            if (e == null || e.getXIndex() != index)
                continue;

            float groupspace = mChart.getBarData().getGroupSpace();
            boolean isStack = h.getStackIndex() < 0 ? false : true;

            // calculate the correct x-position
            float x = index * setCount + dataSetIndex + groupspace / 2f
                    + groupspace * index;

            final float y1;
            final float y2;

            if (isStack) {
                y1 = h.getRange().from;
                y2 = h.getRange().to;
            } else {
                y1 = e.getVal();
                y2 = 0.f;
            }

            prepareBarHighlight(x, y1, y2, barspaceHalf, trans);

            c.drawRect(mBarRect, mHighlightPaint);

            if (mChart.isDrawHighlightArrowEnabled()) {

                mHighlightPaint.setAlpha(255);

                // distance between highlight arrow and bar
                float offsetY = mAnimator.getPhaseY() * 0.07f;

                float[] values = new float[9];
                trans.getPixelToValueMatrix().getValues(values);
                final float xToYRel = Math.abs(values[Matrix.MSCALE_Y] / values[Matrix.MSCALE_X]);

                final float arrowWidth = set.getBarSpace() / 2.f;
                final float arrowHeight = arrowWidth * xToYRel;

                final float yArrow = (y1 > -y2 ? y1 : y1) * mAnimator.getPhaseY();

                Path arrow = new Path();
                arrow.moveTo(x + 0.4f, yArrow + offsetY);
                arrow.lineTo(x + 0.4f + arrowWidth, yArrow + offsetY - arrowHeight);
                arrow.lineTo(x + 0.4f + arrowWidth, yArrow + offsetY + arrowHeight);

                trans.pathValueToPixel(arrow);
                c.drawPath(arrow, mHighlightPaint);
            }
        }
    }
}
 
Example 11
Source File: BarChartRenderer.java    From Stayfit with Apache License 2.0 4 votes vote down vote up
@Override
public void drawHighlighted(Canvas c, Highlight[] indices) {

    int setCount = mChart.getBarData().getDataSetCount();

    for (int i = 0; i < indices.length; i++) {

        Highlight h = indices[i];
        int index = h.getXIndex();

        int dataSetIndex = h.getDataSetIndex();
        IBarDataSet set = mChart.getBarData().getDataSetByIndex(dataSetIndex);

        if (set == null || !set.isHighlightEnabled())
            continue;

        float barspaceHalf = set.getBarSpace() / 2f;
        
        Transformer trans = mChart.getTransformer(set.getAxisDependency());

        mHighlightPaint.setColor(set.getHighLightColor());
        mHighlightPaint.setAlpha(set.getHighLightAlpha());

        // check outofbounds
        if (index >= 0
                && index < (mChart.getXChartMax() * mAnimator.getPhaseX()) / setCount) {

            BarEntry e = set.getEntryForXIndex(index);

            if (e == null || e.getXIndex() != index)
                continue;

            float groupspace = mChart.getBarData().getGroupSpace();
            boolean isStack = h.getStackIndex() < 0 ? false : true;

            // calculate the correct x-position
            float x = index * setCount + dataSetIndex + groupspace / 2f
                    + groupspace * index;

            final float y1;
            final float y2;

            if (isStack) {
                y1 = h.getRange().from;
                y2 = h.getRange().to;
            } else {
                y1 = e.getVal();
                y2 = 0.f;
            }

            prepareBarHighlight(x, y1, y2, barspaceHalf, trans);

            c.drawRect(mBarRect, mHighlightPaint);

            if (mChart.isDrawHighlightArrowEnabled()) {

                mHighlightPaint.setAlpha(255);

                // distance between highlight arrow and bar
                float offsetY = mAnimator.getPhaseY() * 0.07f;

                float[] values = new float[9];
                trans.getPixelToValueMatrix().getValues(values);
                final float xToYRel = Math.abs(values[Matrix.MSCALE_Y] / values[Matrix.MSCALE_X]);

                final float arrowWidth = set.getBarSpace() / 2.f;
                final float arrowHeight = arrowWidth * xToYRel;

                final float yArrow = (y1 > -y2 ? y1 : y1) * mAnimator.getPhaseY();

                Path arrow = new Path();
                arrow.moveTo(x + 0.4f, yArrow + offsetY);
                arrow.lineTo(x + 0.4f + arrowWidth, yArrow + offsetY - arrowHeight);
                arrow.lineTo(x + 0.4f + arrowWidth, yArrow + offsetY + arrowHeight);

                trans.pathValueToPixel(arrow);
                c.drawPath(arrow, mHighlightPaint);
            }
        }
    }
}
 
Example 12
Source File: LineChartRenderer.java    From android-kline with Apache License 2.0 4 votes vote down vote up
protected void drawCubicBezier(ILineDataSet dataSet) {

        float phaseX = Math.max(0.f, Math.min(1.f, mAnimator.getPhaseX()));
        float phaseY = mAnimator.getPhaseY();

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

        mXBounds.set(mChart, dataSet);

        float intensity = dataSet.getCubicIntensity();

        cubicPath.reset();

        if (mXBounds.range >= 1) {

            float prevDx = 0f;
            float prevDy = 0f;
            float curDx = 0f;
            float curDy = 0f;

            // Take an extra point from the left, and an extra from the right.
            // That's because we need 4 points for a cubic bezier (cubic=4), otherwise we get lines moving and doing weird stuff on the edges of the chart.
            // So in the starting `prev` and `cur`, go -2, -1
            // And in the `lastIndex`, add +1

            final int firstIndex = mXBounds.min + 1;
            final int lastIndex = mXBounds.min + mXBounds.range;

            Entry prevPrev;
            Entry prev = dataSet.getEntryForIndex(Math.max(firstIndex - 2, 0));
            Entry cur = dataSet.getEntryForIndex(Math.max(firstIndex - 1, 0));
            Entry next = cur;
            int nextIndex = -1;

            if (cur == null) return;

            // let the spline start
            cubicPath.moveTo(cur.getX(), cur.getY() * phaseY);

            for (int j = mXBounds.min + 1; j <= mXBounds.range + mXBounds.min; j++) {

                prevPrev = prev;
                prev = cur;
                cur = nextIndex == j ? next : dataSet.getEntryForIndex(j);

                nextIndex = j + 1 < dataSet.getEntryCount() ? j + 1 : j;
                next = dataSet.getEntryForIndex(nextIndex);

                prevDx = (cur.getX() - prevPrev.getX()) * intensity;
                prevDy = (cur.getY() - prevPrev.getY()) * intensity;
                curDx = (next.getX() - prev.getX()) * intensity;
                curDy = (next.getY() - prev.getY()) * intensity;

                cubicPath.cubicTo(prev.getX() + prevDx, (prev.getY() + prevDy) * phaseY,
                        cur.getX() - curDx,
                        (cur.getY() - curDy) * phaseY, cur.getX(), cur.getY() * phaseY);
            }
        }

        // if filled is enabled, close the path
        if (dataSet.isDrawFilledEnabled()) {

            cubicFillPath.reset();
            cubicFillPath.addPath(cubicPath);

            drawCubicFill(mBitmapCanvas, dataSet, cubicFillPath, trans, mXBounds);
        }

        mRenderPaint.setColor(dataSet.getColor());

        mRenderPaint.setStyle(Paint.Style.STROKE);

        trans.pathValueToPixel(cubicPath);

        mBitmapCanvas.drawPath(cubicPath, mRenderPaint);

        mRenderPaint.setPathEffect(null);
    }
 
Example 13
Source File: LineChartRenderer.java    From android-kline with Apache License 2.0 4 votes vote down vote up
protected void drawCubicBezier(ILineDataSet dataSet) {

        float phaseX = Math.max(0.f, Math.min(1.f, mAnimator.getPhaseX()));
        float phaseY = mAnimator.getPhaseY();

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

        mXBounds.set(mChart, dataSet);

        float intensity = dataSet.getCubicIntensity();

        cubicPath.reset();

        if (mXBounds.range >= 1) {

            float prevDx = 0f;
            float prevDy = 0f;
            float curDx = 0f;
            float curDy = 0f;

            // Take an extra point from the left, and an extra from the right.
            // That's because we need 4 points for a cubic bezier (cubic=4), otherwise we get lines moving and doing weird stuff on the edges of the chart.
            // So in the starting `prev` and `cur`, go -2, -1
            // And in the `lastIndex`, add +1

            final int firstIndex = mXBounds.min + 1;
            final int lastIndex = mXBounds.min + mXBounds.range;

            Entry prevPrev;
            Entry prev = dataSet.getEntryForIndex(Math.max(firstIndex - 2, 0));
            Entry cur = dataSet.getEntryForIndex(Math.max(firstIndex - 1, 0));
            Entry next = cur;
            int nextIndex = -1;

            if (cur == null) return;

            // let the spline start
            cubicPath.moveTo(cur.getX(), cur.getY() * phaseY);

            for (int j = mXBounds.min + 1; j <= mXBounds.range + mXBounds.min; j++) {

                prevPrev = prev;
                prev = cur;
                cur = nextIndex == j ? next : dataSet.getEntryForIndex(j);

                nextIndex = j + 1 < dataSet.getEntryCount() ? j + 1 : j;
                next = dataSet.getEntryForIndex(nextIndex);

                prevDx = (cur.getX() - prevPrev.getX()) * intensity;
                prevDy = (cur.getY() - prevPrev.getY()) * intensity;
                curDx = (next.getX() - prev.getX()) * intensity;
                curDy = (next.getY() - prev.getY()) * intensity;

                cubicPath.cubicTo(prev.getX() + prevDx, (prev.getY() + prevDy) * phaseY,
                        cur.getX() - curDx,
                        (cur.getY() - curDy) * phaseY, cur.getX(), cur.getY() * phaseY);
            }
        }

        // if filled is enabled, close the path
        if (dataSet.isDrawFilledEnabled()) {

            cubicFillPath.reset();
            cubicFillPath.addPath(cubicPath);

            drawCubicFill(mBitmapCanvas, dataSet, cubicFillPath, trans, mXBounds);
        }

        mRenderPaint.setColor(dataSet.getColor());

        mRenderPaint.setStyle(Paint.Style.STROKE);

        trans.pathValueToPixel(cubicPath);

        mBitmapCanvas.drawPath(cubicPath, mRenderPaint);

        mRenderPaint.setPathEffect(null);
    }
 
Example 14
Source File: LineChartRenderer.java    From Ticket-Analysis with MIT License 4 votes vote down vote up
protected void drawCubicBezier(ILineDataSet dataSet) {

        float phaseX = Math.max(0.f, Math.min(1.f, mAnimator.getPhaseX()));
        float phaseY = mAnimator.getPhaseY();

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

        mXBounds.set(mChart, dataSet);

        float intensity = dataSet.getCubicIntensity();

        cubicPath.reset();

        if (mXBounds.range >= 1) {

            float prevDx = 0f;
            float prevDy = 0f;
            float curDx = 0f;
            float curDy = 0f;

            // Take an extra point from the left, and an extra from the right.
            // That's because we need 4 points for a cubic bezier (cubic=4), otherwise we get lines moving and doing weird stuff on the edges of the chart.
            // So in the starting `prev` and `cur`, go -2, -1
            // And in the `lastIndex`, add +1

            final int firstIndex = mXBounds.min + 1;
            final int lastIndex = mXBounds.min + mXBounds.range;

            Entry prevPrev;
            Entry prev = dataSet.getEntryForIndex(Math.max(firstIndex - 2, 0));
            Entry cur = dataSet.getEntryForIndex(Math.max(firstIndex - 1, 0));
            Entry next = cur;
            int nextIndex = -1;

            if (cur == null) return;

            // let the spline start
            cubicPath.moveTo(cur.getX(), cur.getY() * phaseY);

            for (int j = mXBounds.min + 1; j <= mXBounds.range + mXBounds.min; j++) {

                prevPrev = prev;
                prev = cur;
                cur = nextIndex == j ? next : dataSet.getEntryForIndex(j);

                nextIndex = j + 1 < dataSet.getEntryCount() ? j + 1 : j;
                next = dataSet.getEntryForIndex(nextIndex);

                prevDx = (cur.getX() - prevPrev.getX()) * intensity;
                prevDy = (cur.getY() - prevPrev.getY()) * intensity;
                curDx = (next.getX() - prev.getX()) * intensity;
                curDy = (next.getY() - prev.getY()) * intensity;

                cubicPath.cubicTo(prev.getX() + prevDx, (prev.getY() + prevDy) * phaseY,
                        cur.getX() - curDx,
                        (cur.getY() - curDy) * phaseY, cur.getX(), cur.getY() * phaseY);
            }
        }

        // if filled is enabled, close the path
        if (dataSet.isDrawFilledEnabled()) {

            cubicFillPath.reset();
            cubicFillPath.addPath(cubicPath);

            drawCubicFill(mBitmapCanvas, dataSet, cubicFillPath, trans, mXBounds);
        }

        mRenderPaint.setColor(dataSet.getColor());

        mRenderPaint.setStyle(Paint.Style.STROKE);

        trans.pathValueToPixel(cubicPath);

        mBitmapCanvas.drawPath(cubicPath, mRenderPaint);

        mRenderPaint.setPathEffect(null);
    }
 
Example 15
Source File: LineChartRenderer.java    From NetKnight with Apache License 2.0 4 votes vote down vote up
protected void drawCubicBezier(Canvas c, ILineDataSet dataSet) {

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

        int entryCount = dataSet.getEntryCount();

        Entry entryFrom = dataSet.getEntryForXIndex((mMinX < 0) ? 0 : mMinX, DataSet.Rounding.DOWN);
        Entry entryTo = dataSet.getEntryForXIndex(mMaxX, DataSet.Rounding.UP);

        int diff = (entryFrom == entryTo) ? 1 : 0;
        int minx = Math.max(dataSet.getEntryIndex(entryFrom) - diff - 1, 0);
        int maxx = Math.min(Math.max(minx + 2, dataSet.getEntryIndex(entryTo) + 1), entryCount);

        float phaseX = Math.max(0.f, Math.min(1.f, mAnimator.getPhaseX()));
        float phaseY = mAnimator.getPhaseY();

        float intensity = dataSet.getCubicIntensity();

        cubicPath.reset();

        int size = (int) Math.ceil((maxx - minx) * phaseX + minx);

        if (size - minx >= 2) {

            float prevDx = 0f;
            float prevDy = 0f;
            float curDx = 0f;
            float curDy = 0f;

            Entry prevPrev = dataSet.getEntryForIndex(minx);
            Entry prev = prevPrev;
            Entry cur = prev;
            Entry next = dataSet.getEntryForIndex(minx + 1);

            // let the spline start
            cubicPath.moveTo(cur.getXIndex(), cur.getVal() * phaseY);

            for (int j = minx + 1, count = Math.min(size, entryCount); j < count; j++) {

                prevPrev = dataSet.getEntryForIndex(j == 1 ? 0 : j - 2);
                prev = dataSet.getEntryForIndex(j - 1);
                cur = dataSet.getEntryForIndex(j);
                next = entryCount > j + 1 ? dataSet.getEntryForIndex(j + 1) : cur;

                prevDx = (cur.getXIndex() - prevPrev.getXIndex()) * intensity;
                prevDy = (cur.getVal() - prevPrev.getVal()) * intensity;
                curDx = (next.getXIndex() - prev.getXIndex()) * intensity;
                curDy = (next.getVal() - prev.getVal()) * intensity;

                cubicPath.cubicTo(prev.getXIndex() + prevDx, (prev.getVal() + prevDy) * phaseY,
                        cur.getXIndex() - curDx,
                        (cur.getVal() - curDy) * phaseY, cur.getXIndex(), cur.getVal() * phaseY);
            }
        }

        // if filled is enabled, close the path
        if (dataSet.isDrawFilledEnabled()) {

            cubicFillPath.reset();
            cubicFillPath.addPath(cubicPath);
            // create a new path, this is bad for performance
            drawCubicFill(mBitmapCanvas, dataSet, cubicFillPath, trans,
                    minx, size);
        }

        mRenderPaint.setColor(dataSet.getColor());

        mRenderPaint.setStyle(Paint.Style.STROKE);

        trans.pathValueToPixel(cubicPath);

        mBitmapCanvas.drawPath(cubicPath, mRenderPaint);

        mRenderPaint.setPathEffect(null);
    }
 
Example 16
Source File: LineChartRenderer.java    From StockChart-MPAndroidChart with MIT License 4 votes vote down vote up
protected void drawCubicBezier(ILineDataSet dataSet) {

        float phaseY = mAnimator.getPhaseY();

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

        mXBounds.set(mChart, dataSet);

        float intensity = dataSet.getCubicIntensity();

        cubicPath.reset();

        if (mXBounds.range >= 1) {
            float prevDx = 0f;
            float prevDy = 0f;
            float curDx = 0f;
            float curDy = 0f;

            // Take an extra point from the left, and an extra from the right.
            // That's because we need 4 points for a cubic bezier (cubic=4), otherwise we get lines moving and doing weird stuff on the edges of the chart.
            // So in the starting `prev` and `cur`, go -2, -1
            // And in the `lastIndex`, add +1

            final int firstIndex = mXBounds.min + 1;
            final int lastIndex = mXBounds.min + mXBounds.range;

            Entry prevPrev;
            Entry prev = dataSet.getEntryForIndex(Math.max(firstIndex - 2, 0));
            Entry cur = dataSet.getEntryForIndex(Math.max(firstIndex - 1, 0));
            Entry next = cur;
            int nextIndex = -1;

            if (cur == null) {
                return;
            }

            // let the spline start
            cubicPath.moveTo(cur.getX(), cur.getY() * phaseY);

            for (int j = mXBounds.min + 1; j <= mXBounds.range + mXBounds.min; j++) {

                prevPrev = prev;
                prev = cur;
                cur = nextIndex == j ? next : dataSet.getEntryForIndex(j);

                nextIndex = j + 1 < dataSet.getEntryCount() ? j + 1 : j;
                next = dataSet.getEntryForIndex(nextIndex);

                prevDx = (cur.getX() - prevPrev.getX()) * intensity;
                prevDy = (cur.getY() - prevPrev.getY()) * intensity;
                curDx = (next.getX() - prev.getX()) * intensity;
                curDy = (next.getY() - prev.getY()) * intensity;

                cubicPath.cubicTo(prev.getX() + prevDx, (prev.getY() + prevDy) * phaseY,
                        cur.getX() - curDx,
                        (cur.getY() - curDy) * phaseY, cur.getX(), cur.getY() * phaseY);
            }
        }

        // if filled is enabled, close the path
        if (dataSet.isDrawFilledEnabled()) {

            cubicFillPath.reset();
            cubicFillPath.addPath(cubicPath);

            drawCubicFill(mBitmapCanvas, dataSet, cubicFillPath, trans, mXBounds);
        }

        mRenderPaint.setColor(dataSet.getColor());

        mRenderPaint.setStyle(Paint.Style.STROKE);

        trans.pathValueToPixel(cubicPath);

        mBitmapCanvas.drawPath(cubicPath, mRenderPaint);

        mRenderPaint.setPathEffect(null);
    }
 
Example 17
Source File: LineChartRenderer.java    From android-kline with Apache License 2.0 2 votes vote down vote up
protected void drawHorizontalBezier(ILineDataSet dataSet) {

        float phaseY = mAnimator.getPhaseY();

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

        mXBounds.set(mChart, dataSet);

        cubicPath.reset();

        if (mXBounds.range >= 1) {

            Entry prev = dataSet.getEntryForIndex(mXBounds.min);
            Entry cur = prev;

            // let the spline start
            cubicPath.moveTo(cur.getX(), cur.getY() * phaseY);

            for (int j = mXBounds.min + 1; j <= mXBounds.range + mXBounds.min; j++) {

                prev = cur;
                cur = dataSet.getEntryForIndex(j);

                final float cpx = (prev.getX())
                        + (cur.getX() - prev.getX()) / 2.0f;

                cubicPath.cubicTo(
                        cpx, prev.getY() * phaseY,
                        cpx, cur.getY() * phaseY,
                        cur.getX(), cur.getY() * phaseY);
            }
        }

        // if filled is enabled, close the path
        if (dataSet.isDrawFilledEnabled()) {

            cubicFillPath.reset();
            cubicFillPath.addPath(cubicPath);
            // create a new path, this is bad for performance
            drawCubicFill(mBitmapCanvas, dataSet, cubicFillPath, trans, mXBounds);
        }

        mRenderPaint.setColor(dataSet.getColor());

        mRenderPaint.setStyle(Paint.Style.STROKE);

        trans.pathValueToPixel(cubicPath);

        mBitmapCanvas.drawPath(cubicPath, mRenderPaint);

        mRenderPaint.setPathEffect(null);
    }
 
Example 18
Source File: LineChartRenderer.java    From StockChart-MPAndroidChart with MIT License 2 votes vote down vote up
protected void drawHorizontalBezier(ILineDataSet dataSet) {

        float phaseY = mAnimator.getPhaseY();

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

        mXBounds.set(mChart, dataSet);

        cubicPath.reset();

        if (mXBounds.range >= 1) {

            Entry prev = dataSet.getEntryForIndex(mXBounds.min);
            Entry cur = prev;

            // let the spline start
            cubicPath.moveTo(cur.getX(), cur.getY() * phaseY);

            for (int j = mXBounds.min + 1; j <= mXBounds.range + mXBounds.min; j++) {

                prev = cur;
                cur = dataSet.getEntryForIndex(j);

                final float cpx = (prev.getX())
                        + (cur.getX() - prev.getX()) / 2.0f;

                cubicPath.cubicTo(
                        cpx, prev.getY() * phaseY,
                        cpx, cur.getY() * phaseY,
                        cur.getX(), cur.getY() * phaseY);
            }
        }

        // if filled is enabled, close the path
        if (dataSet.isDrawFilledEnabled()) {

            cubicFillPath.reset();
            cubicFillPath.addPath(cubicPath);
            // create a new path, this is bad for performance
            drawCubicFill(mBitmapCanvas, dataSet, cubicFillPath, trans, mXBounds);
        }

        mRenderPaint.setColor(dataSet.getColor());

        mRenderPaint.setStyle(Paint.Style.STROKE);

        trans.pathValueToPixel(cubicPath);

        mBitmapCanvas.drawPath(cubicPath, mRenderPaint);

        mRenderPaint.setPathEffect(null);
    }
 
Example 19
Source File: LineChartRenderer.java    From NetKnight with Apache License 2.0 2 votes vote down vote up
/**
 * Draws a cubic line.
 *
 * @param c
 * @param dataSet
 */
protected void drawHorizontalBezier(Canvas c, ILineDataSet dataSet) {

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

    int entryCount = dataSet.getEntryCount();

    Entry entryFrom = dataSet.getEntryForXIndex((mMinX < 0) ? 0 : mMinX, DataSet.Rounding.DOWN);
    Entry entryTo = dataSet.getEntryForXIndex(mMaxX, DataSet.Rounding.UP);

    int diff = (entryFrom == entryTo) ? 1 : 0;
    int minx = Math.max(dataSet.getEntryIndex(entryFrom) - diff, 0);
    int maxx = Math.min(Math.max(minx + 2, dataSet.getEntryIndex(entryTo) + 1), entryCount);

    float phaseX = Math.max(0.f, Math.min(1.f, mAnimator.getPhaseX()));
    float phaseY = mAnimator.getPhaseY();

    cubicPath.reset();

    int size = (int) Math.ceil((maxx - minx) * phaseX + minx);

    if (size - minx >= 2) {

        Entry prev = dataSet.getEntryForIndex(minx);
        Entry cur = prev;

        // let the spline start
        cubicPath.moveTo(cur.getXIndex(), cur.getVal() * phaseY);

        for (int j = minx + 1, count = Math.min(size, entryCount); j < count; j++) {

            prev = dataSet.getEntryForIndex(j - 1);
            cur = dataSet.getEntryForIndex(j);

            final float cpx = (float)(prev.getXIndex())
                    + (float)(cur.getXIndex() - prev.getXIndex()) / 2.0f;

            cubicPath.cubicTo(
                    cpx, prev.getVal() * phaseY,
                    cpx, cur.getVal() * phaseY,
                    cur.getXIndex(), cur.getVal() * phaseY);
        }
    }

    // if filled is enabled, close the path
    if (dataSet.isDrawFilledEnabled()) {

        cubicFillPath.reset();
        cubicFillPath.addPath(cubicPath);
        // create a new path, this is bad for performance
        drawCubicFill(mBitmapCanvas, dataSet, cubicFillPath, trans,
                minx, size);
    }

    mRenderPaint.setColor(dataSet.getColor());

    mRenderPaint.setStyle(Paint.Style.STROKE);

    trans.pathValueToPixel(cubicPath);

    mBitmapCanvas.drawPath(cubicPath, mRenderPaint);

    mRenderPaint.setPathEffect(null);
}
 
Example 20
Source File: LineChartRenderer.java    From Ticket-Analysis with MIT License 2 votes vote down vote up
protected void drawHorizontalBezier(ILineDataSet dataSet) {

        float phaseY = mAnimator.getPhaseY();

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

        mXBounds.set(mChart, dataSet);

        cubicPath.reset();

        if (mXBounds.range >= 1) {

            Entry prev = dataSet.getEntryForIndex(mXBounds.min);
            Entry cur = prev;

            // let the spline start
            cubicPath.moveTo(cur.getX(), cur.getY() * phaseY);

            for (int j = mXBounds.min + 1; j <= mXBounds.range + mXBounds.min; j++) {

                prev = cur;
                cur = dataSet.getEntryForIndex(j);

                final float cpx = (prev.getX())
                        + (cur.getX() - prev.getX()) / 2.0f;

                cubicPath.cubicTo(
                        cpx, prev.getY() * phaseY,
                        cpx, cur.getY() * phaseY,
                        cur.getX(), cur.getY() * phaseY);
            }
        }

        // if filled is enabled, close the path
        if (dataSet.isDrawFilledEnabled()) {

            cubicFillPath.reset();
            cubicFillPath.addPath(cubicPath);
            // create a new path, this is bad for performance
            drawCubicFill(mBitmapCanvas, dataSet, cubicFillPath, trans, mXBounds);
        }

        mRenderPaint.setColor(dataSet.getColor());

        mRenderPaint.setStyle(Paint.Style.STROKE);

        trans.pathValueToPixel(cubicPath);

        mBitmapCanvas.drawPath(cubicPath, mRenderPaint);

        mRenderPaint.setPathEffect(null);
    }