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

The following examples show how to use com.github.mikephil.charting.utils.Transformer#pointValuesToPixel() . 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: ScatterChartRenderer.java    From NetKnight with Apache License 2.0 6 votes vote down vote up
protected void drawDataSet(Canvas c, IScatterDataSet dataSet) {

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

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

        final float shapeSize = Utils.convertDpToPixel(dataSet.getScatterShapeSize());

        ScatterBuffer buffer = mScatterBuffers[mChart.getScatterData().getIndexOfDataSet(
                dataSet)];
        buffer.setPhases(phaseX, phaseY);
        buffer.feed(dataSet);

        trans.pointValuesToPixel(buffer.buffer);

        String shape = dataSet.getScatterShape();
        ShapeRenderer renderer = mChart.getShapeRenderer(shape);

        if (renderer != null) {
            renderer.renderShape(c, dataSet, mViewPortHandler, buffer, mRenderPaint, shapeSize);
        } else {
            throw new RuntimeException("No ShapeRenderer found for provided identifier. Please make sure to add a ShapeRenderer" +
                    " capable of rendering the provided shape.");
        }
    }
 
Example 2
Source File: CoupleChartGestureListener.java    From shinny-futures-android with GNU General Public License v3.0 6 votes vote down vote up
@Override
public void onChartScale(MotionEvent me, float scaleX, float scaleY) {
    float[] mBodyBuffers = new float[4];
    mBodyBuffers[0] = 0f;
    mBodyBuffers[1] = 0f;
    mBodyBuffers[2] = 0.8f;
    mBodyBuffers[3] = 0f;
    Transformer transformer = srcChart.getTransformer(YAxis.AxisDependency.LEFT);
    transformer.pointValuesToPixel(mBodyBuffers);
    float px = mBodyBuffers[2] - mBodyBuffers[0];
    int width = ScreenUtils.px2dp(BaseApplication.getContext(), px);
    Identify identify = new Identify();
    identify.set(AMP_USER_KLINE_WIDTH, width);
    Amplitude.getInstance().identify(identify);

    float[] srcVals = new float[9];
    Matrix srcMatrix = srcChart.getViewPortHandler().getMatrixTouch();
    srcMatrix.getValues(srcVals);
    SPUtils.putAndApply(BaseApplication.getContext(), MarketConstants.SCALE_X, srcVals[0]);
    syncCharts();
}
 
Example 3
Source File: ScatterChartRenderer.java    From android-kline with Apache License 2.0 4 votes vote down vote up
protected void drawDataSet(Canvas c, IScatterDataSet dataSet) {

        ViewPortHandler viewPortHandler = mViewPortHandler;

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

        float phaseY = mAnimator.getPhaseY();

        IShapeRenderer renderer = dataSet.getShapeRenderer();
        if (renderer == null) {
            Log.i("MISSING", "There's no IShapeRenderer specified for ScatterDataSet");
            return;
        }

        int max = (int)(Math.min(
                Math.ceil((float)dataSet.getEntryCount() * mAnimator.getPhaseX()),
                (float)dataSet.getEntryCount()));

        for (int i = 0; i < max; i++) {

            Entry e = dataSet.getEntryForIndex(i);

            mPixelBuffer[0] = e.getX();
            mPixelBuffer[1] = e.getY() * phaseY;

            trans.pointValuesToPixel(mPixelBuffer);

            if (!viewPortHandler.isInBoundsRight(mPixelBuffer[0]))
                break;

            if (!viewPortHandler.isInBoundsLeft(mPixelBuffer[0])
                    || !viewPortHandler.isInBoundsY(mPixelBuffer[1]))
                continue;

            mRenderPaint.setColor(dataSet.getColor(i / 2));
            renderer.renderShape(
                    c, dataSet, mViewPortHandler,
                    mPixelBuffer[0], mPixelBuffer[1],
                    mRenderPaint);
        }
    }
 
Example 4
Source File: BubbleChartRenderer.java    From android-kline with Apache License 2.0 4 votes vote down vote up
@Override
public void drawHighlighted(Canvas c, Highlight[] indices) {

    BubbleData bubbleData = mChart.getBubbleData();

    float phaseY = mAnimator.getPhaseY();

    for (Highlight high : indices) {

        IBubbleDataSet set = bubbleData.getDataSetByIndex(high.getDataSetIndex());

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

        final BubbleEntry entry = set.getEntryForXValue(high.getX(), high.getY());

        if (entry.getY() != high.getY())
            continue;

        if (!isInBoundsX(entry, set))
            continue;

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

        sizeBuffer[0] = 0f;
        sizeBuffer[2] = 1f;

        trans.pointValuesToPixel(sizeBuffer);

        boolean normalizeSize = set.isNormalizeSizeEnabled();

        // calcualte the full width of 1 step on the x-axis
        final float maxBubbleWidth = Math.abs(sizeBuffer[2] - sizeBuffer[0]);
        final float maxBubbleHeight = Math.abs(
                mViewPortHandler.contentBottom() - mViewPortHandler.contentTop());
        final float referenceSize = Math.min(maxBubbleHeight, maxBubbleWidth);

        pointBuffer[0] = entry.getX();
        pointBuffer[1] = (entry.getY()) * phaseY;
        trans.pointValuesToPixel(pointBuffer);

        high.setDraw(pointBuffer[0], pointBuffer[1]);

        float shapeHalf = getShapeSize(entry.getSize(),
                set.getMaxSize(),
                referenceSize,
                normalizeSize) / 2f;

        if (!mViewPortHandler.isInBoundsTop(pointBuffer[1] + shapeHalf)
                || !mViewPortHandler.isInBoundsBottom(pointBuffer[1] - shapeHalf))
            continue;

        if (!mViewPortHandler.isInBoundsLeft(pointBuffer[0] + shapeHalf))
            continue;

        if (!mViewPortHandler.isInBoundsRight(pointBuffer[0] - shapeHalf))
            break;

        final int originalColor = set.getColor((int) entry.getX());

        Color.RGBToHSV(Color.red(originalColor), Color.green(originalColor),
                Color.blue(originalColor), _hsvBuffer);
        _hsvBuffer[2] *= 0.5f;
        final int color = Color.HSVToColor(Color.alpha(originalColor), _hsvBuffer);

        mHighlightPaint.setColor(color);
        mHighlightPaint.setStrokeWidth(set.getHighlightCircleWidth());
        c.drawCircle(pointBuffer[0], pointBuffer[1], shapeHalf, mHighlightPaint);
    }
}
 
