Nin*_*der 15 android mpandroidchart
我试图显示X轴标签在它们很长时转到2行.如何在LineChart中实现这一目标?见下面的截图.我想要时间去第二行,而不是留在第二行
Gui*_*nel 25
对于那些想要实现这一目标但保留原始库的人来说,这是一个受@ fgueli修改启发的简单解决方案.这仅适用于一个折线(在标签中添加"\n"),但您可以轻松地根据您的需要进行调整.
子类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);
}
}
Run Code Online (Sandbox Code Playgroud)在所需的图表上设置此渲染器
lineChart.setXAxisRenderer(new CustomXAxisRenderer(lineChart.getViewPortHandler(), lineChart.getXAxis(), lineChart.getTransformer(YAxis.AxisDependency.LEFT)));
Run Code Online (Sandbox Code Playgroud)fgu*_*eli 15
我修改了库以允许使用\n在xAxis上允许多行标签
https://github.com/fabriziogueli/MPAndroidChart/tree/axis_multiline_labels
它现在正在工作,但需要进一步测试,也许需要一些代码样式/调整.
XAxis xAxis = mChart.getXAxis();
xAxis.setMultiLineLabel(true);
Run Code Online (Sandbox Code Playgroud)
然后你可以使用像这样的SimpleDateFormat:
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yy\nHH:mm a");
Run Code Online (Sandbox Code Playgroud)
也许您必须为图表设置额外的底部偏移量:
mChart.setExtraBottomOffset(50);
Run Code Online (Sandbox Code Playgroud)
@Guillaume Jounel 的改进版本支持多个换行符,以及带换行符的字符串。
@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);
for (int i = 1; i < line.length; i++) { // we've already processed 1st line
Utils.drawXAxisValue(c, line[i], x, y + mAxisLabelPaint.getTextSize() * i,
mAxisLabelPaint, anchor, angleDegrees);
}
}
Run Code Online (Sandbox Code Playgroud)
这看起来像是您必须通过根据需要修改库来实现的东西。
当前默认情况下不可能在 x 轴上有多条线。因此,原因是 AndroidCanvas不能简单地将字符串绘制"Line 1\nLine2"成两行,例如像这样。
| 归档时间: |
|
| 查看次数: |
9165 次 |
| 最近记录: |