Created
December 10, 2015 23:55
-
-
Save ElFeesho/0a2c41e4bc975d3990ee to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import android.content.Context; | |
| import android.content.res.TypedArray; | |
| import android.graphics.Canvas; | |
| import android.graphics.Paint; | |
| import android.graphics.RectF; | |
| import android.util.AttributeSet; | |
| import android.view.View; | |
| import java.util.ArrayList; | |
| import java.util.List; | |
| public class PieChartView extends View { | |
| public static class Data { | |
| public final float value; | |
| public final int colour; | |
| public Data(float value, int colour) { | |
| this.value = value; | |
| this.colour = colour; | |
| } | |
| } | |
| private final List<Data> dataPoints = new ArrayList<>(); | |
| private RectF bounds; | |
| private Paint paint = new Paint(); | |
| public PieChartView(Context context) { | |
| this(context, null, 0); | |
| } | |
| public PieChartView(Context context, AttributeSet attrs) { | |
| this(context, attrs, 0); | |
| } | |
| public PieChartView(Context context, AttributeSet attrs, int defStyleAttr) { | |
| super(context, attrs, defStyleAttr); | |
| float strokeWidth = 2.0f; | |
| if (attrs != null) { | |
| TypedArray attributes = context.obtainStyledAttributes(attrs, R.styleable.PieChartView); | |
| strokeWidth = attributes.getFloat(R.styleable.PieChartView_strokeThickness, strokeWidth); | |
| attributes.recycle(); | |
| } | |
| paint.setStrokeWidth(strokeWidth); | |
| paint.setStyle(Paint.Style.STROKE); | |
| paint.setAntiAlias(true); | |
| if (isInEditMode()) { | |
| addData(new Data(10, 0xffff0000)); | |
| addData(new Data(20, 0xff00ff00)); | |
| addData(new Data(30, 0xff0088ff)); | |
| } | |
| } | |
| public void addData(Data data) { | |
| dataPoints.add(data); | |
| postInvalidate(); | |
| } | |
| private float determineTotalData() { | |
| float total = 0; | |
| for (Data dataPoint : dataPoints) { | |
| total += dataPoint.value; | |
| } | |
| return total; | |
| } | |
| @Override | |
| protected void onSizeChanged(int w, int h, int oldw, int oldh) { | |
| super.onSizeChanged(w, h, oldw, oldh); | |
| bounds = new RectF(paint.getStrokeWidth(), paint.getStrokeWidth(), w - paint.getStrokeWidth() * 2, h - paint.getStrokeWidth() * 2); | |
| } | |
| @Override | |
| protected void onDraw(Canvas canvas) { | |
| super.onDraw(canvas); | |
| float lastStartAngle = -90; | |
| float totalData = determineTotalData(); | |
| for (Data dataPoint : dataPoints) { | |
| float sweep = (dataPoint.value / totalData) * 360; | |
| paint.setColor(dataPoint.colour); | |
| canvas.drawArc(bounds, lastStartAngle, sweep, false, paint); | |
| lastStartAngle += sweep; | |
| } | |
| } | |
| } | |
| <?xml version="1.0" encoding="utf-8"?> | |
| <resources> | |
| <declare-styleable name="PieChartView"> | |
| <attr name="strokeThickness" format="float"/> | |
| </declare-styleable> | |
| </resources> | |
| <.......PieChartView | |
| android:id="@+id/piechartview" | |
| app:strokeThickness="6.0" | |
| android:layout_width="100dp" | |
| android:layout_height="100dp"/> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment