├── demo
├── .gitignore
├── src
│ ├── main
│ │ ├── res
│ │ │ ├── values
│ │ │ │ ├── strings.xml
│ │ │ │ ├── colors.xml
│ │ │ │ └── themes.xml
│ │ │ ├── mipmap-hdpi
│ │ │ │ ├── ic_launcher.webp
│ │ │ │ └── ic_launcher_round.webp
│ │ │ ├── mipmap-mdpi
│ │ │ │ ├── ic_launcher.webp
│ │ │ │ └── ic_launcher_round.webp
│ │ │ ├── mipmap-xhdpi
│ │ │ │ ├── ic_launcher.webp
│ │ │ │ └── ic_launcher_round.webp
│ │ │ ├── mipmap-xxhdpi
│ │ │ │ ├── ic_launcher.webp
│ │ │ │ └── ic_launcher_round.webp
│ │ │ ├── mipmap-xxxhdpi
│ │ │ │ ├── ic_launcher.webp
│ │ │ │ └── ic_launcher_round.webp
│ │ │ ├── mipmap-anydpi-v26
│ │ │ │ ├── ic_launcher.xml
│ │ │ │ └── ic_launcher_round.xml
│ │ │ ├── layout
│ │ │ │ ├── item_spinner_textview.xml
│ │ │ │ └── activity_main.xml
│ │ │ ├── values-night
│ │ │ │ └── themes.xml
│ │ │ ├── drawable-v24
│ │ │ │ └── ic_launcher_foreground.xml
│ │ │ └── drawable
│ │ │ │ └── ic_launcher_background.xml
│ │ ├── java
│ │ │ └── com
│ │ │ │ └── zhzc0x
│ │ │ │ └── chart
│ │ │ │ └── demo
│ │ │ │ ├── MyApplication.kt
│ │ │ │ └── MainActivity.kt
│ │ └── AndroidManifest.xml
│ ├── test
│ │ └── java
│ │ │ └── com
│ │ │ └── github
│ │ │ └── zicheng
│ │ │ └── chart
│ │ │ └── ExampleUnitTest.kt
│ └── androidTest
│ │ └── java
│ │ └── com
│ │ └── github
│ │ └── zicheng
│ │ └── chart
│ │ └── ExampleInstrumentedTest.kt
├── proguard-rules.pro
└── build.gradle
├── library
├── .gitignore
├── consumer-rules.pro
├── src
│ ├── main
│ │ ├── AndroidManifest.xml
│ │ ├── java
│ │ │ └── com
│ │ │ │ └── zhzc0x
│ │ │ │ └── chart
│ │ │ │ ├── ext
│ │ │ │ └── Extensions.kt
│ │ │ │ ├── LineChartInfo.kt
│ │ │ │ ├── LiveLineChartView.kt
│ │ │ │ └── LineChartView.kt
│ │ └── res
│ │ │ └── values
│ │ │ └── attrs.xml
│ ├── test
│ │ └── java
│ │ │ └── com
│ │ │ └── github
│ │ │ └── zicheng
│ │ │ └── chart
│ │ │ └── ExampleUnitTest.kt
│ └── androidTest
│ │ └── java
│ │ └── com
│ │ └── github
│ │ └── zicheng
│ │ └── chart
│ │ └── ExampleInstrumentedTest.kt
├── proguard-rules.pro
└── build.gradle
├── demo.gif
├── gradle
└── wrapper
│ ├── gradle-wrapper.jar
│ └── gradle-wrapper.properties
├── settings.gradle
├── .gitignore
├── gradle.properties
├── gradlew.bat
├── gradlew
├── LICENSE
└── README.md
/demo/.gitignore:
--------------------------------------------------------------------------------
1 | /build
--------------------------------------------------------------------------------
/library/.gitignore:
--------------------------------------------------------------------------------
1 | /build
--------------------------------------------------------------------------------
/library/consumer-rules.pro:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/demo.gif:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zhzc0x/linechart-android/HEAD/demo.gif
--------------------------------------------------------------------------------
/demo/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | LineChartView-Android
3 |
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zhzc0x/linechart-android/HEAD/gradle/wrapper/gradle-wrapper.jar
--------------------------------------------------------------------------------
/demo/src/main/res/mipmap-hdpi/ic_launcher.webp:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zhzc0x/linechart-android/HEAD/demo/src/main/res/mipmap-hdpi/ic_launcher.webp
--------------------------------------------------------------------------------
/demo/src/main/res/mipmap-mdpi/ic_launcher.webp:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zhzc0x/linechart-android/HEAD/demo/src/main/res/mipmap-mdpi/ic_launcher.webp
--------------------------------------------------------------------------------
/demo/src/main/res/mipmap-xhdpi/ic_launcher.webp:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zhzc0x/linechart-android/HEAD/demo/src/main/res/mipmap-xhdpi/ic_launcher.webp
--------------------------------------------------------------------------------
/demo/src/main/res/mipmap-xxhdpi/ic_launcher.webp:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zhzc0x/linechart-android/HEAD/demo/src/main/res/mipmap-xxhdpi/ic_launcher.webp
--------------------------------------------------------------------------------
/demo/src/main/res/mipmap-xxxhdpi/ic_launcher.webp:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zhzc0x/linechart-android/HEAD/demo/src/main/res/mipmap-xxxhdpi/ic_launcher.webp
--------------------------------------------------------------------------------
/demo/src/main/res/mipmap-hdpi/ic_launcher_round.webp:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zhzc0x/linechart-android/HEAD/demo/src/main/res/mipmap-hdpi/ic_launcher_round.webp
--------------------------------------------------------------------------------
/demo/src/main/res/mipmap-mdpi/ic_launcher_round.webp:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zhzc0x/linechart-android/HEAD/demo/src/main/res/mipmap-mdpi/ic_launcher_round.webp
--------------------------------------------------------------------------------
/demo/src/main/res/mipmap-xhdpi/ic_launcher_round.webp:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zhzc0x/linechart-android/HEAD/demo/src/main/res/mipmap-xhdpi/ic_launcher_round.webp
--------------------------------------------------------------------------------
/demo/src/main/res/mipmap-xxhdpi/ic_launcher_round.webp:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zhzc0x/linechart-android/HEAD/demo/src/main/res/mipmap-xxhdpi/ic_launcher_round.webp
--------------------------------------------------------------------------------
/demo/src/main/res/mipmap-xxxhdpi/ic_launcher_round.webp:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zhzc0x/linechart-android/HEAD/demo/src/main/res/mipmap-xxxhdpi/ic_launcher_round.webp
--------------------------------------------------------------------------------
/library/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.properties:
--------------------------------------------------------------------------------
1 | #Wed Feb 23 14:06:18 CST 2022
2 | distributionBase=GRADLE_USER_HOME
3 | distributionUrl=https\://services.gradle.org/distributions/gradle-7.3.3-bin.zip
4 | distributionPath=wrapper/dists
5 | zipStorePath=wrapper/dists
6 | zipStoreBase=GRADLE_USER_HOME
7 |
--------------------------------------------------------------------------------
/demo/src/main/res/mipmap-anydpi-v26/ic_launcher.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
--------------------------------------------------------------------------------
/demo/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
--------------------------------------------------------------------------------
/demo/src/main/res/values/colors.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | #FFBB86FC
4 | #FF6200EE
5 | #FF3700B3
6 | #FF03DAC5
7 | #FF018786
8 | #FF000000
9 | #FFFFFFFF
10 |
--------------------------------------------------------------------------------
/settings.gradle:
--------------------------------------------------------------------------------
1 | pluginManagement {
2 | repositories {
3 | gradlePluginPortal()
4 | google()
5 | mavenCentral()
6 | }
7 | }
8 | dependencyResolutionManagement {
9 | repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
10 | repositories {
11 | google()
12 | mavenCentral()
13 | }
14 | }
15 | rootProject.name = "LineChartView-Android"
16 | include ':demo'
17 | include ':library'
18 |
--------------------------------------------------------------------------------
/demo/src/test/java/com/github/zicheng/chart/ExampleUnitTest.kt:
--------------------------------------------------------------------------------
1 | package com.github.zicheng.chart
2 |
3 | import org.junit.Test
4 |
5 | import org.junit.Assert.*
6 |
7 | /**
8 | * Example local unit test, which will execute on the development machine (host).
9 | *
10 | * See [testing documentation](http://d.android.com/tools/testing).
11 | */
12 | class ExampleUnitTest {
13 | @Test
14 | fun addition_isCorrect() {
15 | assertEquals(4, 2 + 2)
16 | }
17 | }
--------------------------------------------------------------------------------
/library/src/test/java/com/github/zicheng/chart/ExampleUnitTest.kt:
--------------------------------------------------------------------------------
1 | package com.github.zicheng.chart
2 |
3 | import org.junit.Test
4 |
5 | import org.junit.Assert.*
6 |
7 | /**
8 | * Example local unit test, which will execute on the development machine (host).
9 | *
10 | * See [testing documentation](http://d.android.com/tools/testing).
11 | */
12 | class ExampleUnitTest {
13 | @Test
14 | fun addition_isCorrect() {
15 | assertEquals(4, 2 + 2)
16 | }
17 | }
--------------------------------------------------------------------------------
/demo/src/main/res/layout/item_spinner_textview.xml:
--------------------------------------------------------------------------------
1 |
2 |
--------------------------------------------------------------------------------
/demo/src/main/java/com/zhzc0x/chart/demo/MyApplication.kt:
--------------------------------------------------------------------------------
1 | package com.zhzc0x.chart.demo
2 |
3 | import android.app.Application
4 | import android.content.res.Resources
5 | import android.util.TypedValue
6 |
7 | class MyApplication: Application() {
8 |
9 | override fun onCreate() {
10 | super.onCreate()
11 | }
12 | }
13 |
14 | internal val Float.dp
15 | get() = TypedValue.applyDimension(
16 | TypedValue.COMPLEX_UNIT_DIP,
17 | this,
18 | Resources.getSystem().displayMetrics
19 | )
20 |
--------------------------------------------------------------------------------
/demo/src/androidTest/java/com/github/zicheng/chart/ExampleInstrumentedTest.kt:
--------------------------------------------------------------------------------
1 | package com.github.zicheng.chart
2 |
3 | import androidx.test.platform.app.InstrumentationRegistry
4 | import androidx.test.ext.junit.runners.AndroidJUnit4
5 |
6 | import org.junit.Test
7 | import org.junit.runner.RunWith
8 |
9 | import org.junit.Assert.*
10 |
11 | /**
12 | * Instrumented test, which will execute on an Android device.
13 | *
14 | * See [testing documentation](http://d.android.com/tools/testing).
15 | */
16 | @RunWith(AndroidJUnit4::class)
17 | class ExampleInstrumentedTest {
18 | @Test
19 | fun useAppContext() {
20 | // Context of the app under test.
21 | val appContext = InstrumentationRegistry.getInstrumentation().targetContext
22 | assertEquals("com.github.zicheng.chart", appContext.packageName)
23 | }
24 | }
--------------------------------------------------------------------------------
/demo/proguard-rules.pro:
--------------------------------------------------------------------------------
1 | # Add project specific ProGuard rules here.
2 | # You can control the set of applied configuration files using the
3 | # proguardFiles setting in build.gradle.
4 | #
5 | # For more details, see
6 | # http://developer.android.com/guide/developing/tools/proguard.html
7 |
8 | # If your project uses WebView with JS, uncomment the following
9 | # and specify the fully qualified class name to the JavaScript interface
10 | # class:
11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
12 | # public *;
13 | #}
14 |
15 | # Uncomment this to preserve the line number information for
16 | # debugging stack traces.
17 | #-keepattributes SourceFile,LineNumberTable
18 |
19 | # If you keep the line number information, uncomment this to
20 | # hide the original source file name.
21 | #-renamesourcefileattribute SourceFile
--------------------------------------------------------------------------------
/library/proguard-rules.pro:
--------------------------------------------------------------------------------
1 | # Add project specific ProGuard rules here.
2 | # You can control the set of applied configuration files using the
3 | # proguardFiles setting in build.gradle.
4 | #
5 | # For more details, see
6 | # http://developer.android.com/guide/developing/tools/proguard.html
7 |
8 | # If your project uses WebView with JS, uncomment the following
9 | # and specify the fully qualified class name to the JavaScript interface
10 | # class:
11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
12 | # public *;
13 | #}
14 |
15 | # Uncomment this to preserve the line number information for
16 | # debugging stack traces.
17 | #-keepattributes SourceFile,LineNumberTable
18 |
19 | # If you keep the line number information, uncomment this to
20 | # hide the original source file name.
21 | #-renamesourcefileattribute SourceFile
--------------------------------------------------------------------------------
/library/src/androidTest/java/com/github/zicheng/chart/ExampleInstrumentedTest.kt:
--------------------------------------------------------------------------------
1 | package com.github.zicheng.chart
2 |
3 | import androidx.test.platform.app.InstrumentationRegistry
4 | import androidx.test.ext.junit.runners.AndroidJUnit4
5 |
6 | import org.junit.Test
7 | import org.junit.runner.RunWith
8 |
9 | import org.junit.Assert.*
10 |
11 | /**
12 | * Instrumented test, which will execute on an Android device.
13 | *
14 | * See [testing documentation](http://d.android.com/tools/testing).
15 | */
16 | @RunWith(AndroidJUnit4::class)
17 | class ExampleInstrumentedTest {
18 | @Test
19 | fun useAppContext() {
20 | // Context of the app under test.
21 | val appContext = InstrumentationRegistry.getInstrumentation().targetContext
22 | assertEquals("com.github.zicheng.chart.test", appContext.packageName)
23 | }
24 | }
--------------------------------------------------------------------------------
/library/src/main/java/com/zhzc0x/chart/ext/Extensions.kt:
--------------------------------------------------------------------------------
1 | package com.zhzc0x.chart.ext
2 |
3 | import android.content.res.Resources
4 | import android.util.DisplayMetrics
5 | import android.util.TypedValue
6 | import java.math.BigDecimal
7 | import java.math.RoundingMode
8 |
9 | internal val displayMetrics: DisplayMetrics = Resources.getSystem().displayMetrics
10 |
11 | internal val Float.dp
12 | get() = TypedValue.applyDimension(
13 | TypedValue.COMPLEX_UNIT_DIP,
14 | this,
15 | displayMetrics
16 | )
17 |
18 | internal val Int.dp
19 | get() = TypedValue.applyDimension(
20 | TypedValue.COMPLEX_UNIT_DIP,
21 | this.toFloat(),
22 | displayMetrics
23 | ).toInt()
24 |
25 | internal fun Float.scale(scale: Int): Float {
26 | return BigDecimal(this.toDouble()).setScale(scale, RoundingMode.HALF_UP).toFloat()
27 | }
28 |
29 |
30 |
--------------------------------------------------------------------------------
/demo/src/main/res/values/themes.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
16 |
--------------------------------------------------------------------------------
/demo/src/main/res/values-night/themes.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
16 |
--------------------------------------------------------------------------------
/demo/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
13 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
--------------------------------------------------------------------------------
/library/src/main/java/com/zhzc0x/chart/LineChartInfo.kt:
--------------------------------------------------------------------------------
1 | package com.zhzc0x.chart
2 |
3 | import androidx.annotation.ColorInt
4 |
5 | var debugLineChart = false
6 |
7 | enum class AmplitudeMode {
8 | FIXED, // 固定模式
9 | MAX_NEGATE, // 自动模式:实时计算最大幅值,最小幅值=最大幅值取反
10 | MAX_MIN // 自动模式:实时计算最大、最小幅值
11 | }
12 |
13 | internal enum class TextAlign {
14 | LEFT, CENTER, RIGHT
15 | }
16 |
17 | data class PointInfo(val x: Float, val y: Float)
18 |
19 | data class AxisInfo(val value: Float, val showText: String = value.toString())
20 |
21 | data class ShowPointInfo(
22 | val x: Float,
23 | val y: Float,
24 | val radius: Float,//px
25 | @ColorInt val color: Int,
26 | val strokeWidth: Float,//px
27 | @ColorInt val strokeColor: Int,
28 | val text: String,
29 | val textSize: Float,//px
30 | @ColorInt val textColor: Int,
31 | val textPadding: Float//px
32 | )
33 |
34 | data class WindowDuration(
35 | val duration: Int, // 窗口时长 ms
36 | val samplingRate: Int // 每秒采样点
37 | )
38 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | # Built application files
2 | *.apk
3 | *.aar
4 | *.ap_
5 | *.aab
6 |
7 | # Files for the ART/Dalvik VM
8 | *.dex
9 |
10 | # Java class files
11 | *.class
12 |
13 | # Generated files
14 | bin/
15 | gen/
16 | out/
17 | # Uncomment the following line in case you need and you don't have the release build type files in your app
18 | # release/
19 |
20 | # Gradle files
21 | .gradle/
22 | build/
23 |
24 | # Local configuration file (sdk path, etc)
25 | local.properties
26 |
27 | # Proguard folder generated by Eclipse
28 | proguard/
29 |
30 | # Log Files
31 | *.log
32 |
33 | # Android Studio Navigation editor temp files
34 | .navigation/
35 |
36 | # Android Studio captures folder
37 | captures/
38 |
39 | # IntelliJ
40 | *.iml
41 | .idea/
42 |
43 | # Keystore files
44 | # Uncomment the following lines if you do not want to check your keystore files in.
45 | #*.jks
46 | #*.keystore
47 |
48 | # External native build folder generated in Android Studio 2.2 and later
49 | .externalNativeBuild
50 | .cxx/
51 |
52 | # Google Services (e.g. APIs or Firebase)
53 | # google-services.json
54 |
55 | # Freeline
56 | freeline.py
57 | freeline/
58 | freeline_project_description.json
59 |
60 | # fastlane
61 | fastlane/report.xml
62 | fastlane/Preview.html
63 | fastlane/screenshots
64 | fastlane/test_output
65 | fastlane/readme.md
66 |
67 | # Version control
68 | vcs.xml
69 |
70 | # lint
71 | lint/intermediates/
72 | lint/generated/
73 | lint/outputs/
74 | lint/tmp/
75 | # lint/reports/
76 |
--------------------------------------------------------------------------------
/demo/build.gradle:
--------------------------------------------------------------------------------
1 | plugins {
2 | id 'com.android.application'
3 | id 'org.jetbrains.kotlin.android'
4 | }
5 |
6 | android {
7 | compileSdk 33
8 |
9 | defaultConfig {
10 | applicationId "com.zhzc0x.chart.demo"
11 | minSdk 21
12 | targetSdk 33
13 | versionCode 1
14 | versionName "1.0"
15 |
16 | testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
17 | }
18 |
19 | buildTypes {
20 | release {
21 | minifyEnabled false
22 | proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
23 | }
24 | }
25 | compileOptions {
26 | sourceCompatibility JavaVersion.VERSION_1_8
27 | targetCompatibility JavaVersion.VERSION_1_8
28 | }
29 | kotlinOptions {
30 | jvmTarget = '1.8'
31 | }
32 | buildFeatures{
33 | viewBinding true
34 | buildConfig true
35 | }
36 | }
37 |
38 | dependencies {
39 |
40 | implementation 'androidx.core:core-ktx:1.10.1'
41 | implementation 'androidx.appcompat:appcompat:1.6.1'
42 | implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
43 | implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.6.1'
44 | implementation 'com.google.android.material:material:1.9.0'
45 | testImplementation 'junit:junit:4.13.2'
46 | androidTestImplementation 'androidx.test.ext:junit:1.1.5'
47 | androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
48 | implementation project(':library')
49 | }
--------------------------------------------------------------------------------
/gradle.properties:
--------------------------------------------------------------------------------
1 | # Project-wide Gradle settings.
2 | # IDE (e.g. Android Studio) users:
3 | # Gradle settings configured through the IDE *will override*
4 | # any settings specified in this file.
5 | # For more details on how to configure your build environment visit
6 | # http://www.gradle.org/docs/current/userguide/build_environment.html
7 | # Specifies the JVM arguments used for the daemon process.
8 | # The setting is particularly useful for tweaking memory settings.
9 | org.gradle.jvmargs=-Xmx2048m -Dfile.encoding=UTF-8
10 | # When configured, Gradle will run in incubating parallel mode.
11 | # This option should only be used with decoupled projects. More details, visit
12 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
13 | # org.gradle.parallel=true
14 | # AndroidX package structure to make it clearer which packages are bundled with the
15 | # Android operating system, and which are packaged with your app"s APK
16 | # https://developer.android.com/topic/libraries/support-library/androidx-rn
17 | android.useAndroidX=true
18 | # Kotlin code style for this project: "official" or "obsolete":
19 | kotlin.code.style=official
20 | # Enables namespacing of each library's R class so that its R class includes only the
21 | # resources declared in the library itself and none from the library's dependencies,
22 | # thereby reducing the size of the R class for that library
23 | android.nonTransitiveRClass=true
24 |
25 | groupId=com.github.zhzc0x
26 | artifactId=linechart-android
27 | versionCode=6
28 | versionName=1.0.6
--------------------------------------------------------------------------------
/library/build.gradle:
--------------------------------------------------------------------------------
1 | plugins {
2 | id 'com.android.library'
3 | id 'org.jetbrains.kotlin.android'
4 | id 'maven-publish'
5 | }
6 | group = project.groupId
7 | version = project.versionName
8 |
9 | android {
10 | compileSdk 33
11 |
12 | defaultConfig {
13 | minSdk 21
14 | targetSdk 33
15 |
16 | testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
17 | consumerProguardFiles "consumer-rules.pro"
18 | }
19 |
20 | buildTypes {
21 | release {
22 | minifyEnabled false
23 | proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
24 | }
25 | }
26 | compileOptions {
27 | sourceCompatibility JavaVersion.VERSION_1_8
28 | targetCompatibility JavaVersion.VERSION_1_8
29 | }
30 | kotlinOptions {
31 | jvmTarget = '1.8'
32 | }
33 | }
34 |
35 | dependencies {
36 |
37 | implementation 'androidx.core:core-ktx:1.10.1'
38 | testImplementation 'junit:junit:4.13.2'
39 | androidTestImplementation 'androidx.test.ext:junit:1.1.5'
40 | androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
41 | }
42 |
43 | afterEvaluate {
44 | publishing {
45 | publications {
46 | // Creates a Maven publication called "release".
47 | release(MavenPublication) {
48 | from components.release
49 | groupId = project.groupId
50 | artifactId = project.artifactId
51 | version = project.versionName
52 | }
53 | }
54 | }
55 | }
--------------------------------------------------------------------------------
/demo/src/main/res/drawable-v24/ic_launcher_foreground.xml:
--------------------------------------------------------------------------------
1 |
7 |
8 |
9 |
15 |
18 |
21 |
22 |
23 |
24 |
30 |
--------------------------------------------------------------------------------
/gradlew.bat:
--------------------------------------------------------------------------------
1 | @rem
2 | @rem Copyright 2015 the original author or authors.
3 | @rem
4 | @rem Licensed under the Apache License, Version 2.0 (the "License");
5 | @rem you may not use this file except in compliance with the License.
6 | @rem You may obtain a copy of the License at
7 | @rem
8 | @rem https://www.apache.org/licenses/LICENSE-2.0
9 | @rem
10 | @rem Unless required by applicable law or agreed to in writing, software
11 | @rem distributed under the License is distributed on an "AS IS" BASIS,
12 | @rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | @rem See the License for the specific language governing permissions and
14 | @rem limitations under the License.
15 | @rem
16 |
17 | @if "%DEBUG%" == "" @echo off
18 | @rem ##########################################################################
19 | @rem
20 | @rem Gradle startup script for Windows
21 | @rem
22 | @rem ##########################################################################
23 |
24 | @rem Set local scope for the variables with windows NT shell
25 | if "%OS%"=="Windows_NT" setlocal
26 |
27 | set DIRNAME=%~dp0
28 | if "%DIRNAME%" == "" set DIRNAME=.
29 | set APP_BASE_NAME=%~n0
30 | set APP_HOME=%DIRNAME%
31 |
32 | @rem Resolve any "." and ".." in APP_HOME to make it shorter.
33 | for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi
34 |
35 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
36 | set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m"
37 |
38 | @rem Find java.exe
39 | if defined JAVA_HOME goto findJavaFromJavaHome
40 |
41 | set JAVA_EXE=java.exe
42 | %JAVA_EXE% -version >NUL 2>&1
43 | if "%ERRORLEVEL%" == "0" goto execute
44 |
45 | echo.
46 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
47 | echo.
48 | echo Please set the JAVA_HOME variable in your environment to match the
49 | echo location of your Java installation.
50 |
51 | goto fail
52 |
53 | :findJavaFromJavaHome
54 | set JAVA_HOME=%JAVA_HOME:"=%
55 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe
56 |
57 | if exist "%JAVA_EXE%" goto execute
58 |
59 | echo.
60 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
61 | echo.
62 | echo Please set the JAVA_HOME variable in your environment to match the
63 | echo location of your Java installation.
64 |
65 | goto fail
66 |
67 | :execute
68 | @rem Setup the command line
69 |
70 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
71 |
72 |
73 | @rem Execute Gradle
74 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %*
75 |
76 | :end
77 | @rem End local scope for the variables with windows NT shell
78 | if "%ERRORLEVEL%"=="0" goto mainEnd
79 |
80 | :fail
81 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
82 | rem the _cmd.exe /c_ return code!
83 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
84 | exit /b 1
85 |
86 | :mainEnd
87 | if "%OS%"=="Windows_NT" endlocal
88 |
89 | :omega
90 |
--------------------------------------------------------------------------------
/demo/src/main/res/drawable/ic_launcher_background.xml:
--------------------------------------------------------------------------------
1 |
2 |
7 |
10 |
15 |
20 |
25 |
30 |
35 |
40 |
45 |
50 |
55 |
60 |
65 |
70 |
75 |
80 |
85 |
90 |
95 |
100 |
105 |
110 |
115 |
120 |
125 |
130 |
135 |
140 |
145 |
150 |
155 |
160 |
165 |
170 |
171 |
--------------------------------------------------------------------------------
/gradlew:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env sh
2 |
3 | #
4 | # Copyright 2015 the original author or authors.
5 | #
6 | # Licensed under the Apache License, Version 2.0 (the "License");
7 | # you may not use this file except in compliance with the License.
8 | # You may obtain a copy of the License at
9 | #
10 | # https://www.apache.org/licenses/LICENSE-2.0
11 | #
12 | # Unless required by applicable law or agreed to in writing, software
13 | # distributed under the License is distributed on an "AS IS" BASIS,
14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 | # See the License for the specific language governing permissions and
16 | # limitations under the License.
17 | #
18 |
19 | ##############################################################################
20 | ##
21 | ## Gradle start up script for UN*X
22 | ##
23 | ##############################################################################
24 |
25 | # Attempt to set APP_HOME
26 | # Resolve links: $0 may be a link
27 | PRG="$0"
28 | # Need this for relative symlinks.
29 | while [ -h "$PRG" ] ; do
30 | ls=`ls -ld "$PRG"`
31 | link=`expr "$ls" : '.*-> \(.*\)$'`
32 | if expr "$link" : '/.*' > /dev/null; then
33 | PRG="$link"
34 | else
35 | PRG=`dirname "$PRG"`"/$link"
36 | fi
37 | done
38 | SAVED="`pwd`"
39 | cd "`dirname \"$PRG\"`/" >/dev/null
40 | APP_HOME="`pwd -P`"
41 | cd "$SAVED" >/dev/null
42 |
43 | APP_NAME="Gradle"
44 | APP_BASE_NAME=`basename "$0"`
45 |
46 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
47 | DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"'
48 |
49 | # Use the maximum available, or set MAX_FD != -1 to use that value.
50 | MAX_FD="maximum"
51 |
52 | warn () {
53 | echo "$*"
54 | }
55 |
56 | die () {
57 | echo
58 | echo "$*"
59 | echo
60 | exit 1
61 | }
62 |
63 | # OS specific support (must be 'true' or 'false').
64 | cygwin=false
65 | msys=false
66 | darwin=false
67 | nonstop=false
68 | case "`uname`" in
69 | CYGWIN* )
70 | cygwin=true
71 | ;;
72 | Darwin* )
73 | darwin=true
74 | ;;
75 | MINGW* )
76 | msys=true
77 | ;;
78 | NONSTOP* )
79 | nonstop=true
80 | ;;
81 | esac
82 |
83 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
84 |
85 |
86 | # Determine the Java command to use to start the JVM.
87 | if [ -n "$JAVA_HOME" ] ; then
88 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
89 | # IBM's JDK on AIX uses strange locations for the executables
90 | JAVACMD="$JAVA_HOME/jre/sh/java"
91 | else
92 | JAVACMD="$JAVA_HOME/bin/java"
93 | fi
94 | if [ ! -x "$JAVACMD" ] ; then
95 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
96 |
97 | Please set the JAVA_HOME variable in your environment to match the
98 | location of your Java installation."
99 | fi
100 | else
101 | JAVACMD="java"
102 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
103 |
104 | Please set the JAVA_HOME variable in your environment to match the
105 | location of your Java installation."
106 | fi
107 |
108 | # Increase the maximum file descriptors if we can.
109 | if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then
110 | MAX_FD_LIMIT=`ulimit -H -n`
111 | if [ $? -eq 0 ] ; then
112 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
113 | MAX_FD="$MAX_FD_LIMIT"
114 | fi
115 | ulimit -n $MAX_FD
116 | if [ $? -ne 0 ] ; then
117 | warn "Could not set maximum file descriptor limit: $MAX_FD"
118 | fi
119 | else
120 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
121 | fi
122 | fi
123 |
124 | # For Darwin, add options to specify how the application appears in the dock
125 | if $darwin; then
126 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
127 | fi
128 |
129 | # For Cygwin or MSYS, switch paths to Windows format before running java
130 | if [ "$cygwin" = "true" -o "$msys" = "true" ] ; then
131 | APP_HOME=`cygpath --path --mixed "$APP_HOME"`
132 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
133 |
134 | JAVACMD=`cygpath --unix "$JAVACMD"`
135 |
136 | # We build the pattern for arguments to be converted via cygpath
137 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
138 | SEP=""
139 | for dir in $ROOTDIRSRAW ; do
140 | ROOTDIRS="$ROOTDIRS$SEP$dir"
141 | SEP="|"
142 | done
143 | OURCYGPATTERN="(^($ROOTDIRS))"
144 | # Add a user-defined pattern to the cygpath arguments
145 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then
146 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)"
147 | fi
148 | # Now convert the arguments - kludge to limit ourselves to /bin/sh
149 | i=0
150 | for arg in "$@" ; do
151 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -`
152 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option
153 |
154 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition
155 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"`
156 | else
157 | eval `echo args$i`="\"$arg\""
158 | fi
159 | i=`expr $i + 1`
160 | done
161 | case $i in
162 | 0) set -- ;;
163 | 1) set -- "$args0" ;;
164 | 2) set -- "$args0" "$args1" ;;
165 | 3) set -- "$args0" "$args1" "$args2" ;;
166 | 4) set -- "$args0" "$args1" "$args2" "$args3" ;;
167 | 5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
168 | 6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
169 | 7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
170 | 8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
171 | 9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
172 | esac
173 | fi
174 |
175 | # Escape application args
176 | save () {
177 | for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done
178 | echo " "
179 | }
180 | APP_ARGS=`save "$@"`
181 |
182 | # Collect all arguments for the java command, following the shell quoting and substitution rules
183 | eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS"
184 |
185 | exec "$JAVACMD" "$@"
186 |
--------------------------------------------------------------------------------
/library/src/main/res/values/attrs.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 |
48 |
49 |
50 |
51 |
52 |
53 |
54 |
55 |
56 |
57 |
58 |
59 |
60 |
61 |
62 |
63 |
64 |
65 |
66 |
67 |
68 |
69 |
70 |
71 |
72 |
73 |
74 |
75 |
76 |
77 |
78 |
79 |
80 |
81 |
82 |
83 |
84 |
85 |
86 |
87 |
88 |
89 |
90 |
91 |
92 |
93 |
94 |
95 |
96 |
97 |
98 |
99 |
100 |
101 |
102 |
103 |
104 |
105 |
106 |
107 |
108 |
109 |
110 |
111 |
112 |
113 |
114 |
115 |
116 |
117 |
118 |
119 |
120 |
121 |
122 |
123 |
124 |
125 |
126 |
127 |
128 |
129 |
130 |
131 |
132 |
133 |
134 |
135 |
136 |
137 |
138 |
139 |
140 |
141 |
142 |
143 |
144 |
145 |
146 |
147 |
148 |
149 |
150 |
151 |
152 |
153 |
154 |
155 |
156 |
157 |
158 |
159 |
160 |
161 |
162 |
163 |
164 |
165 |
166 |
167 |
168 |
169 |
170 |
171 |
172 |
173 |
174 |
175 |
176 |
177 |
178 |
179 |
180 |
181 |
182 |
183 |
184 |
185 |
186 |
187 |
188 |
189 |
190 |
191 |
192 |
193 |
194 |
195 |
196 |
197 |
198 |
--------------------------------------------------------------------------------
/demo/src/main/res/layout/activity_main.xml:
--------------------------------------------------------------------------------
1 |
2 |
9 |
10 |
14 |
15 |
45 |
46 |
53 |
54 |
59 |
60 |
66 |
67 |
68 |
69 |
76 |
77 |
82 |
83 |
89 |
90 |
96 |
97 |
98 |
99 |
106 |
107 |
112 |
113 |
119 |
120 |
121 |
122 |
128 |
129 |
139 |
140 |
147 |
148 |
153 |
154 |
160 |
161 |
162 |
163 |
170 |
171 |
176 |
177 |
183 |
184 |
185 |
186 |
193 |
194 |
199 |
200 |
206 |
207 |
208 |
209 |
216 |
217 |
222 |
223 |
229 |
230 |
235 |
236 |
242 |
243 |
244 |
245 |
246 |
247 |
--------------------------------------------------------------------------------
/demo/src/main/java/com/zhzc0x/chart/demo/MainActivity.kt:
--------------------------------------------------------------------------------
1 | package com.zhzc0x.chart.demo
2 |
3 | import android.graphics.Color
4 | import androidx.appcompat.app.AppCompatActivity
5 | import android.os.Bundle
6 | import android.view.View
7 | import android.widget.AdapterView
8 | import android.widget.ArrayAdapter
9 | import androidx.lifecycle.lifecycleScope
10 | import com.zhzc0x.chart.AmplitudeMode
11 | import com.zhzc0x.chart.AxisInfo
12 | import com.zhzc0x.chart.PointInfo
13 | import com.zhzc0x.chart.ShowPointInfo
14 | import com.zhzc0x.chart.WindowDuration
15 | import com.zhzc0x.chart.demo.databinding.ActivityMainBinding
16 | import kotlinx.coroutines.delay
17 | import kotlinx.coroutines.launch
18 |
19 | class MainActivity : AppCompatActivity() {
20 |
21 | private lateinit var binding: ActivityMainBinding
22 | private val pointXInitValueList = listOf(0, 12, 24)
23 | private var pointXStartDp = 0
24 | private var pointXEndDp = 0
25 |
26 | override fun onCreate(savedInstanceState: Bundle?) {
27 | super.onCreate(savedInstanceState)
28 | binding = ActivityMainBinding.inflate(layoutInflater)
29 | setContentView(binding.root)
30 | binding.lineChartView.setLimitArray(listOf(-50f, 0f, 50f, 100f))
31 | val pointList = ArrayList()
32 | (1..20).forEach { i ->
33 | pointList.add(PointInfo(i.toFloat(), (-100..100).random().toFloat()))
34 | }
35 | val xAxisList = pointList.map { pointInfo ->
36 | AxisInfo(pointInfo.x, pointInfo.x.toInt().toString())
37 | }
38 | binding.lineChartView.setData(
39 | pointList, xAxisList = xAxisList, yAxisList = listOf(
40 | AxisInfo(-100f, "-100"),
41 | AxisInfo(-50f, "-50"),
42 | AxisInfo(0f, "0"),
43 | AxisInfo(50f, "50"),
44 | AxisInfo(100f, "100")
45 | ), pointSpace = 60f
46 | )
47 | binding.drawTypeSpinner.adapter = ArrayAdapter(
48 | this, R.layout.item_spinner_textview,
49 | listOf("折线", "曲线")
50 | )
51 | binding.drawTypeSpinner.onItemSelectedListener =
52 | object : AdapterView.OnItemSelectedListener {
53 | override fun onItemSelected(
54 | parent: AdapterView<*>?, view: View?,
55 | position: Int, id: Long
56 | ) {
57 | binding.lineChartView.setDrawCurve(position == 1)
58 | }
59 |
60 | override fun onNothingSelected(parent: AdapterView<*>?) {
61 | }
62 | }
63 |
64 | binding.pointXStartSpinner.adapter = ArrayAdapter(this, R.layout.item_spinner_textview,
65 | pointXInitValueList.map { "${it}dp" })
66 | binding.pointXStartSpinner.onItemSelectedListener =
67 | object : AdapterView.OnItemSelectedListener {
68 | override fun onItemSelected(
69 | parent: AdapterView<*>?, view: View?,
70 | position: Int, id: Long
71 | ) {
72 | pointXStartDp = pointXInitValueList[position]
73 | binding.lineChartView.setPointXInit(pointXStartDp, pointXEndDp)
74 | }
75 |
76 | override fun onNothingSelected(parent: AdapterView<*>?) {
77 | }
78 | }
79 | binding.pointXEndSpinner.adapter = ArrayAdapter(this, R.layout.item_spinner_textview,
80 | pointXInitValueList.map { "${it}dp" })
81 | binding.pointXEndSpinner.onItemSelectedListener =
82 | object : AdapterView.OnItemSelectedListener {
83 | override fun onItemSelected(
84 | parent: AdapterView<*>?, view: View?,
85 | position: Int, id: Long
86 | ) {
87 | pointXEndDp = pointXInitValueList[position]
88 | binding.lineChartView.setPointXInit(pointXStartDp, pointXEndDp)
89 | }
90 |
91 | override fun onNothingSelected(parent: AdapterView<*>?) {
92 | }
93 | }
94 |
95 | binding.cbShowChartPoint.setOnCheckedChangeListener { _, checked ->
96 | binding.lineChartView.setShowLineChartPoint(checked)
97 | }
98 |
99 | val showPointList = listOf(
100 | ShowPointInfo(
101 | pointList[2].x, pointList[2].y, 9f, Color.WHITE, 3.5f,
102 | Color.RED, pointList[2].y.toString(), 32f, Color.RED, 12f
103 | ),
104 | ShowPointInfo(
105 | pointList[6].x, pointList[6].y, 9f, Color.WHITE, 3.5f,
106 | Color.BLUE, pointList[6].y.toString(), 32f, Color.BLUE, 12f
107 | )
108 | )
109 | binding.cbShowPoints.setOnCheckedChangeListener { _, checked ->
110 | if (checked) {
111 | binding.lineChartView.setShowPoints(showPointList)
112 | } else {
113 | binding.lineChartView.setShowPoints(null)
114 | }
115 | }
116 |
117 | binding.btnAnim.setOnClickListener {
118 | binding.lineChartView.showLineChartAnim()
119 | }
120 |
121 | binding.drawTypeSpinner2.adapter = ArrayAdapter(
122 | this, R.layout.item_spinner_textview,
123 | listOf("折线", "曲线")
124 | )
125 | binding.drawTypeSpinner2.onItemSelectedListener =
126 | object : AdapterView.OnItemSelectedListener {
127 | override fun onItemSelected(
128 | parent: AdapterView<*>?, view: View?,
129 | position: Int, id: Long
130 | ) {
131 | binding.liveLineChartView.setDrawCurve(position == 1)
132 | }
133 |
134 | override fun onNothingSelected(parent: AdapterView<*>?) {
135 | }
136 | }
137 |
138 | var xLimitCount = 0
139 | var yLimitCount = 2
140 | val yAmplitudeRangeList = listOf("100", "0.2", "0.4", "0.6", "0.8", "1", "2", "4", "自动-MAX_NEGATE", "自动-MAX_MIN")
141 | var curAmplitudeRange = 0f
142 | binding.yMaxSpinner.adapter = ArrayAdapter(
143 | this, R.layout.item_spinner_textview,
144 | yAmplitudeRangeList
145 | )
146 | binding.yMaxSpinner.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
147 | override fun onItemSelected(
148 | parent: AdapterView<*>?, view: View?,
149 | position: Int, id: Long
150 | ) {
151 | if (position == yAmplitudeRangeList.size - 1) {
152 | binding.liveLineChartView.setAmplitudeMode(AmplitudeMode.MAX_MIN)
153 | } else if (position == yAmplitudeRangeList.size - 2) {
154 | binding.liveLineChartView.setAmplitudeMode(AmplitudeMode.MAX_NEGATE)
155 | } else {
156 | binding.liveLineChartView.setAmplitudeMode(AmplitudeMode.FIXED)
157 | curAmplitudeRange = yAmplitudeRangeList[position].toFloat()
158 | updateYAxisInfos(curAmplitudeRange, yLimitCount)
159 | }
160 | }
161 |
162 | override fun onNothingSelected(parent: AdapterView<*>?) {
163 | }
164 | }
165 |
166 | val pointSpaceList =
167 | listOf("", "1dp", "1.2dp", "1.4dp", "1.6dp", "1.8dp", "2dp", "3dp", "4dp", "5dp")
168 | binding.pointSpaceSpinner.adapter = ArrayAdapter(
169 | this, R.layout.item_spinner_textview,
170 | pointSpaceList
171 | )
172 | binding.pointSpaceSpinner.onItemSelectedListener =
173 | object : AdapterView.OnItemSelectedListener {
174 | override fun onItemSelected(
175 | parent: AdapterView<*>,
176 | view: View,
177 | position: Int,
178 | id: Long
179 | ) {
180 | val pointSpaceStr = pointSpaceList[position]
181 | if (pointSpaceStr.isNotEmpty()) {
182 | val pointSpace = pointSpaceList[position].replace("dp", "").toFloat().dp
183 | binding.liveLineChartView.setPointSpace(pointSpace)
184 | }
185 | }
186 |
187 | override fun onNothingSelected(parent: AdapterView<*>?) {
188 | }
189 | }
190 |
191 | val xLimitLineCountList = listOf("0", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12")
192 | binding.xLimitCountSpinner.adapter = ArrayAdapter(
193 | this, R.layout.item_spinner_textview,
194 | xLimitLineCountList
195 | )
196 | binding.xLimitCountSpinner.onItemSelectedListener =
197 | object : AdapterView.OnItemSelectedListener {
198 | override fun onItemSelected(
199 | parent: AdapterView<*>,
200 | view: View,
201 | position: Int,
202 | id: Long
203 | ) {
204 | xLimitCount = xLimitLineCountList[position].toInt()
205 | binding.liveLineChartView.setLimitLineCount(xLimitCount, yLimitCount)
206 | }
207 |
208 | override fun onNothingSelected(parent: AdapterView<*>?) {
209 | }
210 | }
211 |
212 | val yLimitLineCountList = listOf("3", "4", "5", "6", "7")
213 | binding.yLimitCountSpinner.adapter = ArrayAdapter(
214 | this, R.layout.item_spinner_textview,
215 | yLimitLineCountList
216 | )
217 | binding.yLimitCountSpinner.onItemSelectedListener =
218 | object : AdapterView.OnItemSelectedListener {
219 | override fun onItemSelected(
220 | parent: AdapterView<*>,
221 | view: View,
222 | position: Int,
223 | id: Long
224 | ) {
225 | yLimitCount = yLimitLineCountList[position].toInt()
226 | binding.liveLineChartView.setLimitLineCount(xLimitCount, yLimitCount)
227 | updateYAxisInfos(curAmplitudeRange, yLimitCount)
228 | }
229 |
230 | override fun onNothingSelected(parent: AdapterView<*>?) {
231 | }
232 | }
233 | lifecycleScope.launch {
234 | delay(1000)
235 | while (true) {
236 | delay(16)
237 | binding.liveLineChartView.addPoint((-900000..900000).random() / 10000f)
238 | // binding.liveLineChartView.addPoint(56250f)
239 | }
240 | }
241 |
242 | binding.liveLineChartView.setWindowDuration(WindowDuration(3000, 60))
243 | }
244 |
245 | private fun updateYAxisInfos(amplitudeRange: Float, yLimitCount: Int) {
246 | val yAxisList = (0 until yLimitCount).map { i ->
247 | AxisInfo((amplitudeRange - amplitudeRange * 2 / (yLimitCount - 1) * i))
248 | }
249 | binding.liveLineChartView.setData(yAxisList)
250 | }
251 | }
252 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | Apache License
2 | Version 2.0, January 2004
3 | http://www.apache.org/licenses/
4 |
5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6 |
7 | 1. Definitions.
8 |
9 | "License" shall mean the terms and conditions for use, reproduction,
10 | and distribution as defined by Sections 1 through 9 of this document.
11 |
12 | "Licensor" shall mean the copyright owner or entity authorized by
13 | the copyright owner that is granting the License.
14 |
15 | "Legal Entity" shall mean the union of the acting entity and all
16 | other entities that control, are controlled by, or are under common
17 | control with that entity. For the purposes of this definition,
18 | "control" means (i) the power, direct or indirect, to cause the
19 | direction or management of such entity, whether by contract or
20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the
21 | outstanding shares, or (iii) beneficial ownership of such entity.
22 |
23 | "You" (or "Your") shall mean an individual or Legal Entity
24 | exercising permissions granted by this License.
25 |
26 | "Source" form shall mean the preferred form for making modifications,
27 | including but not limited to software source code, documentation
28 | source, and configuration files.
29 |
30 | "Object" form shall mean any form resulting from mechanical
31 | transformation or translation of a Source form, including but
32 | not limited to compiled object code, generated documentation,
33 | and conversions to other media types.
34 |
35 | "Work" shall mean the work of authorship, whether in Source or
36 | Object form, made available under the License, as indicated by a
37 | copyright notice that is included in or attached to the work
38 | (an example is provided in the Appendix below).
39 |
40 | "Derivative Works" shall mean any work, whether in Source or Object
41 | form, that is based on (or derived from) the Work and for which the
42 | editorial revisions, annotations, elaborations, or other modifications
43 | represent, as a whole, an original work of authorship. For the purposes
44 | of this License, Derivative Works shall not include works that remain
45 | separable from, or merely link (or bind by name) to the interfaces of,
46 | the Work and Derivative Works thereof.
47 |
48 | "Contribution" shall mean any work of authorship, including
49 | the original version of the Work and any modifications or additions
50 | to that Work or Derivative Works thereof, that is intentionally
51 | submitted to Licensor for inclusion in the Work by the copyright owner
52 | or by an individual or Legal Entity authorized to submit on behalf of
53 | the copyright owner. For the purposes of this definition, "submitted"
54 | means any form of electronic, verbal, or written communication sent
55 | to the Licensor or its representatives, including but not limited to
56 | communication on electronic mailing lists, source code control systems,
57 | and issue tracking systems that are managed by, or on behalf of, the
58 | Licensor for the purpose of discussing and improving the Work, but
59 | excluding communication that is conspicuously marked or otherwise
60 | designated in writing by the copyright owner as "Not a Contribution."
61 |
62 | "Contributor" shall mean Licensor and any individual or Legal Entity
63 | on behalf of whom a Contribution has been received by Licensor and
64 | subsequently incorporated within the Work.
65 |
66 | 2. Grant of Copyright License. Subject to the terms and conditions of
67 | this License, each Contributor hereby grants to You a perpetual,
68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69 | copyright license to reproduce, prepare Derivative Works of,
70 | publicly display, publicly perform, sublicense, and distribute the
71 | Work and such Derivative Works in Source or Object form.
72 |
73 | 3. Grant of Patent License. Subject to the terms and conditions of
74 | this License, each Contributor hereby grants to You a perpetual,
75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76 | (except as stated in this section) patent license to make, have made,
77 | use, offer to sell, sell, import, and otherwise transfer the Work,
78 | where such license applies only to those patent claims licensable
79 | by such Contributor that are necessarily infringed by their
80 | Contribution(s) alone or by combination of their Contribution(s)
81 | with the Work to which such Contribution(s) was submitted. If You
82 | institute patent litigation against any entity (including a
83 | cross-claim or counterclaim in a lawsuit) alleging that the Work
84 | or a Contribution incorporated within the Work constitutes direct
85 | or contributory patent infringement, then any patent licenses
86 | granted to You under this License for that Work shall terminate
87 | as of the date such litigation is filed.
88 |
89 | 4. Redistribution. You may reproduce and distribute copies of the
90 | Work or Derivative Works thereof in any medium, with or without
91 | modifications, and in Source or Object form, provided that You
92 | meet the following conditions:
93 |
94 | (a) You must give any other recipients of the Work or
95 | Derivative Works a copy of this License; and
96 |
97 | (b) You must cause any modified files to carry prominent notices
98 | stating that You changed the files; and
99 |
100 | (c) You must retain, in the Source form of any Derivative Works
101 | that You distribute, all copyright, patent, trademark, and
102 | attribution notices from the Source form of the Work,
103 | excluding those notices that do not pertain to any part of
104 | the Derivative Works; and
105 |
106 | (d) If the Work includes a "NOTICE" text file as part of its
107 | distribution, then any Derivative Works that You distribute must
108 | include a readable copy of the attribution notices contained
109 | within such NOTICE file, excluding those notices that do not
110 | pertain to any part of the Derivative Works, in at least one
111 | of the following places: within a NOTICE text file distributed
112 | as part of the Derivative Works; within the Source form or
113 | documentation, if provided along with the Derivative Works; or,
114 | within a display generated by the Derivative Works, if and
115 | wherever such third-party notices normally appear. The contents
116 | of the NOTICE file are for informational purposes only and
117 | do not modify the License. You may add Your own attribution
118 | notices within Derivative Works that You distribute, alongside
119 | or as an addendum to the NOTICE text from the Work, provided
120 | that such additional attribution notices cannot be construed
121 | as modifying the License.
122 |
123 | You may add Your own copyright statement to Your modifications and
124 | may provide additional or different license terms and conditions
125 | for use, reproduction, or distribution of Your modifications, or
126 | for any such Derivative Works as a whole, provided Your use,
127 | reproduction, and distribution of the Work otherwise complies with
128 | the conditions stated in this License.
129 |
130 | 5. Submission of Contributions. Unless You explicitly state otherwise,
131 | any Contribution intentionally submitted for inclusion in the Work
132 | by You to the Licensor shall be under the terms and conditions of
133 | this License, without any additional terms or conditions.
134 | Notwithstanding the above, nothing herein shall supersede or modify
135 | the terms of any separate license agreement you may have executed
136 | with Licensor regarding such Contributions.
137 |
138 | 6. Trademarks. This License does not grant permission to use the trade
139 | names, trademarks, service marks, or product names of the Licensor,
140 | except as required for reasonable and customary use in describing the
141 | origin of the Work and reproducing the content of the NOTICE file.
142 |
143 | 7. Disclaimer of Warranty. Unless required by applicable law or
144 | agreed to in writing, Licensor provides the Work (and each
145 | Contributor provides its Contributions) on an "AS IS" BASIS,
146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147 | implied, including, without limitation, any warranties or conditions
148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149 | PARTICULAR PURPOSE. You are solely responsible for determining the
150 | appropriateness of using or redistributing the Work and assume any
151 | risks associated with Your exercise of permissions under this License.
152 |
153 | 8. Limitation of Liability. In no event and under no legal theory,
154 | whether in tort (including negligence), contract, or otherwise,
155 | unless required by applicable law (such as deliberate and grossly
156 | negligent acts) or agreed to in writing, shall any Contributor be
157 | liable to You for damages, including any direct, indirect, special,
158 | incidental, or consequential damages of any character arising as a
159 | result of this License or out of the use or inability to use the
160 | Work (including but not limited to damages for loss of goodwill,
161 | work stoppage, computer failure or malfunction, or any and all
162 | other commercial damages or losses), even if such Contributor
163 | has been advised of the possibility of such damages.
164 |
165 | 9. Accepting Warranty or Additional Liability. While redistributing
166 | the Work or Derivative Works thereof, You may choose to offer,
167 | and charge a fee for, acceptance of support, warranty, indemnity,
168 | or other liability obligations and/or rights consistent with this
169 | License. However, in accepting such obligations, You may act only
170 | on Your own behalf and on Your sole responsibility, not on behalf
171 | of any other Contributor, and only if You agree to indemnify,
172 | defend, and hold each Contributor harmless for any liability
173 | incurred by, or claims asserted against, such Contributor by reason
174 | of your accepting any such warranty or additional liability.
175 |
176 | END OF TERMS AND CONDITIONS
177 |
178 | APPENDIX: How to apply the Apache License to your work.
179 |
180 | To apply the Apache License to your work, attach the following
181 | boilerplate notice, with the fields enclosed by brackets "[]"
182 | replaced with your own identifying information. (Don't include
183 | the brackets!) The text should be enclosed in the appropriate
184 | comment syntax for the file format. We also recommend that a
185 | file or class name and description of purpose be included on the
186 | same "printed page" as the copyright notice for easier
187 | identification within third-party archives.
188 |
189 | Copyright [yyyy] [name of copyright owner]
190 |
191 | Licensed under the Apache License, Version 2.0 (the "License");
192 | you may not use this file except in compliance with the License.
193 | You may obtain a copy of the License at
194 |
195 | http://www.apache.org/licenses/LICENSE-2.0
196 |
197 | Unless required by applicable law or agreed to in writing, software
198 | distributed under the License is distributed on an "AS IS" BASIS,
199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200 | See the License for the specific language governing permissions and
201 | limitations under the License.
202 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # LineChartView-Android
2 | 静态波形、动态实时波形绘制
3 |
4 | # Demo效果图
5 |
6 |
7 |
8 | # 使用
9 |
10 | - 添加gradle依赖(version=[](https://jitpack.io/#zhzc0x/linechart-android)
11 | )
12 |
13 | ```groovy
14 | //Add it in your root build.gradle at the end of repositories:
15 | allprojects {
16 | repositories {
17 | ...
18 | maven { url 'https://jitpack.io' }
19 | }
20 | }
21 |
22 | //Add it in your app build.gradle
23 | dependencies {
24 | implementation 'com.github.zhzc0x:linechart-android:$version'
25 | }
26 | ```
27 |
28 | - 布局文件中声明(更多属性说明详见 #自定义属性说明)
29 |
30 | ```xml
31 | //静态波形LineChartView
32 |
62 |
63 | //动态实时波形LiveLineChartView
64 |
74 |
75 | ```
76 |
77 | - Api说明
78 |
79 | ```kotlin
80 | class LineChartView {
81 | ......
82 |
83 | /** 设置限制线 */
84 | fun setLimitArray(limitArray: List)
85 |
86 | /**
87 | * 设置是否绘制曲线
88 | * @see R.attr.drawCurve
89 | * */
90 | fun setDrawCurve(drawCurve: Boolean)
91 |
92 | /**
93 | * 设置是否显示折线点
94 | * @see R.attr.showLineChartPoint
95 | * */
96 | fun setLineChartPoint(show: Boolean)
97 |
98 | /**
99 | * 设置显示折线动画
100 | * @see R.attr.showLineChartAnim
101 | * */
102 | fun showLineChartAnim()
103 |
104 | /**
105 | * 设置显示指定折线点信息list
106 | * 设置后showLineChartPoint和showPointFloatBox属性失效,设置null后恢复
107 | *
108 | * */
109 | fun setShowPoints(showPointList: List?)
110 |
111 | /**
112 | * 设置折线点绘制开始和结束的位置
113 | * @see R.attr.pointXStart = startDp
114 | * @see R.attr.pointXEnd = endDp
115 | *
116 | * */
117 | fun setPointXInit(startDp: Int, endDp: Int)
118 |
119 | /**
120 | * 设置折线数据
121 | *
122 | * @param pointList 点的集合
123 | * @param xAxisList X轴数据集合
124 | * @param yAxisList Y轴数据集合
125 | * @param pointSpace 点的间距
126 | *
127 | * */
128 | @JvmOverloads
129 | fun setData(pointList: List, xAxisList: List? = null, yAxisList: List, pointSpace: Float = 0f)
130 |
131 | ......
132 | }
133 |
134 | class LiveLineChartView {
135 | ......
136 |
137 | /** 往当前屏幕添加折线点 */
138 | fun addPoint(point: Float)
139 |
140 | /** 清空当前屏幕所有的折线点 */
141 | fun reset()
142 |
143 | /**
144 | * 设置是否绘制曲线
145 | * @see R.attr.drawCurve
146 | * */
147 | fun setDrawCurve(drawCurve: Boolean)
148 |
149 | /**
150 | * 设置折线点间距,距离越大,折线移动速度越快,反之越小,单位:px
151 | * @see R.attr.pointSpace
152 | * */
153 | fun setPointSpace(pointSpace: Float)
154 |
155 | /**
156 | * 设置窗口时长信息,根据窗口时长和采样率计算绘制点的间距
157 | *
158 | * @param windowDuration: WindowDuration
159 | * @see com.zhzc0x.chart.WindowDuration
160 | *
161 | * */
162 | fun setWindowDuration(windowDuration: WindowDuration)
163 |
164 | /**
165 | * 设置幅值计算模式
166 | * @param amplitudeMode: AmplitudeMode
167 | * @see com.zhzc0x.chart.mplitudeMode
168 | *
169 | * */
170 | fun setAmplitudeMode(amplitudeMode: AmplitudeMode)
171 |
172 | /**
173 | * 设置Y轴自动幅值自动计算点数,默认1窗口点数windowPoints或者采样率点数 points/s,amplitudeMode != AmplitudeMode.FIXED时生效
174 | * @param autoAmplitudePoints: Int
175 | *
176 | * */
177 | fun setAutoAmplitudePoints(autoAmplitudePoints: Int)
178 |
179 | /**
180 | * 设置自动缩放Y轴幅值阀值因子,amplitudeMode != AmplitudeMode.FIXED时生效
181 | * @param factor: 控制自动缩放阀值,取值范围(0f until 1f),值越大,缩放阀值越小
182 | * @see screenMaxPoints
183 | * */
184 | fun setAutoAmplitudeFactor(factor: Float) {
185 | require(factor >= 0f && factor < 1f)
186 | autoAmplitudeFactor = factor
187 | }
188 |
189 | /** 设置x轴y轴限定线条数 */
190 | fun setLimitLineCount(xLimitLineCount: Int, yLimitLineCount: Int)
191 |
192 | /**
193 | * 设置折线数据
194 | *
195 | * @param yAxisList Y轴数据集合
196 | * @param amplitudeMode 幅值计算模式,默认AmplitudeMode.FIXED
197 | * @see AmplitudeMode
198 | * @param textConverter 文本转换
199 | *
200 | * */
201 | @JvmOverloads
202 | fun setData(
203 | yAxisList: List,
204 | amplitudeMode: AmplitudeMode = AmplitudeMode.FIXED,
205 | textConverter: (Float) -> String = this.textConverter
206 | )
207 |
208 | ......
209 | }
210 | ```
211 |
212 | # 自定义属性说明
213 |
214 | ```xml
215 |
216 |
217 |
218 |
219 |
220 |
221 |
222 |
223 |
224 |
225 |
226 |
227 |
228 |
229 |
230 |
231 |
232 |
233 |
234 |
235 |
236 |
237 |
238 |
239 |
240 |
241 |
242 |
243 |
244 |
245 |
246 |
247 |
248 |
249 |
250 |
251 |
252 |
253 |
254 |
255 |
256 |
257 |
258 |
259 |
260 |
261 |
262 |
263 |
264 |
265 |
266 |
267 |
268 |
269 |
270 |
271 |
272 |
273 |
274 |
275 |
276 |
277 |
278 |
279 |
280 |
281 |
282 |
283 |
284 |
285 |
286 |
287 |
288 |
289 |
290 |
291 |
292 |
293 |
294 |
295 |
296 |
297 |
298 |
299 |
300 |
301 |
302 |
303 |
304 |
305 |
306 |
307 |
308 |
309 |
310 |
311 |
312 |
313 |
314 |
315 |
316 |
317 |
318 |
319 |
320 |
321 |
322 |
323 |
324 |
325 |
326 |
327 |
328 |
329 |
330 |
331 |
332 |
333 |
334 |
335 |
336 |
337 |
338 |
339 |
340 |
341 |
342 |
343 |
344 |
345 |
346 |
347 |
348 |
349 |
350 |
351 |
352 |
353 |
354 |
355 |
356 |
357 |
358 |
359 |
360 |
361 |
362 |
363 |
364 |
365 |
366 |
367 |
368 |
369 |
370 |
371 |
372 |
373 |
374 |
375 |
376 |
377 |
378 |
379 |
380 | ```
381 |
382 |
383 |
384 | # License
385 |
386 | ```
387 | Copyright 2022 zhzc0x
388 |
389 | Licensed under the Apache License, Version 2.0 (the "License");
390 | you may not use this file except in compliance with the License.
391 | You may obtain a copy of the License at
392 |
393 | http://www.apache.org/licenses/LICENSE-2.0
394 |
395 | Unless required by applicable law or agreed to in writing, software
396 | distributed under the License is distributed on an "AS IS" BASIS,
397 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
398 | See the License for the specific language governing permissions and
399 | limitations under the License.
400 | ```
401 |
402 |
--------------------------------------------------------------------------------
/library/src/main/java/com/zhzc0x/chart/LiveLineChartView.kt:
--------------------------------------------------------------------------------
1 | package com.zhzc0x.chart
2 |
3 | import android.content.Context
4 | import android.graphics.*
5 | import android.text.TextPaint
6 | import android.util.AttributeSet
7 | import android.util.Log
8 | import android.view.View
9 | import com.zhzc0x.chart.ext.dp
10 | import com.zhzc0x.chart.ext.scale
11 | import kotlin.math.abs
12 | import kotlin.math.max
13 | import androidx.core.content.withStyledAttributes
14 | import kotlin.math.min
15 |
16 | class LiveLineChartView @JvmOverloads constructor(
17 | context: Context,
18 | attrs: AttributeSet?,
19 | defStyleAttr: Int = 0
20 | ) : View(context, attrs, defStyleAttr) {
21 |
22 | private val tag = LiveLineChartView::class.java.simpleName
23 |
24 | private var showYAxis = true
25 | private var yAxisColor = Color.GRAY
26 | private var yAxisWidth = 1f.dp
27 | private var showYText = true
28 | private var yTextColor = 0
29 | private var yTextSize = 10f.dp
30 | private var yTextAlign = TextAlign.RIGHT
31 | private var showYScaleLine = true
32 | private var yScaleLineColor = 0
33 | private var yScaleLineWidth = 1f.dp
34 | private var yScaleLineLength = 4f.dp
35 |
36 | private var limitLineColor = Color.GRAY
37 | private var limitLineWidth = 1f.dp
38 | private var limitLineLength = 2f.dp
39 | private var limitLineSpace = 2f.dp
40 | private var xLimitLineCount = 2
41 | private var yLimitLineCount = 2
42 |
43 | private var lineChartWidth = 1.5f.dp
44 | private var lineChartColor = Color.LTGRAY
45 | private var lineChartPaddingStart = 30.dp
46 | private var lineChartBgColor = Color.WHITE
47 | private var drawCurve = false // 绘制曲线
48 | private var pointSpace = 1.2f.dp // 折线点间距
49 | private var windowDuration: WindowDuration? = null
50 | private lateinit var linePaint: Paint
51 | private lateinit var lineChartPaint: Paint
52 | private lateinit var limitPaint: Paint
53 | private lateinit var textPaint: TextPaint
54 |
55 | // 折线点对应的数据
56 | private val pointList = ArrayList()
57 | // x起始位置
58 | private var xOrigin = 0f
59 | // y起始位置
60 | private var yOrigin = 0f
61 | private var drawHeight = 0f
62 | private var yMin = 0f
63 | private var yMax = 1f
64 | // y轴坐标对应的数据
65 | private val yAxisList = ArrayList()
66 | private var textConverter: (Float) -> String = { it.scale(1).toString() }
67 | private val lineChartPath = Path()
68 | private var limitLinePath: Path? = null
69 | private var yTextHeight = 0f
70 | private var windowPoints = 0 // 窗口可显示点数
71 | private var autoAmplitudePoints = 0 // 自动缩放Y轴幅值点数
72 | private var autoAmplitudeFactor = 0.1f // 自动缩放阀值因子
73 | private var amplitudeMode = AmplitudeMode.FIXED // 幅值计算模式
74 |
75 | init {
76 | if (attrs != null) {
77 | initCustomAttrs(context, attrs)
78 | }
79 | initPaint()
80 | }
81 |
82 | private fun initCustomAttrs(context: Context, attrs: AttributeSet) {
83 | context.withStyledAttributes(attrs, R.styleable.LiveLineChartView) {
84 | showYAxis = getBoolean(R.styleable.LiveLineChartView_showYAxis, showYAxis)
85 | yAxisColor = getColor(R.styleable.LiveLineChartView_yAxisColor, yAxisColor)
86 | yAxisWidth = getDimensionPixelSize(
87 | R.styleable.LiveLineChartView_yAxisWidth,
88 | yAxisWidth.toInt()
89 | ).toFloat()
90 | showYText = getBoolean(R.styleable.LiveLineChartView_showYText, showYText)
91 | yTextColor = getColor(R.styleable.LiveLineChartView_yTextColor, yAxisColor)
92 | yTextSize = getDimensionPixelSize(
93 | R.styleable.LiveLineChartView_yTextSize,
94 | yTextSize.toInt()
95 | ).toFloat()
96 | val ordinal = getInt(R.styleable.LiveLineChartView_yTextAlign, yTextAlign.ordinal)
97 | yTextAlign = TextAlign.values()[ordinal]
98 | showYScaleLine =
99 | getBoolean(R.styleable.LiveLineChartView_showYScaleLine, showYScaleLine)
100 | yScaleLineColor = getColor(R.styleable.LiveLineChartView_yScaleLineColor, yAxisColor)
101 | yScaleLineWidth = getDimensionPixelSize(
102 | R.styleable.LiveLineChartView_yScaleLineWidth,
103 | yScaleLineWidth.toInt()
104 | ).toFloat()
105 | yScaleLineLength = getDimensionPixelSize(
106 | R.styleable.LiveLineChartView_yScaleLineLength,
107 | yScaleLineLength.toInt()
108 | ).toFloat()
109 |
110 | limitLineColor = getColor(R.styleable.LiveLineChartView_limitLineColor, limitLineColor)
111 | limitLineWidth = getDimensionPixelSize(
112 | R.styleable.LiveLineChartView_limitLineWidth,
113 | limitLineWidth.toInt()
114 | ).toFloat()
115 | limitLineLength = getDimensionPixelSize(
116 | R.styleable.LiveLineChartView_limitLineLength,
117 | limitLineLength.toInt()
118 | ).toFloat()
119 | limitLineSpace = getDimensionPixelSize(
120 | R.styleable.LiveLineChartView_limitLineSpace,
121 | limitLineSpace.toInt()
122 | ).toFloat()
123 | val xLimitLineCount =
124 | getInt(R.styleable.LiveLineChartView_xLimitLineCount, xLimitLineCount)
125 | val yLimitLineCount =
126 | getInt(R.styleable.LiveLineChartView_yLimitLineCount, yLimitLineCount)
127 | setLimitLineCount(xLimitLineCount, yLimitLineCount)
128 |
129 | lineChartColor = getColor(R.styleable.LiveLineChartView_lineChartColor, lineChartColor)
130 | lineChartWidth = getDimensionPixelSize(
131 | R.styleable.LiveLineChartView_lineChartWidth,
132 | lineChartWidth.toInt()
133 | ).toFloat()
134 | lineChartPaddingStart = getDimensionPixelSize(
135 | R.styleable.LiveLineChartView_lineChartPaddingStart,
136 | lineChartPaddingStart
137 | )
138 | lineChartBgColor =
139 | getColor(R.styleable.LiveLineChartView_lineChartBgColor, lineChartBgColor)
140 | drawCurve = getBoolean(R.styleable.LiveLineChartView_drawCurve, drawCurve)
141 | pointSpace = getDimensionPixelSize(
142 | R.styleable.LiveLineChartView_pointSpace,
143 | pointSpace.toInt()
144 | ).toFloat()
145 | val amplitudeModeOrdinal = getInt(R.styleable.LiveLineChartView_amplitudeMode, amplitudeMode.ordinal)
146 | amplitudeMode = AmplitudeMode.values()[amplitudeModeOrdinal]
147 | if (debugLineChart) {
148 | Log.d(tag, "pointSpace=$pointSpace")
149 | }
150 | }
151 | }
152 |
153 | private fun initPaint() {
154 | linePaint = Paint(Paint.ANTI_ALIAS_FLAG)
155 | linePaint.strokeCap = Paint.Cap.ROUND
156 | lineChartPaint = Paint(Paint.ANTI_ALIAS_FLAG)
157 | lineChartPaint.color = lineChartColor
158 | lineChartPaint.style = Paint.Style.STROKE
159 | lineChartPaint.strokeWidth = lineChartWidth
160 | lineChartPaint.strokeCap = Paint.Cap.ROUND
161 | lineChartPaint.strokeJoin = Paint.Join.ROUND
162 | limitPaint = Paint(Paint.ANTI_ALIAS_FLAG)
163 | limitPaint.color = limitLineColor
164 | limitPaint.style = Paint.Style.STROKE
165 | limitPaint.strokeWidth = limitLineWidth
166 | limitPaint.pathEffect = DashPathEffect(floatArrayOf(limitLineSpace, limitLineLength), 0f)
167 | textPaint = TextPaint(Paint.ANTI_ALIAS_FLAG)
168 | }
169 |
170 | private fun initData() {
171 | xOrigin = lineChartPaddingStart + yAxisWidth
172 | yOrigin = viewHeight.toFloat()
173 | updateWindowPoints()
174 | textPaint.textSize = yTextSize
175 | val text = "TEXT"
176 | val textRect = Rect()
177 | textPaint.getTextBounds(text, 0, text.length, textRect)
178 | yTextHeight = textRect.height() * 1.5f
179 | drawHeight = yOrigin - yTextHeight
180 | }
181 |
182 | private fun updateWindowPoints() {
183 | windowPoints = ((viewWidth - lineChartPaddingStart) / pointSpace).toInt()
184 | if (debugLineChart) {
185 | Log.d(tag, "drawWindowWidth=${viewWidth - lineChartPaddingStart}, windowDuration=$windowDuration\n" +
186 | "pointSpace=${pointSpace}, windowPoints=$windowPoints")
187 | }
188 | // 边界case:防止页面未测量完成时出现负数
189 | if (windowPoints < 0) {
190 | windowPoints = 0
191 | }
192 | if (windowDuration != null) {
193 | autoAmplitudePoints = windowDuration!!.samplingRate
194 | } else {
195 | autoAmplitudePoints = windowPoints
196 | }
197 | }
198 |
199 | private var viewWidth = 0
200 | private var viewHeight = 0
201 | override fun onLayout(changed: Boolean, left: Int, top: Int, right: Int, bottom: Int) {
202 | if (!changed) {
203 | super.onLayout(false, left, top, right, bottom)
204 | return
205 | }
206 | viewWidth = width
207 | viewHeight = height
208 | initData()
209 | super.onLayout(true, left, top, right, bottom)
210 | }
211 |
212 | override fun onDraw(canvas: Canvas) {
213 | canvas.drawColor(lineChartBgColor)
214 | drawYAxis(canvas)
215 | drawLimitLine(canvas)
216 | drawLineChart(canvas)
217 | }
218 |
219 | private fun drawYAxis(canvas: Canvas) {
220 | if (showYAxis) {
221 | linePaint.color = yAxisColor
222 | linePaint.strokeWidth = yAxisWidth
223 | canvas.drawLine(xOrigin, yOrigin, xOrigin, 0f, linePaint)
224 | }
225 | if (!showYScaleLine && !showYText) {
226 | return
227 | }
228 | yAxisList.forEach { axisInfo ->
229 | if (showYScaleLine) {
230 | linePaint.color = yScaleLineColor
231 | linePaint.strokeWidth = yScaleLineWidth
232 | val y = getDrawY(axisInfo.value)
233 | canvas.drawLine(xOrigin, y, xOrigin - yScaleLineLength, y, linePaint)
234 | }
235 |
236 | if (showYText) {
237 | textPaint.color = yTextColor
238 | textPaint.textSize = yTextSize
239 | textPaint.textAlign = when (yTextAlign) {
240 | TextAlign.LEFT -> Paint.Align.LEFT
241 | TextAlign.CENTER -> Paint.Align.CENTER
242 | TextAlign.RIGHT -> Paint.Align.RIGHT
243 | }
244 | val textX = when (yTextAlign) {
245 | TextAlign.LEFT -> yScaleLineLength
246 | TextAlign.CENTER -> (xOrigin - yScaleLineLength) / 2
247 | TextAlign.RIGHT -> xOrigin - yScaleLineLength * 2
248 | }
249 | canvas.drawText(axisInfo.showText, textX,
250 | getDrawY(axisInfo.value) + yTextHeight / 3 - yAxisWidth, textPaint)
251 | }
252 | }
253 | }
254 |
255 | /**
256 | * 绘制限定线
257 | *
258 | * */
259 | private fun drawLimitLine(canvas: Canvas) {
260 | // draw x limit
261 | if (xLimitLineCount > 0) {
262 | resetLimitPath()
263 | val limitSpace = (viewWidth - xOrigin) / xLimitLineCount
264 | (1 .. xLimitLineCount).forEach {
265 | val x = it * limitSpace + xOrigin
266 | limitLinePath!!.moveTo(x, yOrigin)
267 | limitLinePath!!.lineTo(x, 0f)
268 | }
269 | canvas.drawPath(limitLinePath!!, limitPaint)
270 | }
271 | // draw y limit
272 | if (yLimitLineCount > 0) {
273 | resetLimitPath()
274 | val limitSpace = drawHeight / (yLimitLineCount - 1)
275 | (0 until yLimitLineCount).forEach {
276 | val y = it * limitSpace + yTextHeight / 2
277 | limitLinePath!!.moveTo(xOrigin, y)
278 | limitLinePath!!.lineTo(viewWidth.toFloat(), y)
279 | }
280 | canvas.drawPath(limitLinePath!!, limitPaint)
281 | }
282 | }
283 |
284 | private fun resetLimitPath() {
285 | if (limitLinePath == null) {
286 | limitLinePath = Path()
287 | } else {
288 | limitLinePath!!.reset()
289 | }
290 | }
291 |
292 | private var startX: Float = 0f
293 | private var startY: Float = 0f
294 | private var endX: Float = 0f
295 | private var endY: Float = 0f
296 | private fun drawLineChart(canvas: Canvas) {
297 | lineChartPath.reset()
298 | pointList.forEachIndexed { index, y ->
299 | endX = xOrigin + pointSpace * index
300 | endY = getDrawY(y)
301 | if (drawCurve) {
302 | //绘制曲线(三阶贝塞尔曲线)
303 | if (index == 0) {
304 | lineChartPath.moveTo(endX, endY)
305 | } else {
306 | val referX = (startX + endX) / 2
307 | lineChartPath.cubicTo(referX, startY, referX, endY, endX, endY)
308 | }
309 | startX = endX
310 | startY = endY
311 | } else {
312 | //绘制折线
313 | if (index == 0) {
314 | lineChartPath.moveTo(endX, endY)
315 | } else {
316 | lineChartPath.lineTo(endX, endY)
317 | }
318 | }
319 | }
320 | canvas.drawPath(lineChartPath, lineChartPaint)
321 | }
322 |
323 | private fun getDrawY(point: Float): Float {
324 | val temp = when {
325 | point > yMax -> {
326 | yMax
327 | }
328 | point < yMin -> {
329 | yMin
330 | }
331 | else -> {
332 | point
333 | }
334 | }
335 | //处理负值的情况,但是point的y值必须在最大值和最小值之间
336 | return drawHeight - drawHeight * ((temp - yMin) / (yMax - yMin)) + yTextHeight / 2
337 | }
338 |
339 | fun addPoint(point: Float) {
340 | if (pointList.size > windowPoints) {
341 | pointList.removeAt(0)
342 | }
343 | pointList.add(point)
344 | calculateAmplitude(point)
345 | invalidate()
346 | }
347 |
348 | private var curMaxPoint = 0f
349 | private var curMinPoint = 0f
350 | private var preMaxPoint = 0f
351 | private var preMinPoint = 0f
352 | private var updatePoints = 0
353 | private fun calculateAmplitude(point: Float) {
354 | if (amplitudeMode == AmplitudeMode.FIXED) {
355 | return
356 | }
357 | if (updatePoints == 0) {
358 | curMaxPoint = point
359 | curMinPoint = point
360 | } else {
361 | mathMaxMinPoint(point)
362 | }
363 | // 控制每绘制固定的点数后计算一次幅值
364 | if (updatePoints >= autoAmplitudePoints) {
365 | updateMaxPoint()
366 | updateMinPoint()
367 | // 减少不必要的更新
368 | if (curMaxPoint == curMinPoint || curMaxPoint == yMax || curMinPoint == yMin) {
369 | updatePoints = 0
370 | return
371 | }
372 | updateYAxisList()
373 | updateAmplitude()
374 | updatePoints = 0
375 | } else {
376 | updatePoints++
377 | }
378 | }
379 |
380 | private fun mathMaxMinPoint(point: Float) {
381 | if (amplitudeMode == AmplitudeMode.MAX_NEGATE) {
382 | curMaxPoint = max(curMaxPoint, abs(point))
383 | curMinPoint = -curMaxPoint
384 | } else if (amplitudeMode == AmplitudeMode.MAX_MIN) {
385 | curMaxPoint = max(curMaxPoint, point)
386 | curMinPoint = min(curMinPoint, point)
387 | }
388 | }
389 |
390 | private fun updateMaxPoint() {
391 | if (curMaxPoint > preMaxPoint || curMaxPoint < preMaxPoint * (1 - autoAmplitudeFactor)) {
392 | curMaxPoint *= (1 + autoAmplitudeFactor)
393 | preMaxPoint = curMaxPoint
394 | } else {
395 | curMaxPoint = preMaxPoint
396 | }
397 | if (debugLineChart) {
398 | Log.d(tag, "updateMaxPoint: curMaxPoint=$curMaxPoint, preMaxPoint=$preMaxPoint")
399 | }
400 | }
401 |
402 | private fun updateMinPoint() {
403 | if (amplitudeMode == AmplitudeMode.MAX_MIN) {
404 | if (curMinPoint < preMinPoint || curMinPoint > preMinPoint * (1 + autoAmplitudeFactor)) {
405 | curMinPoint *= (1 - autoAmplitudeFactor)
406 | preMinPoint = curMinPoint
407 | } else {
408 | curMinPoint = preMinPoint
409 | }
410 | } else {
411 | curMinPoint = -curMaxPoint
412 | }
413 | if (debugLineChart) {
414 | Log.d(tag, "updateMinPoint: curMinPoint=$curMinPoint, preMinPoint=$preMinPoint")
415 | }
416 | }
417 |
418 | private fun updateYAxisList() {
419 | val valueSpace = (curMaxPoint - curMinPoint) / (yAxisList.size - 1)
420 | (0 until yAxisList.size).forEach { i ->
421 | val value = curMaxPoint - valueSpace * i
422 | yAxisList[i] = AxisInfo(value, textConverter(value))
423 | }
424 | if (debugLineChart) {
425 | Log.d(tag, "updateYAxisList: curMaxPoint=$curMaxPoint, curMinPoint=$curMinPoint")
426 | }
427 | }
428 |
429 | fun reset() {
430 | pointList.clear()
431 | invalidate()
432 | }
433 |
434 | override fun onDetachedFromWindow() {
435 | super.onDetachedFromWindow()
436 | pointList.clear()
437 | limitLinePath?.reset()
438 | limitLinePath = null
439 | lineChartPath.reset()
440 | }
441 |
442 | /**
443 | * 设置是否绘制曲线
444 | * @see R.attr.drawCurve
445 | * */
446 | fun setDrawCurve(drawCurve: Boolean) {
447 | this.drawCurve = drawCurve
448 | }
449 |
450 | /**
451 | * 设置折线点间距,距离越大,折线移动速度越快,反之越小,单位:px
452 | * @see R.attr.pointSpace
453 | * */
454 | fun setPointSpace(pointSpace: Float) {
455 | this.pointSpace = pointSpace
456 | updateWindowPoints()
457 | while (pointList.size > windowPoints) {
458 | pointList.removeAt(0)
459 | }
460 | }
461 |
462 | /**
463 | * 设置窗口时长信息,根据窗口时长和采样率计算绘制点的间距
464 | *
465 | * @param windowDuration: WindowDuration
466 | * @see com.zhzc0x.chart.WindowDuration
467 | *
468 | * */
469 | fun setWindowDuration(windowDuration: WindowDuration) {
470 | this.windowDuration = windowDuration
471 | post {
472 | if (isShown) {
473 | val pointSpace = (viewWidth - xOrigin) / (windowDuration.samplingRate * (windowDuration.duration / 1000f))
474 | setPointSpace(pointSpace)
475 | }
476 | }
477 | }
478 |
479 | /**
480 | * 设置幅值计算模式
481 | * @param amplitudeMode: AmplitudeMode
482 | * @see com.zhzc0x.chart.AmplitudeMode
483 | *
484 | * */
485 | fun setAmplitudeMode(amplitudeMode: AmplitudeMode) {
486 | this.amplitudeMode = amplitudeMode
487 | if (amplitudeMode != AmplitudeMode.FIXED) {
488 | updatePoints = 0
489 | }
490 | }
491 |
492 | /**
493 | * 设置Y轴自动幅值计算点数,默认1窗口点数windowPoints或者采样率点数 points/s,amplitudeMode != AmplitudeMode.FIXED时生效
494 | * @param autoAmplitudePoints: Int
495 | *
496 | * */
497 | fun setAutoAmplitudePoints(autoAmplitudePoints: Int) {
498 | this.autoAmplitudePoints = autoAmplitudePoints
499 | }
500 |
501 | /**
502 | * 设置自动缩放Y轴幅值阀值因子,amplitudeMode != AmplitudeMode.FIXED时生效
503 | * @param factor: 控制自动缩放阀值,取值范围(0f until 1f),值越大,缩放阀值越小
504 | *
505 | * */
506 | fun setAutoAmplitudeFactor(factor: Float) {
507 | require(factor >= 0f && factor < 1f)
508 | autoAmplitudeFactor = factor
509 | }
510 |
511 | /** 设置x轴y轴限定线条数 */
512 | fun setLimitLineCount(xLimitLineCount: Int, yLimitLineCount: Int) {
513 | this.xLimitLineCount = xLimitLineCount
514 | this.yLimitLineCount = yLimitLineCount
515 | if (xLimitLineCount < 2) {
516 | if (yAxisList.size < 2) {
517 | throw IllegalArgumentException("xLimitLineCount must be greater than 1 !")
518 | }
519 | }
520 | invalidate()
521 | }
522 |
523 | /**
524 | * 设置折线数据
525 | *
526 | * @param yAxisList Y轴数据集合
527 | * @param amplitudeMode 幅值计算模式,默认AmplitudeMode.FIXED
528 | * @see AmplitudeMode
529 | * @param textConverter 文本转换
530 | *
531 | * */
532 | @JvmOverloads
533 | fun setData(
534 | yAxisList: List,
535 | amplitudeMode: AmplitudeMode = AmplitudeMode.FIXED,
536 | textConverter: (Float) -> String = this.textConverter
537 | ) {
538 | if (yAxisList.size < 2) {
539 | throw IllegalArgumentException("yAxisList.size must be greater than 1 !")
540 | }
541 | this.yAxisList.clear()
542 | this.yAxisList.addAll(yAxisList.map { AxisInfo(it.value, this.textConverter(it.value)) })
543 | this.textConverter = textConverter
544 | updateAmplitude()
545 | if (yMax <= yMin) {
546 | throw IllegalArgumentException("yMax must be greater than yMin! yMax = the first element of yAxisList, yMin = the last element of yAxisList !")
547 | }
548 | setAmplitudeMode(amplitudeMode)
549 | }
550 |
551 | private fun updateAmplitude() {
552 | yMax = this.yAxisList.first().value
553 | yMin = this.yAxisList.last().value
554 | if (debugLineChart) {
555 | Log.d(tag, "updateAmplitude:yMax=$yMax, yMin=$yMin")
556 | }
557 | }
558 | }
559 |
--------------------------------------------------------------------------------
/library/src/main/java/com/zhzc0x/chart/LineChartView.kt:
--------------------------------------------------------------------------------
1 | package com.zhzc0x.chart
2 |
3 | import android.animation.ValueAnimator
4 | import android.annotation.SuppressLint
5 | import android.content.Context
6 | import android.graphics.*
7 | import android.text.TextPaint
8 | import android.util.AttributeSet
9 | import android.util.Log
10 | import android.view.MotionEvent
11 | import android.view.VelocityTracker
12 | import android.view.View
13 | import android.widget.OverScroller
14 | import androidx.core.animation.doOnEnd
15 | import androidx.core.view.ViewCompat
16 | import androidx.core.view.doOnPreDraw
17 | import com.zhzc0x.chart.ext.dp
18 | import com.zhzc0x.chart.ext.scale
19 | import kotlin.math.abs
20 |
21 | class LineChartView @JvmOverloads constructor(
22 | context: Context,
23 | attrs: AttributeSet?,
24 | defStyleAttr: Int = 0
25 | ) : View(context, attrs, defStyleAttr) {
26 |
27 | private val tag = LineChartView::class.java.simpleName
28 |
29 | private var showXAxis = true
30 | private var xAxisColor = Color.GRAY
31 | private var xAxisWidth = 1f.dp
32 | private var showXText = true
33 | private var xTextColor = 0
34 | private var xTextSize = 10f.dp
35 | private var showXScaleLine = true
36 | private var xScaleLineColor = 0
37 | private var xScaleLineWidth = 1f.dp
38 | private var xScaleLineLength = 4f.dp
39 |
40 | private var showYAxis = true
41 | private var yAxisColor = Color.GRAY
42 | private var yAxisWidth = 1f.dp
43 | private var showYText = true
44 | private var yTextColor = 0
45 | private var yTextSize = 10f.dp
46 | private var yTextAlign = TextAlign.LEFT
47 | private var showYScaleLine = true
48 | private var yScaleLineColor = 0
49 | private var yScaleLineWidth = 1f.dp
50 | private var yScaleLineLength = 4f.dp
51 |
52 | private var showAxisArrow = true
53 | private var axisArrowWidth = 6f.dp
54 | private var axisArrowHeight = 3f.dp
55 | private var axisArrowColor = Color.GRAY
56 |
57 | private var limitLineColor = Color.GRAY
58 | private var limitLineWidth = 1f.dp
59 | private var limitLineLength = 2f.dp
60 | private var limitLineSpace = 2f.dp
61 |
62 | private var lineChartWidth = 1.5f.dp
63 | private var lineChartColor = Color.LTGRAY
64 | private var lineChartPaddingTop = 30.dp
65 | private var lineChartPaddingBottom = 15.dp
66 | private var lineChartPaddingStart = 30.dp
67 | private var lineChartBgColor = Color.WHITE
68 | private var drawCurve = false//绘制曲线
69 | private var showLineChartAnim = false
70 |
71 | private var showLineChartPoint = false
72 | private var pointRadius = 3f.dp
73 | private var pointColor = Color.WHITE
74 | private var pointStrokeWidth = 0f
75 | private var pointStrokeColor = Color.WHITE
76 | private var pointSelectedRadius = 0f
77 | private var pointSelectedColor = 0
78 | private var pointSelectedStrokeWidth = 0f
79 | private var pointSelectedStrokeColor = 0
80 | private var pointSelectedOutStrokeWidth = 0f
81 | private var pointSelectedOutStrokeColor = Color.parseColor("#99FFFFFF")
82 | private var pointXStart = 0
83 | private var pointXEnd = 0
84 |
85 | private var showPointFloatBox = true
86 | private var floatBoxPadding = 4f.dp
87 | private var floatBoxColor = Color.WHITE
88 | private var floatBoxTextColor = Color.GRAY
89 | private var floatBoxTextSize = 12f.dp
90 |
91 | private lateinit var linePaint: Paint
92 | private lateinit var lineChartPaint: Paint
93 | private lateinit var limitPaint: Paint
94 | private lateinit var pointPaint: Paint
95 | private lateinit var textPaint: TextPaint
96 |
97 | //折线点对应的数据
98 | private var pointList: List = ArrayList()
99 | private var limitArray: List? = null
100 | //原点坐标x
101 | private var originX = 0f
102 | //原点坐标y
103 | private var originY = 0f
104 | //折线点间距
105 | private var pointSpace = 0f
106 | private var drawWidth = 0f
107 | private var drawHeight = 0f
108 | private var slideX = 0f
109 | private var maxSlideX = 0f
110 | private var minSlideX = 0f
111 | private var selectedIndex = -1
112 | private var xMin = 0f
113 | private var xMax = 0f
114 | //x轴坐标对应的数据
115 | private var xAxisList: List = ArrayList()
116 | set(value) {
117 | field = value
118 | xMin = field.first().value
119 | xMax = field.last().value
120 | }
121 | private var yMin = 0f
122 | private var yMax = 0f
123 | //y轴坐标对应的数据
124 | private var yAxisList: List = ArrayList()
125 | set(value) {
126 | field = value
127 | yMin = field.first().value
128 | yMax = field.last().value
129 | }
130 | private var lineChartPath = Path()
131 | private var limitLinePath: Path? = null
132 | private var textBoxPath: Path? = null
133 | private var xAxisArrowPath: Path? = null
134 | private var yAxisArrowPath: Path? = null
135 | private var saveRect = RectF()
136 | private var clearRect = RectF()
137 | private var xTextHeight = 0
138 | private var yTextHeight = 0
139 |
140 | private var showPointList: List? = null
141 |
142 | init {
143 | if (attrs != null) {
144 | initCustomAttrs(context, attrs)
145 | }
146 | initPaint()
147 | val textRect = Rect()
148 | textPaint.textSize = xTextSize
149 | val text = "TEXT"
150 | textPaint.getTextBounds(text, 0, text.length, textRect)
151 | xTextHeight = textRect.height()
152 | textPaint.textSize = yTextSize
153 | textPaint.getTextBounds(text, 0, text.length, textRect)
154 | yTextHeight = textRect.height()
155 | }
156 |
157 | private fun initCustomAttrs(context: Context, attrs: AttributeSet) {
158 | val ta = context.obtainStyledAttributes(attrs, R.styleable.LineChartView)
159 | showXAxis = ta.getBoolean(R.styleable.LineChartView_showXAxis, showXAxis)
160 | xAxisColor = ta.getColor(R.styleable.LineChartView_xAxisColor, xAxisColor)
161 | xAxisWidth = ta.getDimensionPixelSize(R.styleable.LineChartView_xAxisWidth, xAxisWidth.toInt()).toFloat()
162 | showXText = ta.getBoolean(R.styleable.LineChartView_showXText, showXText)
163 | xTextColor = ta.getColor(R.styleable.LineChartView_xTextColor, xAxisColor)
164 | xTextSize = ta.getDimensionPixelSize(R.styleable.LineChartView_xTextSize, xTextSize.toInt()).toFloat()
165 | showXScaleLine = ta.getBoolean(R.styleable.LineChartView_showXScaleLine, showXScaleLine)
166 | xScaleLineColor = ta.getColor(R.styleable.LineChartView_xScaleLineColor, xAxisColor)
167 | xScaleLineWidth = ta.getDimensionPixelSize(R.styleable.LineChartView_xScaleLineWidth, xScaleLineWidth.toInt()).toFloat()
168 | xScaleLineLength = ta.getDimensionPixelSize(R.styleable.LineChartView_xScaleLineLength, xScaleLineLength.toInt()).toFloat()
169 |
170 | showYAxis = ta.getBoolean(R.styleable.LineChartView_showYAxis, showYAxis)
171 | yAxisColor = ta.getColor(R.styleable.LineChartView_yAxisColor, yAxisColor)
172 | yAxisWidth = ta.getDimensionPixelSize(R.styleable.LineChartView_yAxisWidth, yAxisWidth.toInt()).toFloat()
173 | showYText = ta.getBoolean(R.styleable.LineChartView_showYText, showYText)
174 | yTextColor = ta.getColor(R.styleable.LineChartView_yTextColor, yAxisColor)
175 | yTextSize = ta.getDimensionPixelSize(R.styleable.LineChartView_yTextSize, yTextSize.toInt()).toFloat()
176 | val ordinal = ta.getInt(R.styleable.LineChartView_yTextAlign, yTextAlign.ordinal)
177 | yTextAlign = TextAlign.values()[ordinal]
178 | showYScaleLine = ta.getBoolean(R.styleable.LineChartView_showYScaleLine, showYScaleLine)
179 | yScaleLineColor = ta.getColor(R.styleable.LineChartView_yScaleLineColor, yAxisColor)
180 | yScaleLineWidth = ta.getDimensionPixelSize(R.styleable.LineChartView_yScaleLineWidth, yScaleLineWidth.toInt()).toFloat()
181 | yScaleLineLength = ta.getDimensionPixelSize(R.styleable.LineChartView_yScaleLineLength, yScaleLineLength.toInt()).toFloat()
182 |
183 | showAxisArrow = ta.getBoolean(R.styleable.LineChartView_showAxisArrow, showAxisArrow)
184 | axisArrowWidth = ta.getDimensionPixelSize(R.styleable.LineChartView_axisArrowWidth, axisArrowWidth.toInt()).toFloat()
185 | axisArrowHeight = ta.getDimensionPixelSize(R.styleable.LineChartView_axisArrowHeight, axisArrowHeight.toInt()).toFloat()
186 | axisArrowColor = ta.getColor(R.styleable.LineChartView_axisArrowColor, axisArrowColor)
187 |
188 | limitLineColor = ta.getColor(R.styleable.LineChartView_limitLineColor, limitLineColor)
189 | limitLineWidth = ta.getDimensionPixelSize(R.styleable.LineChartView_limitLineWidth, limitLineWidth.toInt()).toFloat()
190 | limitLineLength = ta.getDimensionPixelSize(R.styleable.LineChartView_limitLineLength, limitLineLength.toInt()).toFloat()
191 | limitLineSpace = ta.getDimensionPixelSize(R.styleable.LineChartView_limitLineSpace, limitLineSpace.toInt()).toFloat()
192 |
193 | lineChartColor = ta.getColor(R.styleable.LineChartView_lineChartColor, lineChartColor)
194 | lineChartWidth = ta.getDimensionPixelSize(R.styleable.LineChartView_lineChartWidth, lineChartWidth.toInt()).toFloat()
195 | lineChartPaddingTop = ta.getDimensionPixelSize(R.styleable.LineChartView_lineChartPaddingTop, lineChartPaddingTop)
196 | lineChartPaddingBottom = ta.getDimensionPixelSize(R.styleable.LineChartView_lineChartPaddingBottom, lineChartPaddingBottom)
197 | lineChartPaddingStart = ta.getDimensionPixelSize(R.styleable.LineChartView_lineChartPaddingStart, lineChartPaddingStart)
198 | lineChartBgColor = ta.getColor(R.styleable.LineChartView_lineChartBgColor, lineChartBgColor)
199 | drawCurve = ta.getBoolean(R.styleable.LineChartView_drawCurve, drawCurve)
200 | showLineChartAnim = ta.getBoolean(R.styleable.LineChartView_showLineChartAnim, showLineChartAnim)
201 |
202 | showLineChartPoint = ta.getBoolean(R.styleable.LineChartView_showLineChartPoint, showLineChartPoint)
203 | pointColor = ta.getColor(R.styleable.LineChartView_pointColor, pointColor)
204 | pointRadius = ta.getDimensionPixelSize(R.styleable.LineChartView_pointRadius, pointRadius.toInt()).toFloat()
205 | pointStrokeColor = ta.getColor(R.styleable.LineChartView_pointStrokeColor, lineChartColor)
206 | pointStrokeWidth = ta.getDimensionPixelSize(R.styleable.LineChartView_pointStrokeWidth, (lineChartWidth / 2).toInt()).toFloat()
207 | pointSelectedStrokeColor = ta.getColor(R.styleable.LineChartView_pointSelectedStrokeColor, lineChartColor)
208 | pointSelectedStrokeWidth = ta.getDimensionPixelSize(
209 | R.styleable.LineChartView_pointSelectedStrokeWidth,
210 | (pointStrokeWidth * 2).toInt()).toFloat()
211 | pointSelectedColor = ta.getColor(R.styleable.LineChartView_pointSelectedColor, pointColor)
212 | pointSelectedRadius = ta.getDimensionPixelSize(
213 | R.styleable.LineChartView_pointSelectedRadius,
214 | (pointRadius + pointStrokeWidth / 2).toInt()).toFloat()
215 | pointSelectedOutStrokeColor = ta.getColor(R.styleable.LineChartView_pointSelectedOutStrokeColor, pointSelectedOutStrokeColor)
216 | pointSelectedOutStrokeWidth = ta.getDimensionPixelSize(R.styleable.LineChartView_pointSelectedOutStrokeWidth, pointSelectedRadius.toInt()).toFloat()
217 | pointXStart = ta.getDimensionPixelSize(R.styleable.LineChartView_pointXStart, pointXStart)
218 | pointXEnd = ta.getDimensionPixelSize(R.styleable.LineChartView_pointXEnd, pointXEnd)
219 |
220 | showPointFloatBox = ta.getBoolean(R.styleable.LineChartView_showPointFloatBox, showPointFloatBox)
221 | floatBoxColor = ta.getColor(R.styleable.LineChartView_floatBoxColor, floatBoxColor)
222 | floatBoxTextColor = ta.getColor(R.styleable.LineChartView_floatBoxTextColor, floatBoxTextColor)
223 | floatBoxTextSize = ta.getDimensionPixelSize(R.styleable.LineChartView_floatBoxTextSize, floatBoxTextSize.toInt()).toFloat()
224 | floatBoxPadding = ta.getDimensionPixelSize(R.styleable.LineChartView_floatBoxPadding, floatBoxPadding.toInt()).toFloat()
225 | ta.recycle()
226 | }
227 |
228 | private fun initPaint() {
229 | linePaint = Paint(Paint.ANTI_ALIAS_FLAG)
230 | linePaint.style = Paint.Style.FILL
231 | linePaint.strokeCap = Paint.Cap.ROUND
232 | lineChartPaint = Paint(Paint.ANTI_ALIAS_FLAG)
233 | lineChartPaint.color = lineChartColor
234 | lineChartPaint.style = Paint.Style.STROKE
235 | lineChartPaint.strokeWidth = lineChartWidth
236 | lineChartPaint.strokeCap = Paint.Cap.ROUND
237 | lineChartPaint.strokeJoin = Paint.Join.ROUND
238 | limitPaint = Paint(Paint.ANTI_ALIAS_FLAG)
239 | limitPaint.color = limitLineColor
240 | limitPaint.style = Paint.Style.STROKE
241 | limitPaint.strokeWidth = limitLineWidth
242 | limitPaint.pathEffect = DashPathEffect(floatArrayOf(limitLineSpace, limitLineLength), 0f)
243 | pointPaint = Paint(Paint.ANTI_ALIAS_FLAG)
244 | pointPaint.strokeCap = Paint.Cap.ROUND
245 | textPaint = TextPaint(Paint.ANTI_ALIAS_FLAG)
246 | }
247 |
248 | private fun initData() {
249 | if (showPointFloatBox) {
250 | selectedIndex = pointList.size - 1
251 | }
252 | if (pointSpace > 0) {
253 | drawWidth = pointSpace * xAxisList.size - pointXEnd
254 | minSlideX = viewWidth - pointSpace * xAxisList.size - pointXStart
255 | //如果大于0,说明绘制没有超过当前View宽度,不需要滑动
256 | if(minSlideX > 0){
257 | minSlideX = maxSlideX
258 | }
259 | } else {
260 | drawWidth = (viewWidth - lineChartPaddingStart).toFloat() - pointXStart - pointXEnd
261 | minSlideX = maxSlideX
262 | }
263 | if (debugLineChart) {
264 | Log.d(tag, "slideX=$slideX, minSlideX=$minSlideX, maxSlideX=$maxSlideX")
265 | }
266 | }
267 |
268 | private var viewWidth = 0
269 | private var viewHeight = 0
270 | override fun onLayout(changed: Boolean, left: Int, top: Int, right: Int, bottom: Int) {
271 | if (!changed) {
272 | super.onLayout(false, left, top, right, bottom)
273 | return
274 | }
275 | viewWidth = width
276 | viewHeight = height
277 | originX = lineChartPaddingStart + yAxisWidth
278 | originY = viewHeight - lineChartPaddingBottom * 1f
279 | drawHeight = originY - lineChartPaddingTop
280 | slideX = originX
281 | maxSlideX = slideX
282 | saveRect.set(0f, 0f, viewWidth.toFloat(), viewHeight.toFloat())
283 | clearRect.set(0f, 0f, originX, viewHeight.toFloat())
284 | super.onLayout(true, left, top, right, bottom)
285 | }
286 |
287 | private val porterDuffMode = PorterDuffXfermode(PorterDuff.Mode.CLEAR)
288 | override fun onDraw(canvas: Canvas) {
289 | super.onDraw(canvas)
290 | if (pointList.isNotEmpty()) {
291 | canvas.drawColor(lineChartBgColor)
292 | drawXAxis(canvas)
293 | drawYAxis(canvas)
294 | drawLimitLine(canvas)
295 | //重新开一个图层
296 | val layerId = canvas.saveLayer(saveRect, null)
297 | if (showLineChartAnim) {
298 | drawAnimBrokenLine(canvas)
299 | } else {
300 | drawLineChart(canvas)
301 | }
302 | if (showPointList == null) {
303 | if(showLineChartPoint){
304 | //绘制所有折现点
305 | drawLineChartPoint(canvas)
306 | }
307 | } else {
308 | //绘制指定折现点
309 | drawShowPointList(canvas)
310 | }
311 |
312 | // 将折线超出x轴坐标的部分截取掉
313 | linePaint.xfermode = porterDuffMode
314 | canvas.drawRect(clearRect, linePaint)
315 | linePaint.xfermode = null
316 | //保存图层
317 | canvas.restoreToCount(layerId)
318 | }
319 | }
320 |
321 | private fun drawXAxis(canvas: Canvas) {
322 | if (showXAxis) {
323 | linePaint.color = xAxisColor
324 | linePaint.strokeWidth = xAxisWidth
325 | val stopX = if (showAxisArrow) {
326 | viewWidth.toFloat() - axisArrowWidth / 2
327 | } else {
328 | viewWidth.toFloat()
329 | }
330 | canvas.drawLine(originX, originY, stopX, originY, linePaint)
331 | if (debugLineChart) {
332 | Log.d(tag,"stopX = $stopX")
333 | }
334 | if (showAxisArrow) {
335 | linePaint.color = axisArrowColor
336 | if (xAxisArrowPath == null) {
337 | xAxisArrowPath = Path()
338 | xAxisArrowPath!!.moveTo(viewWidth.toFloat() - axisArrowWidth, (originY - axisArrowHeight))
339 | xAxisArrowPath!!.lineTo(viewWidth.toFloat() - xAxisWidth, originY * 1f)
340 | xAxisArrowPath!!.lineTo(viewWidth.toFloat() - axisArrowWidth, (originY + axisArrowHeight))
341 | }
342 | canvas.drawPath(xAxisArrowPath!!, linePaint)
343 | }
344 | }
345 | if (!showXScaleLine && !showXText) {
346 | return
347 | }
348 | xAxisList.forEach { axisInfo ->
349 | val x = getDrawX(axisInfo.value)
350 | if (x < originX) {
351 | return@forEach
352 | }
353 | if (showXScaleLine) {
354 | linePaint.color = xScaleLineColor
355 | linePaint.strokeWidth = xScaleLineWidth
356 | canvas.drawLine(x, originY + xAxisWidth / 2f, x, originY + xAxisWidth / 2f + xScaleLineLength, linePaint)
357 | }
358 |
359 | if (showXText) {
360 | textPaint.color = xTextColor
361 | textPaint.textSize = xTextSize
362 | textPaint.textAlign = Paint.Align.CENTER
363 | canvas.drawText(axisInfo.showText, 0, axisInfo.showText.length, x,
364 | originY + (lineChartPaddingBottom + xTextHeight) / 2f, textPaint)
365 | }
366 | }
367 | }
368 |
369 | private fun drawYAxis(canvas: Canvas) {
370 | if (showYAxis) {
371 | linePaint.color = yAxisColor
372 | linePaint.strokeWidth = yAxisWidth
373 | val stopY = if (showAxisArrow) {
374 | axisArrowHeight
375 | } else {
376 | 0f
377 | }
378 | canvas.drawLine(originX, originY - xAxisWidth, originX, stopY, linePaint)
379 | if (showAxisArrow) {
380 | linePaint.color = axisArrowColor
381 | if(yAxisArrowPath == null){
382 | yAxisArrowPath = Path()
383 | }
384 | yAxisArrowPath!!.moveTo((originX - axisArrowHeight), axisArrowWidth)
385 | yAxisArrowPath!!.lineTo((originX), yAxisWidth)
386 | yAxisArrowPath!!.lineTo((originX + axisArrowHeight), axisArrowWidth)
387 | canvas.drawPath(yAxisArrowPath!!, linePaint)
388 | }
389 | }
390 | if (!showYScaleLine && !showYText) {
391 | return
392 | }
393 | yAxisList.forEachIndexed { _, axisInfo ->
394 | if (showYScaleLine) {
395 | linePaint.color = yScaleLineColor
396 | linePaint.strokeWidth = yScaleLineWidth
397 | val y = getDrawY(axisInfo.value)
398 | canvas.drawLine(originX, y, originX - yScaleLineLength, y, linePaint)
399 | }
400 | if (showYText) {
401 | textPaint.color = yTextColor
402 | textPaint.textSize = yTextSize
403 | textPaint.textAlign = when (yTextAlign) {
404 | TextAlign.LEFT -> Paint.Align.LEFT
405 | TextAlign.CENTER -> Paint.Align.CENTER
406 | TextAlign.RIGHT -> Paint.Align.RIGHT
407 | }
408 | val textX = when (yTextAlign) {
409 | TextAlign.LEFT -> yScaleLineLength
410 | TextAlign.CENTER -> (originX - yScaleLineLength) / 2
411 | TextAlign.RIGHT -> originX - yScaleLineLength * 2
412 | }
413 | canvas.drawText(axisInfo.showText, textX,
414 | getDrawY(axisInfo.value) + yTextHeight / 2 - yAxisWidth, textPaint)
415 | }
416 | }
417 | }
418 |
419 | fun setLimitArray(limitArray: List) {
420 | this.limitArray = limitArray
421 | }
422 |
423 | private fun drawLimitLine(canvas: Canvas) {
424 | if (limitArray != null) {
425 | if(limitLinePath == null){
426 | limitLinePath = Path()
427 | } else {
428 | limitLinePath!!.reset()
429 | }
430 | limitArray!!.forEach {
431 | val y = getDrawY(it)
432 | limitLinePath!!.moveTo(originX, y)
433 | limitLinePath!!.lineTo(viewWidth.toFloat(), y)
434 | }
435 | canvas.drawPath(limitLinePath!!, limitPaint)
436 | }
437 | }
438 |
439 | /**
440 | * 绘制指定折线点
441 | *
442 | * @param canvas
443 | */
444 | private fun drawShowPointList(canvas: Canvas){
445 | var x: Float
446 | var y: Float
447 | showPointList!!.forEach { showPoint ->
448 | x = getDrawX(showPoint.x)
449 | y = getDrawY(showPoint.y)
450 | pointPaint.style = Paint.Style.FILL
451 | pointPaint.color = showPoint.color
452 | canvas.drawCircle(x, y, showPoint.radius, pointPaint)
453 | if (showPoint.strokeWidth > 0) {
454 | pointPaint.style = Paint.Style.STROKE
455 | pointPaint.strokeWidth = showPoint.strokeWidth
456 | pointPaint.color = showPoint.strokeColor
457 | canvas.drawCircle(x, y, showPoint.radius, pointPaint)
458 | }
459 | textPaint.color = showPoint.textColor
460 | textPaint.textSize = showPoint.textSize
461 | textPaint.textAlign = Paint.Align.CENTER
462 | canvas.drawText(showPoint.text, x, y - showPoint.textPadding - showPoint.radius, textPaint)
463 | }
464 | }
465 |
466 | /**
467 | * 绘制所有折线点
468 | *
469 | * @param canvas
470 | */
471 | private fun drawLineChartPoint(canvas: Canvas) {
472 | var x: Float
473 | var y: Float
474 | //绘制普通的折线点
475 | pointList.forEach { point ->
476 | x = getDrawX(point.x)
477 | y = getDrawY(point.y)
478 | pointPaint.style = Paint.Style.FILL
479 | pointPaint.color = pointColor
480 | canvas.drawCircle(x, y, pointRadius, pointPaint)
481 | if (pointStrokeWidth > 0) {
482 | pointPaint.style = Paint.Style.STROKE
483 | pointPaint.strokeWidth = pointStrokeWidth
484 | pointPaint.color = pointStrokeColor
485 | canvas.drawCircle(x, y, pointRadius, pointPaint)
486 | }
487 | }
488 | //绘制选中的折线点
489 | if (showPointFloatBox && selectedIndex >= 0) {
490 | val selectedPoint = pointList[selectedIndex]
491 | val selectedX = getDrawX(selectedPoint.x)
492 | val selectedY = getDrawY(selectedPoint.y)
493 | pointPaint.style = Paint.Style.FILL
494 | pointPaint.color = pointSelectedOutStrokeColor
495 | pointPaint.strokeWidth = pointSelectedOutStrokeWidth
496 | canvas.drawCircle(selectedX, selectedY, pointSelectedRadius + pointSelectedOutStrokeWidth, pointPaint)
497 | pointPaint.color = pointSelectedColor
498 | canvas.drawCircle(selectedX, selectedY, pointSelectedRadius, pointPaint)
499 | if (pointSelectedStrokeWidth > 0) {
500 | pointPaint.style = Paint.Style.STROKE
501 | pointPaint.strokeWidth = pointSelectedStrokeWidth
502 | pointPaint.color = pointSelectedStrokeColor
503 | canvas.drawCircle(selectedX, selectedY, pointSelectedRadius, pointPaint)
504 | }
505 | drawFloatTextBox(canvas, selectedX, selectedY - floatBoxPadding * 2, selectedPoint.y)
506 | }
507 | }
508 |
509 | /**
510 | * 绘制显示Y值的浮动框
511 | *
512 | */
513 | private fun drawFloatTextBox(canvas: Canvas, x: Float, y: Float, value: Float) {
514 | pointPaint.color = floatBoxColor
515 | pointPaint.style = Paint.Style.FILL
516 | val text = value.scale(2).toString()
517 | val rect = Rect()
518 | textPaint.color = floatBoxTextColor
519 | textPaint.textSize = floatBoxTextSize
520 | textPaint.textAlign = Paint.Align.CENTER
521 | textPaint.getTextBounds(text, 0, text.length, rect)
522 |
523 | val boxWidth = rect.width() / 2f + floatBoxPadding
524 | val boxHeight = rect.height() + floatBoxPadding * 2
525 | val cornerPathEffect = CornerPathEffect(2.5f.dp)
526 | pointPaint.pathEffect = cornerPathEffect
527 | if (textBoxPath == null) {
528 | textBoxPath = Path()
529 | } else {
530 | textBoxPath!!.reset()
531 | }
532 | textBoxPath!!.moveTo(x, y)
533 | textBoxPath!!.lineTo(x - floatBoxPadding, y - floatBoxPadding)
534 | textBoxPath!!.lineTo(x - boxWidth, y - floatBoxPadding)
535 | textBoxPath!!.lineTo(x - boxWidth, y - boxHeight - floatBoxPadding)
536 | textBoxPath!!.lineTo(x + boxWidth, y - boxHeight - floatBoxPadding)
537 | textBoxPath!!.lineTo(x + boxWidth, y - floatBoxPadding)
538 | textBoxPath!!.lineTo(x + floatBoxPadding, y - floatBoxPadding)
539 | textBoxPath!!.lineTo(x, y)
540 | canvas.drawPath(textBoxPath!!, pointPaint)
541 | canvas.drawText(text, x, y - boxHeight / 2, textPaint)
542 | }
543 |
544 | private var startX: Float = 0f
545 | private var startY: Float = 0f
546 | private var endX: Float = 0f
547 | private var endY: Float = 0f
548 | private fun drawLineChart(canvas: Canvas) {
549 | lineChartPath.reset()
550 | pointList.forEachIndexed { index, point ->
551 | endX = getDrawX(point.x)
552 | endY = getDrawY(point.y)
553 | if (drawCurve) {
554 | //绘制曲线(三阶贝塞尔曲线)
555 | if (index == 0) {
556 | lineChartPath.moveTo(endX, endY)
557 | } else {
558 | val referX = (startX + endX) / 2
559 | lineChartPath.cubicTo(referX, startY, referX, endY, endX, endY)
560 | }
561 | startX = endX
562 | startY = endY
563 | } else {
564 | //绘制折线
565 | if (index == 0) {
566 | lineChartPath.moveTo(endX, endY)
567 | } else {
568 | lineChartPath.lineTo(endX, endY)
569 | }
570 | }
571 | }
572 | canvas.drawPath(lineChartPath, lineChartPaint)
573 | }
574 |
575 | private var currentAnimValue: Float = -1f
576 | private var animPathMeasure: PathMeasure? = null
577 | private fun drawAnimBrokenLine(canvas: Canvas) {
578 | if (currentAnimValue == -1f) {
579 | lineChartPath.reset()
580 | pointList.forEachIndexed { index, point ->
581 | endX = getDrawX(point.x)
582 | endY = getDrawY(point.y)
583 | if (drawCurve) {
584 | //绘制曲线(三阶贝塞尔曲线)
585 | if (index == 0) {
586 | lineChartPath.moveTo(endX, endY)
587 | } else {
588 | val referX = (startX + endX) / 2
589 | lineChartPath.cubicTo(referX, startY, referX, endY, endX, endY)
590 | }
591 | startX = endX
592 | startY = endY
593 | } else {
594 | //绘制折线
595 | if (index == 0) {
596 | lineChartPath.moveTo(endX, endY)
597 | } else {
598 | lineChartPath.lineTo(endX, endY)
599 | }
600 | }
601 | }
602 | animPathMeasure = PathMeasure(lineChartPath, false)
603 | val animator = ValueAnimator.ofFloat(1f, 0f).setDuration(drawWidth.toLong())
604 | animator.addUpdateListener {
605 | currentAnimValue = it.animatedValue as Float
606 | invalidate()
607 | }
608 | animator.doOnEnd {
609 | showLineChartAnim = false
610 | lineChartPaint.pathEffect = null
611 | animPathMeasure = null
612 | }
613 | animator.start()
614 | } else {
615 | val effect = DashPathEffect(floatArrayOf(animPathMeasure!!.length,
616 | animPathMeasure!!.length), animPathMeasure!!.length * currentAnimValue)
617 | lineChartPaint.pathEffect = effect
618 | canvas.drawPath(lineChartPath, lineChartPaint)
619 | }
620 | }
621 |
622 | private fun getDrawX(pointX: Float): Float {
623 | return slideX + drawWidth * ((pointX - xMin) / (xMax - xMin)) + pointXStart
624 | }
625 |
626 | private fun getDrawY(pointY: Float): Float {
627 | //处理负值的情况,但是point的y值必须在最大值和最小值之间
628 | return drawHeight - drawHeight * ((pointY - yMin) / (yMax - yMin)) + lineChartPaddingTop
629 | }
630 |
631 | enum class SlideSate {
632 | LEFT_AND_RIGHT, UP_AND_DOWN, NONE
633 | }
634 |
635 | private var slideSate = SlideSate.NONE
636 | private var velocityTracker: VelocityTracker? = null
637 | private val scroller = OverScroller(context)
638 | private var startTouchX: Float = 0f
639 | private var downX: Float = 0f
640 | private var downY: Float = 0f
641 | private var distanceX = 0f
642 | private var distanceY = 0f
643 | @SuppressLint("ClickableViewAccessibility")
644 | override fun onTouchEvent(event: MotionEvent): Boolean {
645 | //判断动画播放中和禁止滑动
646 | if (currentAnimValue > 0f) {
647 | return false
648 | }
649 | if (velocityTracker == null) {
650 | velocityTracker = VelocityTracker.obtain()
651 | }
652 | velocityTracker!!.addMovement(event)
653 | when (event.action) {
654 | MotionEvent.ACTION_DOWN -> {
655 | downX = event.x
656 | downY = event.y
657 | }
658 | MotionEvent.ACTION_MOVE -> {
659 | if (slideSate == SlideSate.NONE) {
660 | distanceX = abs(event.x - downX)
661 | distanceY = abs(event.y - downY)
662 | if (debugLineChart) {
663 | Log.d(tag,"downX=$downX,downY=$downY, distanceX=$distanceX,distanceY=$distanceY")
664 | }
665 | if (distanceX > 20 && distanceX > distanceY) {
666 | startTouchX = event.x
667 | slideSate = SlideSate.LEFT_AND_RIGHT
668 | }
669 | if (distanceY > 20 && distanceY > distanceX) {
670 | slideSate = SlideSate.UP_AND_DOWN
671 | }
672 | } else if (slideSate == SlideSate.UP_AND_DOWN) {
673 | parent.requestDisallowInterceptTouchEvent(false)
674 | } else if (slideSate == SlideSate.LEFT_AND_RIGHT) {
675 | parent.requestDisallowInterceptTouchEvent(true)
676 | val moveX = event.x - startTouchX
677 | startTouchX = event.x
678 | slideX = when {
679 | slideX + moveX < minSlideX -> minSlideX
680 | slideX + moveX > maxSlideX -> maxSlideX
681 | else -> slideX + moveX
682 | }
683 | invalidate()
684 | }
685 | }
686 | MotionEvent.ACTION_UP, MotionEvent.ACTION_CANCEL -> {
687 | slideSate = SlideSate.NONE
688 | if (showPointFloatBox) {
689 | clickAction(event)
690 | }
691 | velocityTracker!!.computeCurrentVelocity(500)
692 | val velocityY = velocityTracker!!.yVelocity.toInt()
693 | val velocityX = velocityTracker!!.xVelocity.toInt()
694 | val y = event.y.toInt()
695 | scroller.fling(slideX.toInt(), y, velocityX, velocityY, minSlideX.toInt(), maxSlideX.toInt(), y, y)
696 | ViewCompat.postOnAnimation(this, flingRunnable)
697 | velocityTracker!!.recycle()
698 | velocityTracker = null
699 | }
700 | }
701 | return true
702 | }
703 |
704 | private val flingRunnable = Runnable {
705 | if (slideSate == SlideSate.NONE && scroller.computeScrollOffset()) {
706 | slideX = scroller.currX.toFloat()
707 | invalidate()
708 | postOnAnimation()
709 | }
710 | }
711 |
712 | private fun postOnAnimation() {
713 | postOnAnimation(flingRunnable)
714 | }
715 |
716 | /**
717 | * 点击X轴坐标或者折线节点
718 | *
719 | */
720 | private val clickArea = 8f.dp
721 | private fun clickAction(event: MotionEvent) {
722 | val eventX = event.x
723 | val eventY = event.y
724 | for (index in pointList.indices) {
725 | val point = pointList[index]
726 | val x = getDrawX(point.x)
727 | val y = getDrawY(point.y)
728 | //每个节点周围8dp都是可点击区域
729 | if (eventX >= x - clickArea && eventX <= x + clickArea && eventY >= y - clickArea
730 | && eventY <= y + clickArea && selectedIndex != index) {
731 | selectedIndex = index
732 | invalidate()
733 | return
734 | }
735 | }
736 | }
737 |
738 | override fun onDetachedFromWindow() {
739 | super.onDetachedFromWindow()
740 | xAxisArrowPath?.reset()
741 | xAxisArrowPath = null
742 | yAxisArrowPath?.reset()
743 | yAxisArrowPath = null
744 | limitLinePath?.reset()
745 | limitLinePath = null
746 | textBoxPath?.reset()
747 | textBoxPath = null
748 | lineChartPath.reset()
749 | }
750 |
751 | /**
752 | * 设置是否绘制曲线
753 | * @see R.attr.drawCurve
754 | * */
755 | fun setDrawCurve(drawCurve: Boolean) {
756 | this.drawCurve = drawCurve
757 | invalidate()
758 | }
759 |
760 | /**
761 | * 设置是否显示折线点
762 | * @see R.attr.showLineChartPoint
763 | * */
764 | fun setShowLineChartPoint(show: Boolean) {
765 | showLineChartPoint = show
766 | invalidate()
767 | }
768 |
769 | /**
770 | * 设置显示折线动画
771 | * @see R.attr.showLineChartAnim
772 | * */
773 | fun showLineChartAnim() {
774 | if(!showLineChartAnim){
775 | currentAnimValue = -1f
776 | showLineChartAnim = true
777 | invalidate()
778 | }
779 | }
780 |
781 | /**
782 | * 设置显示指定折线点信息list
783 | * 设置后showLineChartPoint和showPointFloatBox属性失效,设置null后恢复
784 | *
785 | * */
786 | fun setShowPoints(showPointList: List?) {
787 | this.showPointList = showPointList
788 | invalidate()
789 | }
790 |
791 | /**
792 | * 设置折线点绘制开始和结束的位置
793 | * @see R.attr.pointXStart = startDp
794 | * @see R.attr.pointXEnd = endDp
795 | *
796 | * */
797 | fun setPointXInit(startDp: Int, endDp: Int) {
798 | pointXStart = startDp.dp
799 | pointXEnd = endDp.dp
800 | //重新初始化绘制数据
801 | initData()
802 | invalidate()
803 | }
804 |
805 | /**
806 | * 设置折线数据
807 | *
808 | * @param pointList 点的集合
809 | * @param xAxisList X轴数据集合,设置为null时=pointList.map { AxisInfo(it.x)}
810 | * @param yAxisList Y轴数据集合
811 | * @param pointSpace 点的间距
812 | *
813 | * */
814 | @JvmOverloads
815 | fun setData(
816 | pointList: List,
817 | xAxisList: List? = null,
818 | yAxisList: List,
819 | pointSpace: Float = 0f
820 | ) {
821 | this.pointList = pointList.sortedBy { it.x }
822 | this.xAxisList = xAxisList?.sortedBy { it.value } ?: this.pointList.map { AxisInfo(it.x) }
823 | this.yAxisList = yAxisList.sortedBy { it.value }
824 | this.pointSpace = pointSpace
825 | doOnPreDraw {
826 | initData()
827 | invalidate()
828 | }
829 | }
830 | }
831 |
--------------------------------------------------------------------------------