1
使用AChartEngine,我希望能夠爲XYSeriesRenderer.setChartValuesTextAlign()指定一種Align.AUTO,使它根據位置不同對齊(實際上,minX點的.LEFT, .RIGHT爲maxX點,其餘爲.CENTER)。這將使所有值易於閱讀。是否有機會在不修改源代碼的情況下實現這樣的顯示?AChartEngine顯示值對齊
使用AChartEngine,我希望能夠爲XYSeriesRenderer.setChartValuesTextAlign()指定一種Align.AUTO,使它根據位置不同對齊(實際上,minX點的.LEFT, .RIGHT爲maxX點,其餘爲.CENTER)。這將使所有值易於閱讀。是否有機會在不修改源代碼的情況下實現這樣的顯示?AChartEngine顯示值對齊
好的,OOP去救援。 :-)
public class LineChartAligned extends LineChart {
private static final long serialVersionUID = 1L;
public LineChartAligned(XYMultipleSeriesDataset dataset, XYMultipleSeriesRenderer renderer) {
super(dataset, renderer);
}
@Override
protected void drawChartValuesText(Canvas canvas, XYSeries series, SimpleSeriesRenderer renderer, Paint paint, List<Float> points, int seriesIndex, int startIndex) {
if (points.size() > 1) {
float previousPointX = points.get(0);
float previousPointY = points.get(1);
for (int k = 0; k < points.size(); k += 2) {
if (k == 2) {
if (Math.abs(points.get(2) - points.get(0)) > renderer.getDisplayChartValuesDistance() || Math.abs(points.get(3) - points.get(1)) > renderer.getDisplayChartValuesDistance()) {
paint.setTextAlign(Align.LEFT); // <<<
drawText(canvas, getLabel(renderer.getChartValuesFormat(), series.getY(startIndex)), points.get(0), points.get(1) - renderer.getChartValuesSpacing(), paint, 0);
paint.setTextAlign(renderer.getChartValuesTextAlign()); // <<<
drawText(canvas, getLabel(renderer.getChartValuesFormat(), series.getY(startIndex + 1)), points.get(2), points.get(3) - renderer.getChartValuesSpacing(), paint, 0);
previousPointX = points.get(2);
previousPointY = points.get(3);
}
}
else if (k > 2) {
if (Math.abs(points.get(k) - previousPointX) > renderer.getDisplayChartValuesDistance() || Math.abs(points.get(k + 1) - previousPointY) > renderer.getDisplayChartValuesDistance()) {
paint.setTextAlign((k >= points.size() - 2) ? Align.RIGHT : renderer.getChartValuesTextAlign()); // <<<
drawText(canvas, getLabel(renderer.getChartValuesFormat(), series.getY(startIndex + k/2)), points.get(k), points.get(k + 1) - renderer.getChartValuesSpacing(), paint, 0);
previousPointX = points.get(k);
previousPointY = points.get(k + 1);
}
}
}
}
else
for (int k = 0; k < points.size(); k += 2)
drawText(canvas, getLabel(renderer.getChartValuesFormat(), series.getY(startIndex + k/2)), points.get(k), points.get(k + 1) - renderer.getChartValuesSpacing(), paint, 0);
}
}
有沒有這種確切的行爲,但你可以使用註釋。你可以把它們放在你想要的任何圖形中:
series.addAnnotation(text, x, y);
是的,謝謝你,但是......它不會很好地出現在一組距離在圖表上,它不會一致,除非我開始計算字符串寬度(考慮到變焦和一切)。此外,如果我沒有弄錯,它會使用圖表標題的顏色和文字大小,這不是一個真正的優勢。 –