Example 5
Source File: HorizontalBarChartRenderer.java    From android-kline with Apache License 2.0 4 votes vote down vote up
@Override
protected void drawDataSet(Canvas c, IBarDataSet dataSet, int index) {

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

    mBarBorderPaint.setColor(dataSet.getBarBorderColor());
    mBarBorderPaint.setStrokeWidth(Utils.convertDpToPixel(dataSet.getBarBorderWidth()));

    final boolean drawBorder = dataSet.getBarBorderWidth() > 0.f;

    float phaseX = mAnimator.getPhaseX();
    float phaseY = mAnimator.getPhaseY();

    // draw the bar shadow before the values
    if (mChart.isDrawBarShadowEnabled()) {
        mShadowPaint.setColor(dataSet.getBarShadowColor());

        BarData barData = mChart.getBarData();

        final float barWidth = barData.getBarWidth();
        final float barWidthHalf = barWidth / 2.0f;
        float x;

        for (int i = 0, count = Math.min((int)(Math.ceil((float)(dataSet.getEntryCount()) * phaseX)), dataSet.getEntryCount());
             i < count;
             i++) {

            BarEntry e = dataSet.getEntryForIndex(i);

            x = e.getX();

            mBarShadowRectBuffer.top = x - barWidthHalf;
            mBarShadowRectBuffer.bottom = x + barWidthHalf;

            trans.rectValueToPixel(mBarShadowRectBuffer);

            if (!mViewPortHandler.isInBoundsTop(mBarShadowRectBuffer.bottom))
                continue;

            if (!mViewPortHandler.isInBoundsBottom(mBarShadowRectBuffer.top))
                break;

            mBarShadowRectBuffer.left = mViewPortHandler.contentLeft();
            mBarShadowRectBuffer.right = mViewPortHandler.contentRight();

            c.drawRect(mBarShadowRectBuffer, mShadowPaint);
        }
    }

    // initialize the buffer
    BarBuffer buffer = mBarBuffers[index];
    buffer.setPhases(phaseX, phaseY);
    buffer.setDataSet(index);
    buffer.setInverted(mChart.isInverted(dataSet.getAxisDependency()));
    buffer.setBarWidth(mChart.getBarData().getBarWidth());

    buffer.feed(dataSet);

    trans.pointValuesToPixel(buffer.buffer);

    final boolean isSingleColor = dataSet.getColors().size() == 1;

    if (isSingleColor) {
        mRenderPaint.setColor(dataSet.getColor());
    }

    for (int j = 0; j < buffer.size(); j += 4) {

        if (!mViewPortHandler.isInBoundsTop(buffer.buffer[j + 3]))
            break;

        if (!mViewPortHandler.isInBoundsBottom(buffer.buffer[j + 1]))
            continue;

        if (!isSingleColor) {
            // Set the color for the currently drawn value. If the index
            // is out of bounds, reuse colors.
            mRenderPaint.setColor(dataSet.getColor(j / 4));
        }

        c.drawRect(buffer.buffer[j], buffer.buffer[j + 1], buffer.buffer[j + 2],
                buffer.buffer[j + 3], mRenderPaint);

        if (drawBorder) {
            c.drawRect(buffer.buffer[j], buffer.buffer[j + 1], buffer.buffer[j + 2],
                    buffer.buffer[j + 3], mBarBorderPaint);
        }
    }
}
 
