Skip to content

Commit e11d4a1

Browse files
authored
Merge pull request #599 from AppDevNext/GradientIssue
Show gradienten issue as a sample
2 parents e2e2332 + 5c291d7 commit e11d4a1

File tree

6 files changed

+113
-0
lines changed

6 files changed

+113
-0
lines changed

app/src/main/AndroidManifest.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
<activity android:name="info.appdev.chartexample.StackedBarActivityNegative" />
5454
<activity android:name="info.appdev.chartexample.BarChartPositiveNegative" />
5555
<activity android:name="info.appdev.chartexample.FilledLineActivity" />
56+
<activity android:name="info.appdev.chartexample.GradientActivity" />
5657
<activity android:name="info.appdev.chartexample.HalfPieChartActivity" />
5758
<activity android:name="info.appdev.chartexample.SpecificPositionsLineChartActivity" />
5859
</application>
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
package info.appdev.chartexample
2+
3+
import android.os.Bundle
4+
import androidx.activity.enableEdgeToEdge
5+
import androidx.core.content.ContextCompat
6+
import androidx.core.view.ViewCompat
7+
import androidx.core.view.WindowInsetsCompat
8+
import info.appdev.chartexample.notimportant.DemoBase
9+
import info.appdev.charting.charts.LineChart
10+
import info.appdev.charting.data.Entry
11+
import info.appdev.charting.data.LineData
12+
import info.appdev.charting.data.LineDataSet
13+
import info.appdev.charting.formatter.IFillFormatter
14+
import info.appdev.charting.interfaces.dataprovider.LineDataProvider
15+
import info.appdev.charting.interfaces.datasets.ILineDataSet
16+
import info.appdev.charting.utils.Utils
17+
18+
class GradientActivity : DemoBase() {
19+
20+
override fun onCreate(savedInstanceState: Bundle?) {
21+
super.onCreate(savedInstanceState)
22+
enableEdgeToEdge()
23+
setContentView(R.layout.activity_gradient)
24+
25+
ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
26+
val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
27+
v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
28+
insets
29+
}
30+
31+
Utils.init(this)
32+
33+
val chart: LineChart = findViewById(R.id.chart)
34+
35+
// Minimal chart setup
36+
chart.description?.isEnabled = false
37+
chart.legend?.isEnabled = false
38+
chart.axisRight.isEnabled = false
39+
chart.setDrawGridBackground(false)
40+
41+
// -----------------------------
42+
// Linear function
43+
// y = k * x
44+
// -----------------------------
45+
val entries = kotlin.collections.ArrayList<Entry>(200)
46+
val k = 2.5f
47+
48+
for (i in 0 until 200) {
49+
entries.add(Entry(i.toFloat(), i * k))
50+
}
51+
52+
val dataSet = LineDataSet(entries, "Linear").apply {
53+
isDrawValues = false
54+
isDrawCirclesEnabled = false
55+
lineWidth = 2f
56+
57+
isDrawFilledEnabled = true
58+
fillAlpha = 255
59+
fillDrawable = ContextCompat.getDrawable(
60+
this@GradientActivity,
61+
R.drawable.gradient_drawable_precipitation
62+
)
63+
64+
fillFormatter = object : IFillFormatter {
65+
override fun getFillLinePosition(
66+
dataSet: ILineDataSet?,
67+
dataProvider: LineDataProvider
68+
): Float = chart.axisLeft.axisMinimum
69+
}
70+
}
71+
72+
chart.axisLeft.axisMinimum = 0f
73+
chart.setData(LineData(dataSet))
74+
chart.invalidate()
75+
}
76+
77+
override fun saveToGallery() = Unit
78+
}

app/src/main/kotlin/info/appdev/chartexample/notimportant/MainActivity.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ import info.appdev.chartexample.CombinedChartActivity
5454
import info.appdev.chartexample.CubicLineChartActivity
5555
import info.appdev.chartexample.DynamicalAddingActivity
5656
import info.appdev.chartexample.FilledLineActivity
57+
import info.appdev.chartexample.GradientActivity
5758
import info.appdev.chartexample.HalfPieChartActivity
5859
import info.appdev.chartexample.HorizontalBarChartActivity
5960
import info.appdev.chartexample.InvertedLineChartActivity
@@ -208,6 +209,9 @@ class MainActivity : ComponentActivity() {
208209

209210
add(ContentItem("Compose Horizontal"))
210211
add(ComposeItem("Horizontal", "Render bar chart horizontally.", HorizontalBarComposeActivity::class.java).toDemoBase())
212+
213+
add(ContentItem("Demonstrate and fix issues"))
214+
add(ContentItem("Gradient", "Show a gradient edge case", GradientActivity::class.java))
211215
}
212216
}
213217
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<shape xmlns:android="http://schemas.android.com/apk/res/android">
3+
<gradient
4+
android:type="linear"
5+
android:angle="-90"
6+
android:endColor="#005183F0"
7+
android:startColor="#5923BAFF" />
8+
</shape>
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
3+
xmlns:app="http://schemas.android.com/apk/res-auto"
4+
xmlns:tools="http://schemas.android.com/tools"
5+
android:id="@+id/main"
6+
android:layout_width="match_parent"
7+
android:layout_height="match_parent"
8+
tools:context=".GradientActivity">
9+
10+
11+
<info.appdev.charting.charts.LineChart
12+
android:id="@+id/chart"
13+
android:layout_width="0dp"
14+
android:layout_height="0dp"
15+
android:layout_margin="16dp"
16+
app:layout_constraintBottom_toBottomOf="parent"
17+
app:layout_constraintEnd_toEndOf="parent"
18+
app:layout_constraintStart_toStartOf="parent"
19+
app:layout_constraintTop_toTopOf="parent" />
20+
21+
22+
</androidx.constraintlayout.widget.ConstraintLayout>
13.8 KB
Loading

0 commit comments

Comments
 (0)