@@ -5,7 +5,7 @@ import android.graphics.Color
55import android.os.Bundle
66import android.view.Menu
77import android.view.MenuItem
8- import com.github.mikephil.charting.charts.LineChart
8+ import androidx.core.net.toUri
99import com.github.mikephil.charting.components.YAxis
1010import com.github.mikephil.charting.data.Entry
1111import com.github.mikephil.charting.data.LineData
@@ -14,8 +14,8 @@ import com.github.mikephil.charting.formatter.IFillFormatter
1414import com.github.mikephil.charting.interfaces.dataprovider.LineDataProvider
1515import com.github.mikephil.charting.interfaces.datasets.ILineDataSet
1616import info.appdev.chartexample.DataTools.Companion.getValues
17+ import info.appdev.chartexample.databinding.ActivityLinechartNoseekbarBinding
1718import 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,79 +25,80 @@ import androidx.core.net.toUri
2525 * the area OUTSIDE the lines!
2626 */
2727class 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
71- val values1 = ArrayList <Entry ?>()
72+ val valuesArray1 = ArrayList <Entry ?>()
7273 val sampleValues = getValues(count + 2 )
7374
7475 for (i in 0 .. < count) {
75- val ` val ` = (sampleValues[i]!! .toFloat() * range) + 50
76- values1 .add(Entry (i.toFloat(), ` val ` ))
76+ val valueY = (sampleValues[i]!! .toFloat() * range) + 50
77+ valuesArray1 .add(Entry (i.toFloat(), valueY ))
7778 }
7879
79- val values2 = ArrayList <Entry ?>()
80+ val valuesArray2 = ArrayList <Entry ?>()
8081
8182 for (i in 0 .. < count) {
82- val ` val ` = (sampleValues[i + 1 ]!! .toFloat() * range) + 450
83- values2 .add(Entry (i.toFloat(), ` val ` ))
83+ val valueY = (sampleValues[i + 1 ]!! .toFloat() * range) + 450
84+ valuesArray2 .add(Entry (i.toFloat(), valueY ))
8485 }
8586
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
94- set1.setEntries(values1 )
95- set2.setEntries(values2 )
96- chart !! .data!! .notifyDataChanged()
97- chart !! .notifyDataSetChanged()
93+ set1 = binding.chart1 .data!! .getDataSetByIndex(0 ) as LineDataSet
94+ set2 = binding.chart1 .data!! .getDataSetByIndex(1 ) as LineDataSet
95+ set1.setEntries(valuesArray1 )
96+ set2.setEntries(valuesArray2 )
97+ binding.chart1 .data!! .notifyDataChanged()
98+ binding.chart1 .notifyDataSetChanged()
9899 } else {
99100 // create a dataset and give it a type
100- set1 = LineDataSet (values1 , " DataSet 1" )
101+ set1 = LineDataSet (valuesArray1 , " DataSet 1" )
101102
102103 set1.axisDependency = YAxis .AxisDependency .LEFT
103104 set1.setColor(Color .rgb(255 , 241 , 46 ))
@@ -113,12 +114,12 @@ 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
120121 // create a dataset and give it a type
121- set2 = LineDataSet (values2 , " DataSet 2" )
122+ set2 = LineDataSet (valuesArray2 , " DataSet 2" )
122123 set2.axisDependency = YAxis .AxisDependency .LEFT
123124 set2.setColor(Color .rgb(255 , 241 , 46 ))
124125 set2.setDrawCircles(false )
@@ -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