/**
  * This has the same functionality as the super class of {@link Histogram}, except it calls
  * getDateLabel instead of just getLabel which will format the label as a date
  */
 public void drawXLabels(
     List<Double> xLabels,
     Double[] xTextLabelLocations,
     Canvas canvas,
     Paint paint,
     int left,
     int top,
     int bottom,
     double xPixelsPerUnit,
     double minX,
     double maxX) {
   XYMultipleSeriesRenderer renderer = mChart.getRenderer();
   int length = xLabels.size();
   boolean showLabels = renderer.isShowLabels();
   boolean showGrid = renderer.isShowGrid();
   for (int i = 0; i < length; i++) {
     double label = xLabels.get(i);
     float xLabel = (float) (left + xPixelsPerUnit * (label - minX));
     if (showLabels) {
       paint.setColor(renderer.getLabelsColor());
       canvas.drawLine(xLabel, bottom, xLabel, bottom + renderer.getLabelsTextSize() / 3, paint);
       mChart.drawText(
           canvas,
           getDateLabel(label),
           xLabel,
           bottom + renderer.getLabelsTextSize() * 4 / 3,
           paint,
           renderer.getXLabelsAngle());
     }
     if (showGrid) {
       paint.setColor(renderer.getGridColor());
       canvas.drawLine(xLabel, bottom, xLabel, top, paint);
     }
   }
   mChart.drawXTextLabels(
       xTextLabelLocations,
       canvas,
       paint,
       showLabels,
       left,
       top,
       bottom,
       xPixelsPerUnit,
       minX,
       maxX);
 }
Example #2
0
 /**
  * The graphical representation of the labels on the X axis.
  *
  * @param xLabels the X labels values
  * @param xTextLabelLocations the X text label locations
  * @param canvas the canvas to paint to
  * @param paint the paint to be used for drawing
  * @param left the left value of the labels area
  * @param top the top value of the labels area
  * @param bottom the bottom value of the labels area
  * @param xPixelsPerUnit the amount of pixels per one unit in the chart labels
  * @param minX the minimum value on the X axis in the chart
  */
 protected void drawXLabels(
     List<Double> xLabels,
     Double[] xTextLabelLocations,
     Canvas canvas,
     Paint paint,
     int left,
     int top,
     int bottom,
     double xPixelsPerUnit,
     double minX,
     float hash_mark_height,
     float max_text_height) {
   int length = xLabels.size();
   boolean showLabels = mRenderer.isShowLabels();
   for (int i = 0; i < length; i++) {
     double label = xLabels.get(i);
     float xLabel = (float) (left + xPixelsPerUnit * (label - minX));
     if (showLabels) {
       paint.setColor(mRenderer.getLabelsColor());
       canvas.drawLine(
           xLabel, bottom, xLabel, bottom + hash_mark_height, paint); // FIXME Magic numbers
       drawText(
           canvas,
           getLabel(label, Axis.X_AXIS),
           xLabel,
           bottom + hash_mark_height + max_text_height,
           paint,
           0);
     }
     if (mRenderer.isShowGrid() && mRenderer.isShowGridVerticalLines()) {
       paint.setColor(GRID_COLOR);
       canvas.drawLine(xLabel, bottom, xLabel, top, paint);
     }
   }
   if (showLabels) {
     paint.setColor(mRenderer.getLabelsColor());
     for (Double location : xTextLabelLocations) {
       float xLabel = (float) (left + xPixelsPerUnit * (location.doubleValue() - minX));
       canvas.drawLine(xLabel, bottom, xLabel, bottom + 4, paint); // FIXME Magic numbers
       drawText(canvas, mRenderer.getXTextLabel(location), xLabel, bottom + 12, paint, 0);
     }
   }
 }
