From 8e5c1fe3191fc862544a785a97ad788e0f09b347 Mon Sep 17 00:00:00 2001 From: Yuriy Bereguliak Date: Fri, 18 Jan 2019 15:47:31 +0200 Subject: [PATCH 01/15] Base implementation for heat chart. --- .idea/modules.xml | 2 +- app/build.gradle | 2 + app/src/main/res/layout/activity_main.xml | 37 ++++++++++--- {hitchart => heatchart}/.gitignore | 0 {hitchart => heatchart}/build.gradle | 0 {hitchart => heatchart}/proguard-rules.pro | 0 .../by/hitchart/ExampleInstrumentedTest.java | 0 .../src/main/AndroidManifest.xml | 0 .../main/java/com/by/hitchart/ChartView.kt | 43 +++++++++++++++ .../com/by/hitchart/data/DrawRangeData.kt | 7 +++ .../java/com/by/hitchart/data/HeatChart.kt | 12 ++++ .../by/hitchart/manager/chart/ChartManager.kt | 14 +++++ .../manager/chart/HeatChartManager.kt | 21 +++++++ .../by/hitchart/manager/draw/DrawManager.kt | 14 +++++ .../manager/draw/HeatDrawController.kt | 55 +++++++++++++++++++ .../hitchart/manager/draw/HeatDrawManager.kt | 23 ++++++++ heatchart/src/main/res/values/colors.xml | 6 ++ heatchart/src/main/res/values/dimens.xml | 4 ++ .../src/main/res/values/strings.xml | 0 .../java/com/by/hitchart/ExampleUnitTest.java | 0 .../main/java/com/by/hitchart/ChartView.kt | 15 ----- settings.gradle | 2 +- 22 files changed, 233 insertions(+), 24 deletions(-) rename {hitchart => heatchart}/.gitignore (100%) rename {hitchart => heatchart}/build.gradle (100%) rename {hitchart => heatchart}/proguard-rules.pro (100%) rename {hitchart => heatchart}/src/androidTest/java/com/by/hitchart/ExampleInstrumentedTest.java (100%) rename {hitchart => heatchart}/src/main/AndroidManifest.xml (100%) create mode 100644 heatchart/src/main/java/com/by/hitchart/ChartView.kt create mode 100644 heatchart/src/main/java/com/by/hitchart/data/DrawRangeData.kt create mode 100644 heatchart/src/main/java/com/by/hitchart/data/HeatChart.kt create mode 100644 heatchart/src/main/java/com/by/hitchart/manager/chart/ChartManager.kt create mode 100644 heatchart/src/main/java/com/by/hitchart/manager/chart/HeatChartManager.kt create mode 100644 heatchart/src/main/java/com/by/hitchart/manager/draw/DrawManager.kt create mode 100644 heatchart/src/main/java/com/by/hitchart/manager/draw/HeatDrawController.kt create mode 100644 heatchart/src/main/java/com/by/hitchart/manager/draw/HeatDrawManager.kt create mode 100644 heatchart/src/main/res/values/colors.xml create mode 100644 heatchart/src/main/res/values/dimens.xml rename {hitchart => heatchart}/src/main/res/values/strings.xml (100%) rename {hitchart => heatchart}/src/test/java/com/by/hitchart/ExampleUnitTest.java (100%) delete mode 100644 hitchart/src/main/java/com/by/hitchart/ChartView.kt diff --git a/.idea/modules.xml b/.idea/modules.xml index 627f1b3..70a2091 100644 --- a/.idea/modules.xml +++ b/.idea/modules.xml @@ -4,7 +4,7 @@ - + \ No newline at end of file diff --git a/app/build.gradle b/app/build.gradle index 8641332..2920d0f 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -30,4 +30,6 @@ dependencies { testImplementation 'junit:junit:4.12' androidTestImplementation 'com.android.support.test:runner:1.0.2' androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2' + + api project(':heatchart') } diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index 980638c..7b243de 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -1,19 +1,42 @@ - + + + android:id="@+id/guideline" + android:orientation="horizontal" + app:layout_constraintGuide_percent="0.15"/> + + + \ No newline at end of file diff --git a/hitchart/.gitignore b/heatchart/.gitignore similarity index 100% rename from hitchart/.gitignore rename to heatchart/.gitignore diff --git a/hitchart/build.gradle b/heatchart/build.gradle similarity index 100% rename from hitchart/build.gradle rename to heatchart/build.gradle diff --git a/hitchart/proguard-rules.pro b/heatchart/proguard-rules.pro similarity index 100% rename from hitchart/proguard-rules.pro rename to heatchart/proguard-rules.pro diff --git a/hitchart/src/androidTest/java/com/by/hitchart/ExampleInstrumentedTest.java b/heatchart/src/androidTest/java/com/by/hitchart/ExampleInstrumentedTest.java similarity index 100% rename from hitchart/src/androidTest/java/com/by/hitchart/ExampleInstrumentedTest.java rename to heatchart/src/androidTest/java/com/by/hitchart/ExampleInstrumentedTest.java diff --git a/hitchart/src/main/AndroidManifest.xml b/heatchart/src/main/AndroidManifest.xml similarity index 100% rename from hitchart/src/main/AndroidManifest.xml rename to heatchart/src/main/AndroidManifest.xml diff --git a/heatchart/src/main/java/com/by/hitchart/ChartView.kt b/heatchart/src/main/java/com/by/hitchart/ChartView.kt new file mode 100644 index 0000000..e8c2b98 --- /dev/null +++ b/heatchart/src/main/java/com/by/hitchart/ChartView.kt @@ -0,0 +1,43 @@ +package com.by.hitchart + +import android.content.Context +import android.graphics.Canvas +import android.util.AttributeSet +import android.view.View +import com.by.hitchart.data.DrawRangeData +import com.by.hitchart.manager.chart.ChartManager +import com.by.hitchart.manager.chart.HeatChartManager + +/** + * HeatChart + * Created by Yuriy Bereguliak on 1/18/19. + */ +class ChartView(context: Context, attrs: AttributeSet? = null) : View(context, attrs) { + + private val chartManager: ChartManager by lazy { + HeatChartManager(context) + } + + //region View + override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) { + val width = View.MeasureSpec.getSize(widthMeasureSpec) + val height = View.MeasureSpec.getSize(heightMeasureSpec) + chartManager.chartData().width = width + chartManager.chartData().height = height + setMeasuredDimension(width, height) + } + + override fun onDraw(canvas: Canvas?) { + super.onDraw(canvas) + canvas?.let { + chartManager.drawManager().draw(it) + } + } + //endregion + + //region ChartView + fun setChartData(data: MutableList) { + chartManager.chartData().rangeData = data + } + //endregion +} \ No newline at end of file diff --git a/heatchart/src/main/java/com/by/hitchart/data/DrawRangeData.kt b/heatchart/src/main/java/com/by/hitchart/data/DrawRangeData.kt new file mode 100644 index 0000000..d1fe877 --- /dev/null +++ b/heatchart/src/main/java/com/by/hitchart/data/DrawRangeData.kt @@ -0,0 +1,7 @@ +package com.by.hitchart.data + +/** + * HeatChart + * Created by Yuriy Bereguliak on 1/18/19. + */ +data class DrawRangeData(val startPoint: Int, val endPoint: Int, val rangeColor: Int) \ No newline at end of file diff --git a/heatchart/src/main/java/com/by/hitchart/data/HeatChart.kt b/heatchart/src/main/java/com/by/hitchart/data/HeatChart.kt new file mode 100644 index 0000000..80a73b4 --- /dev/null +++ b/heatchart/src/main/java/com/by/hitchart/data/HeatChart.kt @@ -0,0 +1,12 @@ +package com.by.hitchart.data + +/** + * HeatChart + * Created by Yuriy Bereguliak on 1/18/19. + */ +data class HeatChart( + var width: Int = 0, + var height: Int = 0, + var parts: Int = 100, + var rangeData: MutableList = mutableListOf() +) \ No newline at end of file diff --git a/heatchart/src/main/java/com/by/hitchart/manager/chart/ChartManager.kt b/heatchart/src/main/java/com/by/hitchart/manager/chart/ChartManager.kt new file mode 100644 index 0000000..3298ea8 --- /dev/null +++ b/heatchart/src/main/java/com/by/hitchart/manager/chart/ChartManager.kt @@ -0,0 +1,14 @@ +package com.by.hitchart.manager.chart + +import com.by.hitchart.data.HeatChart +import com.by.hitchart.manager.draw.DrawManager + +/** + * HeatChart + * Created by Yuriy Bereguliak on 1/18/19. + */ +interface ChartManager { + fun chartData(): HeatChart + + fun drawManager(): DrawManager +} \ No newline at end of file diff --git a/heatchart/src/main/java/com/by/hitchart/manager/chart/HeatChartManager.kt b/heatchart/src/main/java/com/by/hitchart/manager/chart/HeatChartManager.kt new file mode 100644 index 0000000..6cc425e --- /dev/null +++ b/heatchart/src/main/java/com/by/hitchart/manager/chart/HeatChartManager.kt @@ -0,0 +1,21 @@ +package com.by.hitchart.manager.chart + +import android.content.Context +import com.by.hitchart.data.HeatChart +import com.by.hitchart.manager.draw.DrawManager +import com.by.hitchart.manager.draw.HeatDrawManager + +/** + * HeatChart + * Created by Yuriy Bereguliak on 1/18/19. + */ +class HeatChartManager(context: Context) : ChartManager { + + private var drawManager: DrawManager = HeatDrawManager(context) + + //region ChartManager + override fun chartData(): HeatChart = drawManager.chartData() + + override fun drawManager(): DrawManager = drawManager + //endregion +} \ No newline at end of file diff --git a/heatchart/src/main/java/com/by/hitchart/manager/draw/DrawManager.kt b/heatchart/src/main/java/com/by/hitchart/manager/draw/DrawManager.kt new file mode 100644 index 0000000..4453ac7 --- /dev/null +++ b/heatchart/src/main/java/com/by/hitchart/manager/draw/DrawManager.kt @@ -0,0 +1,14 @@ +package com.by.hitchart.manager.draw + +import android.graphics.Canvas +import com.by.hitchart.data.HeatChart + +/** + * HeatChart + * Created by Yuriy Bereguliak on 1/18/19. + */ +interface DrawManager { + fun chartData(): HeatChart + + fun draw(canvas: Canvas) +} \ No newline at end of file diff --git a/heatchart/src/main/java/com/by/hitchart/manager/draw/HeatDrawController.kt b/heatchart/src/main/java/com/by/hitchart/manager/draw/HeatDrawController.kt new file mode 100644 index 0000000..c415d43 --- /dev/null +++ b/heatchart/src/main/java/com/by/hitchart/manager/draw/HeatDrawController.kt @@ -0,0 +1,55 @@ +package com.by.hitchart.manager.draw + +import android.content.Context +import android.graphics.Canvas +import android.graphics.Paint +import android.graphics.Rect +import android.support.v4.content.ContextCompat +import com.by.hitchart.R +import com.by.hitchart.data.HeatChart + +/** + * HeatChart + * Created by Yuriy Bereguliak on 1/18/19. + */ +class HeatDrawController( + context: Context, + private var data: HeatChart +) { + + private var framePaint: Paint + private var rangePaint: Paint + + init { + val res = context.resources + + framePaint = Paint() + framePaint.style = Paint.Style.STROKE + framePaint.isAntiAlias = true + framePaint.strokeWidth = res.getDimension(R.dimen.frame_line_width) + framePaint.color = ContextCompat.getColor(context, R.color.gray_400) + + rangePaint = Paint() + rangePaint.style = Paint.Style.FILL + rangePaint.isAntiAlias = true + rangePaint.color = ContextCompat.getColor(context, R.color.blue) + } + + //region HeatDrawController + fun draw(canvas: Canvas) { + drawFrame(canvas) + } + //endregion + + //region Utility API + private fun drawFrame(canvas: Canvas) { + val rect = Rect() + rect.top = 0 + rect.left = 0 + rect.right = data.width + rect.bottom = data.height + + canvas.drawRect(rect, framePaint) + } + //endregion +} \ No newline at end of file diff --git a/heatchart/src/main/java/com/by/hitchart/manager/draw/HeatDrawManager.kt b/heatchart/src/main/java/com/by/hitchart/manager/draw/HeatDrawManager.kt new file mode 100644 index 0000000..4452ee4 --- /dev/null +++ b/heatchart/src/main/java/com/by/hitchart/manager/draw/HeatDrawManager.kt @@ -0,0 +1,23 @@ +package com.by.hitchart.manager.draw + +import android.content.Context +import android.graphics.Canvas +import com.by.hitchart.data.HeatChart + +/** + * HeatChart + * Created by Yuriy Bereguliak on 1/18/19. + */ +class HeatDrawManager(context: Context) : DrawManager { + + private val chart = HeatChart() + private val drawController = HeatDrawController(context, chart) + + //region DrawManager + override fun chartData() = chart + + override fun draw(canvas: Canvas) { + drawController.draw(canvas) + } + //endregion +} \ No newline at end of file diff --git a/heatchart/src/main/res/values/colors.xml b/heatchart/src/main/res/values/colors.xml new file mode 100644 index 0000000..c3810cd --- /dev/null +++ b/heatchart/src/main/res/values/colors.xml @@ -0,0 +1,6 @@ + + + #BDBDBD + + #406dbef4 + \ No newline at end of file diff --git a/heatchart/src/main/res/values/dimens.xml b/heatchart/src/main/res/values/dimens.xml new file mode 100644 index 0000000..1f0a827 --- /dev/null +++ b/heatchart/src/main/res/values/dimens.xml @@ -0,0 +1,4 @@ + + + 3dp + \ No newline at end of file diff --git a/hitchart/src/main/res/values/strings.xml b/heatchart/src/main/res/values/strings.xml similarity index 100% rename from hitchart/src/main/res/values/strings.xml rename to heatchart/src/main/res/values/strings.xml diff --git a/hitchart/src/test/java/com/by/hitchart/ExampleUnitTest.java b/heatchart/src/test/java/com/by/hitchart/ExampleUnitTest.java similarity index 100% rename from hitchart/src/test/java/com/by/hitchart/ExampleUnitTest.java rename to heatchart/src/test/java/com/by/hitchart/ExampleUnitTest.java diff --git a/hitchart/src/main/java/com/by/hitchart/ChartView.kt b/hitchart/src/main/java/com/by/hitchart/ChartView.kt deleted file mode 100644 index 9789a38..0000000 --- a/hitchart/src/main/java/com/by/hitchart/ChartView.kt +++ /dev/null @@ -1,15 +0,0 @@ -package com.by.hitchart - -import android.content.Context -import android.graphics.Canvas -import android.util.AttributeSet -import android.view.View - -class ChartView(context: Context?, attrs: AttributeSet? = null) : View(context, attrs) { - - //region View - override fun onDraw(canvas: Canvas?) { - super.onDraw(canvas) - } - //endregion -} \ No newline at end of file diff --git a/settings.gradle b/settings.gradle index c8f83de..c5115ef 100644 --- a/settings.gradle +++ b/settings.gradle @@ -1 +1 @@ -include ':app', ':hitchart' +include ':app', ':heatchart' From 5c020bbc4253f5095476189b855f1e5562731198 Mon Sep 17 00:00:00 2001 From: Yuriy Bereguliak Date: Fri, 18 Jan 2019 16:20:05 +0200 Subject: [PATCH 02/15] Base implementatino of heat line chart. --- .../bereguliak/yuriy/hitchart/MainActivity.kt | 18 ++++++++++++++- .../main/java/com/by/hitchart/ChartView.kt | 4 +++- .../com/by/hitchart/data/DrawRangeData.kt | 7 +++++- .../java/com/by/hitchart/data/HeatChart.kt | 9 ++++++-- .../manager/draw/HeatDrawController.kt | 23 +++++++++++++++++++ 5 files changed, 56 insertions(+), 5 deletions(-) diff --git a/app/src/main/java/com/bereguliak/yuriy/hitchart/MainActivity.kt b/app/src/main/java/com/bereguliak/yuriy/hitchart/MainActivity.kt index 01aa0d6..8ad6dbb 100644 --- a/app/src/main/java/com/bereguliak/yuriy/hitchart/MainActivity.kt +++ b/app/src/main/java/com/bereguliak/yuriy/hitchart/MainActivity.kt @@ -1,12 +1,28 @@ package com.bereguliak.yuriy.hitchart -import android.support.v7.app.AppCompatActivity import android.os.Bundle +import android.support.v7.app.AppCompatActivity +import com.by.hitchart.data.DrawRangeData +import kotlinx.android.synthetic.main.activity_main.* class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) + + val mutableListFirst = mutableListOf() + mutableListFirst.add(DrawRangeData(0, 20)) + mutableListFirst.add(DrawRangeData(10, 25)) + mutableListFirst.add(DrawRangeData(12, 20)) + mutableListFirst.add(DrawRangeData(15, 18)) + mutableListFirst.add(DrawRangeData(35, 44)) + mutableListFirst.add(DrawRangeData(40, 50)) + mutableListFirst.add(DrawRangeData(43, 55)) + mutableListFirst.add(DrawRangeData(45, 70)) + mutableListFirst.add(DrawRangeData(80, 90)) + mutableListFirst.add(DrawRangeData(85, 98)) + mutableListFirst.add(DrawRangeData(88, 100)) + topChartView.setChartData(mutableListFirst) } } diff --git a/heatchart/src/main/java/com/by/hitchart/ChartView.kt b/heatchart/src/main/java/com/by/hitchart/ChartView.kt index e8c2b98..e00331f 100644 --- a/heatchart/src/main/java/com/by/hitchart/ChartView.kt +++ b/heatchart/src/main/java/com/by/hitchart/ChartView.kt @@ -37,7 +37,9 @@ class ChartView(context: Context, attrs: AttributeSet? = null) : View(context, a //region ChartView fun setChartData(data: MutableList) { - chartManager.chartData().rangeData = data + post { + chartManager.chartData().rangeData = data + } } //endregion } \ No newline at end of file diff --git a/heatchart/src/main/java/com/by/hitchart/data/DrawRangeData.kt b/heatchart/src/main/java/com/by/hitchart/data/DrawRangeData.kt index d1fe877..fce29d5 100644 --- a/heatchart/src/main/java/com/by/hitchart/data/DrawRangeData.kt +++ b/heatchart/src/main/java/com/by/hitchart/data/DrawRangeData.kt @@ -4,4 +4,9 @@ package com.by.hitchart.data * HeatChart * Created by Yuriy Bereguliak on 1/18/19. */ -data class DrawRangeData(val startPoint: Int, val endPoint: Int, val rangeColor: Int) \ No newline at end of file +data class DrawRangeData(val startPoint: Int, val endPoint: Int, val rangeColor: Int = -1) { + + //region DrawRangeData + fun isColorValid() = rangeColor != -1 + //endregion +} \ No newline at end of file diff --git a/heatchart/src/main/java/com/by/hitchart/data/HeatChart.kt b/heatchart/src/main/java/com/by/hitchart/data/HeatChart.kt index 80a73b4..10791fe 100644 --- a/heatchart/src/main/java/com/by/hitchart/data/HeatChart.kt +++ b/heatchart/src/main/java/com/by/hitchart/data/HeatChart.kt @@ -7,6 +7,11 @@ package com.by.hitchart.data data class HeatChart( var width: Int = 0, var height: Int = 0, - var parts: Int = 100, + var parts: Float = 100.0f, var rangeData: MutableList = mutableListOf() -) \ No newline at end of file +) { + + //region HeatChart + fun calculateMinPart(): Float = width / parts + //endregion +} \ No newline at end of file diff --git a/heatchart/src/main/java/com/by/hitchart/manager/draw/HeatDrawController.kt b/heatchart/src/main/java/com/by/hitchart/manager/draw/HeatDrawController.kt index c415d43..5f54cac 100644 --- a/heatchart/src/main/java/com/by/hitchart/manager/draw/HeatDrawController.kt +++ b/heatchart/src/main/java/com/by/hitchart/manager/draw/HeatDrawController.kt @@ -4,6 +4,7 @@ import android.content.Context import android.graphics.Canvas import android.graphics.Paint import android.graphics.Rect +import android.graphics.RectF import android.support.v4.content.ContextCompat import com.by.hitchart.R import com.by.hitchart.data.HeatChart @@ -38,6 +39,7 @@ class HeatDrawController( //region HeatDrawController fun draw(canvas: Canvas) { drawFrame(canvas) + drawData(canvas) } //endregion @@ -51,5 +53,26 @@ class HeatDrawController( canvas.drawRect(rect, framePaint) } + + private fun drawData(canvas: Canvas) { + val minPartWidth = data.calculateMinPart() + + data.rangeData.forEach { rangeData -> + val left = rangeData.startPoint * minPartWidth + val right = rangeData.endPoint * minPartWidth + + val rect = RectF() + rect.left = left + rect.top = 0f + rect.right = right + rect.bottom = data.height.toFloat() + + if (rangeData.isColorValid()) { + rangePaint.color = rangeData.rangeColor + } + + canvas.drawRect(rect, rangePaint) + } + } //endregion } \ No newline at end of file From f3b346574fa81fedeb04420e3c55f6e3cca1ef97 Mon Sep 17 00:00:00 2001 From: Yuriy Bereguliak Date: Fri, 18 Jan 2019 16:21:35 +0200 Subject: [PATCH 03/15] Updated package name. --- .../java/com/bereguliak/yuriy/hitchart/MainActivity.kt | 2 +- app/src/main/res/layout/activity_main.xml | 4 ++-- .../{hitchart => heatchart}/ExampleInstrumentedTest.java | 2 +- heatchart/src/main/AndroidManifest.xml | 2 +- .../main/java/com/by/{hitchart => heatchart}/ChartView.kt | 8 ++++---- .../com/by/{hitchart => heatchart}/data/DrawRangeData.kt | 2 +- .../java/com/by/{hitchart => heatchart}/data/HeatChart.kt | 2 +- .../{hitchart => heatchart}/manager/chart/ChartManager.kt | 6 +++--- .../manager/chart/HeatChartManager.kt | 8 ++++---- .../{hitchart => heatchart}/manager/draw/DrawManager.kt | 4 ++-- .../manager/draw/HeatDrawController.kt | 6 +++--- .../manager/draw/HeatDrawManager.kt | 4 ++-- .../com/by/{hitchart => heatchart}/ExampleUnitTest.java | 2 +- 13 files changed, 26 insertions(+), 26 deletions(-) rename heatchart/src/androidTest/java/com/by/{hitchart => heatchart}/ExampleInstrumentedTest.java (96%) rename heatchart/src/main/java/com/by/{hitchart => heatchart}/ChartView.kt (86%) rename heatchart/src/main/java/com/by/{hitchart => heatchart}/data/DrawRangeData.kt (89%) rename heatchart/src/main/java/com/by/{hitchart => heatchart}/data/HeatChart.kt (91%) rename heatchart/src/main/java/com/by/{hitchart => heatchart}/manager/chart/ChartManager.kt (55%) rename heatchart/src/main/java/com/by/{hitchart => heatchart}/manager/chart/HeatChartManager.kt (68%) rename heatchart/src/main/java/com/by/{hitchart => heatchart}/manager/draw/DrawManager.kt (70%) rename heatchart/src/main/java/com/by/{hitchart => heatchart}/manager/draw/HeatDrawController.kt (94%) rename heatchart/src/main/java/com/by/{hitchart => heatchart}/manager/draw/HeatDrawManager.kt (85%) rename heatchart/src/test/java/com/by/{hitchart => heatchart}/ExampleUnitTest.java (93%) diff --git a/app/src/main/java/com/bereguliak/yuriy/hitchart/MainActivity.kt b/app/src/main/java/com/bereguliak/yuriy/hitchart/MainActivity.kt index 8ad6dbb..d027807 100644 --- a/app/src/main/java/com/bereguliak/yuriy/hitchart/MainActivity.kt +++ b/app/src/main/java/com/bereguliak/yuriy/hitchart/MainActivity.kt @@ -2,7 +2,7 @@ package com.bereguliak.yuriy.hitchart import android.os.Bundle import android.support.v7.app.AppCompatActivity -import com.by.hitchart.data.DrawRangeData +import com.by.heatchart.data.DrawRangeData import kotlinx.android.synthetic.main.activity_main.* class MainActivity : AppCompatActivity() { diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index 7b243de..dca6bbf 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -7,7 +7,7 @@ android:layout_height="match_parent" tools:context=".MainActivity"> - - + package="com.by.heatchart"/> diff --git a/heatchart/src/main/java/com/by/hitchart/ChartView.kt b/heatchart/src/main/java/com/by/heatchart/ChartView.kt similarity index 86% rename from heatchart/src/main/java/com/by/hitchart/ChartView.kt rename to heatchart/src/main/java/com/by/heatchart/ChartView.kt index e00331f..1c752df 100644 --- a/heatchart/src/main/java/com/by/hitchart/ChartView.kt +++ b/heatchart/src/main/java/com/by/heatchart/ChartView.kt @@ -1,12 +1,12 @@ -package com.by.hitchart +package com.by.heatchart import android.content.Context import android.graphics.Canvas import android.util.AttributeSet import android.view.View -import com.by.hitchart.data.DrawRangeData -import com.by.hitchart.manager.chart.ChartManager -import com.by.hitchart.manager.chart.HeatChartManager +import com.by.heatchart.data.DrawRangeData +import com.by.heatchart.manager.chart.ChartManager +import com.by.heatchart.manager.chart.HeatChartManager /** * HeatChart diff --git a/heatchart/src/main/java/com/by/hitchart/data/DrawRangeData.kt b/heatchart/src/main/java/com/by/heatchart/data/DrawRangeData.kt similarity index 89% rename from heatchart/src/main/java/com/by/hitchart/data/DrawRangeData.kt rename to heatchart/src/main/java/com/by/heatchart/data/DrawRangeData.kt index fce29d5..2092515 100644 --- a/heatchart/src/main/java/com/by/hitchart/data/DrawRangeData.kt +++ b/heatchart/src/main/java/com/by/heatchart/data/DrawRangeData.kt @@ -1,4 +1,4 @@ -package com.by.hitchart.data +package com.by.heatchart.data /** * HeatChart diff --git a/heatchart/src/main/java/com/by/hitchart/data/HeatChart.kt b/heatchart/src/main/java/com/by/heatchart/data/HeatChart.kt similarity index 91% rename from heatchart/src/main/java/com/by/hitchart/data/HeatChart.kt rename to heatchart/src/main/java/com/by/heatchart/data/HeatChart.kt index 10791fe..3bed94c 100644 --- a/heatchart/src/main/java/com/by/hitchart/data/HeatChart.kt +++ b/heatchart/src/main/java/com/by/heatchart/data/HeatChart.kt @@ -1,4 +1,4 @@ -package com.by.hitchart.data +package com.by.heatchart.data /** * HeatChart diff --git a/heatchart/src/main/java/com/by/hitchart/manager/chart/ChartManager.kt b/heatchart/src/main/java/com/by/heatchart/manager/chart/ChartManager.kt similarity index 55% rename from heatchart/src/main/java/com/by/hitchart/manager/chart/ChartManager.kt rename to heatchart/src/main/java/com/by/heatchart/manager/chart/ChartManager.kt index 3298ea8..6972945 100644 --- a/heatchart/src/main/java/com/by/hitchart/manager/chart/ChartManager.kt +++ b/heatchart/src/main/java/com/by/heatchart/manager/chart/ChartManager.kt @@ -1,7 +1,7 @@ -package com.by.hitchart.manager.chart +package com.by.heatchart.manager.chart -import com.by.hitchart.data.HeatChart -import com.by.hitchart.manager.draw.DrawManager +import com.by.heatchart.data.HeatChart +import com.by.heatchart.manager.draw.DrawManager /** * HeatChart diff --git a/heatchart/src/main/java/com/by/hitchart/manager/chart/HeatChartManager.kt b/heatchart/src/main/java/com/by/heatchart/manager/chart/HeatChartManager.kt similarity index 68% rename from heatchart/src/main/java/com/by/hitchart/manager/chart/HeatChartManager.kt rename to heatchart/src/main/java/com/by/heatchart/manager/chart/HeatChartManager.kt index 6cc425e..4a7fb77 100644 --- a/heatchart/src/main/java/com/by/hitchart/manager/chart/HeatChartManager.kt +++ b/heatchart/src/main/java/com/by/heatchart/manager/chart/HeatChartManager.kt @@ -1,9 +1,9 @@ -package com.by.hitchart.manager.chart +package com.by.heatchart.manager.chart import android.content.Context -import com.by.hitchart.data.HeatChart -import com.by.hitchart.manager.draw.DrawManager -import com.by.hitchart.manager.draw.HeatDrawManager +import com.by.heatchart.data.HeatChart +import com.by.heatchart.manager.draw.DrawManager +import com.by.heatchart.manager.draw.HeatDrawManager /** * HeatChart diff --git a/heatchart/src/main/java/com/by/hitchart/manager/draw/DrawManager.kt b/heatchart/src/main/java/com/by/heatchart/manager/draw/DrawManager.kt similarity index 70% rename from heatchart/src/main/java/com/by/hitchart/manager/draw/DrawManager.kt rename to heatchart/src/main/java/com/by/heatchart/manager/draw/DrawManager.kt index 4453ac7..15cfa3e 100644 --- a/heatchart/src/main/java/com/by/hitchart/manager/draw/DrawManager.kt +++ b/heatchart/src/main/java/com/by/heatchart/manager/draw/DrawManager.kt @@ -1,7 +1,7 @@ -package com.by.hitchart.manager.draw +package com.by.heatchart.manager.draw import android.graphics.Canvas -import com.by.hitchart.data.HeatChart +import com.by.heatchart.data.HeatChart /** * HeatChart diff --git a/heatchart/src/main/java/com/by/hitchart/manager/draw/HeatDrawController.kt b/heatchart/src/main/java/com/by/heatchart/manager/draw/HeatDrawController.kt similarity index 94% rename from heatchart/src/main/java/com/by/hitchart/manager/draw/HeatDrawController.kt rename to heatchart/src/main/java/com/by/heatchart/manager/draw/HeatDrawController.kt index 5f54cac..d917a7b 100644 --- a/heatchart/src/main/java/com/by/hitchart/manager/draw/HeatDrawController.kt +++ b/heatchart/src/main/java/com/by/heatchart/manager/draw/HeatDrawController.kt @@ -1,4 +1,4 @@ -package com.by.hitchart.manager.draw +package com.by.heatchart.manager.draw import android.content.Context import android.graphics.Canvas @@ -6,8 +6,8 @@ import android.graphics.Paint import android.graphics.Rect import android.graphics.RectF import android.support.v4.content.ContextCompat -import com.by.hitchart.R -import com.by.hitchart.data.HeatChart +import com.by.heatchart.R +import com.by.heatchart.data.HeatChart /** * HeatChart diff --git a/heatchart/src/main/java/com/by/hitchart/manager/draw/HeatDrawManager.kt b/heatchart/src/main/java/com/by/heatchart/manager/draw/HeatDrawManager.kt similarity index 85% rename from heatchart/src/main/java/com/by/hitchart/manager/draw/HeatDrawManager.kt rename to heatchart/src/main/java/com/by/heatchart/manager/draw/HeatDrawManager.kt index 4452ee4..71948b1 100644 --- a/heatchart/src/main/java/com/by/hitchart/manager/draw/HeatDrawManager.kt +++ b/heatchart/src/main/java/com/by/heatchart/manager/draw/HeatDrawManager.kt @@ -1,8 +1,8 @@ -package com.by.hitchart.manager.draw +package com.by.heatchart.manager.draw import android.content.Context import android.graphics.Canvas -import com.by.hitchart.data.HeatChart +import com.by.heatchart.data.HeatChart /** * HeatChart diff --git a/heatchart/src/test/java/com/by/hitchart/ExampleUnitTest.java b/heatchart/src/test/java/com/by/heatchart/ExampleUnitTest.java similarity index 93% rename from heatchart/src/test/java/com/by/hitchart/ExampleUnitTest.java rename to heatchart/src/test/java/com/by/heatchart/ExampleUnitTest.java index 104b5c4..c01edd4 100644 --- a/heatchart/src/test/java/com/by/hitchart/ExampleUnitTest.java +++ b/heatchart/src/test/java/com/by/heatchart/ExampleUnitTest.java @@ -1,4 +1,4 @@ -package com.by.hitchart; +package com.by.heatchart; import org.junit.Test; From 8610f00799a0ae18015edef378e0cb390e793a13 Mon Sep 17 00:00:00 2001 From: Yuriy Bereguliak Date: Fri, 18 Jan 2019 16:43:22 +0200 Subject: [PATCH 04/15] Updated data source names. --- .../bereguliak/yuriy/hitchart/MainActivity.kt | 44 +++++++++++++------ app/src/main/res/layout/activity_main.xml | 1 + .../main/java/com/by/heatchart/ChartView.kt | 6 +-- .../java/com/by/heatchart/data/HeatChart.kt | 8 ++-- .../com/by/heatchart/data/HeatChartDataSet.kt | 10 +++++ .../{DrawRangeData.kt => HeatChartEntry.kt} | 4 +- .../manager/draw/HeatDrawController.kt | 12 ++--- heatchart/src/main/res/values/colors.xml | 3 +- 8 files changed, 59 insertions(+), 29 deletions(-) create mode 100644 heatchart/src/main/java/com/by/heatchart/data/HeatChartDataSet.kt rename heatchart/src/main/java/com/by/heatchart/data/{DrawRangeData.kt => HeatChartEntry.kt} (55%) diff --git a/app/src/main/java/com/bereguliak/yuriy/hitchart/MainActivity.kt b/app/src/main/java/com/bereguliak/yuriy/hitchart/MainActivity.kt index d027807..d76ddcf 100644 --- a/app/src/main/java/com/bereguliak/yuriy/hitchart/MainActivity.kt +++ b/app/src/main/java/com/bereguliak/yuriy/hitchart/MainActivity.kt @@ -1,8 +1,10 @@ package com.bereguliak.yuriy.hitchart import android.os.Bundle +import android.support.v4.content.ContextCompat import android.support.v7.app.AppCompatActivity -import com.by.heatchart.data.DrawRangeData +import com.by.heatchart.data.HeatChartDataSet +import com.by.heatchart.data.HeatChartEntry import kotlinx.android.synthetic.main.activity_main.* class MainActivity : AppCompatActivity() { @@ -11,18 +13,32 @@ class MainActivity : AppCompatActivity() { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) - val mutableListFirst = mutableListOf() - mutableListFirst.add(DrawRangeData(0, 20)) - mutableListFirst.add(DrawRangeData(10, 25)) - mutableListFirst.add(DrawRangeData(12, 20)) - mutableListFirst.add(DrawRangeData(15, 18)) - mutableListFirst.add(DrawRangeData(35, 44)) - mutableListFirst.add(DrawRangeData(40, 50)) - mutableListFirst.add(DrawRangeData(43, 55)) - mutableListFirst.add(DrawRangeData(45, 70)) - mutableListFirst.add(DrawRangeData(80, 90)) - mutableListFirst.add(DrawRangeData(85, 98)) - mutableListFirst.add(DrawRangeData(88, 100)) - topChartView.setChartData(mutableListFirst) + val mutableListFirst = mutableListOf() + with(mutableListFirst) { + add(HeatChartEntry(0, 20)) + add(HeatChartEntry(10, 25)) + add(HeatChartEntry(12, 20)) + add(HeatChartEntry(15, 18)) + add(HeatChartEntry(35, 44)) + add(HeatChartEntry(40, 50)) + add(HeatChartEntry(43, 55)) + add(HeatChartEntry(45, 70)) + add(HeatChartEntry(80, 90)) + add(HeatChartEntry(85, 98)) + add(HeatChartEntry(88, 100)) + } + topChartView.setChartData(HeatChartDataSet(rangeData = mutableListFirst)) + + val color = ContextCompat.getColor(this, R.color.green) + val mutableListWithColor = mutableListOf() + with(mutableListWithColor) { + add(HeatChartEntry(0, 20, color)) + add(HeatChartEntry(10, 25, color)) + add(HeatChartEntry(45, 70, color)) + add(HeatChartEntry(75, 90, color)) + add(HeatChartEntry(80, 90, color)) + add(HeatChartEntry(85, 95, color)) + } + secondChart.setChartData(HeatChartDataSet(rangeData = mutableListWithColor)) } } diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index dca6bbf..0576587 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -25,6 +25,7 @@ app:layout_constraintGuide_percent="0.15"/> ) { + fun setChartData(dataSet: HeatChartDataSet) { post { - chartManager.chartData().rangeData = data + chartManager.chartData().dataSet = dataSet } } //endregion diff --git a/heatchart/src/main/java/com/by/heatchart/data/HeatChart.kt b/heatchart/src/main/java/com/by/heatchart/data/HeatChart.kt index 3bed94c..1dcc12d 100644 --- a/heatchart/src/main/java/com/by/heatchart/data/HeatChart.kt +++ b/heatchart/src/main/java/com/by/heatchart/data/HeatChart.kt @@ -7,11 +7,13 @@ package com.by.heatchart.data data class HeatChart( var width: Int = 0, var height: Int = 0, - var parts: Float = 100.0f, - var rangeData: MutableList = mutableListOf() + var backgroundColor: Int = -1, + var dataSet: HeatChartDataSet = HeatChartDataSet() ) { //region HeatChart - fun calculateMinPart(): Float = width / parts + fun isBackgroundColorValid() = backgroundColor != -1 + + fun calculateMinPart(): Float = width / dataSet.parts //endregion } \ No newline at end of file diff --git a/heatchart/src/main/java/com/by/heatchart/data/HeatChartDataSet.kt b/heatchart/src/main/java/com/by/heatchart/data/HeatChartDataSet.kt new file mode 100644 index 0000000..b1544e3 --- /dev/null +++ b/heatchart/src/main/java/com/by/heatchart/data/HeatChartDataSet.kt @@ -0,0 +1,10 @@ +package com.by.heatchart.data + +/** + * HitChart + * Created by Yuriy Bereguliak on 1/18/19. + */ +data class HeatChartDataSet( + var parts: Float = 100.0f, + var rangeData: MutableList = mutableListOf() +) \ No newline at end of file diff --git a/heatchart/src/main/java/com/by/heatchart/data/DrawRangeData.kt b/heatchart/src/main/java/com/by/heatchart/data/HeatChartEntry.kt similarity index 55% rename from heatchart/src/main/java/com/by/heatchart/data/DrawRangeData.kt rename to heatchart/src/main/java/com/by/heatchart/data/HeatChartEntry.kt index 2092515..b0307ca 100644 --- a/heatchart/src/main/java/com/by/heatchart/data/DrawRangeData.kt +++ b/heatchart/src/main/java/com/by/heatchart/data/HeatChartEntry.kt @@ -4,9 +4,9 @@ package com.by.heatchart.data * HeatChart * Created by Yuriy Bereguliak on 1/18/19. */ -data class DrawRangeData(val startPoint: Int, val endPoint: Int, val rangeColor: Int = -1) { +data class HeatChartEntry(val startPoint: Int, val endPoint: Int, val rangeColor: Int = -1) { - //region DrawRangeData + //region HeatChartEntry fun isColorValid() = rangeColor != -1 //endregion } \ No newline at end of file diff --git a/heatchart/src/main/java/com/by/heatchart/manager/draw/HeatDrawController.kt b/heatchart/src/main/java/com/by/heatchart/manager/draw/HeatDrawController.kt index d917a7b..36b9141 100644 --- a/heatchart/src/main/java/com/by/heatchart/manager/draw/HeatDrawController.kt +++ b/heatchart/src/main/java/com/by/heatchart/manager/draw/HeatDrawController.kt @@ -15,7 +15,7 @@ import com.by.heatchart.data.HeatChart */ class HeatDrawController( context: Context, - private var data: HeatChart + private var heatChart: HeatChart ) { private var framePaint: Paint @@ -48,16 +48,16 @@ class HeatDrawController( val rect = Rect() rect.top = 0 rect.left = 0 - rect.right = data.width - rect.bottom = data.height + rect.right = heatChart.width + rect.bottom = heatChart.height canvas.drawRect(rect, framePaint) } private fun drawData(canvas: Canvas) { - val minPartWidth = data.calculateMinPart() + val minPartWidth = heatChart.calculateMinPart() - data.rangeData.forEach { rangeData -> + heatChart.dataSet.rangeData.forEach { rangeData -> val left = rangeData.startPoint * minPartWidth val right = rangeData.endPoint * minPartWidth @@ -65,7 +65,7 @@ class HeatDrawController( rect.left = left rect.top = 0f rect.right = right - rect.bottom = data.height.toFloat() + rect.bottom = heatChart.height.toFloat() if (rangeData.isColorValid()) { rangePaint.color = rangeData.rangeColor diff --git a/heatchart/src/main/res/values/colors.xml b/heatchart/src/main/res/values/colors.xml index c3810cd..e10b4b8 100644 --- a/heatchart/src/main/res/values/colors.xml +++ b/heatchart/src/main/res/values/colors.xml @@ -2,5 +2,6 @@ #BDBDBD - #406dbef4 + #406dbef4 + #403cb051 \ No newline at end of file From 7c561f461e9b5b46a878c1fb05676d3323c8160e Mon Sep 17 00:00:00 2001 From: Yuriy Bereguliak Date: Fri, 18 Jan 2019 16:49:02 +0200 Subject: [PATCH 05/15] Updated color assigning. --- .../bereguliak/yuriy/hitchart/MainActivity.kt | 21 ++++++++++++------- .../com/by/heatchart/data/HeatChartDataSet.kt | 11 ++++++++-- .../com/by/heatchart/data/HeatChartEntry.kt | 2 +- 3 files changed, 23 insertions(+), 11 deletions(-) diff --git a/app/src/main/java/com/bereguliak/yuriy/hitchart/MainActivity.kt b/app/src/main/java/com/bereguliak/yuriy/hitchart/MainActivity.kt index d76ddcf..6196144 100644 --- a/app/src/main/java/com/bereguliak/yuriy/hitchart/MainActivity.kt +++ b/app/src/main/java/com/bereguliak/yuriy/hitchart/MainActivity.kt @@ -29,16 +29,21 @@ class MainActivity : AppCompatActivity() { } topChartView.setChartData(HeatChartDataSet(rangeData = mutableListFirst)) - val color = ContextCompat.getColor(this, R.color.green) val mutableListWithColor = mutableListOf() with(mutableListWithColor) { - add(HeatChartEntry(0, 20, color)) - add(HeatChartEntry(10, 25, color)) - add(HeatChartEntry(45, 70, color)) - add(HeatChartEntry(75, 90, color)) - add(HeatChartEntry(80, 90, color)) - add(HeatChartEntry(85, 95, color)) + add(HeatChartEntry(0, 20)) + add(HeatChartEntry(10, 25)) + add(HeatChartEntry(45, 70)) + add(HeatChartEntry(75, 90)) + add(HeatChartEntry(80, 90)) + add(HeatChartEntry(85, 95)) } - secondChart.setChartData(HeatChartDataSet(rangeData = mutableListWithColor)) + + val dataSet = HeatChartDataSet() + dataSet.parts = 100f + dataSet.rangeData = mutableListWithColor + dataSet.assignColorToRanges(ContextCompat.getColor(this, R.color.green)) + + secondChart.setChartData(dataSet) } } diff --git a/heatchart/src/main/java/com/by/heatchart/data/HeatChartDataSet.kt b/heatchart/src/main/java/com/by/heatchart/data/HeatChartDataSet.kt index b1544e3..34355a1 100644 --- a/heatchart/src/main/java/com/by/heatchart/data/HeatChartDataSet.kt +++ b/heatchart/src/main/java/com/by/heatchart/data/HeatChartDataSet.kt @@ -5,6 +5,13 @@ package com.by.heatchart.data * Created by Yuriy Bereguliak on 1/18/19. */ data class HeatChartDataSet( - var parts: Float = 100.0f, + var parts: Float = 100f, var rangeData: MutableList = mutableListOf() -) \ No newline at end of file +) { + + //region HeatChartDataSet + fun assignColorToRanges(color: Int) { + rangeData.forEach { it.rangeColor = color } + } + //endregion +} \ No newline at end of file diff --git a/heatchart/src/main/java/com/by/heatchart/data/HeatChartEntry.kt b/heatchart/src/main/java/com/by/heatchart/data/HeatChartEntry.kt index b0307ca..bc6e4f9 100644 --- a/heatchart/src/main/java/com/by/heatchart/data/HeatChartEntry.kt +++ b/heatchart/src/main/java/com/by/heatchart/data/HeatChartEntry.kt @@ -4,7 +4,7 @@ package com.by.heatchart.data * HeatChart * Created by Yuriy Bereguliak on 1/18/19. */ -data class HeatChartEntry(val startPoint: Int, val endPoint: Int, val rangeColor: Int = -1) { +data class HeatChartEntry(val startPoint: Int, val endPoint: Int, var rangeColor: Int = -1) { //region HeatChartEntry fun isColorValid() = rangeColor != -1 From e43736b5acefd2884d2c51b98343250588271d91 Mon Sep 17 00:00:00 2001 From: Yuriy Bereguliak Date: Fri, 18 Jan 2019 16:50:25 +0200 Subject: [PATCH 06/15] Renamed. --- app/src/main/res/values/strings.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index e3c0787..241531a 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -1,3 +1,3 @@ - HitChart + HeatChart From 5737c2ba416938efc3021230e1b50d6e36366495 Mon Sep 17 00:00:00 2001 From: Yuriy Bereguliak Date: Fri, 18 Jan 2019 17:25:38 +0200 Subject: [PATCH 07/15] Start added empty range background. --- heatchart/src/main/java/com/by/heatchart/data/HeatChart.kt | 3 ++- .../com/by/heatchart/manager/draw/HeatDrawController.kt | 6 ++++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/heatchart/src/main/java/com/by/heatchart/data/HeatChart.kt b/heatchart/src/main/java/com/by/heatchart/data/HeatChart.kt index 1dcc12d..692df93 100644 --- a/heatchart/src/main/java/com/by/heatchart/data/HeatChart.kt +++ b/heatchart/src/main/java/com/by/heatchart/data/HeatChart.kt @@ -7,12 +7,13 @@ package com.by.heatchart.data data class HeatChart( var width: Int = 0, var height: Int = 0, + var isApplyToEmptyRangeColor: Boolean = false, var backgroundColor: Int = -1, var dataSet: HeatChartDataSet = HeatChartDataSet() ) { //region HeatChart - fun isBackgroundColorValid() = backgroundColor != -1 + fun isBackgroundColorValid() = backgroundColor != -1 && isApplyToEmptyRangeColor fun calculateMinPart(): Float = width / dataSet.parts //endregion diff --git a/heatchart/src/main/java/com/by/heatchart/manager/draw/HeatDrawController.kt b/heatchart/src/main/java/com/by/heatchart/manager/draw/HeatDrawController.kt index 36b9141..f262d7a 100644 --- a/heatchart/src/main/java/com/by/heatchart/manager/draw/HeatDrawController.kt +++ b/heatchart/src/main/java/com/by/heatchart/manager/draw/HeatDrawController.kt @@ -57,6 +57,7 @@ class HeatDrawController( private fun drawData(canvas: Canvas) { val minPartWidth = heatChart.calculateMinPart() + // Draw ranges from data set heatChart.dataSet.rangeData.forEach { rangeData -> val left = rangeData.startPoint * minPartWidth val right = rangeData.endPoint * minPartWidth @@ -73,6 +74,11 @@ class HeatDrawController( canvas.drawRect(rect, rangePaint) } + + // Find empty ranges and apply color if needed + if (!heatChart.isBackgroundColorValid()) { + return + } } //endregion } \ No newline at end of file From cb1817e02adf6a5981790e184fbad9274802a756 Mon Sep 17 00:00:00 2001 From: Yuriy Bereguliak Date: Tue, 22 Jan 2019 11:46:10 +0200 Subject: [PATCH 08/15] Updated package structure. --- .idea/misc.xml | 2 +- app/src/main/res/layout/activity_main.xml | 4 +- .../{manager => core}/chart/ChartManager.kt | 2 +- .../chart/RangeHeatChartManager.kt} | 4 +- .../java/com/by/heatchart/data/HeatChart.kt | 4 -- .../com/by/heatchart/data/HeatChartEntry.kt | 4 -- .../manager/draw/HeatDrawController.kt | 38 +++++++++---------- .../by/heatchart/view/IndexHeatChartView.kt | 8 ++++ .../RangeHeatChartView.kt} | 13 ++++--- 9 files changed, 40 insertions(+), 39 deletions(-) rename heatchart/src/main/java/com/by/heatchart/{manager => core}/chart/ChartManager.kt (86%) rename heatchart/src/main/java/com/by/heatchart/{manager/chart/HeatChartManager.kt => core/chart/RangeHeatChartManager.kt} (82%) create mode 100644 heatchart/src/main/java/com/by/heatchart/view/IndexHeatChartView.kt rename heatchart/src/main/java/com/by/heatchart/{ChartView.kt => view/RangeHeatChartView.kt} (75%) diff --git a/.idea/misc.xml b/.idea/misc.xml index 29bb4c5..8a8f75b 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -5,7 +5,7 @@ - + diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index 0576587..9edc25a 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -7,7 +7,7 @@ android:layout_height="match_parent" tools:context=".MainActivity"> - - Date: Tue, 22 Jan 2019 12:24:46 +0200 Subject: [PATCH 09/15] Created abstraction layer. 1 - abstract draw controller 2 - abstract data manager --- .idea/misc.xml | 2 +- .../bereguliak/yuriy/hitchart/MainActivity.kt | 46 +++++----- app/src/main/res/layout/activity_main.xml | 4 +- .../by/heatchart/core/chart/ChartManager.kt | 12 +-- .../core/chart/RangeHeatChartManager.kt | 21 ----- .../core/controller/BaseDrawController.kt | 51 +++++++++++ .../core/controller/DrawController.kt | 13 +++ .../heatchart/core/manager/BaseDrawManager.kt | 29 +++++++ .../by/heatchart/core/manager/DrawManager.kt | 18 ++++ .../data/{HeatChart.kt => ChartData.kt} | 9 +- .../com/by/heatchart/data/HeatChartEntry.kt | 8 -- .../RangeHeatChartDataSet.kt} | 8 +- .../data/range/RangeHeatChartEntry.kt | 8 ++ .../by/heatchart/manager/draw/DrawManager.kt | 14 ---- .../manager/draw/HeatDrawController.kt | 84 ------------------- .../heatchart/manager/draw/HeatDrawManager.kt | 23 ----- .../view/{ => index}/IndexHeatChartView.kt | 2 +- .../view/{ => range}/RangeHeatChartView.kt | 19 ++--- .../controller/RangeHeatDrawController.kt | 61 ++++++++++++++ .../range/manager/RangeHeatDrawManager.kt | 30 +++++++ 20 files changed, 260 insertions(+), 202 deletions(-) delete mode 100644 heatchart/src/main/java/com/by/heatchart/core/chart/RangeHeatChartManager.kt create mode 100644 heatchart/src/main/java/com/by/heatchart/core/controller/BaseDrawController.kt create mode 100644 heatchart/src/main/java/com/by/heatchart/core/controller/DrawController.kt create mode 100644 heatchart/src/main/java/com/by/heatchart/core/manager/BaseDrawManager.kt create mode 100644 heatchart/src/main/java/com/by/heatchart/core/manager/DrawManager.kt rename heatchart/src/main/java/com/by/heatchart/data/{HeatChart.kt => ChartData.kt} (55%) delete mode 100644 heatchart/src/main/java/com/by/heatchart/data/HeatChartEntry.kt rename heatchart/src/main/java/com/by/heatchart/data/{HeatChartDataSet.kt => range/RangeHeatChartDataSet.kt} (55%) create mode 100644 heatchart/src/main/java/com/by/heatchart/data/range/RangeHeatChartEntry.kt delete mode 100644 heatchart/src/main/java/com/by/heatchart/manager/draw/DrawManager.kt delete mode 100644 heatchart/src/main/java/com/by/heatchart/manager/draw/HeatDrawController.kt delete mode 100644 heatchart/src/main/java/com/by/heatchart/manager/draw/HeatDrawManager.kt rename heatchart/src/main/java/com/by/heatchart/view/{ => index}/IndexHeatChartView.kt (71%) rename heatchart/src/main/java/com/by/heatchart/view/{ => range}/RangeHeatChartView.kt (66%) create mode 100644 heatchart/src/main/java/com/by/heatchart/view/range/controller/RangeHeatDrawController.kt create mode 100644 heatchart/src/main/java/com/by/heatchart/view/range/manager/RangeHeatDrawManager.kt diff --git a/.idea/misc.xml b/.idea/misc.xml index 8a8f75b..29bb4c5 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -5,7 +5,7 @@ - + diff --git a/app/src/main/java/com/bereguliak/yuriy/hitchart/MainActivity.kt b/app/src/main/java/com/bereguliak/yuriy/hitchart/MainActivity.kt index 6196144..530d0f8 100644 --- a/app/src/main/java/com/bereguliak/yuriy/hitchart/MainActivity.kt +++ b/app/src/main/java/com/bereguliak/yuriy/hitchart/MainActivity.kt @@ -3,8 +3,8 @@ package com.bereguliak.yuriy.hitchart import android.os.Bundle import android.support.v4.content.ContextCompat import android.support.v7.app.AppCompatActivity -import com.by.heatchart.data.HeatChartDataSet -import com.by.heatchart.data.HeatChartEntry +import com.by.heatchart.data.range.RangeHeatChartDataSet +import com.by.heatchart.data.range.RangeHeatChartEntry import kotlinx.android.synthetic.main.activity_main.* class MainActivity : AppCompatActivity() { @@ -13,33 +13,33 @@ class MainActivity : AppCompatActivity() { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) - val mutableListFirst = mutableListOf() + val mutableListFirst = mutableListOf() with(mutableListFirst) { - add(HeatChartEntry(0, 20)) - add(HeatChartEntry(10, 25)) - add(HeatChartEntry(12, 20)) - add(HeatChartEntry(15, 18)) - add(HeatChartEntry(35, 44)) - add(HeatChartEntry(40, 50)) - add(HeatChartEntry(43, 55)) - add(HeatChartEntry(45, 70)) - add(HeatChartEntry(80, 90)) - add(HeatChartEntry(85, 98)) - add(HeatChartEntry(88, 100)) + add(RangeHeatChartEntry(0, 20)) + add(RangeHeatChartEntry(10, 25)) + add(RangeHeatChartEntry(12, 20)) + add(RangeHeatChartEntry(15, 18)) + add(RangeHeatChartEntry(35, 44)) + add(RangeHeatChartEntry(40, 50)) + add(RangeHeatChartEntry(43, 55)) + add(RangeHeatChartEntry(45, 70)) + add(RangeHeatChartEntry(80, 90)) + add(RangeHeatChartEntry(85, 98)) + add(RangeHeatChartEntry(88, 100)) } - topChartView.setChartData(HeatChartDataSet(rangeData = mutableListFirst)) + topChartView.setChartData(RangeHeatChartDataSet(rangeData = mutableListFirst)) - val mutableListWithColor = mutableListOf() + val mutableListWithColor = mutableListOf() with(mutableListWithColor) { - add(HeatChartEntry(0, 20)) - add(HeatChartEntry(10, 25)) - add(HeatChartEntry(45, 70)) - add(HeatChartEntry(75, 90)) - add(HeatChartEntry(80, 90)) - add(HeatChartEntry(85, 95)) + add(RangeHeatChartEntry(0, 20)) + add(RangeHeatChartEntry(10, 25)) + add(RangeHeatChartEntry(45, 70)) + add(RangeHeatChartEntry(75, 90)) + add(RangeHeatChartEntry(80, 90)) + add(RangeHeatChartEntry(85, 95)) } - val dataSet = HeatChartDataSet() + val dataSet = RangeHeatChartDataSet() dataSet.parts = 100f dataSet.rangeData = mutableListWithColor dataSet.assignColorToRanges(ContextCompat.getColor(this, R.color.green)) diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index 9edc25a..0a26ba1 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -7,7 +7,7 @@ android:layout_height="match_parent" tools:context=".MainActivity"> - - { + fun chartData(): ChartData - fun drawManager(): DrawManager + fun drawManager(): DrawManager } \ No newline at end of file diff --git a/heatchart/src/main/java/com/by/heatchart/core/chart/RangeHeatChartManager.kt b/heatchart/src/main/java/com/by/heatchart/core/chart/RangeHeatChartManager.kt deleted file mode 100644 index a0019d8..0000000 --- a/heatchart/src/main/java/com/by/heatchart/core/chart/RangeHeatChartManager.kt +++ /dev/null @@ -1,21 +0,0 @@ -package com.by.heatchart.core.chart - -import android.content.Context -import com.by.heatchart.data.HeatChart -import com.by.heatchart.manager.draw.DrawManager -import com.by.heatchart.manager.draw.HeatDrawManager - -/** - * HeatChart - * Created by Yuriy Bereguliak on 1/18/19. - */ -class RangeHeatChartManager(context: Context) : ChartManager { - - private var drawManager: DrawManager = HeatDrawManager(context) - - //region ChartManager - override fun chartData(): HeatChart = drawManager.chartData() - - override fun drawManager(): DrawManager = drawManager - //endregion -} \ No newline at end of file diff --git a/heatchart/src/main/java/com/by/heatchart/core/controller/BaseDrawController.kt b/heatchart/src/main/java/com/by/heatchart/core/controller/BaseDrawController.kt new file mode 100644 index 0000000..69eaad8 --- /dev/null +++ b/heatchart/src/main/java/com/by/heatchart/core/controller/BaseDrawController.kt @@ -0,0 +1,51 @@ +package com.by.heatchart.core.controller + +import android.content.Context +import android.graphics.Canvas +import android.graphics.Paint +import android.graphics.Rect +import android.support.annotation.CallSuper +import android.support.v4.content.ContextCompat +import com.by.heatchart.R +import com.by.heatchart.data.ChartData +import java.lang.ref.WeakReference + +/** + * HitChart + * Created by Yuriy Bereguliak on 1/22/19. + */ +abstract class BaseDrawController(context: Context, private val chartData: ChartData) : DrawController { + + protected val contextReference = WeakReference(context) + private var framePaint = Paint() + + init { + contextReference.get()?.let { + val res = it.resources + + framePaint.style = Paint.Style.STROKE + framePaint.isAntiAlias = true + framePaint.strokeWidth = res.getDimension(R.dimen.frame_line_width) + framePaint.color = ContextCompat.getColor(it, R.color.gray_400) + } + } + + //region BaseDrawController + protected fun drawFrame(canvas: Canvas) { + val rect = Rect() + rect.top = 0 + rect.left = 0 + rect.right = chartData.width + rect.bottom = chartData.height + + canvas.drawRect(rect, framePaint) + } + //endregion + + //region DrawController + @CallSuper + override fun release() { + contextReference.clear() + } + //endregion +} \ No newline at end of file diff --git a/heatchart/src/main/java/com/by/heatchart/core/controller/DrawController.kt b/heatchart/src/main/java/com/by/heatchart/core/controller/DrawController.kt new file mode 100644 index 0000000..6668590 --- /dev/null +++ b/heatchart/src/main/java/com/by/heatchart/core/controller/DrawController.kt @@ -0,0 +1,13 @@ +package com.by.heatchart.core.controller + +import android.graphics.Canvas + +/** + * HitChart + * Created by Yuriy Bereguliak on 1/22/19. + */ +interface DrawController { + fun draw(canvas: Canvas, data: T) + + fun release() +} \ No newline at end of file diff --git a/heatchart/src/main/java/com/by/heatchart/core/manager/BaseDrawManager.kt b/heatchart/src/main/java/com/by/heatchart/core/manager/BaseDrawManager.kt new file mode 100644 index 0000000..5218068 --- /dev/null +++ b/heatchart/src/main/java/com/by/heatchart/core/manager/BaseDrawManager.kt @@ -0,0 +1,29 @@ +package com.by.heatchart.core.manager + +import com.by.heatchart.core.controller.DrawController +import com.by.heatchart.data.ChartData + +/** + * HitChart + * Created by Yuriy Bereguliak on 1/22/19. + */ +abstract class BaseDrawManager : DrawManager { + + protected val chart = ChartData() + + protected val drawController: DrawController by lazy { + initDrawController() + } + + //region BaseDrawManager + abstract fun initDrawController(): DrawController + //endregion + + //region DrawManager + override fun chartData() = chart + + override fun release() { + drawController.release() + } + //endregion +} \ No newline at end of file diff --git a/heatchart/src/main/java/com/by/heatchart/core/manager/DrawManager.kt b/heatchart/src/main/java/com/by/heatchart/core/manager/DrawManager.kt new file mode 100644 index 0000000..1e0385f --- /dev/null +++ b/heatchart/src/main/java/com/by/heatchart/core/manager/DrawManager.kt @@ -0,0 +1,18 @@ +package com.by.heatchart.core.manager + +import android.graphics.Canvas +import com.by.heatchart.data.ChartData + +/** + * ChartData + * Created by Yuriy Bereguliak on 1/18/19. + */ +interface DrawManager { + fun chartData(): ChartData + + fun draw(canvas: Canvas) + + fun dataSet(dataSet: T) + + fun release() +} \ No newline at end of file diff --git a/heatchart/src/main/java/com/by/heatchart/data/HeatChart.kt b/heatchart/src/main/java/com/by/heatchart/data/ChartData.kt similarity index 55% rename from heatchart/src/main/java/com/by/heatchart/data/HeatChart.kt rename to heatchart/src/main/java/com/by/heatchart/data/ChartData.kt index 5176dfb..f47eea4 100644 --- a/heatchart/src/main/java/com/by/heatchart/data/HeatChart.kt +++ b/heatchart/src/main/java/com/by/heatchart/data/ChartData.kt @@ -1,16 +1,15 @@ package com.by.heatchart.data -data class HeatChart( +data class ChartData( var width: Int = 0, var height: Int = 0, var isApplyToEmptyRangeColor: Boolean = false, - var backgroundColor: Int = -1, - var dataSet: HeatChartDataSet = HeatChartDataSet() + var backgroundColor: Int = -1 ) { - //region HeatChart + //region ChartData fun isBackgroundColorValid() = backgroundColor != -1 && isApplyToEmptyRangeColor - fun calculateMinPart(): Float = width / dataSet.parts + fun calculateMinPart(parts: Float): Float = width / parts //endregion } \ No newline at end of file diff --git a/heatchart/src/main/java/com/by/heatchart/data/HeatChartEntry.kt b/heatchart/src/main/java/com/by/heatchart/data/HeatChartEntry.kt deleted file mode 100644 index be7d624..0000000 --- a/heatchart/src/main/java/com/by/heatchart/data/HeatChartEntry.kt +++ /dev/null @@ -1,8 +0,0 @@ -package com.by.heatchart.data - -data class HeatChartEntry(val startPoint: Int, val endPoint: Int, var rangeColor: Int = -1) { - - //region HeatChartEntry - fun isColorValid() = rangeColor != -1 - //endregion -} \ No newline at end of file diff --git a/heatchart/src/main/java/com/by/heatchart/data/HeatChartDataSet.kt b/heatchart/src/main/java/com/by/heatchart/data/range/RangeHeatChartDataSet.kt similarity index 55% rename from heatchart/src/main/java/com/by/heatchart/data/HeatChartDataSet.kt rename to heatchart/src/main/java/com/by/heatchart/data/range/RangeHeatChartDataSet.kt index 34355a1..9285a49 100644 --- a/heatchart/src/main/java/com/by/heatchart/data/HeatChartDataSet.kt +++ b/heatchart/src/main/java/com/by/heatchart/data/range/RangeHeatChartDataSet.kt @@ -1,15 +1,15 @@ -package com.by.heatchart.data +package com.by.heatchart.data.range /** * HitChart * Created by Yuriy Bereguliak on 1/18/19. */ -data class HeatChartDataSet( +data class RangeHeatChartDataSet( var parts: Float = 100f, - var rangeData: MutableList = mutableListOf() + var rangeData: MutableList = mutableListOf() ) { - //region HeatChartDataSet + //region RangeHeatChartDataSet fun assignColorToRanges(color: Int) { rangeData.forEach { it.rangeColor = color } } diff --git a/heatchart/src/main/java/com/by/heatchart/data/range/RangeHeatChartEntry.kt b/heatchart/src/main/java/com/by/heatchart/data/range/RangeHeatChartEntry.kt new file mode 100644 index 0000000..38119d6 --- /dev/null +++ b/heatchart/src/main/java/com/by/heatchart/data/range/RangeHeatChartEntry.kt @@ -0,0 +1,8 @@ +package com.by.heatchart.data.range + +data class RangeHeatChartEntry(val startPoint: Int, val endPoint: Int, var rangeColor: Int = -1) { + + //region RangeHeatChartEntry + fun isColorValid() = rangeColor != -1 + //endregion +} \ No newline at end of file diff --git a/heatchart/src/main/java/com/by/heatchart/manager/draw/DrawManager.kt b/heatchart/src/main/java/com/by/heatchart/manager/draw/DrawManager.kt deleted file mode 100644 index 15cfa3e..0000000 --- a/heatchart/src/main/java/com/by/heatchart/manager/draw/DrawManager.kt +++ /dev/null @@ -1,14 +0,0 @@ -package com.by.heatchart.manager.draw - -import android.graphics.Canvas -import com.by.heatchart.data.HeatChart - -/** - * HeatChart - * Created by Yuriy Bereguliak on 1/18/19. - */ -interface DrawManager { - fun chartData(): HeatChart - - fun draw(canvas: Canvas) -} \ No newline at end of file diff --git a/heatchart/src/main/java/com/by/heatchart/manager/draw/HeatDrawController.kt b/heatchart/src/main/java/com/by/heatchart/manager/draw/HeatDrawController.kt deleted file mode 100644 index 7eec2f1..0000000 --- a/heatchart/src/main/java/com/by/heatchart/manager/draw/HeatDrawController.kt +++ /dev/null @@ -1,84 +0,0 @@ -package com.by.heatchart.manager.draw - -import android.content.Context -import android.graphics.Canvas -import android.graphics.Paint -import android.graphics.Rect -import android.graphics.RectF -import android.support.v4.content.ContextCompat -import com.by.heatchart.R -import com.by.heatchart.data.HeatChart - -class HeatDrawController( - private var context: Context?, - private var heatChart: HeatChart -) { - - private var framePaint = Paint() - private var rangePaint = Paint() - - init { - context?.let { - val res = it.resources - - framePaint.style = Paint.Style.STROKE - framePaint.isAntiAlias = true - framePaint.strokeWidth = res.getDimension(R.dimen.frame_line_width) - framePaint.color = ContextCompat.getColor(it, R.color.gray_400) - - rangePaint.style = Paint.Style.FILL - rangePaint.isAntiAlias = true - rangePaint.color = ContextCompat.getColor(it, R.color.blue) - } - } - - //region HeatDrawController - fun draw(canvas: Canvas) { - drawFrame(canvas) - drawData(canvas) - } - - fun release() { - context = null - } - //endregion - - //region Utility API - private fun drawFrame(canvas: Canvas) { - val rect = Rect() - rect.top = 0 - rect.left = 0 - rect.right = heatChart.width - rect.bottom = heatChart.height - - canvas.drawRect(rect, framePaint) - } - - private fun drawData(canvas: Canvas) { - val minPartWidth = heatChart.calculateMinPart() - - // Draw ranges from data set - heatChart.dataSet.rangeData.forEach { rangeData -> - val left = rangeData.startPoint * minPartWidth - val right = rangeData.endPoint * minPartWidth - - val rect = RectF() - rect.left = left - rect.top = 0f - rect.right = right - rect.bottom = heatChart.height.toFloat() - - if (rangeData.isColorValid()) { - rangePaint.color = rangeData.rangeColor - } - - canvas.drawRect(rect, rangePaint) - } - - // Find empty ranges and apply color if needed - if (!heatChart.isBackgroundColorValid()) { - return - } - } - //endregion -} \ No newline at end of file diff --git a/heatchart/src/main/java/com/by/heatchart/manager/draw/HeatDrawManager.kt b/heatchart/src/main/java/com/by/heatchart/manager/draw/HeatDrawManager.kt deleted file mode 100644 index 71948b1..0000000 --- a/heatchart/src/main/java/com/by/heatchart/manager/draw/HeatDrawManager.kt +++ /dev/null @@ -1,23 +0,0 @@ -package com.by.heatchart.manager.draw - -import android.content.Context -import android.graphics.Canvas -import com.by.heatchart.data.HeatChart - -/** - * HeatChart - * Created by Yuriy Bereguliak on 1/18/19. - */ -class HeatDrawManager(context: Context) : DrawManager { - - private val chart = HeatChart() - private val drawController = HeatDrawController(context, chart) - - //region DrawManager - override fun chartData() = chart - - override fun draw(canvas: Canvas) { - drawController.draw(canvas) - } - //endregion -} \ No newline at end of file diff --git a/heatchart/src/main/java/com/by/heatchart/view/IndexHeatChartView.kt b/heatchart/src/main/java/com/by/heatchart/view/index/IndexHeatChartView.kt similarity index 71% rename from heatchart/src/main/java/com/by/heatchart/view/IndexHeatChartView.kt rename to heatchart/src/main/java/com/by/heatchart/view/index/IndexHeatChartView.kt index 8705c56..338ee10 100644 --- a/heatchart/src/main/java/com/by/heatchart/view/IndexHeatChartView.kt +++ b/heatchart/src/main/java/com/by/heatchart/view/index/IndexHeatChartView.kt @@ -1,4 +1,4 @@ -package com.by.heatchart.view +package com.by.heatchart.view.index /** * HitChart diff --git a/heatchart/src/main/java/com/by/heatchart/view/RangeHeatChartView.kt b/heatchart/src/main/java/com/by/heatchart/view/range/RangeHeatChartView.kt similarity index 66% rename from heatchart/src/main/java/com/by/heatchart/view/RangeHeatChartView.kt rename to heatchart/src/main/java/com/by/heatchart/view/range/RangeHeatChartView.kt index 50ffc96..e39a150 100644 --- a/heatchart/src/main/java/com/by/heatchart/view/RangeHeatChartView.kt +++ b/heatchart/src/main/java/com/by/heatchart/view/range/RangeHeatChartView.kt @@ -1,21 +1,20 @@ -package com.by.heatchart.view +package com.by.heatchart.view.range import android.content.Context import android.graphics.Canvas import android.util.AttributeSet import android.view.View -import com.by.heatchart.data.HeatChartDataSet -import com.by.heatchart.core.chart.ChartManager -import com.by.heatchart.core.chart.RangeHeatChartManager +import com.by.heatchart.data.range.RangeHeatChartDataSet +import com.by.heatchart.view.range.manager.RangeHeatDrawManager /** - * HeatChart + * ChartData * Created by Yuriy Bereguliak on 1/18/19. */ class RangeHeatChartView(context: Context, attrs: AttributeSet? = null) : View(context, attrs) { - private val chartManager: ChartManager by lazy { - RangeHeatChartManager(context) + private val chartManager by lazy { + RangeHeatDrawManager(context) } //region View @@ -30,15 +29,15 @@ class RangeHeatChartView(context: Context, attrs: AttributeSet? = null) : View(c override fun onDraw(canvas: Canvas?) { super.onDraw(canvas) canvas?.let { - chartManager.drawManager().draw(it) + chartManager.draw(it) } } //endregion //region RangeHeatChartView - fun setChartData(dataSet: HeatChartDataSet) { + fun setChartData(dataSetRange: RangeHeatChartDataSet) { post { - chartManager.chartData().dataSet = dataSet + chartManager.dataSet(dataSetRange) invalidate() } } diff --git a/heatchart/src/main/java/com/by/heatchart/view/range/controller/RangeHeatDrawController.kt b/heatchart/src/main/java/com/by/heatchart/view/range/controller/RangeHeatDrawController.kt new file mode 100644 index 0000000..8f38f9f --- /dev/null +++ b/heatchart/src/main/java/com/by/heatchart/view/range/controller/RangeHeatDrawController.kt @@ -0,0 +1,61 @@ +package com.by.heatchart.view.range.controller + +import android.content.Context +import android.graphics.Canvas +import android.graphics.Paint +import android.graphics.RectF +import android.support.v4.content.ContextCompat +import com.by.heatchart.R +import com.by.heatchart.core.controller.BaseDrawController +import com.by.heatchart.data.ChartData +import com.by.heatchart.data.range.RangeHeatChartDataSet + +class RangeHeatDrawController(context: Context, private var chartData: ChartData) : + BaseDrawController(context, chartData) { + + private var rangePaint = Paint() + + init { + contextReference.get()?.let { + rangePaint.style = Paint.Style.FILL + rangePaint.isAntiAlias = true + rangePaint.color = ContextCompat.getColor(it, R.color.blue) + } + } + + //region DrawController + override fun draw(canvas: Canvas, data: RangeHeatChartDataSet) { + drawFrame(canvas) + drawData(canvas, data) + } + //endregion + + //region Utility API + private fun drawData(canvas: Canvas, data: RangeHeatChartDataSet) { + val minPartWidth = chartData.calculateMinPart(data.parts) + + // Draw ranges from data set + data.rangeData.forEach { rangeData -> + val left = rangeData.startPoint * minPartWidth + val right = rangeData.endPoint * minPartWidth + + val rect = RectF() + rect.left = left + rect.top = 0f + rect.right = right + rect.bottom = chartData.height.toFloat() + + if (rangeData.isColorValid()) { + rangePaint.color = rangeData.rangeColor + } + + canvas.drawRect(rect, rangePaint) + } + + // Find empty ranges and apply color if needed + if (!chartData.isBackgroundColorValid()) { + return + } + } + //endregion +} \ No newline at end of file diff --git a/heatchart/src/main/java/com/by/heatchart/view/range/manager/RangeHeatDrawManager.kt b/heatchart/src/main/java/com/by/heatchart/view/range/manager/RangeHeatDrawManager.kt new file mode 100644 index 0000000..a793d44 --- /dev/null +++ b/heatchart/src/main/java/com/by/heatchart/view/range/manager/RangeHeatDrawManager.kt @@ -0,0 +1,30 @@ +package com.by.heatchart.view.range.manager + +import android.content.Context +import android.graphics.Canvas +import com.by.heatchart.core.manager.BaseDrawManager +import com.by.heatchart.data.range.RangeHeatChartDataSet +import com.by.heatchart.view.range.controller.RangeHeatDrawController + +/** + * ChartData + * Created by Yuriy Bereguliak on 1/18/19. + */ +class RangeHeatDrawManager(private val context: Context) : BaseDrawManager() { + + private var dataSet = RangeHeatChartDataSet() + + //region BaseDrawManager + override fun initDrawController() = RangeHeatDrawController(context, chart) + //endregion + + //region DrawManager + override fun dataSet(dataSet: RangeHeatChartDataSet) { + this.dataSet = dataSet + } + + override fun draw(canvas: Canvas) { + drawController.draw(canvas, dataSet) + } + //endregion +} \ No newline at end of file From eaeb48df1c4954cb8f44b4ff2cbff12f7dbb8a5b Mon Sep 17 00:00:00 2001 From: Yuriy Bereguliak Date: Tue, 22 Jan 2019 12:33:59 +0200 Subject: [PATCH 10/15] Created abstract chart view layer. --- .../bereguliak/yuriy/hitchart/MainActivity.kt | 4 +- .../by/heatchart/core/chart/ChartManager.kt | 14 ------ .../by/heatchart/core/view/BaseChartView.kt | 50 +++++++++++++++++++ .../core/view/ChartViewController.kt | 11 ++++ .../view/range/RangeHeatChartView.kt | 36 ++----------- 5 files changed, 68 insertions(+), 47 deletions(-) delete mode 100644 heatchart/src/main/java/com/by/heatchart/core/chart/ChartManager.kt create mode 100644 heatchart/src/main/java/com/by/heatchart/core/view/BaseChartView.kt create mode 100644 heatchart/src/main/java/com/by/heatchart/core/view/ChartViewController.kt diff --git a/app/src/main/java/com/bereguliak/yuriy/hitchart/MainActivity.kt b/app/src/main/java/com/bereguliak/yuriy/hitchart/MainActivity.kt index 530d0f8..2dea3ef 100644 --- a/app/src/main/java/com/bereguliak/yuriy/hitchart/MainActivity.kt +++ b/app/src/main/java/com/bereguliak/yuriy/hitchart/MainActivity.kt @@ -27,7 +27,7 @@ class MainActivity : AppCompatActivity() { add(RangeHeatChartEntry(85, 98)) add(RangeHeatChartEntry(88, 100)) } - topChartView.setChartData(RangeHeatChartDataSet(rangeData = mutableListFirst)) + topChartView.dataSet(RangeHeatChartDataSet(rangeData = mutableListFirst)) val mutableListWithColor = mutableListOf() with(mutableListWithColor) { @@ -44,6 +44,6 @@ class MainActivity : AppCompatActivity() { dataSet.rangeData = mutableListWithColor dataSet.assignColorToRanges(ContextCompat.getColor(this, R.color.green)) - secondChart.setChartData(dataSet) + secondChart.dataSet(dataSet) } } diff --git a/heatchart/src/main/java/com/by/heatchart/core/chart/ChartManager.kt b/heatchart/src/main/java/com/by/heatchart/core/chart/ChartManager.kt deleted file mode 100644 index d727f0a..0000000 --- a/heatchart/src/main/java/com/by/heatchart/core/chart/ChartManager.kt +++ /dev/null @@ -1,14 +0,0 @@ -package com.by.heatchart.core.chart - -import com.by.heatchart.data.ChartData -import com.by.heatchart.core.manager.DrawManager - -/** - * ChartData - * Created by Yuriy Bereguliak on 1/18/19. - */ -interface ChartManager { - fun chartData(): ChartData - - fun drawManager(): DrawManager -} \ No newline at end of file diff --git a/heatchart/src/main/java/com/by/heatchart/core/view/BaseChartView.kt b/heatchart/src/main/java/com/by/heatchart/core/view/BaseChartView.kt new file mode 100644 index 0000000..add0726 --- /dev/null +++ b/heatchart/src/main/java/com/by/heatchart/core/view/BaseChartView.kt @@ -0,0 +1,50 @@ +package com.by.heatchart.core.view + +import android.content.Context +import android.graphics.Canvas +import android.util.AttributeSet +import android.view.View +import com.by.heatchart.core.manager.DrawManager + +/** + * HitChart + * Created by Yuriy Bereguliak on 1/22/19. + */ +abstract class BaseChartView(context: Context, attrs: AttributeSet? = null) : View(context, attrs), + ChartViewController { + + private val chartManager: DrawManager by lazy { initChartManager() } + + //region BaseChartView + abstract fun initChartManager(): DrawManager + //endregion + + //region View + override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) { + val width = View.MeasureSpec.getSize(widthMeasureSpec) + val height = View.MeasureSpec.getSize(heightMeasureSpec) + chartManager.chartData().width = width + chartManager.chartData().height = height + setMeasuredDimension(width, height) + } + + override fun onDraw(canvas: Canvas?) { + super.onDraw(canvas) + canvas?.let { + chartManager.draw(it) + } + } + //endregion + + //region ChartViewController + override fun dataSet(dataSet: T) { + post { + chartManager.dataSet(dataSet) + } + } + + override fun release() { + chartManager.release() + } + //endregion +} \ No newline at end of file diff --git a/heatchart/src/main/java/com/by/heatchart/core/view/ChartViewController.kt b/heatchart/src/main/java/com/by/heatchart/core/view/ChartViewController.kt new file mode 100644 index 0000000..64fa0ff --- /dev/null +++ b/heatchart/src/main/java/com/by/heatchart/core/view/ChartViewController.kt @@ -0,0 +1,11 @@ +package com.by.heatchart.core.view + +/** + * HitChart + * Created by Yuriy Bereguliak on 1/22/19. + */ +interface ChartViewController { + fun dataSet(dataSet: T) + + fun release() +} \ No newline at end of file diff --git a/heatchart/src/main/java/com/by/heatchart/view/range/RangeHeatChartView.kt b/heatchart/src/main/java/com/by/heatchart/view/range/RangeHeatChartView.kt index e39a150..5441914 100644 --- a/heatchart/src/main/java/com/by/heatchart/view/range/RangeHeatChartView.kt +++ b/heatchart/src/main/java/com/by/heatchart/view/range/RangeHeatChartView.kt @@ -1,9 +1,8 @@ package com.by.heatchart.view.range import android.content.Context -import android.graphics.Canvas import android.util.AttributeSet -import android.view.View +import com.by.heatchart.core.view.BaseChartView import com.by.heatchart.data.range.RangeHeatChartDataSet import com.by.heatchart.view.range.manager.RangeHeatDrawManager @@ -11,35 +10,10 @@ import com.by.heatchart.view.range.manager.RangeHeatDrawManager * ChartData * Created by Yuriy Bereguliak on 1/18/19. */ -class RangeHeatChartView(context: Context, attrs: AttributeSet? = null) : View(context, attrs) { +class RangeHeatChartView(context: Context, attrs: AttributeSet? = null) : + BaseChartView(context, attrs) { - private val chartManager by lazy { - RangeHeatDrawManager(context) - } - - //region View - override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) { - val width = View.MeasureSpec.getSize(widthMeasureSpec) - val height = View.MeasureSpec.getSize(heightMeasureSpec) - chartManager.chartData().width = width - chartManager.chartData().height = height - setMeasuredDimension(width, height) - } - - override fun onDraw(canvas: Canvas?) { - super.onDraw(canvas) - canvas?.let { - chartManager.draw(it) - } - } - //endregion - - //region RangeHeatChartView - fun setChartData(dataSetRange: RangeHeatChartDataSet) { - post { - chartManager.dataSet(dataSetRange) - invalidate() - } - } + //region BaseChartView + override fun initChartManager() = RangeHeatDrawManager(context) //endregion } \ No newline at end of file From 3dabeedbeaa5a414a76db1f13209ae4fd62c3af5 Mon Sep 17 00:00:00 2001 From: Yuriy Bereguliak Date: Tue, 22 Jan 2019 12:47:46 +0200 Subject: [PATCH 11/15] Created index heat chart. --- .../data/index/IndexHeatChartDataEntry.kt | 7 +++++ .../data/index/IndexHeatChartDataSet.kt | 7 +++++ .../index/IndexHeatChartDrawController.kt | 28 ++++++++++++++++++ .../view/index/IndexHeatChartDrawManager.kt | 29 +++++++++++++++++++ .../view/index/IndexHeatChartView.kt | 12 +++++++- .../view/range/RangeHeatChartView.kt | 1 - .../RangeHeatDrawController.kt | 2 +- .../{manager => }/RangeHeatDrawManager.kt | 4 +-- 8 files changed, 85 insertions(+), 5 deletions(-) create mode 100644 heatchart/src/main/java/com/by/heatchart/data/index/IndexHeatChartDataEntry.kt create mode 100644 heatchart/src/main/java/com/by/heatchart/data/index/IndexHeatChartDataSet.kt create mode 100644 heatchart/src/main/java/com/by/heatchart/view/index/IndexHeatChartDrawController.kt create mode 100644 heatchart/src/main/java/com/by/heatchart/view/index/IndexHeatChartDrawManager.kt rename heatchart/src/main/java/com/by/heatchart/view/range/{controller => }/RangeHeatDrawController.kt (97%) rename heatchart/src/main/java/com/by/heatchart/view/range/{manager => }/RangeHeatDrawManager.kt (86%) diff --git a/heatchart/src/main/java/com/by/heatchart/data/index/IndexHeatChartDataEntry.kt b/heatchart/src/main/java/com/by/heatchart/data/index/IndexHeatChartDataEntry.kt new file mode 100644 index 0000000..351db21 --- /dev/null +++ b/heatchart/src/main/java/com/by/heatchart/data/index/IndexHeatChartDataEntry.kt @@ -0,0 +1,7 @@ +package com.by.heatchart.data.index + +/** + * HitChart + * Created by Yuriy Bereguliak on 1/22/19. + */ +data class IndexHeatChartDataEntry(val index: Int, val number: Int, val color: Int = -1) \ No newline at end of file diff --git a/heatchart/src/main/java/com/by/heatchart/data/index/IndexHeatChartDataSet.kt b/heatchart/src/main/java/com/by/heatchart/data/index/IndexHeatChartDataSet.kt new file mode 100644 index 0000000..ab1b7bc --- /dev/null +++ b/heatchart/src/main/java/com/by/heatchart/data/index/IndexHeatChartDataSet.kt @@ -0,0 +1,7 @@ +package com.by.heatchart.data.index + +/** + * HitChart + * Created by Yuriy Bereguliak on 1/22/19. + */ +class IndexHeatChartDataSet(var parts: Int = 100, var data: MutableList = mutableListOf()) \ No newline at end of file diff --git a/heatchart/src/main/java/com/by/heatchart/view/index/IndexHeatChartDrawController.kt b/heatchart/src/main/java/com/by/heatchart/view/index/IndexHeatChartDrawController.kt new file mode 100644 index 0000000..c945e49 --- /dev/null +++ b/heatchart/src/main/java/com/by/heatchart/view/index/IndexHeatChartDrawController.kt @@ -0,0 +1,28 @@ +package com.by.heatchart.view.index + +import android.content.Context +import android.graphics.Canvas +import com.by.heatchart.core.controller.BaseDrawController +import com.by.heatchart.data.ChartData +import com.by.heatchart.data.index.IndexHeatChartDataSet + +/** + * HitChart + * Created by Yuriy Bereguliak on 1/22/19. + */ +class IndexHeatChartDrawController(context: Context, chartData: ChartData) : + BaseDrawController(context, chartData) { + + //region BaseDrawController + override fun draw(canvas: Canvas, data: IndexHeatChartDataSet) { + drawFrame(canvas) + drawData(canvas, data) + } + //endregion + + //region Utility API + private fun drawData(canvas: Canvas, data: IndexHeatChartDataSet) { + + } + //endregion +} \ No newline at end of file diff --git a/heatchart/src/main/java/com/by/heatchart/view/index/IndexHeatChartDrawManager.kt b/heatchart/src/main/java/com/by/heatchart/view/index/IndexHeatChartDrawManager.kt new file mode 100644 index 0000000..a44d8b0 --- /dev/null +++ b/heatchart/src/main/java/com/by/heatchart/view/index/IndexHeatChartDrawManager.kt @@ -0,0 +1,29 @@ +package com.by.heatchart.view.index + +import android.content.Context +import android.graphics.Canvas +import com.by.heatchart.core.manager.BaseDrawManager +import com.by.heatchart.data.index.IndexHeatChartDataSet + +/** + * HitChart + * Created by Yuriy Bereguliak on 1/22/19. + */ +class IndexHeatChartDrawManager(private val context: Context) : BaseDrawManager() { + + private var dataSet = IndexHeatChartDataSet() + + //region BaseDrawManager + override fun initDrawController() = IndexHeatChartDrawController(context, chart) + //endregion + + //region Description + override fun draw(canvas: Canvas) { + drawController.draw(canvas, dataSet) + } + + override fun dataSet(dataSet: IndexHeatChartDataSet) { + this.dataSet = dataSet + } + //endregion +} \ No newline at end of file diff --git a/heatchart/src/main/java/com/by/heatchart/view/index/IndexHeatChartView.kt b/heatchart/src/main/java/com/by/heatchart/view/index/IndexHeatChartView.kt index 338ee10..4777ba4 100644 --- a/heatchart/src/main/java/com/by/heatchart/view/index/IndexHeatChartView.kt +++ b/heatchart/src/main/java/com/by/heatchart/view/index/IndexHeatChartView.kt @@ -1,8 +1,18 @@ package com.by.heatchart.view.index +import android.content.Context +import android.util.AttributeSet +import com.by.heatchart.core.view.BaseChartView +import com.by.heatchart.data.index.IndexHeatChartDataSet + /** * HitChart * Created by Yuriy Bereguliak on 1/22/19. */ -class IndexHeatChartView { +class IndexHeatChartView(context: Context, attrs: AttributeSet? = null) : + BaseChartView(context, attrs) { + + //region BaseChartView + override fun initChartManager() = IndexHeatChartDrawManager(context) + //endregion } \ No newline at end of file diff --git a/heatchart/src/main/java/com/by/heatchart/view/range/RangeHeatChartView.kt b/heatchart/src/main/java/com/by/heatchart/view/range/RangeHeatChartView.kt index 5441914..9edeee2 100644 --- a/heatchart/src/main/java/com/by/heatchart/view/range/RangeHeatChartView.kt +++ b/heatchart/src/main/java/com/by/heatchart/view/range/RangeHeatChartView.kt @@ -4,7 +4,6 @@ import android.content.Context import android.util.AttributeSet import com.by.heatchart.core.view.BaseChartView import com.by.heatchart.data.range.RangeHeatChartDataSet -import com.by.heatchart.view.range.manager.RangeHeatDrawManager /** * ChartData diff --git a/heatchart/src/main/java/com/by/heatchart/view/range/controller/RangeHeatDrawController.kt b/heatchart/src/main/java/com/by/heatchart/view/range/RangeHeatDrawController.kt similarity index 97% rename from heatchart/src/main/java/com/by/heatchart/view/range/controller/RangeHeatDrawController.kt rename to heatchart/src/main/java/com/by/heatchart/view/range/RangeHeatDrawController.kt index 8f38f9f..49c41e7 100644 --- a/heatchart/src/main/java/com/by/heatchart/view/range/controller/RangeHeatDrawController.kt +++ b/heatchart/src/main/java/com/by/heatchart/view/range/RangeHeatDrawController.kt @@ -1,4 +1,4 @@ -package com.by.heatchart.view.range.controller +package com.by.heatchart.view.range import android.content.Context import android.graphics.Canvas diff --git a/heatchart/src/main/java/com/by/heatchart/view/range/manager/RangeHeatDrawManager.kt b/heatchart/src/main/java/com/by/heatchart/view/range/RangeHeatDrawManager.kt similarity index 86% rename from heatchart/src/main/java/com/by/heatchart/view/range/manager/RangeHeatDrawManager.kt rename to heatchart/src/main/java/com/by/heatchart/view/range/RangeHeatDrawManager.kt index a793d44..fee0c60 100644 --- a/heatchart/src/main/java/com/by/heatchart/view/range/manager/RangeHeatDrawManager.kt +++ b/heatchart/src/main/java/com/by/heatchart/view/range/RangeHeatDrawManager.kt @@ -1,10 +1,10 @@ -package com.by.heatchart.view.range.manager +package com.by.heatchart.view.range import android.content.Context import android.graphics.Canvas import com.by.heatchart.core.manager.BaseDrawManager import com.by.heatchart.data.range.RangeHeatChartDataSet -import com.by.heatchart.view.range.controller.RangeHeatDrawController +import com.by.heatchart.view.range.RangeHeatDrawController /** * ChartData From d4292b2d4fc1e8d3f3f5ac9285a6c5647069f13d Mon Sep 17 00:00:00 2001 From: Yuriy Bereguliak Date: Tue, 22 Jan 2019 13:09:02 +0200 Subject: [PATCH 12/15] Updated sample ui and chart. --- .../bereguliak/yuriy/hitchart/MainActivity.kt | 29 +++++++++++++++++++ app/src/main/res/layout/activity_main.xml | 17 +++++++++++ 2 files changed, 46 insertions(+) diff --git a/app/src/main/java/com/bereguliak/yuriy/hitchart/MainActivity.kt b/app/src/main/java/com/bereguliak/yuriy/hitchart/MainActivity.kt index 2dea3ef..8eace0e 100644 --- a/app/src/main/java/com/bereguliak/yuriy/hitchart/MainActivity.kt +++ b/app/src/main/java/com/bereguliak/yuriy/hitchart/MainActivity.kt @@ -3,16 +3,29 @@ package com.bereguliak.yuriy.hitchart import android.os.Bundle import android.support.v4.content.ContextCompat import android.support.v7.app.AppCompatActivity +import com.by.heatchart.data.index.IndexHeatChartDataEntry +import com.by.heatchart.data.index.IndexHeatChartDataSet import com.by.heatchart.data.range.RangeHeatChartDataSet import com.by.heatchart.data.range.RangeHeatChartEntry import kotlinx.android.synthetic.main.activity_main.* class MainActivity : AppCompatActivity() { + //region AppCompatActivity override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) + initFirstRangeChart() + + initSecondCustomRangeChart() + + initThirdIndexChart() + } + //endregion + + //region Utility API + private fun initFirstRangeChart() { val mutableListFirst = mutableListOf() with(mutableListFirst) { add(RangeHeatChartEntry(0, 20)) @@ -28,7 +41,9 @@ class MainActivity : AppCompatActivity() { add(RangeHeatChartEntry(88, 100)) } topChartView.dataSet(RangeHeatChartDataSet(rangeData = mutableListFirst)) + } + private fun initSecondCustomRangeChart() { val mutableListWithColor = mutableListOf() with(mutableListWithColor) { add(RangeHeatChartEntry(0, 20)) @@ -46,4 +61,18 @@ class MainActivity : AppCompatActivity() { secondChart.dataSet(dataSet) } + + private fun initThirdIndexChart() { + val mutableList = mutableListOf() + + with(mutableList) { + add(IndexHeatChartDataEntry(0, 2)) + add(IndexHeatChartDataEntry(2, 1)) + add(IndexHeatChartDataEntry(5, 6)) + add(IndexHeatChartDataEntry(7, 3)) + } + + indexChartView.dataSet(IndexHeatChartDataSet(10f, mutableList)) + } + //endregion } diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index 0a26ba1..72f9899 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -40,4 +40,21 @@ android:id="@+id/guideline2" android:orientation="horizontal" app:layout_constraintGuide_percent="0.30"/> + + + + \ No newline at end of file From 422eff96dab88ad21f0474f947e64f4a1de79f60 Mon Sep 17 00:00:00 2001 From: Yuriy Bereguliak Date: Tue, 22 Jan 2019 13:09:21 +0200 Subject: [PATCH 13/15] Draw index heat chart. --- .../core/controller/BaseDrawController.kt | 2 +- .../data/index/IndexHeatChartDataEntry.kt | 6 ++- .../data/index/IndexHeatChartDataSet.kt | 9 ++++- .../index/IndexHeatChartDrawController.kt | 38 ++++++++++++++++++- 4 files changed, 51 insertions(+), 4 deletions(-) diff --git a/heatchart/src/main/java/com/by/heatchart/core/controller/BaseDrawController.kt b/heatchart/src/main/java/com/by/heatchart/core/controller/BaseDrawController.kt index 69eaad8..ab20c27 100644 --- a/heatchart/src/main/java/com/by/heatchart/core/controller/BaseDrawController.kt +++ b/heatchart/src/main/java/com/by/heatchart/core/controller/BaseDrawController.kt @@ -17,7 +17,7 @@ import java.lang.ref.WeakReference abstract class BaseDrawController(context: Context, private val chartData: ChartData) : DrawController { protected val contextReference = WeakReference(context) - private var framePaint = Paint() + protected var framePaint = Paint() init { contextReference.get()?.let { diff --git a/heatchart/src/main/java/com/by/heatchart/data/index/IndexHeatChartDataEntry.kt b/heatchart/src/main/java/com/by/heatchart/data/index/IndexHeatChartDataEntry.kt index 351db21..c1ced0d 100644 --- a/heatchart/src/main/java/com/by/heatchart/data/index/IndexHeatChartDataEntry.kt +++ b/heatchart/src/main/java/com/by/heatchart/data/index/IndexHeatChartDataEntry.kt @@ -4,4 +4,8 @@ package com.by.heatchart.data.index * HitChart * Created by Yuriy Bereguliak on 1/22/19. */ -data class IndexHeatChartDataEntry(val index: Int, val number: Int, val color: Int = -1) \ No newline at end of file +data class IndexHeatChartDataEntry(val index: Int, val number: Int, var color: Int = -1) { + //region IndexHeatChartDataEntry + fun isColorValid() = color != -1 + //endregion +} \ No newline at end of file diff --git a/heatchart/src/main/java/com/by/heatchart/data/index/IndexHeatChartDataSet.kt b/heatchart/src/main/java/com/by/heatchart/data/index/IndexHeatChartDataSet.kt index ab1b7bc..0410ae9 100644 --- a/heatchart/src/main/java/com/by/heatchart/data/index/IndexHeatChartDataSet.kt +++ b/heatchart/src/main/java/com/by/heatchart/data/index/IndexHeatChartDataSet.kt @@ -4,4 +4,11 @@ package com.by.heatchart.data.index * HitChart * Created by Yuriy Bereguliak on 1/22/19. */ -class IndexHeatChartDataSet(var parts: Int = 100, var data: MutableList = mutableListOf()) \ No newline at end of file +class IndexHeatChartDataSet(var parts: Float = 100f, var data: MutableList = mutableListOf()) { + + //region IndexHeatChartDataSet + fun assignColorToRanges(color: Int) { + data.forEach { it.color = color } + } + //endregion +} \ No newline at end of file diff --git a/heatchart/src/main/java/com/by/heatchart/view/index/IndexHeatChartDrawController.kt b/heatchart/src/main/java/com/by/heatchart/view/index/IndexHeatChartDrawController.kt index c945e49..3b34dfb 100644 --- a/heatchart/src/main/java/com/by/heatchart/view/index/IndexHeatChartDrawController.kt +++ b/heatchart/src/main/java/com/by/heatchart/view/index/IndexHeatChartDrawController.kt @@ -2,6 +2,10 @@ package com.by.heatchart.view.index import android.content.Context import android.graphics.Canvas +import android.graphics.Paint +import android.graphics.RectF +import android.support.v4.content.ContextCompat +import com.by.heatchart.R import com.by.heatchart.core.controller.BaseDrawController import com.by.heatchart.data.ChartData import com.by.heatchart.data.index.IndexHeatChartDataSet @@ -10,9 +14,19 @@ import com.by.heatchart.data.index.IndexHeatChartDataSet * HitChart * Created by Yuriy Bereguliak on 1/22/19. */ -class IndexHeatChartDrawController(context: Context, chartData: ChartData) : +class IndexHeatChartDrawController(context: Context, private var chartData: ChartData) : BaseDrawController(context, chartData) { + private var rangePaint = Paint() + + init { + contextReference.get()?.let { + rangePaint.style = Paint.Style.FILL + rangePaint.isAntiAlias = true + rangePaint.color = ContextCompat.getColor(it, R.color.blue) + } + } + //region BaseDrawController override fun draw(canvas: Canvas, data: IndexHeatChartDataSet) { drawFrame(canvas) @@ -22,7 +36,29 @@ class IndexHeatChartDrawController(context: Context, chartData: ChartData) : //region Utility API private fun drawData(canvas: Canvas, data: IndexHeatChartDataSet) { + val minPartWidth = chartData.calculateMinPart(data.parts) + + data.data.forEach { entry -> + val left = entry.index * minPartWidth + val right = left + minPartWidth + val rect = RectF() + rect.left = left + rect.top = 0f + rect.right = right + rect.bottom = chartData.height.toFloat() + + if (entry.isColorValid()) { + rangePaint.color = entry.color + } + + for (i in 0..entry.number) { + canvas.drawRect(rect, rangePaint) + } + } } + +// canvas.drawLines(floatArrayOf(rect.left + minPartWidth, rect.top, rect.right, rect.bottom), framePaint) + //endregion } \ No newline at end of file From 39d89e2855a4822a31a03d915ea6f486d289d407 Mon Sep 17 00:00:00 2001 From: Yuriy Bereguliak Date: Tue, 22 Jan 2019 14:09:56 +0200 Subject: [PATCH 14/15] Updated item range border. --- .../core/controller/BaseDrawController.kt | 2 +- .../index/IndexHeatChartDrawController.kt | 21 ++++++++++++++++--- heatchart/src/main/res/values/dimens.xml | 1 + 3 files changed, 20 insertions(+), 4 deletions(-) diff --git a/heatchart/src/main/java/com/by/heatchart/core/controller/BaseDrawController.kt b/heatchart/src/main/java/com/by/heatchart/core/controller/BaseDrawController.kt index ab20c27..69eaad8 100644 --- a/heatchart/src/main/java/com/by/heatchart/core/controller/BaseDrawController.kt +++ b/heatchart/src/main/java/com/by/heatchart/core/controller/BaseDrawController.kt @@ -17,7 +17,7 @@ import java.lang.ref.WeakReference abstract class BaseDrawController(context: Context, private val chartData: ChartData) : DrawController { protected val contextReference = WeakReference(context) - protected var framePaint = Paint() + private var framePaint = Paint() init { contextReference.get()?.let { diff --git a/heatchart/src/main/java/com/by/heatchart/view/index/IndexHeatChartDrawController.kt b/heatchart/src/main/java/com/by/heatchart/view/index/IndexHeatChartDrawController.kt index 3b34dfb..ee0ed3b 100644 --- a/heatchart/src/main/java/com/by/heatchart/view/index/IndexHeatChartDrawController.kt +++ b/heatchart/src/main/java/com/by/heatchart/view/index/IndexHeatChartDrawController.kt @@ -18,9 +18,17 @@ class IndexHeatChartDrawController(context: Context, private var chartData: Char BaseDrawController(context, chartData) { private var rangePaint = Paint() + private var borderPaint = Paint() init { contextReference.get()?.let { + val res = it.resources + + borderPaint.style = Paint.Style.STROKE + borderPaint.isAntiAlias = true + borderPaint.strokeWidth = res.getDimension(R.dimen.frame_border_width) + borderPaint.color = ContextCompat.getColor(it, R.color.gray_400) + rangePaint.style = Paint.Style.FILL rangePaint.isAntiAlias = true rangePaint.color = ContextCompat.getColor(it, R.color.blue) @@ -31,10 +39,20 @@ class IndexHeatChartDrawController(context: Context, private var chartData: Char override fun draw(canvas: Canvas, data: IndexHeatChartDataSet) { drawFrame(canvas) drawData(canvas, data) + drawElementsBounds(canvas, data) } //endregion //region Utility API + private fun drawElementsBounds(canvas: Canvas, data: IndexHeatChartDataSet) { + val minPartWidth = chartData.calculateMinPart(data.parts) + + for (i in 0..data.parts.toInt()) { + val left = i * minPartWidth + minPartWidth + canvas.drawLines(floatArrayOf(left, 0f, left, chartData.height.toFloat()), borderPaint) + } + } + private fun drawData(canvas: Canvas, data: IndexHeatChartDataSet) { val minPartWidth = chartData.calculateMinPart(data.parts) @@ -57,8 +75,5 @@ class IndexHeatChartDrawController(context: Context, private var chartData: Char } } } - -// canvas.drawLines(floatArrayOf(rect.left + minPartWidth, rect.top, rect.right, rect.bottom), framePaint) - //endregion } \ No newline at end of file diff --git a/heatchart/src/main/res/values/dimens.xml b/heatchart/src/main/res/values/dimens.xml index 1f0a827..e0f3e48 100644 --- a/heatchart/src/main/res/values/dimens.xml +++ b/heatchart/src/main/res/values/dimens.xml @@ -1,4 +1,5 @@ 3dp + 1dp \ No newline at end of file From 1df649945b6fef351ad9a74ee82539c6454e1fc3 Mon Sep 17 00:00:00 2001 From: Yuriy Bereguliak Date: Tue, 22 Jan 2019 14:44:09 +0200 Subject: [PATCH 15/15] Created styleable attributes. --- .../bereguliak/yuriy/hitchart/MainActivity.kt | 2 - app/src/main/res/layout/activity_main.xml | 6 +++ app/src/main/res/values/colors.xml | 2 + .../core/controller/BaseDrawController.kt | 8 +--- .../by/heatchart/core/view/BaseChartView.kt | 40 +++++++++++++++++++ .../java/com/by/heatchart/data/ChartData.kt | 7 +++- .../index/IndexHeatChartDrawController.kt | 12 ++---- .../view/range/RangeHeatDrawController.kt | 6 +-- heatchart/src/main/res/values/attrs.xml | 11 +++++ heatchart/src/main/res/values/dimens.xml | 4 +- 10 files changed, 75 insertions(+), 23 deletions(-) create mode 100644 heatchart/src/main/res/values/attrs.xml diff --git a/app/src/main/java/com/bereguliak/yuriy/hitchart/MainActivity.kt b/app/src/main/java/com/bereguliak/yuriy/hitchart/MainActivity.kt index 8eace0e..8913f3f 100644 --- a/app/src/main/java/com/bereguliak/yuriy/hitchart/MainActivity.kt +++ b/app/src/main/java/com/bereguliak/yuriy/hitchart/MainActivity.kt @@ -1,7 +1,6 @@ package com.bereguliak.yuriy.hitchart import android.os.Bundle -import android.support.v4.content.ContextCompat import android.support.v7.app.AppCompatActivity import com.by.heatchart.data.index.IndexHeatChartDataEntry import com.by.heatchart.data.index.IndexHeatChartDataSet @@ -57,7 +56,6 @@ class MainActivity : AppCompatActivity() { val dataSet = RangeHeatChartDataSet() dataSet.parts = 100f dataSet.rangeData = mutableListWithColor - dataSet.assignColorToRanges(ContextCompat.getColor(this, R.color.green)) secondChart.dataSet(dataSet) } diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index 72f9899..c58bcca 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -14,6 +14,8 @@ app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" android:layout_margin="8dp" + app:chartColor="@color/green" + app:chartFrameWidth="@dimen/chart_border_width" app:layout_constraintTop_toTopOf="parent" app:layout_constraintBottom_toTopOf="@+id/guideline"/> @@ -29,6 +31,8 @@ android:layout_margin="8dp" android:layout_width="0dp" android:layout_height="0dp" + app:chartColor="@color/pink" + app:chartFrameWidth="@dimen/chart_border_width" app:layout_constraintBottom_toTopOf="@+id/guideline2" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" @@ -46,6 +50,8 @@ android:layout_width="0dp" android:layout_height="0dp" android:layout_margin="8dp" + app:chartFrameWidth="@dimen/chart_border_width" + app:chartFrameSeparatorWidth="@dimen/chart_separator_width" app:layout_constraintBottom_toTopOf="@+id/guideline3" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" diff --git a/app/src/main/res/values/colors.xml b/app/src/main/res/values/colors.xml index 69b2233..d7929e8 100644 --- a/app/src/main/res/values/colors.xml +++ b/app/src/main/res/values/colors.xml @@ -3,4 +3,6 @@ #008577 #00574B #D81B60 + + #40D81B60 diff --git a/heatchart/src/main/java/com/by/heatchart/core/controller/BaseDrawController.kt b/heatchart/src/main/java/com/by/heatchart/core/controller/BaseDrawController.kt index 69eaad8..fefc6ad 100644 --- a/heatchart/src/main/java/com/by/heatchart/core/controller/BaseDrawController.kt +++ b/heatchart/src/main/java/com/by/heatchart/core/controller/BaseDrawController.kt @@ -5,8 +5,6 @@ import android.graphics.Canvas import android.graphics.Paint import android.graphics.Rect import android.support.annotation.CallSuper -import android.support.v4.content.ContextCompat -import com.by.heatchart.R import com.by.heatchart.data.ChartData import java.lang.ref.WeakReference @@ -21,12 +19,10 @@ abstract class BaseDrawController(context: Context, private val chartData: init { contextReference.get()?.let { - val res = it.resources - framePaint.style = Paint.Style.STROKE framePaint.isAntiAlias = true - framePaint.strokeWidth = res.getDimension(R.dimen.frame_line_width) - framePaint.color = ContextCompat.getColor(it, R.color.gray_400) + framePaint.strokeWidth = chartData.frameBorderWidth + framePaint.color = chartData.frameColor } } diff --git a/heatchart/src/main/java/com/by/heatchart/core/view/BaseChartView.kt b/heatchart/src/main/java/com/by/heatchart/core/view/BaseChartView.kt index add0726..9651122 100644 --- a/heatchart/src/main/java/com/by/heatchart/core/view/BaseChartView.kt +++ b/heatchart/src/main/java/com/by/heatchart/core/view/BaseChartView.kt @@ -2,8 +2,10 @@ package com.by.heatchart.core.view import android.content.Context import android.graphics.Canvas +import android.support.v4.content.ContextCompat import android.util.AttributeSet import android.view.View +import com.by.heatchart.R import com.by.heatchart.core.manager.DrawManager /** @@ -15,6 +17,44 @@ abstract class BaseChartView(context: Context, attrs: AttributeSet? = null) : private val chartManager: DrawManager by lazy { initChartManager() } + init { + attrs?.let { + val typedArray = context.obtainStyledAttributes(it, R.styleable.BaseChartView) + + chartManager.chartData().chartColor = typedArray.getColor( + R.styleable.BaseChartView_chartColor, + ContextCompat.getColor(context, R.color.blue) + ) + + chartManager.chartData().frameColor = typedArray.getColor( + R.styleable.BaseChartView_chartFrameColor, + ContextCompat.getColor(context, R.color.gray_400) + ) + + chartManager.chartData().backgroundColor = typedArray.getColor( + R.styleable.BaseChartView_chartBackgroundColor, + ContextCompat.getColor(context, R.color.gray_400) + ) + + chartManager.chartData().separatorColor = typedArray.getColor( + R.styleable.BaseChartView_chartSeparatorColor, + ContextCompat.getColor(context, R.color.gray_400) + ) + + chartManager.chartData().frameBorderWidth = typedArray.getDimension( + R.styleable.BaseChartView_chartFrameWidth, + context.resources.getDimension(R.dimen.chart_border_width) + ) + + chartManager.chartData().frameSeparatorWidth = typedArray.getDimension( + R.styleable.BaseChartView_chartFrameSeparatorWidth, + context.resources.getDimension(R.dimen.chart_separator_width) + ) + + typedArray.recycle() + } + } + //region BaseChartView abstract fun initChartManager(): DrawManager //endregion diff --git a/heatchart/src/main/java/com/by/heatchart/data/ChartData.kt b/heatchart/src/main/java/com/by/heatchart/data/ChartData.kt index f47eea4..f8b6a72 100644 --- a/heatchart/src/main/java/com/by/heatchart/data/ChartData.kt +++ b/heatchart/src/main/java/com/by/heatchart/data/ChartData.kt @@ -4,7 +4,12 @@ data class ChartData( var width: Int = 0, var height: Int = 0, var isApplyToEmptyRangeColor: Boolean = false, - var backgroundColor: Int = -1 + var backgroundColor: Int = -1, + var frameColor: Int = -1, + var separatorColor: Int = -1, + var chartColor: Int = -1, + var frameBorderWidth: Float = -1f, + var frameSeparatorWidth: Float = -1f ) { //region ChartData diff --git a/heatchart/src/main/java/com/by/heatchart/view/index/IndexHeatChartDrawController.kt b/heatchart/src/main/java/com/by/heatchart/view/index/IndexHeatChartDrawController.kt index ee0ed3b..090081e 100644 --- a/heatchart/src/main/java/com/by/heatchart/view/index/IndexHeatChartDrawController.kt +++ b/heatchart/src/main/java/com/by/heatchart/view/index/IndexHeatChartDrawController.kt @@ -4,8 +4,6 @@ import android.content.Context import android.graphics.Canvas import android.graphics.Paint import android.graphics.RectF -import android.support.v4.content.ContextCompat -import com.by.heatchart.R import com.by.heatchart.core.controller.BaseDrawController import com.by.heatchart.data.ChartData import com.by.heatchart.data.index.IndexHeatChartDataSet @@ -22,24 +20,22 @@ class IndexHeatChartDrawController(context: Context, private var chartData: Char init { contextReference.get()?.let { - val res = it.resources - borderPaint.style = Paint.Style.STROKE borderPaint.isAntiAlias = true - borderPaint.strokeWidth = res.getDimension(R.dimen.frame_border_width) - borderPaint.color = ContextCompat.getColor(it, R.color.gray_400) + borderPaint.strokeWidth = chartData.frameSeparatorWidth + borderPaint.color = chartData.separatorColor rangePaint.style = Paint.Style.FILL rangePaint.isAntiAlias = true - rangePaint.color = ContextCompat.getColor(it, R.color.blue) + rangePaint.color = chartData.chartColor } } //region BaseDrawController override fun draw(canvas: Canvas, data: IndexHeatChartDataSet) { - drawFrame(canvas) drawData(canvas, data) drawElementsBounds(canvas, data) + drawFrame(canvas) } //endregion diff --git a/heatchart/src/main/java/com/by/heatchart/view/range/RangeHeatDrawController.kt b/heatchart/src/main/java/com/by/heatchart/view/range/RangeHeatDrawController.kt index 49c41e7..5f14170 100644 --- a/heatchart/src/main/java/com/by/heatchart/view/range/RangeHeatDrawController.kt +++ b/heatchart/src/main/java/com/by/heatchart/view/range/RangeHeatDrawController.kt @@ -4,8 +4,6 @@ import android.content.Context import android.graphics.Canvas import android.graphics.Paint import android.graphics.RectF -import android.support.v4.content.ContextCompat -import com.by.heatchart.R import com.by.heatchart.core.controller.BaseDrawController import com.by.heatchart.data.ChartData import com.by.heatchart.data.range.RangeHeatChartDataSet @@ -19,14 +17,14 @@ class RangeHeatDrawController(context: Context, private var chartData: ChartData contextReference.get()?.let { rangePaint.style = Paint.Style.FILL rangePaint.isAntiAlias = true - rangePaint.color = ContextCompat.getColor(it, R.color.blue) + rangePaint.color = chartData.chartColor } } //region DrawController override fun draw(canvas: Canvas, data: RangeHeatChartDataSet) { - drawFrame(canvas) drawData(canvas, data) + drawFrame(canvas) } //endregion diff --git a/heatchart/src/main/res/values/attrs.xml b/heatchart/src/main/res/values/attrs.xml new file mode 100644 index 0000000..552b868 --- /dev/null +++ b/heatchart/src/main/res/values/attrs.xml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/heatchart/src/main/res/values/dimens.xml b/heatchart/src/main/res/values/dimens.xml index e0f3e48..8ddd7d5 100644 --- a/heatchart/src/main/res/values/dimens.xml +++ b/heatchart/src/main/res/values/dimens.xml @@ -1,5 +1,5 @@ - 3dp - 1dp + 3dp + 1dp \ No newline at end of file