Example 6
Source File: BarChartRenderer.java    From NetKnight with Apache License 2.0 4 votes vote down vote up
protected void drawDataSet(Canvas c, IBarDataSet dataSet, int index) {

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

        mShadowPaint.setColor(dataSet.getBarShadowColor());
        mBarBorderPaint.setColor(dataSet.getBarBorderColor());
        mBarBorderPaint.setStrokeWidth(Utils.convertDpToPixel(dataSet.getBarBorderWidth()));

        final boolean drawBorder = dataSet.getBarBorderWidth() > 0.f;

        float phaseX = mAnimator.getPhaseX();
        float phaseY = mAnimator.getPhaseY();

        // initialize the buffer
        BarBuffer buffer = mBarBuffers[index];
        buffer.setPhases(phaseX, phaseY);
        buffer.setBarSpace(dataSet.getBarSpace());
        buffer.setDataSet(index);
        buffer.setInverted(mChart.isInverted(dataSet.getAxisDependency()));

        buffer.feed(dataSet);

        trans.pointValuesToPixel(buffer.buffer);

        // draw the bar shadow before the values
        if (mChart.isDrawBarShadowEnabled()) {

            for (int j = 0; j < buffer.size(); j += 4) {

                if (!mViewPortHandler.isInBoundsLeft(buffer.buffer[j + 2]))
                    continue;

                if (!mViewPortHandler.isInBoundsRight(buffer.buffer[j]))
                    break;

                c.drawRect(buffer.buffer[j], mViewPortHandler.contentTop(),
                        buffer.buffer[j + 2],
                        mViewPortHandler.contentBottom(), mShadowPaint);
            }
        }

        // if multiple colors
        if (dataSet.getColors().size() > 1) {

            for (int j = 0; j < buffer.size(); j += 4) {

                if (!mViewPortHandler.isInBoundsLeft(buffer.buffer[j + 2]))
                    continue;

                if (!mViewPortHandler.isInBoundsRight(buffer.buffer[j]))
                    break;

                // Set the color for the currently drawn value. If the index
                // is out of bounds, reuse colors.
                mRenderPaint.setColor(dataSet.getColor(j / 4));
                c.drawRect(buffer.buffer[j], buffer.buffer[j + 1], buffer.buffer[j + 2],
                        buffer.buffer[j + 3], mRenderPaint);

                if (drawBorder) {
                    c.drawRect(buffer.buffer[j], buffer.buffer[j + 1], buffer.buffer[j + 2],
                            buffer.buffer[j + 3], mBarBorderPaint);
                }
            }
        } else {

            mRenderPaint.setColor(dataSet.getColor());

            for (int j = 0; j < buffer.size(); j += 4) {

                if (!mViewPortHandler.isInBoundsLeft(buffer.buffer[j + 2]))
                    continue;

                if (!mViewPortHandler.isInBoundsRight(buffer.buffer[j]))
                    break;

                c.drawRect(buffer.buffer[j], buffer.buffer[j + 1], buffer.buffer[j + 2],
                        buffer.buffer[j + 3], mRenderPaint);

                if (drawBorder) {
                    c.drawRect(buffer.buffer[j], buffer.buffer[j + 1], buffer.buffer[j + 2],
                            buffer.buffer[j + 3], mBarBorderPaint);
                }
            }
        }
    }
 
Example 7
Source File: BarChartRenderer.java    From android-kline with Apache License 2.0 4 votes vote down vote up
protected void drawDataSet(Canvas c, IBarDataSet dataSet, int index) {

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

        mBarBorderPaint.setColor(dataSet.getBarBorderColor());
        mBarBorderPaint.setStrokeWidth(Utils.convertDpToPixel(dataSet.getBarBorderWidth()));

        final boolean drawBorder = dataSet.getBarBorderWidth() > 0.f;

        float phaseX = mAnimator.getPhaseX();
        float phaseY = mAnimator.getPhaseY();

        // draw the bar shadow before the values
        if (mChart.isDrawBarShadowEnabled()) {
            mShadowPaint.setColor(dataSet.getBarShadowColor());

            BarData barData = mChart.getBarData();

            final float barWidth = barData.getBarWidth();
            final float barWidthHalf = barWidth / 2.0f;
            float x;

            for (int i = 0, count = Math.min((int)(Math.ceil((float)(dataSet.getEntryCount()) * phaseX)), dataSet.getEntryCount());
                i < count;
                i++) {

                BarEntry e = dataSet.getEntryForIndex(i);

                x = e.getX();

                mBarShadowRectBuffer.left = x - barWidthHalf;
                mBarShadowRectBuffer.right = x + barWidthHalf;

                trans.rectValueToPixel(mBarShadowRectBuffer);

                if (!mViewPortHandler.isInBoundsLeft(mBarShadowRectBuffer.right))
                    continue;

                if (!mViewPortHandler.isInBoundsRight(mBarShadowRectBuffer.left))
                    break;

                mBarShadowRectBuffer.top = mViewPortHandler.contentTop();
                mBarShadowRectBuffer.bottom = mViewPortHandler.contentBottom();

                c.drawRect(mBarShadowRectBuffer, mShadowPaint);
            }
        }

        // initialize the buffer
        BarBuffer buffer = mBarBuffers[index];
        buffer.setPhases(phaseX, phaseY);
        buffer.setDataSet(index);
        buffer.setInverted(mChart.isInverted(dataSet.getAxisDependency()));
        buffer.setBarWidth(mChart.getBarData().getBarWidth());

        buffer.feed(dataSet);

        trans.pointValuesToPixel(buffer.buffer);

        final boolean isSingleColor = dataSet.getColors().size() == 1;

        if (isSingleColor) {
            mRenderPaint.setColor(dataSet.getColor());
        }

        for (int j = 0; j < buffer.size(); j += 4) {

            if (!mViewPortHandler.isInBoundsLeft(buffer.buffer[j + 2]))
                continue;

            if (!mViewPortHandler.isInBoundsRight(buffer.buffer[j]))
                break;

            if (!isSingleColor) {
                // Set the color for the currently drawn value. If the index
                // is out of bounds, reuse colors.
                mRenderPaint.setColor(dataSet.getColor(j / 4));
            }

            c.drawRect(buffer.buffer[j], buffer.buffer[j + 1], buffer.buffer[j + 2],
                    buffer.buffer[j + 3], mRenderPaint);

            if (drawBorder) {
                c.drawRect(buffer.buffer[j], buffer.buffer[j + 1], buffer.buffer[j + 2],
                        buffer.buffer[j + 3], mBarBorderPaint);
            }
        }
    }
 
