当前位置:网站首页>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);
}
}
}
边栏推荐
- 高效远程办公手册| 社区征文
- JS 中的集合引用类型
- Interpretation of tamigou project: 34% equity transfer of Jining Huayuan Project Management Co., Ltd
- Huawei cloud releases desktop ide codearts
- uva11995
- Eureka's timedsupersortask class (periodic task with automatic interval adjustment)
- Is it safe to open a securities account by downloading the app of qiniu business school? Is there a risk?
- Detailed explanation and examples of common parameters of curl
- SCCM creates a client collection based on the installed app and periodically pushes application updates
- How to read AI summit papers?
猜你喜欢

南京大学 静态软件分析(static program analyzes)-- introduction 学习笔记

Centos7 deploying MySQL environment

###数据库的高可用配置(mysql)
![[deeply understand tcapulusdb technology] tcapulusdb import data](/img/bd/999a0d2020f68b3bcee6b617328dfc.png)
[deeply understand tcapulusdb technology] tcapulusdb import data

Work content of service governance

Distributed transaction processing scheme big PK

修修补补一时爽,果断重构有担当——聊聊CRM分布式缓存优化

5000字解析:实战化场景下的容器安全攻防之道

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

华为云发布桌面IDE-CodeArts
随机推荐
May the mountains and rivers be safe
Distributed transaction processing scheme big PK
Educoder web exercises - form structure
Pre training weekly (50): no decoding converter, neural prompt search, gradient space dimensionality reduction
【深入理解TcaplusDB技术】TcaplusDB导入数据
百度交易中台之钱包系统架构浅析
###数据库的高可用配置(mysql)
Work content of service governance
How to use search engine?
咨询:微证券是什么证券公司,开户安全吗?
What is Devops in an article?
[course assignment] floating point operation analysis and precision improvement
PHP uses grafika to synthesize pictures and generate poster images
17 commonly used o & M monitoring systems
uva11995
2. data type
『忘了再学』Shell流程控制 — 35、多分支case条件语句
Summary of the latest remote deployment O & M tools
Pingcap was selected as the "voice of customers" of Gartner cloud database in 2022, and won the highest score of "outstanding performer"
Libcef common concepts framework features