Custom view of Limit Line in MPAndroidChart

前端 未结 2 1456
北恋
北恋 2020-12-06 06:31

Is it possible to replace LimitLine with custom layout? So it looks something like this:

I see few solutions for that:

  1. Perhaps t
相关标签:
2条回答
  • 2020-12-06 06:58
    <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();
    
    0 讨论(0)
  • 2020-12-06 07:20

    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:

    `

    protected void drawYLabels(Canvas c, float fixedPosition, float[] positions, float offset) {
    
        // draw labels
        for (int i = 0; i < mYAxis.mEntryCount; i++) {
    
            String text = 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 = new float[2];
        for (LimitLine l : limitLines) {
            Paint paint = new Paint();
            paint.setStyle(Paint.Style.FILL);
            paint.setColor(l.getTextColor());
    
            Paint textPaint = 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);
            float paddingVert = Utils.convertDpToPixel(3);
            float paddingHoriz = Utils.convertDpToPixel(5);
            float height = Utils.calcTextHeight(textPaint, l.getLabel());
            float width = Utils.calcTextWidth(textPaint, l.getLabel());
            float posY = 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.

    0 讨论(0)
提交回复
热议问题