Skip to content

Commit e93c27a

Browse files
committed
ViewBinding FilledLine
1 parent fd6949b commit e93c27a

File tree

1 file changed

+29
-30
lines changed

1 file changed

+29
-30
lines changed

app/src/main/kotlin/info/appdev/chartexample/FilledLineActivity.kt

Lines changed: 29 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import android.graphics.Color
55
import android.os.Bundle
66
import android.view.Menu
77
import android.view.MenuItem
8-
import com.github.mikephil.charting.charts.LineChart
8+
import androidx.core.net.toUri
99
import com.github.mikephil.charting.components.YAxis
1010
import com.github.mikephil.charting.data.Entry
1111
import com.github.mikephil.charting.data.LineData
@@ -14,8 +14,8 @@ import com.github.mikephil.charting.formatter.IFillFormatter
1414
import com.github.mikephil.charting.interfaces.dataprovider.LineDataProvider
1515
import com.github.mikephil.charting.interfaces.datasets.ILineDataSet
1616
import info.appdev.chartexample.DataTools.Companion.getValues
17+
import info.appdev.chartexample.databinding.ActivityLinechartNoseekbarBinding
1718
import info.appdev.chartexample.notimportant.DemoBase
18-
import androidx.core.net.toUri
1919

2020
/**
2121
* This works by inverting the background and desired "fill" color. First, we draw the fill color
@@ -25,48 +25,49 @@ import androidx.core.net.toUri
2525
* the area OUTSIDE the lines!
2626
*/
2727
class FilledLineActivity : DemoBase() {
28-
private var chart: LineChart? = null
2928
private val fillColor = Color.argb(150, 51, 181, 229)
3029

30+
private lateinit var binding: ActivityLinechartNoseekbarBinding
31+
3132
override fun onCreate(savedInstanceState: Bundle?) {
3233
super.onCreate(savedInstanceState)
33-
setContentView(R.layout.activity_linechart_noseekbar)
34+
binding = ActivityLinechartNoseekbarBinding.inflate(layoutInflater)
35+
val view = binding.root
36+
setContentView(view)
3437

35-
chart = findViewById(R.id.chart1)
36-
chart!!.setBackgroundColor(Color.WHITE)
37-
chart!!.setGridBackgroundColor(fillColor)
38-
chart!!.setDrawGridBackground(true)
38+
binding.chart1.setBackgroundColor(Color.WHITE)
39+
binding.chart1.setGridBackgroundColor(fillColor)
40+
binding.chart1.setDrawGridBackground(true)
3941

40-
chart!!.setDrawBorders(true)
42+
binding.chart1.setDrawBorders(true)
4143

4244
// no description text
43-
chart!!.description.isEnabled = false
45+
binding.chart1.description.isEnabled = false
4446

4547
// if disabled, scaling can be done on x- and y-axis separately
46-
chart!!.setPinchZoom(false)
48+
binding.chart1.setPinchZoom(false)
4749

48-
val l = chart!!.legend
50+
val l = binding.chart1.legend
4951
l.isEnabled = false
5052

51-
val xAxis = chart!!.xAxis
53+
val xAxis = binding.chart1.xAxis
5254
xAxis.isEnabled = false
5355

54-
val leftAxis = chart!!.axisLeft
56+
val leftAxis = binding.chart1.axisLeft
5557
leftAxis.setAxisMaximum(900f)
5658
leftAxis.setAxisMinimum(-250f)
5759
leftAxis.setDrawAxisLine(false)
5860
leftAxis.setDrawZeroLine(false)
5961
leftAxis.setDrawGridLines(false)
6062

61-
chart!!.axisRight.isEnabled = false
63+
binding.chart1.axisRight.isEnabled = false
6264

63-
// add data
6465
setData(60f)
6566

66-
chart!!.invalidate()
67+
binding.chart1.invalidate()
6768
}
6869

69-
private fun setData(range: Float) {
70+
private fun setData(@Suppress("SameParameterValue") range: Float) {
7071
val count = 100
7172
val values1 = ArrayList<Entry?>()
7273
val sampleValues = getValues(count + 2)
@@ -86,15 +87,15 @@ class FilledLineActivity : DemoBase() {
8687
val set1: LineDataSet
8788
val set2: LineDataSet
8889

89-
if (chart!!.data != null &&
90-
chart!!.data!!.getDataSetCount() > 0
90+
if (binding.chart1.data != null &&
91+
binding.chart1.data!!.getDataSetCount() > 0
9192
) {
92-
set1 = chart!!.data!!.getDataSetByIndex(0) as LineDataSet
93-
set2 = chart!!.data!!.getDataSetByIndex(1) as LineDataSet
93+
set1 = binding.chart1.data!!.getDataSetByIndex(0) as LineDataSet
94+
set2 = binding.chart1.data!!.getDataSetByIndex(1) as LineDataSet
9495
set1.setEntries(values1)
9596
set2.setEntries(values2)
96-
chart!!.data!!.notifyDataChanged()
97-
chart!!.notifyDataSetChanged()
97+
binding.chart1.data!!.notifyDataChanged()
98+
binding.chart1.notifyDataSetChanged()
9899
} else {
99100
// create a dataset and give it a type
100101
set1 = LineDataSet(values1, "DataSet 1")
@@ -113,7 +114,7 @@ class FilledLineActivity : DemoBase() {
113114
override fun getFillLinePosition(dataSet: ILineDataSet?, dataProvider: LineDataProvider?): Float {
114115
// change the return value here to better understand the effect
115116
// return 0;
116-
return chart!!.axisLeft.axisMinimum
117+
return binding.chart1.axisLeft.axisMinimum
117118
}
118119
}
119120

@@ -133,7 +134,7 @@ class FilledLineActivity : DemoBase() {
133134
override fun getFillLinePosition(dataSet: ILineDataSet?, dataProvider: LineDataProvider?): Float {
134135
// change the return value here to better understand the effect
135136
// return 600;
136-
return chart!!.axisLeft.axisMaximum
137+
return binding.chart1.axisLeft.axisMaximum
137138
}
138139
}
139140

@@ -145,8 +146,7 @@ class FilledLineActivity : DemoBase() {
145146
val data = LineData(dataSets)
146147
data.setDrawValues(false)
147148

148-
// set data
149-
chart!!.setData(data)
149+
binding.chart1.setData(data)
150150
}
151151
}
152152

@@ -168,6 +168,5 @@ class FilledLineActivity : DemoBase() {
168168
return true
169169
}
170170

171-
public override fun saveToGallery() { /* Intentionally left empty */
172-
}
171+
public override fun saveToGallery() = Unit // Intentionally left empty
173172
}

0 commit comments

Comments
 (0)