当前位置:网站首页>Customize view to draw line chart (support zoom)
Customize view to draw line chart (support zoom)
2022-06-21 13:17:00 【Azadoo】
results : No matter how big the line chart is Where to put it The line chart can be scaled
Main idea :
- Data processing :
According to the data quantity and layout width 、 coordinate Calculation point x、y Coordinate spacing distance and corresponding coordinates - draw :
stay onDraw Within the method need Path、Paint The object of path Responsible for drawing polylines ,Paint Responsible for setting the color 、 Shape and other information
Drawing lines : The first point needs to use path.moveTo() Later use path.lineTo() To realize the broken line Finally using canvas.drawPath() Draw it out
Drawing of points : Use it directly canvas.drawPoint() Drawing
package com.example.myapplication;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.DashPathEffect;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PointF;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.View;
import androidx.annotation.Nullable;
import java.util.ArrayList;
public class MyLineChartView extends View {
private double[] dataList = {120.0, 130.0, 160.0, 115.0, 180.0};
private ArrayList<PointF> xyList = new ArrayList<PointF>();
private Paint mLinePaint = new Paint();
private double minWeight = Integer.MAX_VALUE;
private double maxWeight = 0;
private Context mContext;
private double spaceX;
private int spaceY;
public MyLineChartView(Context context) {
this(context, null);
this.mContext = context;
}
public MyLineChartView(Context context, @Nullable AttributeSet attrs) {
this(context, attrs, 0);
this.mContext = context;
}
public MyLineChartView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
this.mContext = context;
int a = mContext.getResources().getDisplayMetrics().widthPixels;
// Find the maximum and minimum
for (int i = 0; i< dataList.length; i++) {
if (dataList[i] < minWeight) {
minWeight = dataList[i];
}
if (dataList[i] > maxWeight) {
maxWeight = dataList[i];
}
}
// Polyline brush
mLinePaint.setColor(Color.RED);
mLinePaint.setStrokeWidth(dp2px(5));
mLinePaint.setStyle(Paint.Style.STROKE);
mLinePaint.setAntiAlias(true);
}
@Override
protected void onDraw(Canvas canvas) {
calculationData(getLeft(), getTop(), getRight(), getBottom());
int a = getWidth();
Path path = new Path();
Paint paint= new Paint();
paint.setAntiAlias(true);// Sawtooth removal
paint.setColor(Color.BLACK);// Color
paint.setStrokeCap(Paint.Cap.ROUND);
paint.setStrokeWidth(40);
for (int i = 0; i < dataList.length; i++) {
if (i == 0) {
path.moveTo(xyList.get(i).x, xyList.get(i).y);
} else {
path.lineTo(xyList.get(i).x, xyList.get(i).y);
}
}
canvas.drawPath(path, mLinePaint);// Broken line
for (int i = 0; i < dataList.length; i++) {
canvas.drawPoint(xyList.get(i).x, xyList.get(i).y, paint);
}
}
protected float dp2px(float v) {
return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, v, getResources().getDisplayMetrics());
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
super.onLayout(changed, left, top, right, bottom);
}
private void calculationData(int left, int top, int right, int bottom) {
spaceX = (right - left) / (dataList.length + 1);
spaceY = (bottom - top) / 6;
// Set up xy coordinate
for (int i = 0; i< dataList.length; i++) {
PointF pointF = new PointF((int) (spaceX * ( i + 1 )), (int) ((1 - ((dataList[i] - minWeight)
/ (maxWeight - minWeight))) * (bottom - top )) );
xyList.add(pointF);
}
}
}
边栏推荐
- Setting of Seaborn drawing style
- 不止于ZeRO:BMTrain技术原理浅析
- uva11729
- Highly available configuration of database (MySQL)
- Pingcap was selected as the "voice of customers" of Gartner cloud database in 2022, and won the highest score of "outstanding performer"
- 如何阅读AI顶会论文?
- Summary of the latest remote deployment O & M tools
- Repair for a while, decisively reconstruct and take responsibility -- talk about CRM distributed cache optimization
- Flink CDC MongoDB Connector 的实现原理和使用实践
- 南京大学 静态软件分析(static program analyzes)-- introduction 学习笔记
猜你喜欢

Summary of several ways to calculate distance
MySQL constraints (descriptions of various conditions when creating tables)

Nanjing University static program analyses -- Introduction learning notes

分布式事务处理方案大 PK

Are you still using generator to generate crud code of XXX management system? Let's see what I wrote

How to read AI summit papers?
Collection reference type in JS
![[course assignment] floating point operation analysis and precision improvement](/img/de/ae3c1a41b81dffd8e4299bbe926099.png)
[course assignment] floating point operation analysis and precision improvement

PingCAP 入选 2022 Gartner 云数据库“客户之声”,获评“卓越表现者”最高分

Kubernetes快速實戰與核心原理剖析
随机推荐
【深入理解TcaplusDB技术】TcaplusDB导入数据
Pretraining Weekly No. 50: No Decode converter, neural prompt search, gradient Space reduction
Test the interface between app and wechat applet
Kube-prometheus grafana安装插件和grafana-image-renderer
机器学习探索共价键碳的亚稳态相图,未来可用于发现或设计亚稳态材料
How to use search engine?
2022年中国手机银行年度专题分析
17 commonly used o & M monitoring systems
How to delete floating IP manually
Questions and answers No. 43: application performance probe monitoring principle PHP probe
Hands on data analysis unit 2 section 4 data visualization
居家办公初体验之新得分享| 社区征文
Memcached(高性能内存对象缓存)
Educator web exercise - create a drop-down list
Is the micro securities account opening regular and safe? How to open an account?
Efficient remote office manual | community essay solicitation
[course assignment] floating point operation analysis and precision improvement
Two common schemes for handling interface idempotence
CP Instruction Learning
Talk about the delay of public network