Android绘制简单饼状图(2)

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.RectF;
import android.graphics.Paint.Style;
import android.util.AttributeSet;

public class PieChart extends BaseChart {
 public static final String DEFAULT_TITLE = "Pie Chart";
 public static final boolean DEFAULT_DISPLAY_RADIUS = true;
 public static final int DEFAULT_RADIUS_LENGTH = 80;
 public static final int DEFAULT_RADIUS_COLOR = Color.WHITE;
 public static final int DEFAULT_CIRCLE_BORDER_COLOR = Color.WHITE;
 public static final Point DEFAULT_POSITION = new Point(0, 0);


 private List<TitleValueColorEntity> data;
 private String title = DEFAULT_TITLE;
 private Point position = DEFAULT_POSITION;
 private int radiusLength = DEFAULT_RADIUS_LENGTH;
 private int radiusColor = DEFAULT_RADIUS_COLOR;
 private int circleBorderColor = DEFAULT_CIRCLE_BORDER_COLOR;
 private boolean displayRadius = DEFAULT_DISPLAY_RADIUS;

/*
  * (non-Javadoc)
  *
  * @param context
  *
  * @see cn.limc.androidcharts.view.BaseChart#BaseChart(Context)
  */
 public PieChart(Context context) {
  super(context);
 }

/*
  * (non-Javadoc)
  *
  * @param context
  *
  * @param attrs
  *
  * @param defStyle
  *
  * @see cn.limc.androidcharts.view.BaseChart#BaseChart(Context,
  * AttributeSet, int)
  */
 public PieChart(Context context, AttributeSet attrs, int defStyle) {
  super(context, attrs, defStyle);
 }

/*
  * (non-Javadoc)
  *
  * @param context
  *
  * @param attrs
  *
  * @see cn.limc.androidcharts.view.BaseChart#BaseChart(Context,
  * AttributeSet)
  */
 public PieChart(Context context, AttributeSet attrs) {
  super(context, attrs);
 }

/*
  * (non-Javadoc)
  *
  * <p>Called when is going to draw this chart</p>
  * <p>銉併儯銉笺儓銈掓浉銇忓墠銆併儭銈姐儍銉夈倰鍛笺伓</p>
  * <p>缁樺埗鍥捐〃鏃惰皟鐢/p>
  *
  * @param canvas
  *
  * @see android.view.View#onDraw(android.graphics.Canvas)
  */
 @Override
 protected void onDraw(Canvas canvas) {
  super.onDraw(canvas);

// get safe rect
  int rect = super.getWidth() > super.getHeight() ? super.getHeight()
    : super.getWidth();

// calculate radius length
  radiusLength = (int) ((rect / 2f) * 0.90);

// calculate position
  position = new Point((int) (getWidth() / 2f), (int) (getHeight() / 2f));

// draw this chart
  drawCircle(canvas);

// draw data on chart
  drawData(canvas);
 }

protected void drawCircle(Canvas canvas) {

Paint mPaintCircleBorder = new Paint();
  mPaintCircleBorder.setColor(Color.WHITE);
  mPaintCircleBorder.setStyle(Style.STROKE);
  mPaintCircleBorder.setStrokeWidth(2);
  mPaintCircleBorder.setAntiAlias(true);

// draw a circle
  canvas.drawCircle(position.x, position.y, radiusLength,
    mPaintCircleBorder);
 }

protected void drawData(Canvas canvas) {
  if (null != data) {
   // sum all data's value
   float sum = 0;
   for (int i = 0; i < data.size(); i++) {
    sum = sum + data.get(i).getValue();
   }

Paint mPaintFill = new Paint();
   mPaintFill.setStyle(Style.FILL);
   mPaintFill.setAntiAlias(true);

Paint mPaintBorder = new Paint();
   mPaintBorder.setStyle(Style.STROKE);
   mPaintBorder.setColor(radiusColor);
   mPaintBorder.setAntiAlias(true);

int offset = -90;
   // draw arcs of every piece
   for (int j = 0; j < data.size(); j++) {
    TitleValueColorEntity e = data.get(j);

// get color
    mPaintFill.setColor(e.getColor());

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

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