Example 8
Source File: HorizontalBarChartRenderer.java    From Stayfit with Apache License 2.0 4 votes vote down vote up
@Override
protected void drawDataSet(Canvas c, IBarDataSet dataSet, int index) {

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

    mShadowPaint.setColor(dataSet.getBarShadowColor());

    float phaseX = mAnimator.getPhaseX();
    float phaseY = mAnimator.getPhaseY();

    // initialize the buffer
    BarBuffer buffer = mBarBuffers[index];
    buffer.setPhases(phaseX, phaseY);
    buffer.setBarSpace(dataSet.getBarSpace());
    buffer.setDataSet(index);
    buffer.setInverted(mChart.isInverted(dataSet.getAxisDependency()));

    buffer.feed(dataSet);

    trans.pointValuesToPixel(buffer.buffer);

    for (int j = 0; j < buffer.size(); j += 4) {

        if (!mViewPortHandler.isInBoundsTop(buffer.buffer[j + 3]))
            break;

        if (!mViewPortHandler.isInBoundsBottom(buffer.buffer[j + 1]))
            continue;

        if (mChart.isDrawBarShadowEnabled()) {
            c.drawRect(mViewPortHandler.contentLeft(), buffer.buffer[j + 1],
                    mViewPortHandler.contentRight(),
                    buffer.buffer[j + 3], mShadowPaint);
        }

        // Set the color for the currently drawn value. If the index
        // is
        // out of bounds, reuse colors.
        mRenderPaint.setColor(dataSet.getColor(j / 4));
        c.drawRect(buffer.buffer[j], buffer.buffer[j + 1], buffer.buffer[j + 2],
                buffer.buffer[j + 3], mRenderPaint);
    }
}
 
Example 9
Source File: LineChartRenderer.java    From iMoney with Apache License 2.0 4 votes vote down vote up
/**
 * Draws a normal line.
 * 
 * @param c
 * @param dataSet
 * @param entries
 */
protected void drawLinear(Canvas c, LineDataSet dataSet, List<Entry> entries) {

    int dataSetIndex = mChart.getLineData().getIndexOfDataSet(dataSet);

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

    float phaseX = mAnimator.getPhaseX();
    float phaseY = mAnimator.getPhaseY();

    mRenderPaint.setStyle(Paint.Style.STROKE);

    Canvas canvas = null;

    // if the data-set is dashed, draw on bitmap-canvas
    if (dataSet.isDashedLineEnabled()) {
        canvas = mBitmapCanvas;
    } else {
        canvas = c;
    }

    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 - 4;

    LineBuffer buffer = mLineBuffers[dataSetIndex];
    buffer.setPhases(phaseX, phaseY);
    buffer.limitFrom(minx);
    buffer.limitTo(maxx);
    buffer.feed(entries);

    trans.pointValuesToPixel(buffer.buffer);

    // more than 1 color
    if (dataSet.getColors().size() > 1) {

        for (int j = 0; j < range; j += 4) {

            if (!mViewPortHandler.isInBoundsRight(buffer.buffer[j]))
                break;

            // make sure the lines don't do shitty things outside
            // bounds
            if (!mViewPortHandler.isInBoundsLeft(buffer.buffer[j + 2])
                    || (!mViewPortHandler.isInBoundsTop(buffer.buffer[j + 1]) && !mViewPortHandler
                            .isInBoundsBottom(buffer.buffer[j + 3]))
                    || (!mViewPortHandler.isInBoundsTop(buffer.buffer[j + 1]) && !mViewPortHandler
                            .isInBoundsBottom(buffer.buffer[j + 3])))
                continue;

            // get the color that is set for this line-segment
            mRenderPaint.setColor(dataSet.getColor(j / 4 + minx));

            canvas.drawLine(buffer.buffer[j], buffer.buffer[j + 1],
                    buffer.buffer[j + 2], buffer.buffer[j + 3], mRenderPaint);
        }

    } else { // only one color per dataset

        mRenderPaint.setColor(dataSet.getColor());

        // c.drawLines(buffer.buffer, mRenderPaint);
        canvas.drawLines(buffer.buffer, 0, range,
                mRenderPaint);
    }

    mRenderPaint.setPathEffect(null);

    // if drawing filled is enabled
    if (dataSet.isDrawFilledEnabled() && entries.size() > 0) {
        drawLinearFill(c, dataSet, entries, minx, maxx, trans);
    }
}
 
Example 10
Source File: LineChartRenderer.java    From NetKnight with Apache License 2.0 4 votes vote down vote up
protected void drawCircles(Canvas c) {

        mRenderPaint.setStyle(Paint.Style.FILL);

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

        float[] circlesBuffer = new float[2];

        List<ILineDataSet> dataSets = mChart.getLineData().getDataSets();

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

            ILineDataSet dataSet = dataSets.get(i);

            if (!dataSet.isVisible() || !dataSet.isDrawCirclesEnabled() ||
                    dataSet.getEntryCount() == 0)
                continue;

            mCirclePaintInner.setColor(dataSet.getCircleHoleColor());

            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;
            if (dataSet.getMode() == LineDataSet.Mode.CUBIC_BEZIER)
                diff += 1;

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

            float circleRadius = dataSet.getCircleRadius();
            float circleHoleRadius = dataSet.getCircleHoleRadius();
            boolean drawCircleHole = dataSet.isDrawCircleHoleEnabled() &&
                    circleHoleRadius < circleRadius &&
                    circleHoleRadius > 0.f;
            boolean drawTransparentCircleHole = drawCircleHole &&
                    dataSet.getCircleHoleColor() == ColorTemplate.COLOR_NONE;

            for (int j = minx,
                 count = (int) Math.ceil((maxx - minx) * phaseX + minx);
                 j < count;
                 j ++) {

                Entry e = dataSet.getEntryForIndex(j);

                if (e == null) break;

                circlesBuffer[0] = e.getXIndex();
                circlesBuffer[1] = e.getVal() * phaseY;

                trans.pointValuesToPixel(circlesBuffer);

                if (!mViewPortHandler.isInBoundsRight(circlesBuffer[0]))
                    break;

                // make sure the circles don't do shitty things outside
                // bounds
                if (!mViewPortHandler.isInBoundsLeft(circlesBuffer[0]) ||
                        !mViewPortHandler.isInBoundsY(circlesBuffer[1]))
                    continue;

                mRenderPaint.setColor(dataSet.getCircleColor(j));

                if (drawTransparentCircleHole) {

                    // Begin path for circle with hole
                    mCirclePathBuffer.reset();

                    mCirclePathBuffer.addCircle(circlesBuffer[0], circlesBuffer[1],
                            circleRadius,
                            Path.Direction.CW);

                    // Cut hole in path
                    mCirclePathBuffer.addCircle(circlesBuffer[0], circlesBuffer[1],
                            circleHoleRadius,
                            Path.Direction.CCW);

                    // Fill in-between
                    c.drawPath(mCirclePathBuffer, mRenderPaint);

                } else {

                    c.drawCircle(circlesBuffer[0], circlesBuffer[1],
                            circleRadius,
                            mRenderPaint);

                    if (drawCircleHole) {
                        c.drawCircle(circlesBuffer[0], circlesBuffer[1],
                                circleHoleRadius,
                                mCirclePaintInner);
                    }
                }
            }
        }
    }
 
