Пример #1
0
 /**
  * The graphical representation of a text, to handle both HORIZONTAL and VERTICAL orientations and
  * extra rotation angles.
  *
  * @param canvas the canvas to paint to
  * @param text the text to be rendered
  * @param x the X axis location of the text
  * @param y the Y axis location of the text
  * @param paint the paint to be used for drawing
  * @param extraAngle the array of points to be used for drawing the series
  */
 protected void drawText(
     Canvas canvas, String text, float x, float y, Paint paint, int extraAngle) {
   int angle = -mRenderer.getOrientation().getAngle() + extraAngle;
   if (angle != 0) {
     // canvas.scale(1 / mScale, mScale);
     canvas.rotate(angle, x, y);
   }
   canvas.drawText(text, x, y, paint);
   if (angle != 0) {
     canvas.rotate(-angle, x, y);
     // canvas.scale(mScale, 1 / mScale);
   }
 }
Пример #2
0
  // ========================================================================
  void drawAllDataSets(
      Canvas canvas,
      Rect frame,
      Paint hash_mark_label_paint,
      double xPixelsPerUnit,
      double yPixelsPerUnit,
      double ySecondaryPixelsPerUnit) {
    //			double xPixelsPerUnit, double ySecondaryPixelsPerUnit) {

    MinMax x_span = mRenderer.getXAxisSpan();

    for (int i = 0; i < mDataset.getSeriesCount(); i++) {
      XYSeries series = mDataset.getSeriesAt(i);
      if (series.getItemCount() == 0) {
        continue;
      }

      SimpleSeriesRenderer seriesRenderer = mRenderer.getSeriesRendererAt(i);

      MinMax vertical_series_span;
      double vertical_pixels_per_unit;
      if (mRenderer.hasSecondaryYAxis() && seriesRenderer.getUsesSecondaryAxis()) {
        vertical_series_span = mRenderer.getYSecondaryAxisSpan();
        vertical_pixels_per_unit = ySecondaryPixelsPerUnit;
      } else {
        vertical_series_span = mRenderer.getYPrimaryAxisSpan();
        vertical_pixels_per_unit = yPixelsPerUnit;
      }

      List<PointF> points = new ArrayList<PointF>();
      for (int j = 0; j < series.getItemCount(); j++) {
        PointF point =
            new PointF(
                (float)
                    (frame.left
                        + xPixelsPerUnit
                            * (series.getX(j).doubleValue() - x_span.min.doubleValue())),
                (float)
                    (frame.bottom
                        - vertical_pixels_per_unit
                            * (series.getY(j).doubleValue()
                                - vertical_series_span.min.doubleValue())));
        points.add(point);
      }

      drawSeries(
          canvas,
          hash_mark_label_paint,
          points,
          seriesRenderer,
          (float) xPixelsPerUnit,
          (float) vertical_pixels_per_unit,
          Math.min(
              frame.bottom,
              (float)
                  (frame.bottom
                      + vertical_pixels_per_unit * vertical_series_span.min.doubleValue())),
          i);

      // Render glyphs atop the chart if necessary
      if (isRenderPoints(seriesRenderer)) {
        ScatterChart pointsChart = new ScatterChart(mDataset, mRenderer);
        pointsChart.drawSeries(
            canvas,
            hash_mark_label_paint,
            points,
            seriesRenderer,
            (float) xPixelsPerUnit,
            (float) vertical_pixels_per_unit,
            0,
            i);
      }

      hash_mark_label_paint.setTextSize(DEFAULT_HASH_MARK_TEXT_SIZE);
      if (Orientation.HORIZONTAL.equals(mRenderer.getOrientation())) {
        hash_mark_label_paint.setTextAlign(Align.CENTER);
      } else {
        hash_mark_label_paint.setTextAlign(Align.LEFT);
      }
      if (mRenderer.isDisplayChartValues()) {
        drawChartValuesText(canvas, series, hash_mark_label_paint, points, i);
      }
    }
  }
Пример #3
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);
    }
  }