Skip to content

Commit 4eb1563

Browse files
committed
update: LineChartActivity.kt
1 parent a124d06 commit 4eb1563

9 files changed

Lines changed: 295 additions & 3 deletions

File tree

app/src/main/AndroidManifest.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@
3636
android:name=".ui.chart.PieChartActivity"
3737
android:exported="false" />
3838

39+
<activity
40+
android:name=".ui.chart.LineChartActivity"
41+
android:exported="false" />
42+
3943
</application>
4044

4145
</manifest>

app/src/main/java/com/qomunal/opensource/androidresearch/common/ext/ChartExt.kt

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
package com.qomunal.opensource.androidresearch.common.ext
22

3+
import android.graphics.DashPathEffect
4+
import com.github.mikephil.charting.data.Entry
5+
import com.github.mikephil.charting.data.LineDataSet
6+
import com.github.mikephil.charting.formatter.IFillFormatter
37
import com.github.mikephil.charting.utils.ColorTemplate
48

59
/**
@@ -30,4 +34,41 @@ fun getTemplateColorChart() : ArrayList<Int?> {
3034
colors.add(ColorTemplate.getHoloBlue())
3135

3236
return colors
37+
}
38+
39+
40+
fun initLineDataSet(label: String, color: Int, values: ArrayList<Entry?>): LineDataSet {
41+
// create a dataset and give it a type
42+
return LineDataSet(values, label).apply {
43+
setDrawIcons(false)
44+
45+
// draw dashed line
46+
enableDashedLine(10f, 5f, 0f)
47+
48+
// black lines and points
49+
setColor(color)
50+
setCircleColor(color)
51+
52+
// line thickness and point size
53+
setLineWidth(3f)
54+
circleRadius = 4f
55+
56+
// draw points as solid circles
57+
setDrawCircleHole(false)
58+
setDrawValues(false)
59+
setDrawFilled(false)
60+
61+
// customize legend entry
62+
formLineWidth = 1f
63+
formLineDashEffect = DashPathEffect(floatArrayOf(10f, 5f), 0f)
64+
formSize = 15f
65+
66+
// text size of values
67+
valueTextSize = 9f
68+
mode = LineDataSet.Mode.CUBIC_BEZIER
69+
70+
// draw selection line as dashed
71+
enableDashedHighlightLine(10f, 5f, 0f)
72+
}
73+
3374
}
Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
package com.qomunal.opensource.androidresearch.ui.chart
2+
3+
import android.graphics.Color
4+
import android.os.Bundle
5+
import com.github.mikephil.charting.components.XAxis
6+
import com.github.mikephil.charting.data.Entry
7+
import com.github.mikephil.charting.data.LineData
8+
import com.github.mikephil.charting.formatter.IndexAxisValueFormatter
9+
import com.github.mikephil.charting.highlight.Highlight
10+
import com.github.mikephil.charting.interfaces.datasets.ILineDataSet
11+
import com.github.mikephil.charting.listener.OnChartValueSelectedListener
12+
import com.qomunal.opensource.androidresearch.R
13+
import com.qomunal.opensource.androidresearch.common.base.BaseActivity
14+
import com.qomunal.opensource.androidresearch.common.ext.initLineDataSet
15+
import com.qomunal.opensource.androidresearch.databinding.ActivityLineChartBinding
16+
import timber.log.Timber
17+
18+
/**
19+
* Created by faisalamircs on 30/09/2025
20+
* -----------------------------------------
21+
* Name : Muhammad Faisal Amir
22+
* E-mail : faisalamircs@gmail.com
23+
* Github : github.com/amirisback
24+
* -----------------------------------------
25+
*/
26+
27+
28+
class LineChartActivity : BaseActivity<ActivityLineChartBinding>() {
29+
30+
override fun setupViewBinding(): ActivityLineChartBinding {
31+
return ActivityLineChartBinding.inflate(layoutInflater)
32+
}
33+
34+
override fun initObserver() {
35+
}
36+
37+
override fun initUI() {
38+
title = "LineChartActivity1Custom"
39+
setupLineChart()
40+
setupLineChartData(10, 180f)
41+
}
42+
43+
44+
override fun onCreate(savedInstanceState: Bundle?) {
45+
super.onCreate(savedInstanceState)
46+
}
47+
48+
private fun setupLineChart() {
49+
binding.chart.apply {
50+
51+
setBackgroundColor(Color.WHITE)
52+
setTouchEnabled(true)
53+
setDrawGridBackground(false)
54+
description.isEnabled = false
55+
axisRight.isEnabled = false
56+
57+
// set listeners
58+
setOnChartValueSelectedListener(object : OnChartValueSelectedListener {
59+
override fun onValueSelected(e: Entry, h: Highlight?) {
60+
Timber.tag("Entry selected").i(e.toString())
61+
Timber.tag("LOW HIGH")
62+
.i("low: ${getLowestVisibleX()}, high: ${getHighestVisibleX()}")
63+
Timber.tag("MIN MAX")
64+
.i("xMin: $xChartMin, xMax: $xChartMax, yMin: $yChartMin, yMax: $yChartMax")
65+
}
66+
67+
override fun onNothingSelected() {
68+
Timber.tag("Nothing selected").i("Nothing selected.")
69+
}
70+
})
71+
72+
73+
// create marker to display box when values are selected
74+
val mv = MyMarkerView(this@LineChartActivity, R.layout.custom_marker_view)
75+
76+
// Set the marker to the chart
77+
mv.chartView = this
78+
marker = mv
79+
80+
setDragEnabled(true)
81+
setScaleEnabled(true)
82+
setPinchZoom(true)
83+
84+
val xAxis = xAxis
85+
xAxis.enableGridDashedLine(10f, 10f, 0f)
86+
xAxis.position = XAxis.XAxisPosition.BOTTOM
87+
xAxis.granularity = 1f
88+
xAxis.valueFormatter = IndexAxisValueFormatter(
89+
listOf(
90+
"Jan",
91+
"Feb",
92+
"Mar",
93+
"Apr",
94+
"May",
95+
"Jun",
96+
"Jul",
97+
"Aug",
98+
"Sep",
99+
"Oct",
100+
"Nov",
101+
"Dec"
102+
)
103+
)
104+
105+
val yAxis = axisLeft
106+
// disable dual axis (only use LEFT axis)
107+
axisRight.isEnabled = false
108+
109+
// horizontal grid lines
110+
yAxis.enableGridDashedLine(10f, 10f, 0f)
111+
112+
// axis range
113+
yAxis.setAxisMinimum(0f)
114+
115+
// draw limit lines behind data instead of on top
116+
yAxis.setDrawLimitLinesBehindData(true)
117+
xAxis.setDrawLimitLinesBehindData(true)
118+
119+
legend.isEnabled = false
120+
121+
// draw points over time
122+
animateX(1500)
123+
}
124+
}
125+
126+
private fun setupLineChartData(count: Int, range: Float) {
127+
val values1 = ArrayList<Entry?>()
128+
val values2 = ArrayList<Entry?>()
129+
val values3 = ArrayList<Entry?>()
130+
131+
for (i in 0..<count) {
132+
val `val` = (Math.random() * range).toFloat() - 30
133+
values1.add(Entry(i.toFloat(), `val`))
134+
}
135+
136+
for (i in 0..<count) {
137+
val `val` = (Math.random() * range).toFloat() - 30
138+
values2.add(Entry(i.toFloat(), `val`))
139+
}
140+
141+
for (i in 0..<count) {
142+
val `val` = (Math.random() * range).toFloat() - 30
143+
values3.add(Entry(i.toFloat(), `val`))
144+
}
145+
146+
val set1 = initLineDataSet("DataSet 1", Color.GREEN, values1)
147+
val set2 = initLineDataSet("DataSet 2", Color.YELLOW, values2)
148+
val set3 = initLineDataSet("DataSet 3", Color.BLACK, values3)
149+
150+
val dataSets = ArrayList<ILineDataSet?>()
151+
dataSets.add(set1) // add the data sets
152+
dataSets.add(set2) // add the data sets
153+
dataSets.add(set3) // add the data sets
154+
155+
// create a data object with the data sets
156+
val data = LineData(dataSets)
157+
158+
// set data
159+
binding.chart.setData(data)
160+
}
161+
162+
163+
164+
165+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.qomunal.opensource.androidresearch.ui.chart
2+
3+
import android.annotation.SuppressLint
4+
import android.content.Context
5+
import android.widget.TextView
6+
import com.github.mikephil.charting.components.MarkerView
7+
import com.github.mikephil.charting.data.CandleEntry
8+
import com.github.mikephil.charting.data.Entry
9+
import com.github.mikephil.charting.highlight.Highlight
10+
import com.github.mikephil.charting.utils.MPPointF
11+
import com.github.mikephil.charting.utils.Utils
12+
import com.qomunal.opensource.androidresearch.R
13+
14+
/**
15+
* Custom implementation of the MarkerView.
16+
*
17+
* @author Philipp Jahoda
18+
*/
19+
20+
@SuppressLint("ViewConstructor")
21+
class MyMarkerView(context: Context?, layoutResource: Int) : MarkerView(context, layoutResource) {
22+
23+
private val tvContent: TextView = findViewById(R.id.tvContent)
24+
25+
// runs every time the MarkerView is redrawn, can be used to update the
26+
// content (user-interface)
27+
override fun refreshContent(e: Entry, highlight: Highlight?) {
28+
if (e is CandleEntry) {
29+
val ce = e
30+
31+
tvContent.text = Utils.formatNumber(ce.high, 0, true)
32+
} else {
33+
tvContent.text = Utils.formatNumber(e.y, 0, true)
34+
}
35+
36+
super.refreshContent(e, highlight)
37+
}
38+
39+
override fun getOffset(): MPPointF {
40+
return MPPointF(-(width / 2).toFloat(), -height.toFloat())
41+
}
42+
}

app/src/main/java/com/qomunal/opensource/androidresearch/ui/main/MainActivity.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import androidx.activity.viewModels
66
import com.qomunal.opensource.androidresearch.common.base.BaseActivity
77
import com.qomunal.opensource.androidresearch.common.ext.showToast
88
import com.qomunal.opensource.androidresearch.databinding.ActivityMainBinding
9+
import com.qomunal.opensource.androidresearch.ui.chart.LineChartActivity
910
import com.qomunal.opensource.androidresearch.ui.chart.PieChartActivity
1011

1112
class MainActivity : BaseActivity<ActivityMainBinding>() {
@@ -30,7 +31,7 @@ class MainActivity : BaseActivity<ActivityMainBinding>() {
3031
}
3132

3233
btnLineChart.setOnClickListener {
33-
34+
startActivity(Intent(this@MainActivity, LineChartActivity::class.java))
3435
}
3536
}
3637
}
3.31 KB
Loading
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
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+
android:layout_width="match_parent"
5+
android:layout_height="match_parent">
6+
7+
<com.github.mikephil.charting.charts.LineChart
8+
android:id="@+id/chart"
9+
android:layout_width="0dp"
10+
android:layout_height="0dp"
11+
android:layout_margin="@dimen/dimen_16dp"
12+
app:layout_constraintBottom_toBottomOf="parent"
13+
app:layout_constraintEnd_toEndOf="parent"
14+
app:layout_constraintStart_toStartOf="parent"
15+
app:layout_constraintTop_toTopOf="parent" />
16+
17+
</androidx.constraintlayout.widget.ConstraintLayout>