Example 11
Source File: BubbleChartRenderer.java    From iMoney with Apache License 2.0 4 votes vote down vote up
@Override
public void drawHighlighted(Canvas c, Highlight[] indices) {

    BubbleData bubbleData = mChart.getBubbleData();

    float phaseX = mAnimator.getPhaseX();
    float phaseY = mAnimator.getPhaseY();

    for (Highlight indice : indices) {

        BubbleDataSet dataSet = bubbleData.getDataSetByIndex(indice.getDataSetIndex());

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

        Entry entryFrom = dataSet.getEntryForXIndex(mMinX);
        Entry entryTo = dataSet.getEntryForXIndex(mMaxX);

        int minx = dataSet.getEntryPosition(entryFrom);
        int maxx = Math.min(dataSet.getEntryPosition(entryTo) + 1, dataSet.getEntryCount());

        final BubbleEntry entry = (BubbleEntry) bubbleData.getEntryForHighlight(indice);
        if (entry == null || entry.getXIndex() != indice.getXIndex())
            continue;

        Transformer trans = mChart.getTransformer(dataSet.getAxisDependency());
        
        sizeBuffer[0] = 0f;
        sizeBuffer[2] = 1f;

        trans.pointValuesToPixel(sizeBuffer);
        
        // calcualte the full width of 1 step on the x-axis
        final float maxBubbleWidth = Math.abs(sizeBuffer[2] - sizeBuffer[0]);
        final float maxBubbleHeight = Math.abs(mViewPortHandler.contentBottom() - mViewPortHandler.contentTop());
        final float referenceSize = Math.min(maxBubbleHeight, maxBubbleWidth);

        pointBuffer[0] = (float) (entry.getXIndex() - minx) * phaseX + (float) minx;
        pointBuffer[1] = (float) (entry.getVal()) * phaseY;
        trans.pointValuesToPixel(pointBuffer);

        float shapeHalf = getShapeSize(entry.getSize(), dataSet.getMaxSize(), referenceSize) / 2f;

        if (!mViewPortHandler.isInBoundsTop(pointBuffer[1] + shapeHalf)
                || !mViewPortHandler.isInBoundsBottom(pointBuffer[1] - shapeHalf))
            continue;

        if (!mViewPortHandler.isInBoundsLeft(pointBuffer[0] + shapeHalf))
            continue;

        if (!mViewPortHandler.isInBoundsRight(pointBuffer[0] - shapeHalf))
            break;

        if (indice.getXIndex() < minx || indice.getXIndex() >= maxx)
            continue;

        final int originalColor = dataSet.getColor(entry.getXIndex());

        Color.RGBToHSV(Color.red(originalColor), Color.green(originalColor),
                Color.blue(originalColor), _hsvBuffer);
        _hsvBuffer[2] *= 0.5f;
        final int color = Color.HSVToColor(Color.alpha(originalColor), _hsvBuffer);

        mHighlightPaint.setColor(color);
        mHighlightPaint.setStrokeWidth(dataSet.getHighlightCircleWidth());
        c.drawCircle(pointBuffer[0], pointBuffer[1], shapeHalf, mHighlightPaint);
    }
}
 
Example 12
Source File: BubbleChartRenderer.java    From Stayfit with Apache License 2.0 4 votes vote down vote up
@Override
public void drawHighlighted(Canvas c, Highlight[] indices) {

    BubbleData bubbleData = mChart.getBubbleData();

    float phaseX = mAnimator.getPhaseX();
    float phaseY = mAnimator.getPhaseY();

    for (Highlight indice : indices) {

        IBubbleDataSet dataSet = bubbleData.getDataSetByIndex(indice.getDataSetIndex());

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

        BubbleEntry entryFrom = dataSet.getEntryForXIndex(mMinX);
        BubbleEntry entryTo = dataSet.getEntryForXIndex(mMaxX);

        int minx = dataSet.getEntryIndex(entryFrom);
        int maxx = Math.min(dataSet.getEntryIndex(entryTo) + 1, dataSet.getEntryCount());

        final BubbleEntry entry = (BubbleEntry) bubbleData.getEntryForHighlight(indice);
        if (entry == null || entry.getXIndex() != indice.getXIndex())
            continue;

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

        sizeBuffer[0] = 0f;
        sizeBuffer[2] = 1f;

        trans.pointValuesToPixel(sizeBuffer);

        // calcualte the full width of 1 step on the x-axis
        final float maxBubbleWidth = Math.abs(sizeBuffer[2] - sizeBuffer[0]);
        final float maxBubbleHeight = Math.abs(mViewPortHandler.contentBottom() - mViewPortHandler.contentTop());
        final float referenceSize = Math.min(maxBubbleHeight, maxBubbleWidth);

        pointBuffer[0] = (float) (entry.getXIndex() - minx) * phaseX + (float) minx;
        pointBuffer[1] = (float) (entry.getVal()) * phaseY;
        trans.pointValuesToPixel(pointBuffer);

        float shapeHalf = getShapeSize(entry.getSize(), dataSet.getMaxSize(), referenceSize) / 2f;

        if (!mViewPortHandler.isInBoundsTop(pointBuffer[1] + shapeHalf)
                || !mViewPortHandler.isInBoundsBottom(pointBuffer[1] - shapeHalf))
            continue;

        if (!mViewPortHandler.isInBoundsLeft(pointBuffer[0] + shapeHalf))
            continue;

        if (!mViewPortHandler.isInBoundsRight(pointBuffer[0] - shapeHalf))
            break;

        if (indice.getXIndex() < minx || indice.getXIndex() >= maxx)
            continue;

        final int originalColor = dataSet.getColor(entry.getXIndex());

        Color.RGBToHSV(Color.red(originalColor), Color.green(originalColor),
                Color.blue(originalColor), _hsvBuffer);
        _hsvBuffer[2] *= 0.5f;
        final int color = Color.HSVToColor(Color.alpha(originalColor), _hsvBuffer);

        mHighlightPaint.setColor(color);
        mHighlightPaint.setStrokeWidth(dataSet.getHighlightCircleWidth());
        c.drawCircle(pointBuffer[0], pointBuffer[1], shapeHalf, mHighlightPaint);
    }
}
 
