gpt4 book ai didi

android - MPAndroidChart - 如何在 LimitLine 左侧显示文本?

转载 作者:行者123 更新时间:2023-12-04 23:44:32 26 4
gpt4 key购买 nike

我正在尝试在 LimitLine 的左侧显示文本,如下所示:
enter image description here
然而,这些是我设置 Label 位置的唯一选项。为极限线。
enter image description here
我正在使用 LimitLine.LimitLabelPosition.LEFT_TOP它只显示Label在极限线之上。
enter image description here

YAxis leftAxis = mChart.getAxisLeft();
LimitLine minimumLimit = new LimitLine(50f, "Minimum Limit");
minimumLimit.setLineWidth(0.5f);
minimumLimit.setTextColor(ContextCompat.getColor(this, R.color.white_60_opacity));
minimumLimit.setLabelPosition(LimitLine.LimitLabelPosition.LEFT_TOP);
leftAxis.addLimitLine(minimumLimit);
如何显示 LimitLine LimitLine 左侧的标签?
编辑:
我也试过使用方法 .setXOffset(50f).setYOffset(50f)但这只会改变标签的位置,而不是 minimumLimit 行.
enter image description here

最佳答案

您可以通过使用自定义 YAxisRenderer 来实现此目的。稍微修改覆盖方法public void renderLimitLines(Canvas c) .
为此目的所需的修改是:
1 .计算每条限制线的标签宽度,以便能够将限制线移动到正确的 x 位置,如下所示:

limitLinePath.moveTo(mViewPortHandler.contentLeft()+getLabelTextWidth(l), pts[1]);
2 .要将标签绘制到新的 x,y 位置,如下所示:
c.drawText(label, mViewPortHandler.contentLeft() + xOffset, pts[1]+l.getYOffset(), mLimitLinePaint);
以下是自定义 MyYAxisRenderer包含上述修改:
public class MyYAxisRenderer extends YAxisRenderer {

private final Paint textPaint;

public MyYAxisRenderer(ViewPortHandler viewPortHandler, YAxis yAxis, Transformer trans) {
super(viewPortHandler, yAxis, trans);
textPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
}

@Override
public void renderLimitLines(Canvas c) {

List<LimitLine> limitLines = mYAxis.getLimitLines();

if (limitLines == null || limitLines.size() <= 0)
return;

float[] pts = mRenderLimitLinesBuffer;
pts[0] = 0;
pts[1] = 0;
Path limitLinePath = mRenderLimitLines;
limitLinePath.reset();

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

LimitLine l = limitLines.get(i);

if (!l.isEnabled())
continue;

int clipRestoreCount = c.save();
mLimitLineClippingRect.set(mViewPortHandler.getContentRect());
mLimitLineClippingRect.inset(0.f, -l.getLineWidth());
c.clipRect(mLimitLineClippingRect);

mLimitLinePaint.setStyle(Paint.Style.STROKE);
mLimitLinePaint.setColor(l.getLineColor());
mLimitLinePaint.setStrokeWidth(l.getLineWidth());
mLimitLinePaint.setPathEffect(l.getDashPathEffect());

pts[1] = l.getLimit();

mTrans.pointValuesToPixel(pts);

limitLinePath.moveTo(mViewPortHandler.contentLeft()+getLabelTextWidth(l), pts[1]);
limitLinePath.lineTo(mViewPortHandler.contentRight(), pts[1]);

c.drawPath(limitLinePath, mLimitLinePaint);
limitLinePath.reset();

String label = l.getLabel();

// if drawing the limit-value label is enabled
if (label != null && !label.equals("")) {

mLimitLinePaint.setStyle(l.getTextStyle());
mLimitLinePaint.setPathEffect(null);
mLimitLinePaint.setColor(l.getTextColor());
mLimitLinePaint.setTypeface(l.getTypeface());
mLimitLinePaint.setStrokeWidth(0.5f);
mLimitLinePaint.setTextSize(l.getTextSize());

final float labelLineHeight = Utils.calcTextHeight(mLimitLinePaint, label);
float xOffset = getLimitLineXOffset(l);
float yOffset = l.getLineWidth() + labelLineHeight + l.getYOffset();

final LimitLine.LimitLabelPosition position = l.getLabelPosition();

//draw the label on the left in the same y position of the limit line
mLimitLinePaint.setTextAlign(Paint.Align.LEFT);
c.drawText(label,
mViewPortHandler.contentLeft() + xOffset,
pts[1]+l.getYOffset(), mLimitLinePaint);
}

c.restoreToCount(clipRestoreCount);
}
}

private float getLimitLineXOffset(LimitLine l){
return Utils.convertDpToPixel(4f) + l.getXOffset();
}

private float getLabelTextWidth(LimitLine l) {

String label = l.getLabel();
if (label != null && !label.equals("")) {

textPaint.setStyle(l.getTextStyle());
textPaint.setPathEffect(null);
textPaint.setColor(l.getTextColor());
textPaint.setTypeface(l.getTypeface());
textPaint.setStrokeWidth(0.5f);
textPaint.setTextSize(l.getTextSize());

int textWidth = Utils.calcTextWidth(textPaint, label);
float xOffset = getLimitLineXOffset(l);

return textWidth + (xOffset*2);
}
return 0;
}
}
在上面的渲染器中,我添加了两个辅助函数,一个用于计算标签文本宽度 private float getLabelTextWidth(LimitLine l)对于特定的限制线和一个获取每个限制线的 x 偏移量 private float getLimitLineXOffset(LimitLine l)您可以根据自己的需要进行修改。
您可以使用上面的渲染器,如下所示:
lineChart.setRendererLeftYAxis(new MyYAxisRenderer(lineChart.getViewPortHandler(), lineChart.getAxisLeft(), lineChart.getTransformer(YAxis.AxisDependency.LEFT)));
结果:
limit_lines
注:这是用 v3.1.0 ('com.github.PhilJay:MPAndroidChart:v3.1.0') 测试的

关于android - MPAndroidChart - 如何在 LimitLine 左侧显示文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70647089/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com