Skip to content

Commit

Permalink
feat: Make sample more customizable (#43)
Browse files Browse the repository at this point in the history
  • Loading branch information
Aviortheking authored Feb 16, 2023
1 parent 1a812ac commit 3df7541
Show file tree
Hide file tree
Showing 24 changed files with 796 additions and 529 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ chart.refresh()
<img width="40%" src=".github/usage-example.jpg" />
</p>

_note: Every charts used above used a helper function to have Material 3 colors [See MainFragment.kt the materialTheme function](./sample/src/main/java/com/dzeio/chartstest/ui/MainFragment.kt)_
_note: Every charts used above used a helper function to have Material 3 colors [See the MaterialUtils.kt class](sample/src/main/java/com/dzeio/chartsapp/utils/MaterialUtils.kt)_

## Build

Expand Down
3 changes: 3 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ buildscript {
}
dependencies {
classpath("com.android.tools.build:gradle:7.4.1")

// Safe Navigation
classpath("androidx.navigation:navigation-safe-args-gradle-plugin:2.5.3")
}
}

Expand Down
12 changes: 6 additions & 6 deletions library/src/main/java/com/dzeio/charts/ChartView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class ChartView @JvmOverloads constructor(context: Context?, attrs: AttributeSet
View(context, attrs), ChartViewInterface {

private companion object {
const val TAG = "Charts/ChartView"
const val TAG = "ChartView"
}

override val animator: Animation = Animation()
Expand Down Expand Up @@ -97,6 +97,10 @@ class ChartView @JvmOverloads constructor(context: Context?, attrs: AttributeSet

refresh()
}
setOnToggleScroll {
// note: true == no scroll
parent?.requestDisallowInterceptTouchEvent(!it && (yAxis.scrollEnabled || xAxis.scrollEnabled))
}
setOnChartClick { x, y ->
if (getDataset().isEmpty()) {
return@setOnChartClick
Expand Down Expand Up @@ -161,15 +165,11 @@ class ChartView @JvmOverloads constructor(context: Context?, attrs: AttributeSet

// invalidate the view
invalidate()
// removeCallbacks(animator)
// post(animator)
}

private var lastRun = runUpdates

override fun onDraw(canvas: Canvas) {
// don't draw anything if everything is empty
if (!runUpdates && lastRun == runUpdates && series.isEmpty() || series.maxOf { it.entries.size } == 0) {
if (!runUpdates || series.isEmpty() || series.maxOf { it.entries.size } == 0) {
super.onDraw(canvas)
return
}
Expand Down
15 changes: 5 additions & 10 deletions library/src/main/java/com/dzeio/charts/axis/XAxis.kt
Original file line number Diff line number Diff line change
Expand Up @@ -92,18 +92,14 @@ class XAxis(

var maxHeight = 0f

val graphIncrement = space.width() / (labelCount - 1)
val valueIncrement = getDataWidth() / (labelCount - 1)
val valueIncrement = getDataWidth() / (labelCount - 1).coerceAtLeast(1)
for (index in 0 until labelCount) {
val text = onValueFormat(x + valueIncrement * index)
textPaint.getTextBounds(text, 0, text.length, rect)
getPositionOnRect(valueIncrement, space)
maxHeight = maxHeight.coerceAtLeast(rect.height().toFloat() + 1)

var xPos = space.left + graphIncrement * index

if (xPos + rect.width() > space.right) {
xPos = space.right - rect.width()
}
val xPos = getPositionOnRect(x + valueIncrement * index, space).toFloat()

canvas.drawText(
text,
Expand Down Expand Up @@ -134,8 +130,8 @@ class XAxis(
.coerceIn(1.0, drawableSpace.width().toDouble())

// handle grouped series
if (view.type == ChartType.GROUPED) {
return result / view.series.size - spacing / 2 * (view.series.size - 1)
if (view.type == ChartType.GROUPED && view.series.size > 1) {
return ((result - (spacing / 2 * view.series.size)) / view.series.size).coerceAtLeast(1.0)
}

return result
Expand All @@ -145,5 +141,4 @@ class XAxis(
// TODO: handle the auto dataWidth better (still not sure it is good enough)
return dataWidth ?: (getXMax() - getXMin() + 1)
}

}
2 changes: 1 addition & 1 deletion library/src/main/java/com/dzeio/charts/axis/YAxis.kt
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ class YAxis(
val max = getYMax() - min
var maxWidth = 0f

val valueIncrement = max / (labelCount - 1)
val valueIncrement = max / (labelCount - 1).coerceAtLeast(1)
for (index in 0 until labelCount) {
val value = min + (valueIncrement * index)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,11 @@ class Annotation(

val xCenter = view.xAxis.getEntryWidth(space) / 2.0 + x

if (xCenter < space.left || xCenter > space.right) {
entry = null
return
}

val xText = annotationSubTitleFormat.invoke(entry!!)
val yText = annotationTitleFormat.invoke(entry!!)

Expand Down
160 changes: 0 additions & 160 deletions library/src/main/java/com/dzeio/charts/series/BarSerie.kt.old

This file was deleted.

23 changes: 17 additions & 6 deletions sample/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,14 +1,23 @@
plugins {
// Android Application?
id("com.android.application")

// Support for kotlin in Android
kotlin("android")

// Safe Navigation
id("androidx.navigation.safeargs")

// keep at bottom
kotlin("kapt")
}

android {
namespace = "com.dzeio.chartstest"
namespace = "com.dzeio.chartsapp"
compileSdk = 33

defaultConfig {
applicationId = "com.dzeio.chartstest"
applicationId = "com.dzeio.chartsapp"
minSdk = 21
targetSdk = 33
versionCode = 1
Expand All @@ -31,19 +40,21 @@ android {
kotlinOptions {
jvmTarget = "11"
}

buildFeatures {
viewBinding = true
dataBinding = true
}
}

dependencies {

implementation(project(":library"))

// Material Design
implementation("com.google.android.material:material:1.7.0")
implementation("com.google.android.material:material:1.8.0")

// Navigation because I don't want to maintain basic transactions and shit
implementation("androidx.navigation:navigation-fragment-ktx:2.5.3")
}
implementation("androidx.navigation:navigation-ui-ktx:2.5.3")
}
4 changes: 2 additions & 2 deletions sample/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

<application
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:label="Dzeio Charts"
android:theme="@style/Theme.Charts">
<activity
android:name=".ui.MainActivity"
Expand All @@ -16,4 +16,4 @@
</activity>
</application>

</manifest>
</manifest>
Loading

0 comments on commit 3df7541

Please # to comment.