当前位置:网站首页>The pie chart with dimension lines can set various parameter options
The pie chart with dimension lines can set various parameter options
2022-06-24 08:42:00 【Simon66991】
PieChartView
package cn.simon.view;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.RectF;
import android.text.TextPaint;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.View;
import java.util.ArrayList;
import java.util.List;
import cn.simon.R;
/** * Pie chart , With dimension lines , You can set a variety of parameter options by yourself */
public class PieChartView extends View {
private TextPaint mTextPaint;
private float mTextWidth;
private float mTextHeight;
/** * Pie radius */
private float pieChartCircleRadius = 100;
private float textBottom;
/** * Record text size */
private float mTextSize = 14;
/** * Rectangular area occupied by pie chart ( Not including words ) */
private RectF pieChartCircleRectF = new RectF();
/** * Pie chart information list */
private List<PieceDataHolder> pieceDataHolders = new ArrayList<>();
/** * Marking line length */
private float markerLineLength = 30f;
public PieChartView(Context context) {
super(context);
init(null, 0);
}
public PieChartView(Context context, AttributeSet attrs) {
super(context, attrs);
init(attrs, 0);
}
public PieChartView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init(attrs, defStyle);
}
private void init(AttributeSet attrs, int defStyle) {
// Load attributes
final TypedArray a = getContext().obtainStyledAttributes(
attrs, R.styleable.PieChartView, defStyle, 0);
pieChartCircleRadius = a.getDimension(
R.styleable.PieChartView_circleRadius,
pieChartCircleRadius);
mTextSize = a.getDimension(R.styleable.PieChartView_textSize, mTextSize)/getResources().getDisplayMetrics().density;
a.recycle();
// Set up a default TextPaint object
mTextPaint = new TextPaint();
mTextPaint.setFlags(Paint.ANTI_ALIAS_FLAG);
mTextPaint.setTextAlign(Paint.Align.LEFT);
// Update TextPaint and text measurements from attributes
invalidateTextPaintAndMeasurements();
}
private void invalidateTextPaintAndMeasurements() {
mTextPaint.setTextSize(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, mTextSize, getContext().getResources().getDisplayMetrics()));
Paint.FontMetrics fontMetrics = mTextPaint.getFontMetrics();
mTextHeight = fontMetrics.descent - fontMetrics.ascent;
textBottom = fontMetrics.bottom;
}
/** * Set the radius of the pie chart * * @param pieChartCircleRadius Radius of pie chart (px) */
public void setPieChartCircleRadius(int pieChartCircleRadius) {
this.pieChartCircleRadius = pieChartCircleRadius;
invalidate();
}
/** * Set the length of the marking line * * @param markerLineLength Length of marking line (px) */
public void setMarkerLineLength(int markerLineLength) {
this.markerLineLength = markerLineLength;
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
initPieChartCircleRectF();
drawAllSectors(canvas);
}
private void drawAllSectors(Canvas canvas) {
float sum = 0f;
for (PieceDataHolder pieceDataHolder : pieceDataHolders) {
sum += pieceDataHolder.value;
}
float sum2 = 0f;
for (PieceDataHolder pieceDataHolder : pieceDataHolders) {
float startAngel = sum2 / sum * 360;
sum2 += pieceDataHolder.value;
float sweepAngel = pieceDataHolder.value / sum * 360;
drawSector(canvas, pieceDataHolder.color, startAngel, sweepAngel);
drawMarkerLineAndText(canvas, pieceDataHolder.color, startAngel + sweepAngel / 2, pieceDataHolder.marker);
}
}
private void initPieChartCircleRectF() {
pieChartCircleRectF.left = getWidth() / 2 - pieChartCircleRadius;
pieChartCircleRectF.top = getHeight() / 2 - pieChartCircleRadius;
pieChartCircleRectF.right = pieChartCircleRectF.left + pieChartCircleRadius * 2;
pieChartCircleRectF.bottom = pieChartCircleRectF.top + pieChartCircleRadius * 2;
}
/** * Gets the example dimension attribute value. * * @return The example dimension attribute value.(sp) */
public float getTextSize() {
return mTextSize;
}
/** * Sets the view's text dimension attribute value. In the PieChartView view, this dimension * is the font size. * * @param textSize The text dimension attribute value to use.(sp) */
public void setTextSize(float textSize) {
mTextSize = textSize;
invalidateTextPaintAndMeasurements();
}
/** * Set the data to be displayed in the pie chart * * @param data The list of data */
public void setData(List<PieceDataHolder> data) {
if (data != null) {
pieceDataHolders.clear();
pieceDataHolders.addAll(data);
}
invalidate();
}
/** * Drawing fan * * @param canvas canvas * @param color To draw the color of the fan * @param startAngle Starting angle * @param sweepAngle End angle */
protected void drawSector(Canvas canvas, int color, float startAngle, float sweepAngle) {
Paint paint = new Paint();
paint.setFlags(Paint.ANTI_ALIAS_FLAG);
paint.setStyle(Paint.Style.FILL);
paint.setColor(color);
canvas.drawArc(pieChartCircleRectF, startAngle, sweepAngle, true, paint);
}
/** * Draw dimension lines and tag text * * @param canvas canvas * @param color The color of the mark * @param rotateAngel Mark the rotation angle between the line and the horizontal */
protected void drawMarkerLineAndText(Canvas canvas, int color, float rotateAngel, String text) {
Paint paint = new Paint();
paint.setFlags(Paint.ANTI_ALIAS_FLAG);
paint.setStyle(Paint.Style.STROKE);
paint.setColor(color);
Path path = new Path();
path.close();
path.moveTo(getWidth() / 2, getHeight() / 2);
final float x = (float) (getWidth() / 2 + (markerLineLength + pieChartCircleRadius) * Math.cos(Math.toRadians(rotateAngel)));
final float y = (float) (getHeight() / 2 + (markerLineLength + pieChartCircleRadius) * Math.sin(Math.toRadians(rotateAngel)));
path.lineTo(x, y);
float landLineX;
if (270f > rotateAngel && rotateAngel > 90f) {
landLineX = x - 20;
} else {
landLineX = x + 20;
}
path.lineTo(landLineX, y);
canvas.drawPath(path, paint);
mTextPaint.setColor(color);
if (270f > rotateAngel && rotateAngel > 90f) {
float textWidth = mTextPaint.measureText(text);
canvas.drawText(text, landLineX - textWidth, y + mTextHeight / 2 - textBottom, mTextPaint);
} else {
canvas.drawText(text, landLineX, y + mTextHeight / 2 - textBottom, mTextPaint);
}
}
/** * The information holder of each piece of the pie chart */
public static final class PieceDataHolder {
/** * The size of the value of each sector */
private float value;
/** * The color of the fan */
private int color;
/** * Marking of each block */
private String marker;
public PieceDataHolder(float value, int color, String marker) {
this.value = value;
this.color = color;
this.marker = marker;
}
}
}
attrs_pie_chart_view.xml
<resources>
<declare-styleable name="PieChartView">
<attr name="circleRadius" format="dimension" />
<attr name="textSize" format="dimension" />
<attr name="exampleDrawable" format="color|reference" />
</declare-styleable>
</resources>
边栏推荐
- ZUCC_ Principles of compiling language and compilation_ Experiment 04 language and grammar
- Common misconceptions in Tencent conference API - signature error_ code 200003
- 数据库迁移从PostgreSQL迁移到 MYSQL
- Fundamentals of 3D mathematics [17] inverse square theorem
- Easycvr invokes the interface parameter acquisition method and precautions of device video recording on the page
- 深度学习与神经网络:最值得关注的6大趋势
- JUC personal simple notes
- PHP代码加密的几种方案
- api平台通用签名机制
- [untitled]
猜你喜欢

