Skip to content Skip to sidebar Skip to footer

Custom View Of Limit Line In Mpandroidchart

Is it possible to replace LimitLine with custom layout? So it looks something like this: I see few solutions for that: Perhaps there're methods inside library for customizations

Solution 1:

<ScrollView><LinearLayout/><FrameLayout><Chart/><TextView/><FrameLauyout></ScrollView>

Use ViewPortHandler to get offset of chart

float offsetTop = mChart.getViewPortHandler().offsetTop();
    float offsetLeft = mChart.getViewPortHandler().offsetLeft();
    float offsetRight = mChart.getViewPortHandler().offsetRight();
    float chartHeight = mChart.getViewPortHandler().contentHeight();

Solution 2:

This is not a good way of doing it. I've done it by extending the YAxisRenderer.java file where the labels are actually drawn. They are not views, they are drawn on canvas. Here is my code for the labels:

`

protectedvoiddrawYLabels(Canvas c, float fixedPosition, float[] positions, float offset) {

    // draw labelsfor (inti=0; i < mYAxis.mEntryCount; i++) {

        Stringtext= mYAxis.getFormattedLabel(i);

        if (!mYAxis.isDrawTopYLabelEntryEnabled() && i >= mYAxis.mEntryCount - 1)
            return;

        c.drawText(text, fixedPosition, positions[i * 2 + 1] + offset, mAxisLabelPaint);
    }

    // limitline labels

    List<LimitLine> limitLines = mYAxis.getLimitLines();
    float[] pts = newfloat[2];
    for (LimitLine l : limitLines) {
        Paintpaint=newPaint();
        paint.setStyle(Paint.Style.FILL);
        paint.setColor(l.getTextColor());

        PainttextPaint= mAxisLabelPaint;
        textPaint.setColor(l.getLineLabelTextColor());
        textPaint.setTextSize(mAxisLabelPaint.getTextSize());
        textPaint.setPathEffect(null);
        textPaint.setTypeface(l.getTypeface());
        textPaint.setStrokeWidth(0.5f);
        textPaint.setStyle(l.getTextStyle());

        pts[1] = l.getLimit();
        mTrans.pointValuesToPixel(pts);
        floatpaddingVert= Utils.convertDpToPixel(3);
        floatpaddingHoriz= Utils.convertDpToPixel(5);
        floatheight= Utils.calcTextHeight(textPaint, l.getLabel());
        floatwidth= Utils.calcTextWidth(textPaint, l.getLabel());
        floatposY= pts[1] + height / 2;

        c.drawRect(fixedPosition - paddingHoriz, posY - height - paddingVert, fixedPosition + width + paddingHoriz*2, posY + paddingVert, paint);
        c.drawText(l.getLabel(), fixedPosition, posY, textPaint);
    }

}

`

Please note that you must use mTrans.pointValuesToPixel(pts) to convert your Y values to pixels.

Post a Comment for "Custom View Of Limit Line In Mpandroidchart"