Example 13
Source File: HorizontalBarChartRenderer.java    From NetKnight with Apache License 2.0 4 votes vote down vote up
@Override
protected void drawDataSet(Canvas c, IBarDataSet dataSet, int index) {

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

    mShadowPaint.setColor(dataSet.getBarShadowColor());
    mBarBorderPaint.setColor(dataSet.getBarBorderColor());
    mBarBorderPaint.setStrokeWidth(Utils.convertDpToPixel(dataSet.getBarBorderWidth()));

    final boolean drawBorder = dataSet.getBarBorderWidth() > 0.f;

    float phaseX = mAnimator.getPhaseX();
    float phaseY = mAnimator.getPhaseY();

    // initialize the buffer
    BarBuffer buffer = mBarBuffers[index];
    buffer.setPhases(phaseX, phaseY);
    buffer.setBarSpace(dataSet.getBarSpace());
    buffer.setDataSet(index);
    buffer.setInverted(mChart.isInverted(dataSet.getAxisDependency()));

    buffer.feed(dataSet);

    trans.pointValuesToPixel(buffer.buffer);

    for (int j = 0; j < buffer.size(); j += 4) {

        if (!mViewPortHandler.isInBoundsTop(buffer.buffer[j + 3]))
            break;

        if (!mViewPortHandler.isInBoundsBottom(buffer.buffer[j + 1]))
            continue;

        if (mChart.isDrawBarShadowEnabled()) {
            c.drawRect(mViewPortHandler.contentLeft(), buffer.buffer[j + 1],
                    mViewPortHandler.contentRight(),
                    buffer.buffer[j + 3], mShadowPaint);
        }

        // Set the color for the currently drawn value. If the index
        // is out of bounds, reuse colors.
        mRenderPaint.setColor(dataSet.getColor(j / 4));
        c.drawRect(buffer.buffer[j], buffer.buffer[j + 1], buffer.buffer[j + 2],
                buffer.buffer[j + 3], mRenderPaint);

        if (drawBorder) {
            c.drawRect(buffer.buffer[j], buffer.buffer[j + 1], buffer.buffer[j + 2],
                    buffer.buffer[j + 3], mBarBorderPaint);
        }
    }
}
 
Example 14
Source File: ScatterChartRenderer.java    From StockChart-MPAndroidChart with MIT License 4 votes vote down vote up
protected void drawDataSet(Canvas c, IScatterDataSet dataSet) {

        if (dataSet.getEntryCount() < 1)
            return;

        ViewPortHandler viewPortHandler = mViewPortHandler;

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

        float phaseY = mAnimator.getPhaseY();

        IShapeRenderer renderer = dataSet.getShapeRenderer();
        if (renderer == null) {
            Log.i("MISSING", "There's no IShapeRenderer specified for ScatterDataSet");
            return;
        }

        int max = (int)(Math.min(
                Math.ceil((float)dataSet.getEntryCount() * mAnimator.getPhaseX()),
                (float)dataSet.getEntryCount()));

        for (int i = 0; i < max; i++) {

            Entry e = dataSet.getEntryForIndex(i);

            mPixelBuffer[0] = e.getX();
            mPixelBuffer[1] = e.getY() * phaseY;

            trans.pointValuesToPixel(mPixelBuffer);

            if (!viewPortHandler.isInBoundsRight(mPixelBuffer[0]))
                break;

            if (!viewPortHandler.isInBoundsLeft(mPixelBuffer[0])
                    || !viewPortHandler.isInBoundsY(mPixelBuffer[1]))
                continue;

            mRenderPaint.setColor(dataSet.getColor(i / 2));
            renderer.renderShape(
                    c, dataSet, mViewPortHandler,
                    mPixelBuffer[0], mPixelBuffer[1],
                    mRenderPaint);
        }
    }
 
