Android绘制简单饼状图(3)

RectF oval = new RectF(position.x - radiusLength, position.y
      - radiusLength, position.x + radiusLength, position.y
      + radiusLength);
    int sweep = Math.round(e.getValue() / sum * 360f);
    canvas.drawArc(oval, offset, sweep, true, mPaintFill);
    canvas.drawArc(oval, offset, sweep, true, mPaintBorder);
    offset = offset + sweep;
   }

float sumvalue = 0f;
   for (TitleValueColorEntity e : data) {
    float value = e.getValue();
    sumvalue = sumvalue + value;
    float rate = (sumvalue - value / 2) / sum;
    mPaintFill.setColor(Color.BLUE);

// percentage
    float percentage = (int) (value / sum * 10000) / 100f;

float offsetX = (float) (position.x - radiusLength * 0.5
      * Math.sin(rate * -2 * Math.PI));
    float offsetY = (float) (position.y - radiusLength * 0.5
      * Math.cos(rate * -2 * Math.PI));

Paint mPaintFont = new Paint();
    mPaintFont.setColor(Color.WHITE);
    mPaintFont.setTextSize(MyUtils.sp2px(getContext(), 15));
    mPaintFont.setAntiAlias(true);
    mPaintFont.setStrokeWidth(5);

// draw titles
    String title = e.getTitle();
    float realx = 0;
    float realy = 0;

// TODO title position
    if (offsetX < position.x) {
     realx = offsetX - mPaintFont.measureText(title) - 5;
    } else if (offsetX > position.x) {
     realx = offsetX + 5;
    } else if (offsetX == position.x) {
     realx = offsetX - (mPaintFont.measureText(title)/2);
    }

if (offsetY >= position.y) {
     if (value / sum < 0.2f) {
      realy = offsetY + 10;
     } else {
      realy = offsetY + 5;
     }
    } else if (offsetY < position.y) {
     if (value / sum < 0.2f) {
      realy = offsetY - 10;
     } else {
      realy = offsetY + 5;
     }
    }

if(percentage != 0.0f) {
     canvas.drawText(title, realx, realy, mPaintFont);
     canvas.drawText(String.valueOf((int)value) + "min", realx, realy + MyUtils.sp2px(getContext(), 15), mPaintFont);
     canvas.drawText(String.valueOf(percentage) + "%", realx,
       realy + MyUtils.sp2px(getContext(), 30), mPaintFont);
    }
   }
  }
 }

/**
  * @return the data
  */
 public List<TitleValueColorEntity> getData() {
  return data;
 }

/**
  * @param data
  *            the data to set
  */
 public void setData(List<TitleValueColorEntity> data) {
  this.data = data;
 }

/**
  * @return the title
  */
 public String getTitle() {
  return title;
 }

/**
  * @param title
  *            the title to set
  */
 public void setTitle(String title) {
  this.title = title;
 }

/**
  * @return the position
  */
 public Point getPosition() {
  return position;
 }

/**
  * @param position
  *            the position to set
  */
 public void setPosition(Point position) {
  this.position = position;
 }

/**
  * @return the radiusLength
  */
 public int getRadiusLength() {
  return radiusLength;
 }

/**
  * @param radiusLength
  *            the radiusLength to set
  */
 public void setRadiusLength(int radiusLength) {
  this.radiusLength = radiusLength;
 }

/**
  * @return the radiusColor
  */
 public int getRadiusColor() {
  return radiusColor;
 }

/**
  * @param radiusColor
  *            the radiusColor to set
  */
 public void setRadiusColor(int radiusColor) {
  this.radiusColor = radiusColor;
 }

/**
  * @return the circleBorderColor
  */
 public int getCircleBorderColor() {
  return circleBorderColor;
 }

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://www.heiqu.com/51502872597f0fe68fce7a596068373b.html