├── app ├── .gitignore ├── src │ ├── main │ │ ├── res │ │ │ ├── values │ │ │ │ ├── strings.xml │ │ │ │ ├── themes.xml │ │ │ │ └── colors.xml │ │ │ ├── values-night │ │ │ │ └── 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 │ │ │ ├── drawable-v24 │ │ │ │ └── ic_launcher_foreground.xml │ │ │ └── drawable │ │ │ │ └── ic_launcher_background.xml │ │ ├── java │ │ │ └── ru │ │ │ │ └── shirobokov │ │ │ │ └── marketchart │ │ │ │ ├── chart │ │ │ │ ├── Candle.kt │ │ │ │ ├── MarketChartState.kt │ │ │ │ └── MarketChart.kt │ │ │ │ └── ChartActivity.kt │ │ └── AndroidManifest.xml │ ├── test │ │ └── java │ │ │ └── ru │ │ │ └── shirobokov │ │ │ └── marketchart │ │ │ └── ExampleUnitTest.kt │ └── androidTest │ │ └── java │ │ └── ru │ │ └── shirobokov │ │ └── marketchart │ │ └── ExampleInstrumentedTest.kt ├── proguard-rules.pro └── build.gradle ├── README.md ├── screenshot └── image.gif ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── .gitignore ├── settings.gradle ├── gradle.properties ├── gradlew.bat └── gradlew /app/.gitignore: -------------------------------------------------------------------------------- 1 | /build -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## Рыночный график на Compose 2 | 3 | ![](screenshot/image.gif) 4 | -------------------------------------------------------------------------------- /screenshot/image.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ShirobokovNE/MarketChart/HEAD/screenshot/image.gif -------------------------------------------------------------------------------- /app/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | MarketChart 3 | -------------------------------------------------------------------------------- /app/src/main/res/values/themes.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | -------------------------------------------------------------------------------- /app/src/main/res/values-night/themes.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ShirobokovNE/MarketChart/HEAD/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /app/src/main/res/mipmap-hdpi/ic_launcher.webp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ShirobokovNE/MarketChart/HEAD/app/src/main/res/mipmap-hdpi/ic_launcher.webp -------------------------------------------------------------------------------- /app/src/main/res/mipmap-mdpi/ic_launcher.webp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ShirobokovNE/MarketChart/HEAD/app/src/main/res/mipmap-mdpi/ic_launcher.webp -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xhdpi/ic_launcher.webp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ShirobokovNE/MarketChart/HEAD/app/src/main/res/mipmap-xhdpi/ic_launcher.webp -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxhdpi/ic_launcher.webp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ShirobokovNE/MarketChart/HEAD/app/src/main/res/mipmap-xxhdpi/ic_launcher.webp -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxxhdpi/ic_launcher.webp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ShirobokovNE/MarketChart/HEAD/app/src/main/res/mipmap-xxxhdpi/ic_launcher.webp -------------------------------------------------------------------------------- /app/src/main/res/mipmap-hdpi/ic_launcher_round.webp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ShirobokovNE/MarketChart/HEAD/app/src/main/res/mipmap-hdpi/ic_launcher_round.webp -------------------------------------------------------------------------------- /app/src/main/res/mipmap-mdpi/ic_launcher_round.webp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ShirobokovNE/MarketChart/HEAD/app/src/main/res/mipmap-mdpi/ic_launcher_round.webp -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xhdpi/ic_launcher_round.webp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ShirobokovNE/MarketChart/HEAD/app/src/main/res/mipmap-xhdpi/ic_launcher_round.webp -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxhdpi/ic_launcher_round.webp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ShirobokovNE/MarketChart/HEAD/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.webp -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.webp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ShirobokovNE/MarketChart/HEAD/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.webp -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.iml 2 | .gradle 3 | /local.properties 4 | .idea 5 | .DS_Store 6 | /build 7 | /captures 8 | .externalNativeBuild 9 | .cxx 10 | local.properties 11 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Sun Sep 19 09:42:06 MSK 2021 2 | distributionBase=GRADLE_USER_HOME 3 | distributionUrl=https\://services.gradle.org/distributions/gradle-7.0.2-bin.zip 4 | distributionPath=wrapper/dists 5 | zipStorePath=wrapper/dists 6 | zipStoreBase=GRADLE_USER_HOME 7 | -------------------------------------------------------------------------------- /app/src/main/res/mipmap-anydpi-v26/ic_launcher.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /app/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /settings.gradle: -------------------------------------------------------------------------------- 1 | dependencyResolutionManagement { 2 | repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS) 3 | repositories { 4 | google() 5 | mavenCentral() 6 | jcenter() // Warning: this repository is going to shut down soon 7 | } 8 | } 9 | rootProject.name = "MarketChart" 10 | include ':app' 11 | -------------------------------------------------------------------------------- /app/src/main/java/ru/shirobokov/marketchart/chart/Candle.kt: -------------------------------------------------------------------------------- 1 | package ru.shirobokov.marketchart.chart 2 | 3 | import java.time.LocalDateTime 4 | 5 | data class Candle( 6 | val time: LocalDateTime, 7 | val open: Float, 8 | val close: Float, 9 | val high: Float, 10 | val low: Float 11 | ) : Comparable { 12 | 13 | override fun compareTo(other: Candle) = if (time.isBefore(other.time)) -1 else 1 14 | } 15 | -------------------------------------------------------------------------------- /app/src/main/res/values/colors.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | #FFBB86FC 4 | #FF6200EE 5 | #FF3700B3 6 | #FF03DAC5 7 | #FF018786 8 | #FF000000 9 | #FFFFFFFF 10 | -------------------------------------------------------------------------------- /app/src/test/java/ru/shirobokov/marketchart/ExampleUnitTest.kt: -------------------------------------------------------------------------------- 1 | package ru.shirobokov.marketchart 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 | } -------------------------------------------------------------------------------- /app/src/androidTest/java/ru/shirobokov/marketchart/ExampleInstrumentedTest.kt: -------------------------------------------------------------------------------- 1 | package ru.shirobokov.marketchart 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("ru.shirobokov.e", appContext.packageName) 23 | } 24 | } -------------------------------------------------------------------------------- /app/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 -------------------------------------------------------------------------------- /app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 11 | 12 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | -------------------------------------------------------------------------------- /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 | # Automatically convert third-party libraries to use AndroidX 19 | android.enableJetifier=true 20 | # Kotlin code style for this project: "official" or "obsolete": 21 | kotlin.code.style=official -------------------------------------------------------------------------------- /app/src/main/java/ru/shirobokov/marketchart/ChartActivity.kt: -------------------------------------------------------------------------------- 1 | package ru.shirobokov.marketchart 2 | 3 | import MarketChart 4 | import android.os.Bundle 5 | import androidx.activity.compose.setContent 6 | import androidx.appcompat.app.AppCompatActivity 7 | import ru.shirobokov.marketchart.chart.Candle 8 | import java.time.LocalDateTime 9 | 10 | class ChartActivity : AppCompatActivity() { 11 | 12 | override fun onCreate(savedInstanceState: Bundle?) { 13 | super.onCreate(savedInstanceState) 14 | val candles = mutableListOf() 15 | assets.open("quotes.txt").use { 16 | it.bufferedReader().forEachLine { line -> 17 | val splitStrings = line.split(" ") 18 | 19 | val year = splitStrings[0].substring(0, 4).toInt() 20 | val month = splitStrings[0].substring(4, 6).toInt() 21 | val day = splitStrings[0].substring(6, 8).toInt() 22 | val hour = splitStrings[1].substring(0, 2).toInt() 23 | val minute = splitStrings[1].substring(2, 4).toInt() 24 | 25 | val dateTime = LocalDateTime.of(year, month, day, hour, minute) 26 | val open = splitStrings[2].toFloat() 27 | val high = splitStrings[3].toFloat() 28 | val low = splitStrings[4].toFloat() 29 | val close = splitStrings[5].toFloat() 30 | 31 | candles.add(Candle(dateTime, open, close, high, low)) 32 | } 33 | candles.sort() 34 | } 35 | 36 | setContent { 37 | MarketChart(candles) 38 | } 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | plugins { 2 | id("com.android.application") 3 | id("kotlin-android") 4 | } 5 | 6 | android { 7 | compileSdk = 30 8 | buildToolsVersion = "30.0.3" 9 | 10 | defaultConfig { 11 | applicationId = "ru.shirobokov.marketchart" 12 | minSdk = 21 13 | targetSdk = 30 14 | versionCode = 1 15 | versionName = "1.0" 16 | 17 | testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner" 18 | vectorDrawables { 19 | useSupportLibrary = true 20 | } 21 | } 22 | compileOptions { 23 | coreLibraryDesugaringEnabled = true 24 | sourceCompatibility = JavaVersion.VERSION_1_8 25 | targetCompatibility = JavaVersion.VERSION_1_8 26 | } 27 | kotlinOptions { 28 | jvmTarget = "1.8" 29 | } 30 | buildFeatures { 31 | compose = true 32 | } 33 | composeOptions { 34 | kotlinCompilerExtensionVersion = "1.0.2" 35 | } 36 | } 37 | 38 | dependencies { 39 | implementation("androidx.core:core-ktx:1.6.0") 40 | implementation("androidx.appcompat:appcompat:1.3.1") 41 | 42 | implementation("androidx.compose.ui:ui:1.0.4") 43 | implementation("androidx.compose.material:material:1.0.4") 44 | implementation("androidx.compose.ui:ui-tooling:1.0.4") 45 | implementation("androidx.activity:activity-compose:1.3.1") 46 | 47 | coreLibraryDesugaring("com.android.tools:desugar_jdk_libs:1.1.5") 48 | 49 | testImplementation("junit:junit:4.13.2") 50 | androidTestImplementation("androidx.test.ext:junit:1.1.3") 51 | androidTestImplementation("androidx.test.espresso:espresso-core:3.4.0") 52 | } -------------------------------------------------------------------------------- /app/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 | -------------------------------------------------------------------------------- /app/src/main/java/ru/shirobokov/marketchart/chart/MarketChartState.kt: -------------------------------------------------------------------------------- 1 | package ru.shirobokov.marketchart.chart 2 | 3 | import androidx.compose.foundation.gestures.ScrollableState 4 | import androidx.compose.foundation.gestures.TransformableState 5 | import androidx.compose.runtime.derivedStateOf 6 | import androidx.compose.runtime.getValue 7 | import androidx.compose.runtime.mutableStateOf 8 | import androidx.compose.runtime.saveable.Saver 9 | import androidx.compose.runtime.saveable.listSaver 10 | import androidx.compose.runtime.setValue 11 | import kotlin.math.roundToInt 12 | 13 | class MarketChartState { 14 | 15 | private var candles = listOf() 16 | private var visibleCandleCount by mutableStateOf(0) 17 | private var scrollOffset by mutableStateOf(0f) 18 | private var viewWidth = 0f 19 | private var viewHeight = 0f 20 | private var candleInGrid = Float.MAX_VALUE 21 | 22 | private val maxPrice by derivedStateOf { visibleCandles.maxOfOrNull { it.high } ?: 0f } 23 | private val minPrice by derivedStateOf { visibleCandles.minOfOrNull { it.low } ?: 0f } 24 | 25 | val transformableState = TransformableState { zoomChange, _, _ -> scaleView(zoomChange) } 26 | 27 | val scrollableState = ScrollableState { 28 | scrollOffset = if (it > 0) { 29 | (scrollOffset - it.scrolledCandles).coerceAtLeast(0f) 30 | } else { 31 | (scrollOffset - it.scrolledCandles).coerceAtMost(candles.lastIndex.toFloat()) 32 | } 33 | it 34 | } 35 | 36 | private val Float.scrolledCandles: Float 37 | get() = this * visibleCandleCount.toFloat() / viewWidth 38 | 39 | var timeLines by mutableStateOf(listOf()) 40 | 41 | val priceLines by derivedStateOf { 42 | val priceItem = (maxPrice - minPrice) / PRICES_COUNT 43 | mutableListOf().apply { repeat(PRICES_COUNT) { if (it > 0) add(maxPrice - priceItem * it) } } 44 | } 45 | 46 | val visibleCandles by derivedStateOf { 47 | if (candles.isNotEmpty()) { 48 | candles.subList( 49 | scrollOffset.roundToInt().coerceAtLeast(0), 50 | (scrollOffset.roundToInt() + visibleCandleCount).coerceAtMost(candles.size) 51 | ) 52 | } else { 53 | emptyList() 54 | } 55 | } 56 | 57 | private fun scaleView(zoomChange: Float) { 58 | if ((zoomChange < 1f && visibleCandleCount / zoomChange <= MAX_CANDLES) || 59 | (zoomChange > 1f && visibleCandleCount / zoomChange >= MIN_CANDLES) 60 | ) { 61 | visibleCandleCount = (visibleCandleCount / zoomChange).roundToInt() 62 | } 63 | } 64 | 65 | fun setViewSize(width: Float, height: Float) { 66 | viewWidth = width 67 | viewHeight = height 68 | } 69 | 70 | fun calculateGridWidth() { 71 | val candleWidth = viewWidth / visibleCandleCount 72 | val currentGridWidth = candleInGrid * candleWidth 73 | when { 74 | currentGridWidth < MIN_GRID_WIDTH -> { 75 | candleInGrid = MAX_GRID_WIDTH / candleWidth 76 | timeLines = candles.filterIndexed { index, _ -> index % candleInGrid.roundToInt() == 0 } 77 | } 78 | currentGridWidth > MAX_GRID_WIDTH -> { 79 | candleInGrid = MIN_GRID_WIDTH / candleWidth 80 | timeLines = candles.filterIndexed { index, _ -> index % candleInGrid.roundToInt() == 0 } 81 | } 82 | } 83 | } 84 | 85 | fun xOffset(candle: Candle) = viewWidth * visibleCandles.indexOf(candle).toFloat() / visibleCandleCount.toFloat() 86 | fun yOffset(value: Float) = viewHeight * (maxPrice - value) / (maxPrice - minPrice) 87 | 88 | companion object { 89 | private const val MAX_GRID_WIDTH = 500 90 | private const val MIN_GRID_WIDTH = 250 91 | private const val MAX_CANDLES = 100 92 | private const val MIN_CANDLES = 30 93 | private const val START_CANDLES = 60 94 | private const val PRICES_COUNT = 10 95 | 96 | fun getState(candles: List, visibleCandleCount: Int? = null, scrollOffset: Float? = null) = 97 | MarketChartState().apply { 98 | this.candles = candles 99 | this.visibleCandleCount = visibleCandleCount ?: START_CANDLES 100 | this.scrollOffset = scrollOffset ?: candles.size.toFloat() - this.visibleCandleCount 101 | } 102 | 103 | @Suppress("UNCHECKED_CAST") 104 | val Saver: Saver = listSaver( 105 | save = { listOf(it.candles, it.scrollOffset, it.visibleCandleCount) }, 106 | restore = { 107 | getState( 108 | candles = it[0] as List, 109 | visibleCandleCount = it[2] as Int, 110 | scrollOffset = it[1] as Float 111 | ) 112 | } 113 | ) 114 | } 115 | } 116 | -------------------------------------------------------------------------------- /app/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 | -------------------------------------------------------------------------------- /app/src/main/java/ru/shirobokov/marketchart/chart/MarketChart.kt: -------------------------------------------------------------------------------- 1 | import android.graphics.Rect 2 | import androidx.compose.foundation.Canvas 3 | import androidx.compose.foundation.background 4 | import androidx.compose.foundation.gestures.Orientation 5 | import androidx.compose.foundation.gestures.scrollable 6 | import androidx.compose.foundation.gestures.transformable 7 | import androidx.compose.foundation.layout.BoxWithConstraints 8 | import androidx.compose.foundation.layout.fillMaxSize 9 | import androidx.compose.runtime.Composable 10 | import androidx.compose.runtime.saveable.rememberSaveable 11 | import androidx.compose.ui.Modifier 12 | import androidx.compose.ui.geometry.Offset 13 | import androidx.compose.ui.geometry.Size 14 | import androidx.compose.ui.graphics.Color 15 | import androidx.compose.ui.graphics.Paint 16 | import androidx.compose.ui.graphics.PathEffect 17 | import androidx.compose.ui.graphics.drawscope.drawIntoCanvas 18 | import androidx.compose.ui.graphics.nativeCanvas 19 | import androidx.compose.ui.graphics.toArgb 20 | import androidx.compose.ui.tooling.preview.Devices 21 | import androidx.compose.ui.tooling.preview.Preview 22 | import androidx.compose.ui.unit.dp 23 | import androidx.compose.ui.unit.sp 24 | import ru.shirobokov.marketchart.chart.Candle 25 | import ru.shirobokov.marketchart.chart.MarketChartState 26 | import java.text.DecimalFormat 27 | import java.time.LocalDateTime 28 | import java.time.format.DateTimeFormatter 29 | 30 | @Composable 31 | fun MarketChart(candles: List) { 32 | 33 | val state = rememberSaveable(saver = MarketChartState.Saver) { MarketChartState.getState(candles) } 34 | 35 | val decimalFormat = DecimalFormat("##.00") 36 | val timeFormatter = DateTimeFormatter.ofPattern("dd.MM, HH:mm") 37 | val bounds = Rect() 38 | val textPaint = Paint().asFrameworkPaint().apply { 39 | isAntiAlias = true 40 | textSize = 35.sp.value 41 | color = Color.White.toArgb() 42 | } 43 | 44 | BoxWithConstraints(modifier = Modifier.fillMaxSize()) { 45 | val chartWidth = constraints.maxWidth - 128.dp.value 46 | val chartHeight = constraints.maxHeight - 64.dp.value 47 | 48 | state.setViewSize(chartWidth, chartHeight) 49 | state.calculateGridWidth() 50 | 51 | Canvas( 52 | modifier = Modifier 53 | .fillMaxSize() 54 | .background(Color(0xFF182028)) 55 | .scrollable(state.scrollableState, Orientation.Horizontal) 56 | .transformable(state.transformableState) 57 | ) { 58 | drawLine( 59 | color = Color.White, 60 | strokeWidth = 2.dp.value, 61 | start = Offset(0f, chartHeight), 62 | end = Offset(chartWidth, chartHeight) 63 | ) 64 | 65 | drawLine( 66 | color = Color.White, 67 | strokeWidth = 2.dp.value, 68 | start = Offset(chartWidth, 0f), 69 | end = Offset(chartWidth, chartHeight) 70 | ) 71 | 72 | state.timeLines.forEach { candle -> 73 | val offset = state.xOffset(candle) 74 | if (offset !in 0f..chartWidth) return@forEach 75 | drawLine( 76 | color = Color.White, 77 | strokeWidth = 1.dp.value, 78 | start = Offset(offset, 0f), 79 | end = Offset(offset, chartHeight), 80 | pathEffect = PathEffect.dashPathEffect(intervals = floatArrayOf(10f, 20f), phase = 5f) 81 | ) 82 | drawIntoCanvas { 83 | val text = candle.time.format(timeFormatter) 84 | textPaint.getTextBounds(text, 0, text.length, bounds) 85 | val textHeight = bounds.height() 86 | val textWidth = bounds.width() 87 | it.nativeCanvas.drawText( 88 | text, 89 | offset - textWidth / 2, 90 | chartHeight + 8.dp.value + textHeight, 91 | textPaint 92 | ) 93 | } 94 | } 95 | 96 | state.priceLines.forEach { value: Float -> 97 | val yOffset = state.yOffset(value) 98 | val text = decimalFormat.format(value) 99 | drawLine( 100 | color = Color.White, 101 | strokeWidth = 1.dp.value, 102 | start = Offset(0f, yOffset), 103 | end = Offset(chartWidth, yOffset), 104 | pathEffect = PathEffect.dashPathEffect(intervals = floatArrayOf(10f, 20f), phase = 5f) 105 | ) 106 | drawIntoCanvas { 107 | textPaint.getTextBounds(text, 0, text.length, bounds) 108 | val textHeight = bounds.height() 109 | it.nativeCanvas.drawText( 110 | text, 111 | chartWidth + 8.dp.value, 112 | yOffset + textHeight / 2, 113 | textPaint 114 | ) 115 | } 116 | } 117 | 118 | state.visibleCandles.forEach { candle -> 119 | val xOffset = state.xOffset(candle) 120 | drawLine( 121 | color = Color.White, 122 | strokeWidth = 2.dp.value, 123 | start = Offset(xOffset, state.yOffset(candle.low)), 124 | end = Offset(xOffset, state.yOffset(candle.high)) 125 | ) 126 | if (candle.open > candle.close) { 127 | drawRect( 128 | color = Color.Red, 129 | topLeft = Offset(xOffset - 6.dp.value, state.yOffset(candle.open)), 130 | size = Size(12.dp.value, state.yOffset(candle.close) - state.yOffset(candle.open)) 131 | ) 132 | } else { 133 | drawRect( 134 | color = Color.Green, 135 | topLeft = Offset(xOffset - 6.dp.value, state.yOffset(candle.close)), 136 | size = Size(12.dp.value, state.yOffset(candle.open) - state.yOffset(candle.close)) 137 | ) 138 | } 139 | } 140 | } 141 | } 142 | } 143 | 144 | @Preview(device = Devices.PIXEL_2) 145 | @Composable 146 | fun MarketChartPreview() { 147 | MarketChart( 148 | listOf( 149 | Candle(LocalDateTime.now().plusHours(21), 16f, 20f, 22f, 15f), 150 | Candle(LocalDateTime.now().plusHours(20), 11f, 16f, 16f, 10f), 151 | Candle(LocalDateTime.now().plusHours(19), 8f, 10f, 11f, 7f), 152 | Candle(LocalDateTime.now().plusHours(18), 6f, 8f, 9f, 5f), 153 | Candle(LocalDateTime.now().plusHours(17), 10f, 6f, 11f, 6f), 154 | Candle(LocalDateTime.now().plusHours(16), 14f, 10f, 15f, 10f), 155 | Candle(LocalDateTime.now().plusHours(15), 12f, 14f, 15f, 10f), 156 | Candle(LocalDateTime.now().plusHours(14), 10f, 12f, 13f, 10f), 157 | Candle(LocalDateTime.now().plusHours(13), 15f, 10f, 16f, 9f), 158 | Candle(LocalDateTime.now().plusHours(12), 14f, 15f, 15f, 12f), 159 | Candle(LocalDateTime.now().plusHours(11), 14f, 14f, 15f, 10f), 160 | Candle(LocalDateTime.now().plusHours(10), 12f, 14f, 15f, 10f), 161 | Candle(LocalDateTime.now().plusHours(9), 10f, 12f, 13f, 10f), 162 | Candle(LocalDateTime.now().plusHours(8), 11f, 10f, 16f, 9f), 163 | Candle(LocalDateTime.now().plusHours(7), 10f, 11f, 12f, 10f), 164 | Candle(LocalDateTime.now().plusHours(6), 6f, 10f, 12f, 4f), 165 | Candle(LocalDateTime.now().plusHours(5), 4f, 6f, 7f, 4f), 166 | Candle(LocalDateTime.now().plusHours(4), 6f, 4f, 8f, 4f), 167 | Candle(LocalDateTime.now().plusHours(3), 3f, 6f, 6f, 2f), 168 | Candle(LocalDateTime.now().plusHours(2), 4f, 3f, 5f, 2f), 169 | Candle(LocalDateTime.now().plusHours(1), 2f, 4f, 6f, 1f) 170 | ).sorted() 171 | ) 172 | } 173 | --------------------------------------------------------------------------------