AChartEngine显示值对齐

时间:2013-06-19 13:45:02

标签: android achartengine

使用AChartEngine,我希望能够为XYSeriesRenderer.setChartValuesTextAlign()指定一种Align.AUTO,它会使它根据位置不同地对齐(实际上,.LEFT用于minX点,.RIGHT用于maxX点,其余的.CENTER)。这将使所有值易于辨认。有没有机会在不修改源的情况下实现这样的显示?

2 个答案:

答案 0 :(得分:1)

没有这种确切的行为,但您可以为此使用注释。您可以将它们放在图表中的任何位置:

series.addAnnotation(text, x, y);

答案 1 :(得分:1)

好的,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);
  }

}