Example 15
Source File: LineChartRenderer.java    From StockChart-MPAndroidChart with MIT License 4 votes vote down vote up
protected void drawCircles(Canvas c) {

        mRenderPaint.setStyle(Paint.Style.FILL);

        float phaseY = mAnimator.getPhaseY();

        mCirclesBuffer[0] = 0;
        mCirclesBuffer[1] = 0;

        List<ILineDataSet> dataSets = mChart.getLineData().getDataSets();

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

            ILineDataSet dataSet = dataSets.get(i);

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

            mCirclePaintInner.setColor(dataSet.getCircleHoleColor());

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

            mXBounds.set(mChart, dataSet);

            float circleRadius = dataSet.getCircleRadius();
            float circleHoleRadius = dataSet.getCircleHoleRadius();
            boolean drawCircleHole = dataSet.isDrawCircleHoleEnabled() &&
                    circleHoleRadius < circleRadius &&
                    circleHoleRadius > 0.f;
            boolean drawTransparentCircleHole = drawCircleHole &&
                    dataSet.getCircleHoleColor() == ColorTemplate.COLOR_NONE;

            DataSetImageCache imageCache;

            if (mImageCaches.containsKey(dataSet)) {
                imageCache = mImageCaches.get(dataSet);
            } else {
                imageCache = new DataSetImageCache();
                mImageCaches.put(dataSet, imageCache);
            }

            boolean changeRequired = imageCache.init(dataSet);

            // only fill the cache with new bitmaps if a change is required
            if (changeRequired) {
                imageCache.fill(dataSet, drawCircleHole, drawTransparentCircleHole);
            }

            int boundsRangeCount = mXBounds.range + mXBounds.min;

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

                Entry e = dataSet.getEntryForIndex(j);

                if (e == null) {
                    break;
                }

                mCirclesBuffer[0] = e.getX();
                mCirclesBuffer[1] = e.getY() * phaseY;

                trans.pointValuesToPixel(mCirclesBuffer);

                if (!mViewPortHandler.isInBoundsRight(mCirclesBuffer[0])) {
                    break;
                }

                if (!mViewPortHandler.isInBoundsLeft(mCirclesBuffer[0]) ||
                        !mViewPortHandler.isInBoundsY(mCirclesBuffer[1])) {
                    continue;
                }

                Bitmap circleBitmap = imageCache.getBitmap(j);

                if (circleBitmap != null) {
                    c.drawBitmap(circleBitmap, mCirclesBuffer[0] - circleRadius, mCirclesBuffer[1] - circleRadius, null);
                }
            }
        }
    }
 
Example 16
Source File: HorizontalBarChartRenderer.java    From iMoney with Apache License 2.0 4 votes vote down vote up
protected void drawDataSet(Canvas c, BarDataSet dataSet, int index) {

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

        mShadowPaint.setColor(dataSet.getBarShadowColor());

        float phaseX = mAnimator.getPhaseX();
        float phaseY = mAnimator.getPhaseY();

        List<BarEntry> entries = dataSet.getYVals();

        // initialize the buffer
        BarBuffer buffer = mBarBuffers[index];
        buffer.setPhases(phaseX, phaseY);
        buffer.setBarSpace(dataSet.getBarSpace());
        buffer.setDataSet(index);
        buffer.setInverted(mChart.isInverted(dataSet.getAxisDependency()));

        buffer.feed(entries);

        trans.pointValuesToPixel(buffer.buffer);

        for (int j = 0; j < buffer.size(); j += 4) {

            if (!mViewPortHandler.isInBoundsTop(buffer.buffer[j + 3]))
                break;

            if (!mViewPortHandler.isInBoundsBottom(buffer.buffer[j + 1]))
                continue;

            if (mChart.isDrawBarShadowEnabled()) {
                c.drawRect(mViewPortHandler.contentLeft(), buffer.buffer[j + 1],
                        mViewPortHandler.contentRight(),
                        buffer.buffer[j + 3], mShadowPaint);
            }

            // Set the color for the currently drawn value. If the index
            // is
            // out of bounds, reuse colors.
            mRenderPaint.setColor(dataSet.getColor(j / 4));
            c.drawRect(buffer.buffer[j], buffer.buffer[j + 1], buffer.buffer[j + 2],
                    buffer.buffer[j + 3], mRenderPaint);
        }
    }
 
Example 17
Source File: BubbleChartRenderer.java    From iMoney with Apache License 2.0 2 votes vote down vote up
protected void drawDataSet(Canvas c, BubbleDataSet dataSet) {

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

        float phaseX = mAnimator.getPhaseX();
        float phaseY = mAnimator.getPhaseY();

        List<BubbleEntry> 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());

        sizeBuffer[0] = 0f;
        sizeBuffer[2] = 1f;

        trans.pointValuesToPixel(sizeBuffer);

        // calcualte the full width of 1 step on the x-axis
        final float maxBubbleWidth = Math.abs(sizeBuffer[2] - sizeBuffer[0]);
        final float maxBubbleHeight = Math.abs(mViewPortHandler.contentBottom() - mViewPortHandler.contentTop());
        final float referenceSize = Math.min(maxBubbleHeight, maxBubbleWidth);

        for (int j = minx; j < maxx; j++) {

            final BubbleEntry entry = entries.get(j);

            pointBuffer[0] = (float) (entry.getXIndex() - minx) * phaseX + (float) minx;
            pointBuffer[1] = (float) (entry.getVal()) * phaseY;
            trans.pointValuesToPixel(pointBuffer);

            float shapeHalf = getShapeSize(entry.getSize(), dataSet.getMaxSize(), referenceSize) / 2f;

            if (!mViewPortHandler.isInBoundsTop(pointBuffer[1] + shapeHalf)
                    || !mViewPortHandler.isInBoundsBottom(pointBuffer[1] - shapeHalf))
                continue;

            if (!mViewPortHandler.isInBoundsLeft(pointBuffer[0] + shapeHalf))
                continue;

            if (!mViewPortHandler.isInBoundsRight(pointBuffer[0] - shapeHalf))
                break;

            final int color = dataSet.getColor(entry.getXIndex());

            mRenderPaint.setColor(color);
            c.drawCircle(pointBuffer[0], pointBuffer[1], shapeHalf, mRenderPaint);
        }
    }
 
