Skip to content Skip to sidebar Skip to footer

Round Corners For Both Negative And Positive Bars Of Horizontal Bar Chart Using Mpandroidchart

I am trying to make the corners of horizontal bar chart rounded, here's the CustomChartRenderer code: package com.almaraiquest.Utils import android.graphics.* import com.github.mi

Solution 1:

It is little late to answer here but may be helpful for others. We can achieve it by little custom coding.

first of all we need to create a custom class

publicclassRoundedHorizontalBarChartRendererextendsHorizontalBarChartRenderer {
    publicRoundedHorizontalBarChartRenderer(BarDataProvider chart, ChartAnimator animator, ViewPortHandler viewPortHandler) {
        super(chart, animator, viewPortHandler);
    }

    privatefloat mRadius=5f;

    publicvoidsetmRadius(float mRadius) {
        this.mRadius = mRadius;
    }

    @OverrideprotectedvoiddrawDataSet(Canvas c, IBarDataSet dataSet, int index) {

        Transformertrans= mChart.getTransformer(dataSet.getAxisDependency());

        mShadowPaint.setColor(dataSet.getBarShadowColor());

        floatphaseX= mAnimator.getPhaseX();
        floatphaseY= mAnimator.getPhaseY();


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

        buffer.feed(dataSet);

        trans.pointValuesToPixel(buffer.buffer);

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

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

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

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

                if (mChart.isDrawBarShadowEnabled()) {
                    if (mRadius > 0)
                        c.drawRoundRect(newRectF(buffer.buffer[j], mViewPortHandler.contentTop(),
                                buffer.buffer[j + 2],
                                mViewPortHandler.contentBottom()), mRadius, mRadius, mShadowPaint);
                    else
                        c.drawRect(buffer.buffer[j], mViewPortHandler.contentTop(),
                                buffer.buffer[j + 2],
                                mViewPortHandler.contentBottom(), mShadowPaint);
                }

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

            mRenderPaint.setColor(dataSet.getColor());

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

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

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

                if (mChart.isDrawBarShadowEnabled()) {
                    if (mRadius > 0)
                        c.drawRoundRect(newRectF(buffer.buffer[j], mViewPortHandler.contentTop(),
                                buffer.buffer[j + 2],
                                mViewPortHandler.contentBottom()), mRadius, mRadius, mShadowPaint);
                    else
                        c.drawRect(buffer.buffer[j], buffer.buffer[j + 1], buffer.buffer[j + 2],
                                buffer.buffer[j + 3], mRenderPaint);
                }

                if (mRadius > 0)
                    c.drawRoundRect(newRectF(buffer.buffer[j], buffer.buffer[j + 1], buffer.buffer[j + 2],
                            buffer.buffer[j + 3]), mRadius, mRadius, mRenderPaint);
                else
                    c.drawRect(buffer.buffer[j], buffer.buffer[j + 1], buffer.buffer[j + 2],
                            buffer.buffer[j + 3], mRenderPaint);
            }
        }
    }
}

now, in your Activity

BarChartbarchart= (BarChart) findViewById( R.id.barchart );
        RoundedHorizontalBarChartRenderer roundedBarChartRenderer= newRoundedHorizontalBarChartRenderer(barchart , barchart.getAnimator(), barchart.getViewPortHandler());
        roundedBarChartRenderer.setmRadius(20f);
        barchart.setRenderer(roundedBarChartRenderer);

Hope this is helpful, please accept the answer if found helpful.

Post a Comment for "Round Corners For Both Negative And Positive Bars Of Horizontal Bar Chart Using Mpandroidchart"