在MPAndroidChart库中,如何在很长的时间内将X轴标签包装为两行? [英] In MPAndroidChart Library, How to wrap X Axis Labels to two lines when long?
问题描述
我试图显示X轴标签长到2行.如何在LineChart中实现这一目标?请参见下面的屏幕截图.我想有时间去第二行而不是停留在日期旁边
I am trying to show X axis label to go to 2 lines when they are long. How to achieve this in LineChart? See screenshot below. I want time to go to second line instead of staying next to date
推荐答案
对于像我这样想要实现但保留原始库的人来说,这是一个受@fgueli修改启发的简单解决方案.这仅适用于一个换行符(在标签中添加"\ n"),但您可以轻松地将其适应您的需求.
For those like me who want to achieve this but keep the original library, here is a simple solution inspired by @fgueli's modifications. This applies for one break line only (add "\n" in your labels) but you can easily adapt it to your needs.
-
XAxisRenderer子类
Subclass XAxisRenderer
public class CustomXAxisRenderer extends XAxisRenderer {
public CustomXAxisRenderer(ViewPortHandler viewPortHandler, XAxis xAxis, Transformer trans) {
super(viewPortHandler, xAxis, trans);
}
@Override
protected void drawLabel(Canvas c, String formattedLabel, float x, float y, MPPointF anchor, float angleDegrees) {
String line[] = formattedLabel.split("\n");
Utils.drawXAxisValue(c, line[0], x, y, mAxisLabelPaint, anchor, angleDegrees);
Utils.drawXAxisValue(c, line[1], x + mAxisLabelPaint.getTextSize(), y + mAxisLabelPaint.getTextSize(), mAxisLabelPaint, anchor, angleDegrees);
}
}
-
在所需图表上设置此渲染器
Set this renderer on the desired chart
lineChart.setXAxisRenderer(new CustomXAxisRenderer(lineChart.getViewPortHandler(), lineChart.getXAxis(), lineChart.getTransformer(YAxis.AxisDependency.LEFT)));
- 享受!
这篇关于在MPAndroidChart库中,如何在很长的时间内将X轴标签包装为两行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!