ZUCC_编译语言原理与编译_实验02 FSharp OCaml语言

ZUCC_ Principles of compiling language and compilation_ Experiment 05 regular expression, finite automata, lexical analysis

ZUCC_ Principles of compiling language and compilation_ Experiment 02 fsharp Ocaml language

表单图片上传在Chorme中无法查看请求体的二进制图片信息

js中通过key查找和更新对象中指定值的方法

Permission model DAC ACL RBAC ABAC

ZUCC_编译语言原理与编译_实验04 语言与文法

MySQL 因字符集问题插入中文数据时提示代码 :1366

MATLAB Camera Calibrator相机标定

ZUCC_ Principles of compiling language and compilation_ Experiment 04 language and grammar
随机推荐
数据库,查询本月借出书的数量,如果高于10本时,显示“本月借出书大于10本”,否则显示“本月借出书小于10本”
jwt(json web token)
Qt源码分析--QObject(2)
Easynvr and easyrtc platforms use go language to manage projects. Summary of the use of govendor and gomod
QTimer定时器不起作用的原因
什么是SRE?一文详解SRE运维体系
Common date formatter and QT method for obtaining current time
Fundamentals of 3D mathematics [17] inverse square theorem
Common CVM transcribes audio using virtual sound card
JUC个人简单笔记
QT writing security video monitoring system 36 onvif continuous movement
How to implement approval function in Tekton
数据库迁移从PostgreSQL迁移到 MYSQL
leetcode 1642. Furthest Building You Can Reach(能到达的最远的建筑)
【关于运维和网工的差别,一文说透】
ZUCC_ Principles of compiling language and compilation_ Experiment 04 language and grammar
Pyqt common system events
[acnoi2022] not a structure, more like a structure
定时备份数据库脚本
Promise的使用場景