Example 18
Source File: BubbleChartRenderer.java    From android-kline with Apache License 2.0 2 votes vote down vote up
protected void drawDataSet(Canvas c, IBubbleDataSet dataSet) {

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

        float phaseY = mAnimator.getPhaseY();

        mXBounds.set(mChart, dataSet);

        sizeBuffer[0] = 0f;
        sizeBuffer[2] = 1f;

        trans.pointValuesToPixel(sizeBuffer);

        boolean normalizeSize = dataSet.isNormalizeSizeEnabled();

        // calcualte the full width of 1 step on the x-axis
        final float maxBubbleWidth = Math.abs(sizeBuffer[2] - sizeBuffer[0]);
        final float maxBubbleHeight = Math.abs(mViewPortHandler.contentBottom() - mViewPortHandler.contentTop());
        final float referenceSize = Math.min(maxBubbleHeight, maxBubbleWidth);

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

            final BubbleEntry entry = dataSet.getEntryForIndex(j);

            pointBuffer[0] = entry.getX();
            pointBuffer[1] = (entry.getY()) * phaseY;
            trans.pointValuesToPixel(pointBuffer);

            float shapeHalf = getShapeSize(entry.getSize(), dataSet.getMaxSize(), referenceSize, normalizeSize) / 2f;

            if (!mViewPortHandler.isInBoundsTop(pointBuffer[1] + shapeHalf)
                    || !mViewPortHandler.isInBoundsBottom(pointBuffer[1] - shapeHalf))
                continue;

            if (!mViewPortHandler.isInBoundsLeft(pointBuffer[0] + shapeHalf))
                continue;

            if (!mViewPortHandler.isInBoundsRight(pointBuffer[0] - shapeHalf))
                break;

            final int color = dataSet.getColor((int) entry.getX());

            mRenderPaint.setColor(color);
            c.drawCircle(pointBuffer[0], pointBuffer[1], shapeHalf, mRenderPaint);
        }
    }
 
Example 19
Source File: BubbleChartRenderer.java    From android-kline with Apache License 2.0 2 votes vote down vote up
protected void drawDataSet(Canvas c, IBubbleDataSet dataSet) {

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

        float phaseY = mAnimator.getPhaseY();

        mXBounds.set(mChart, dataSet);

        sizeBuffer[0] = 0f;
        sizeBuffer[2] = 1f;

        trans.pointValuesToPixel(sizeBuffer);

        boolean normalizeSize = dataSet.isNormalizeSizeEnabled();

        // calcualte the full width of 1 step on the x-axis
        final float maxBubbleWidth = Math.abs(sizeBuffer[2] - sizeBuffer[0]);
        final float maxBubbleHeight = Math.abs(mViewPortHandler.contentBottom() - mViewPortHandler.contentTop());
        final float referenceSize = Math.min(maxBubbleHeight, maxBubbleWidth);

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

            final BubbleEntry entry = dataSet.getEntryForIndex(j);

            pointBuffer[0] = entry.getX();
            pointBuffer[1] = (entry.getY()) * phaseY;
            trans.pointValuesToPixel(pointBuffer);

            float shapeHalf = getShapeSize(entry.getSize(), dataSet.getMaxSize(), referenceSize, normalizeSize) / 2f;

            if (!mViewPortHandler.isInBoundsTop(pointBuffer[1] + shapeHalf)
                    || !mViewPortHandler.isInBoundsBottom(pointBuffer[1] - shapeHalf))
                continue;

            if (!mViewPortHandler.isInBoundsLeft(pointBuffer[0] + shapeHalf))
                continue;

            if (!mViewPortHandler.isInBoundsRight(pointBuffer[0] - shapeHalf))
                break;

            final int color = dataSet.getColor((int) entry.getX());

            mRenderPaint.setColor(color);
            c.drawCircle(pointBuffer[0], pointBuffer[1], shapeHalf, mRenderPaint);
        }
    }
 
Example 20
Source File: BubbleChartRenderer.java    From StockChart-MPAndroidChart with MIT License 2 votes vote down vote up
protected void drawDataSet(Canvas c, IBubbleDataSet dataSet) {

        if (dataSet.getEntryCount() < 1)
            return;

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

        float phaseY = mAnimator.getPhaseY();

        mXBounds.set(mChart, dataSet);

        sizeBuffer[0] = 0f;
        sizeBuffer[2] = 1f;

        trans.pointValuesToPixel(sizeBuffer);

        boolean normalizeSize = dataSet.isNormalizeSizeEnabled();

        // calcualte the full width of 1 step on the x-axis
        final float maxBubbleWidth = Math.abs(sizeBuffer[2] - sizeBuffer[0]);
        final float maxBubbleHeight = Math.abs(mViewPortHandler.contentBottom() - mViewPortHandler.contentTop());
        final float referenceSize = Math.min(maxBubbleHeight, maxBubbleWidth);

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

            final BubbleEntry entry = dataSet.getEntryForIndex(j);

            pointBuffer[0] = entry.getX();
            pointBuffer[1] = (entry.getY()) * phaseY;
            trans.pointValuesToPixel(pointBuffer);

            float shapeHalf = getShapeSize(entry.getSize(), dataSet.getMaxSize(), referenceSize, normalizeSize) / 2f;

            if (!mViewPortHandler.isInBoundsTop(pointBuffer[1] + shapeHalf)
                    || !mViewPortHandler.isInBoundsBottom(pointBuffer[1] - shapeHalf)) {
                continue;
            }

            if (!mViewPortHandler.isInBoundsLeft(pointBuffer[0] + shapeHalf)) {
                continue;
            }

            if (!mViewPortHandler.isInBoundsRight(pointBuffer[0] - shapeHalf)) {
                break;
            }

            final int color = dataSet.getColor((int) entry.getX());

            mRenderPaint.setColor(color);
            c.drawCircle(pointBuffer[0], pointBuffer[1], shapeHalf, mRenderPaint);
        }
    }