Example #1
0
 @Override
 protected ClickableArea[] clickableAreasForPoints(
     List<Float> points, List<Double> values, float yAxisValue, int seriesIndex, int startIndex) {
   int seriesNr = mDataset.getSeriesCount();
   int length = points.size();
   ClickableArea[] ret = new ClickableArea[length / 2];
   float halfDiffX = getHalfDiffX(points, length, seriesNr);
   for (int i = 0; i < length; i += 2) {
     float x = points.get(i);
     float y = points.get(i + 1);
     if (mType == Type.STACKED || mType == Type.HEAPED) {
       ret[i / 2] =
           new ClickableArea(
               new RectF(
                   x - halfDiffX, Math.min(y, yAxisValue), x + halfDiffX, Math.max(y, yAxisValue)),
               values.get(i),
               values.get(i + 1));
     } else {
       float startX = x - seriesNr * halfDiffX + seriesIndex * 2 * halfDiffX;
       ret[i / 2] =
           new ClickableArea(
               new RectF(
                   startX,
                   Math.min(y, yAxisValue),
                   startX + 2 * halfDiffX,
                   Math.max(y, yAxisValue)),
               values.get(i),
               values.get(i + 1));
     }
   }
   return ret;
 }
Example #2
0
 protected int getGradientPartialColor(int minColor, int maxColor, float fraction) {
   int alpha =
       Math.round(fraction * Color.alpha(minColor) + (1 - fraction) * Color.alpha(maxColor));
   int r = Math.round(fraction * Color.red(minColor) + (1 - fraction) * Color.red(maxColor));
   int g = Math.round(fraction * Color.green(minColor) + (1 - fraction) * Color.green(maxColor));
   int b = Math.round(fraction * Color.blue(minColor) + (1 - fraction) * Color.blue((maxColor)));
   return Color.argb(alpha, r, g, b);
 }
Example #3
0
  /**
   * Draws a bar.
   *
   * @param canvas the canvas
   * @param xMin the X axis minimum
   * @param yMin the Y axis minimum
   * @param xMax the X axis maximum
   * @param yMax the Y axis maximum
   * @param scale the scale index
   * @param seriesIndex the current series index
   * @param paint the paint
   */
  protected void drawBar(
      Canvas canvas,
      float xMin,
      float yMin,
      float xMax,
      float yMax,
      int scale,
      int seriesIndex,
      Paint paint) {
    // Fix negative bars issue in Android 4.2
    float temp;
    if (xMin > xMax) {
      temp = xMin;
      xMin = xMax;
      xMax = temp;
    }
    if (yMin > yMax) {
      temp = yMin;
      yMin = yMax;
      yMax = temp;
    }

    SimpleSeriesRenderer renderer = mRenderer.getSeriesRendererAt(seriesIndex);
    if (renderer.isGradientEnabled()) {
      float minY =
          (float) toScreenPoint(new double[] {0, renderer.getGradientStopValue()}, scale)[1];
      float maxY =
          (float) toScreenPoint(new double[] {0, renderer.getGradientStartValue()}, scale)[1];
      float gradientMinY = Math.max(minY, Math.min(yMin, yMax));
      float gradientMaxY = Math.min(maxY, Math.max(yMin, yMax));
      int gradientMinColor = renderer.getGradientStopColor();
      int gradientMaxColor = renderer.getGradientStartColor();
      int gradientStartColor = gradientMaxColor;
      int gradientStopColor = gradientMinColor;

      if (yMin < minY) {
        paint.setColor(gradientMinColor);
        canvas.drawRect(
            Math.round(xMin), Math.round(yMin), Math.round(xMax), Math.round(gradientMinY), paint);
      } else {
        gradientStopColor =
            getGradientPartialColor(
                gradientMinColor, gradientMaxColor, (maxY - gradientMinY) / (maxY - minY));
      }
      if (yMax > maxY) {
        paint.setColor(gradientMaxColor);
        canvas.drawRect(
            Math.round(xMin), Math.round(gradientMaxY), Math.round(xMax), Math.round(yMax), paint);
      } else {
        gradientStartColor =
            getGradientPartialColor(
                gradientMaxColor, gradientMinColor, (gradientMaxY - minY) / (maxY - minY));
      }
      GradientDrawable gradient =
          new GradientDrawable(
              Orientation.BOTTOM_TOP, new int[] {gradientStartColor, gradientStopColor});
      gradient.setBounds(
          Math.round(xMin), Math.round(gradientMinY), Math.round(xMax), Math.round(gradientMaxY));
      gradient.draw(canvas);
    } else {
      if (Math.abs(yMin - yMax) < 1) {
        if (yMin < yMax) {
          yMax = yMin + 1;
        } else {
          yMax = yMin - 1;
        }
      }
      canvas.drawRect(
          Math.round(xMin), Math.round(yMin), Math.round(xMax), Math.round(yMax), paint);
    }
  }