当前位置:网站首页>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>
边栏推荐
- Shell basic operator -- arithmetic operator
- Scénarios d'utilisation de la promesse
- Take my brother to do the project. It's cold
- Markdown to realize text link jump
- Centos7安装jdk8以及mysql5.7以及Navicat连接虚拟机mysql的出错以及解决方法(附mysql下载出错解决办法)
- OpenCV to realize the basic transformation of image
- api平台通用签名机制
- 2021-03-16 comp9021 class 9 notes
- Send custom events in QT
- Easynvr and easyrtc platforms use go language to manage projects. Summary of the use of govendor and gomod
猜你喜欢

为什么ping不通,而traceroute却可以通

【无标题】

How to improve the customer retention rate in the operation of independent stations? Customer segmentation is very important!

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

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

分布式 | 如何与 DBLE 进行“秘密通话”

什么是SRE?一文详解SRE运维体系

ZUCC_编译语言原理与编译_实验03 编译器入门

MATLAB Camera Calibrator相机标定
![[explain the difference between operation and maintenance and network engineering]](/img/2b/945f468588e729336e2e973e777623.jpg)
[explain the difference between operation and maintenance and network engineering]
随机推荐
There was an error checking the latest version of pip
String转Base64
2021-03-04 comp9021 class 6 notes
中国芯片独角兽公司
The reason why the qtimer timer does not work
JS to get the last element of the array
Micro build low code online "quick registration applet" capability
Tencent cloud ASR product PHP realizes real-time voice authentication request
Introduction to NC machine tool programming [G-code]
QTimer定时器不起作用的原因
Matlab求解线性方程组Ax=b
Shell basic operator -- arithmetic operator
xtrabackup做数据备份
Easydss anonymous live channel data volume instability optimization scheme sharing
How to improve the customer retention rate in the operation of independent stations? Customer segmentation is very important!
IIS build wordpress5.7 manually
Ordering of MySQL composite index
Easynvr and easyrtc platforms use go language to manage projects. Summary of the use of govendor and gomod
Live broadcast appointment: growth of Mengxin Product Manager
Take my brother to do the project. It's cold