当前位置:网站首页>Compose Canvas line chart
Compose Canvas line chart
2022-07-26 07:17:00 【Ango can't move】
package com.anguomob.compose.ui.components
import android.util.Log
import androidx.compose.foundation.Canvas
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.Divider
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.drawscope.Stroke
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.layout.layoutId
import androidx.compose.ui.platform.LocalConfiguration
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.unit.Constraints
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.constraintlayout.compose.ConstraintLayout
import androidx.constraintlayout.compose.ConstraintSet
import androidx.constraintlayout.compose.Dimension
import coil.compose.AsyncImage
import com.anguomob.compose.model.ArticleEntity
import com.anguomob.compose.model.VideoEntity
private const val TAG = "ChartView"
@Composable
fun ChartView(modifier: Modifier = Modifier, points: List<Float>) {
// The height of each line
val heightForRow = 24
// Total number of lines
val countForRow = 5
// Small circle radius
val circleRadius = 2.5
// Canvas width
val canvasWidth = LocalConfiguration.current.screenWidthDp - 8 * 2
// Canvas height
val canvasHeight = heightForRow * countForRow + circleRadius * 2
// no 8 dp representative 1 integral Every line 3 integral
val perY = 8 //24 * 5 /15
// Bisector width
val averageOfWidth = canvasWidth / 7
Canvas(modifier = modifier.size(width = canvasWidth.dp, canvasHeight.dp), onDraw = {
// Background horizontal line
for (index in 0..countForRow) {
val startY = (index * heightForRow.toFloat() + circleRadius).dp.toPx()
val endX = size.width
val endY = startY
drawLine(Color(0xFFEEEEEE),
start = Offset(x = 0f, y = startY),
end = Offset(x = endX, y = endY),
strokeWidth = 2.5f)
}
// Draw a small circle 、 Broken line
for (index in 0 until points.count()) {
Log.e(TAG, "index:${index} ");
val centerX = (averageOfWidth * index + averageOfWidth / 2).dp.toPx()
val centerY =
(heightForRow * countForRow - points[index] * perY + circleRadius).dp.toPx()
val circleCenter = Offset(centerX, centerY)
// spot
drawCircle(Color(0xff149ee7), radius = circleRadius.dp.toPx(),
center = circleCenter,
style = Stroke(width = 5f)
)
// Line
if (index < points.count() - 1) {
val nextPointOffsetX = (averageOfWidth * (index + 1) + averageOfWidth / 2).dp.toPx()
val nextPointOffsetY =
(heightForRow * countForRow - points[(index + 1)] * perY + circleRadius).dp.toPx()
val nextPoint = Offset(nextPointOffsetX, nextPointOffsetY)
drawLine(Color(0xFF149EE7), start = circleCenter, end = nextPoint, strokeWidth = 5f)
}
}
})
}
The data is
var pointsOfWeek by mutableStateOf(listOf(3f, 2f, 6f, 9.3f, 10f, 15f, 8f))
private setThe effect is

The next few weeks are because there is no drawText
therefore
Row() {
taskViewModel.weeks.forEach {
Text(text = it,
color = Color(0xff999999),
fontSize = 12.sp,
modifier = Modifier.weight(1f),
textAlign = TextAlign.Center
)
}
}
val weeks = listOf("02.05", "02.06", "02.07", "02.08", "02.09", "02.10", " Today, ")
边栏推荐
- 3.0.0 alpha 重磅发布!九大新功能、全新 UI 解锁调度系统新能力
- College degree sales career, from the third tier 4K to the first tier 20k+, I am very satisfied with myself
- [QT] how to obtain the number of rows and columns of qtableview and qtablewidget
- Countdown 2 days! Based on the cross development practice of Apache dolphin scheduler & tidb, you can greatly improve your efficiency from writing to scheduling
- 20220725 自动控制原理中的卷积Convolution
- Manifest merger failed with multiple errors, see logs
- Orthogonality of 20220724 trigonometric function system
- Opencv learning warp Perspective
- Airiot IOT platform enables the container industry to build a welding station information monitoring system
- QT: list box, table, tree control
猜你喜欢

The interface automation test with a monthly salary of 12k+ takes you to get started in 3 minutes

Question: can't download sh shellcheck Please install it manually and some commands of shell script

A guide for you to fully use TS

Hands on practice - teach you how to make an intelligent fish tank with STM32

Relevant configurations of pychart: change font style and size, change picture background, and change the font color of console output

Flame diagram analysis Flink backpressure

Compose canvas custom circular progress bar

C51 and MDK coexist keil5 installation tutorial

Apache dolphin scheduler & tidb joint meetup | focus on application development capabilities under the development of open source ecosystem

Drools(2):Drools快速入门
随机推荐
【QT】详解 *.pro、*.pri、*.prf、*.prl文件
Difference between shape and size ()
Opencv learning color detection
配置Flask
How to convert multi row data into multi column data in MySQL
[arm learning (8) AXF tool analysis]
Airiot IOT platform enables the container industry to build a welding station information monitoring system
Compose canvas custom circular progress bar
3.0.0 alpha 重磅发布!九大新功能、全新 UI 解锁调度系统新能力
Talent column | can't use Apache dolphin scheduler? The most complete introductory tutorial written by the boss in one month [3]
NFT数字藏品系统开发:华为发布首款珍藏版数字藏品
How to expand and repartition the C disk?
LTS(Light-Task-Scheduler)
【一库】妙啊!这个库组织npm脚本简直爆炸!
Why can't extern compile variables decorated with const?
达人专栏 | 还不会用 Apache Dolphinscheduler?大佬用时一个月写出的最全入门教程【三】
Leetcode:1898. maximum number of removable characters [if you want to delete some IDX from a pile of things, don't use pop]
Drools(3):Drools基础语法(1)
PG operation and maintenance -- logical backup and physical backup practice
With Huawei cloud welink, you can connect to the world even in the countryside