예제 #1
0
  // ========================================================================
  float measureXaxisTickLabels(Paint hash_mark_label_paint, MinMax x_span) {

    Rect text_bounds_holder = new Rect();

    List<Integer> x_label_heights = new ArrayList<Integer>();
    for (Double xlabel : MathHelper.getLabels(x_span, mRenderer.getXLabels())) {
      String x_label = getLabel(xlabel, Axis.X_AXIS);
      hash_mark_label_paint.getTextBounds(x_label, 0, x_label.length(), text_bounds_holder);

      int label_height = text_bounds_holder.height();
      x_label_heights.add(label_height);
    }
    float max_x_label_height = Collections.max(x_label_heights);
    return max_x_label_height
        + VERTICAL_AXIS_LINE_LABEL_CLEARANCE
        + DEFAULT_HORIZONTAL_AXIS_HASH_MARK_LENGTH;
  }
예제 #2
0
  /**
   * The graphical representation of the XY chart.
   *
   * @param canvas the canvas to paint to
   * @param x the top left x value of the view to draw to
   * @param y the top left y value of the view to draw to
   * @param width the width of the view to draw to
   * @param height the height of the view to draw to
   */
  @Override
  public void draw(Canvas canvas, int width, int height) {

    TextPaint hash_mark_label_paint = new TextPaint();
    hash_mark_label_paint.setTextSize(DEFAULT_HASH_MARK_TEXT_SIZE);
    hash_mark_label_paint.setTypeface(DefaultRenderer.REGULAR_TEXT_FONT);
    hash_mark_label_paint.setAntiAlias(getAntiAliased());

    boolean rotate = Orientation.VERTICAL.equals(mRenderer.getOrientation());
    mScale = (float) (height) / width;
    mTranslate = Math.abs(width - height) / 2;
    if (mScale < 1) {
      mTranslate *= -1;
    }
    mCenter = new PointF(width / 2, height / 2);
    if (rotate) {
      transform(canvas, mRenderer.getOrientation().getAngle(), false);
    }

    MinMax x_span = mRenderer.getXAxisSpan();

    float vertical_axis_hash_mark_width = 0;
    float vertical_secondary_axis_hash_mark_width = 0;
    float horizontal_axis_label_height = 0;

    // Measure all y-axis label widths to determine the axis line position
    if (mRenderer.isShowLabels() && mRenderer.getShowYAxis()) {
      vertical_axis_hash_mark_width = measurePrimaryYaxisTickLabels(hash_mark_label_paint);

      if (mRenderer.hasSecondaryYAxis()) {
        Log.e(TAG, "Has secondary axis!!!");
        vertical_secondary_axis_hash_mark_width =
            measureSecondaryYaxisTickLabels(hash_mark_label_paint);
      }
    }

    if (mRenderer.isShowLabels() && mRenderer.getShowXAxis()) {
      horizontal_axis_label_height = measureXaxisTickLabels(hash_mark_label_paint, x_span);
    }

    Rect frame =
        new Rect(
            (int) Math.ceil(vertical_axis_hash_mark_width),
            0,
            width - (int) Math.ceil(vertical_secondary_axis_hash_mark_width),
            height - (int) horizontal_axis_label_height);

    double xPixelsPerUnit = getPixelsPerUnit(frame.width(), x_span);
    double yPixelsPerUnit = getPixelsPerUnit(frame.height(), mRenderer.getYPrimaryAxisSpan());
    double ySecondaryPixelsPerUnit = 0;
    if (mRenderer.hasSecondaryYAxis())
      ySecondaryPixelsPerUnit = getPixelsPerUnit(frame.height(), mRenderer.getYSecondaryAxisSpan());

    if (mRenderer.isShowLabels() || mRenderer.isShowGrid()) {

      List<Double> xLabels = MathHelper.getLabels(x_span, mRenderer.getXLabels());
      if (mRenderer.isShowLabels()) {
        hash_mark_label_paint.setColor(mRenderer.getLabelsColor());
        hash_mark_label_paint.setTextAlign(Align.CENTER);
      }

      drawXLabels(
          xLabels,
          mRenderer.getXTextLabelLocations(),
          canvas,
          hash_mark_label_paint,
          frame.left,
          frame.top,
          frame.bottom,
          xPixelsPerUnit,
          x_span.min.doubleValue(),
          DEFAULT_HORIZONTAL_AXIS_HASH_MARK_LENGTH,
          horizontal_axis_label_height - DEFAULT_HORIZONTAL_AXIS_HASH_MARK_LENGTH);

      drawVerticalAxisLabels(
          canvas,
          frame,
          hash_mark_label_paint,
          yPixelsPerUnit,
          ySecondaryPixelsPerUnit,
          vertical_axis_hash_mark_width,
          vertical_secondary_axis_hash_mark_width);
    }

    // This draws the plot boundaries.
    drawPlotBoundaries(canvas, frame, hash_mark_label_paint);

    // This draws the plot boundaries.
    drawAllDataSets(
        canvas,
        frame,
        hash_mark_label_paint,
        xPixelsPerUnit,
        yPixelsPerUnit,
        ySecondaryPixelsPerUnit);

    if (rotate) {
      transform(canvas, mRenderer.getOrientation().getAngle(), true);
    }
  }