Example #3
0
  protected void drawVerticalAxisLabels(
      Canvas canvas,
      Rect frame,
      Paint hash_mark_label_paint,
      double yPixelsPerUnit,
      double ySecondaryPixelsPerUnit,
      float vertical_axis_hash_mark_width,
      float vertical_secondary_axis_hash_mark_width) {

    MinMax y_span = mRenderer.getYPrimaryAxisSpan();

    List<Double> yLabels = MathHelper.getLabels(y_span, mRenderer.getYLabels());

    List<List<Double>> vertical_axis_datasets = new ArrayList<List<Double>>();
    List<MinMax> vertical_axis_spans = new ArrayList<MinMax>();
    vertical_axis_datasets.add(yLabels);
    vertical_axis_spans.add(y_span);
    if (mRenderer.hasSecondaryYAxis()) {
      MinMax secondary_span = mRenderer.getYSecondaryAxisSpan();
      vertical_axis_datasets.add(
          MathHelper.getLabels(secondary_span, mRenderer.getSecondaryYLabels()));
      vertical_axis_spans.add(secondary_span);
    }

    double[] vertical_pixels_per_unit = new double[] {yPixelsPerUnit, ySecondaryPixelsPerUnit};
    for (int vertical_axis_index = 0;
        vertical_axis_index < vertical_axis_datasets.size();
        vertical_axis_index++) {
      //			List<Double> vertical_labels = vertical_axis_datasets.get(0);
      List<Double> vertical_labels = vertical_axis_datasets.get(vertical_axis_index);

      MinMax label_span = vertical_axis_spans.get(vertical_axis_index);

      hash_mark_label_paint.setTextAlign(label_alignments[vertical_axis_index]);
      for (int i = 0; i < vertical_labels.size(); i++) {
        double label = vertical_labels.get(i);
        float label_value =
            (float)
                (frame.bottom
                    - vertical_pixels_per_unit[vertical_axis_index]
                        * (label - label_span.min.doubleValue()));

        float grid_line_startx, grid_line_stopx, hash_mark_startx, hash_mark_stopx;
        float label_x_offset;

        if (vertical_axis_index == 0) {

          grid_line_startx = frame.left;
          grid_line_stopx = frame.right;
          hash_mark_startx = grid_line_startx - vertical_axis_hash_mark_width;
          hash_mark_stopx = grid_line_startx;

          label_x_offset = frame.left - VERTICAL_AXIS_LINE_LABEL_CLEARANCE;

        } else {

          grid_line_startx = frame.left;
          grid_line_stopx = frame.right;
          hash_mark_startx = grid_line_stopx;
          hash_mark_stopx = grid_line_stopx + vertical_secondary_axis_hash_mark_width;

          label_x_offset = frame.right + VERTICAL_AXIS_LINE_LABEL_CLEARANCE;
        }

        if (mRenderer.isShowLabels()) {
          hash_mark_label_paint.setColor(grid_line_colors[vertical_axis_index]);
          //					hash_mark_label_paint.setColor(mRenderer.getLabelsColor());	// XXX
          canvas.drawLine(
              hash_mark_startx, label_value, hash_mark_stopx, label_value, hash_mark_label_paint);

          String label_string = getLabel(label, vertical_axis_enums[vertical_axis_index]);
          hash_mark_label_paint.measureText(label_string);
          drawText(
              canvas,
              getLabel(label, vertical_axis_enums[vertical_axis_index]),
              label_x_offset,
              label_value - VERTICAL_AXIS_LABEL_HASH_MARK_CLEARANCE,
              hash_mark_label_paint,
              0);
        }

        if (mRenderer.isShowGrid() && mRenderer.isShowGridHorizontalLines()) {
          hash_mark_label_paint.setColor(grid_line_colors[vertical_axis_index]);
          hash_mark_label_paint.setPathEffect(grid_line_path_effects[vertical_axis_index]);
          canvas.drawLine(
              grid_line_startx, label_value, grid_line_stopx, label_value, hash_mark_label_paint);

          hash_mark_label_paint.setPathEffect(null);
        }
      }
    }
  }
Example #4
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);
    }
  }