app/src/main/res/layout/activity_main.xml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
android:layout_width="wrap_content"
1010
android:layout_height="wrap_content"
1111
android:text="Pie Chart"
12-
app:layout_constraintBottom_toBottomOf="parent"
1312
app:layout_constraintEnd_toEndOf="parent"
1413
app:layout_constraintStart_toStartOf="parent"
1514
app:layout_constraintTop_toTopOf="parent" />
@@ -19,7 +18,6 @@
1918
android:layout_width="wrap_content"
2019
android:layout_height="wrap_content"
2120
android:text="Line Chart"
22-
app:layout_constraintBottom_toBottomOf="parent"
2321
app:layout_constraintEnd_toEndOf="parent"
2422
app:layout_constraintStart_toStartOf="parent"
2523
app:layout_constraintTop_toBottomOf="@id/btn_pie_chart" />
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
3+
xmlns:tools="http://schemas.android.com/tools"
4+
android:layout_width="wrap_content"
5+
android:layout_height="40dp"
6+
android:background="@drawable/marker2"
7+
tools:ignore="Overdraw">
8+
9+
<TextView
10+
android:id="@+id/tvContent"
11+
android:layout_width="wrap_content"
12+
android:layout_height="wrap_content"
13+
android:layout_centerHorizontal="true"
14+
android:layout_marginTop="7dp"
15+
android:layout_marginLeft="5dp"
16+
android:layout_marginRight="5dp"
17+
android:text=""
18+
android:textSize="12sp"
19+
android:textColor="@android:color/white"
20+
android:ellipsize="end"
21+
android:singleLine="true"
22+
android:textAppearance="?android:attr/textAppearanceSmall" />
23+
24+
</RelativeLayout>

0 commit comments

Comments
 (0)