@@ -9,10 +9,8 @@ import android.view.MenuItem
99import android.view.MotionEvent
1010import android.widget.SeekBar
1111import android.widget.SeekBar.OnSeekBarChangeListener
12- import android.widget.TextView
1312import androidx.core.content.ContextCompat
1413import androidx.core.net.toUri
15- import com.github.mikephil.charting.charts.LineChart
1614import com.github.mikephil.charting.components.Legend
1715import com.github.mikephil.charting.data.Entry
1816import com.github.mikephil.charting.data.LineData
@@ -24,60 +22,52 @@ import com.github.mikephil.charting.listener.OnChartGestureListener
2422import com.github.mikephil.charting.listener.OnChartValueSelectedListener
2523import com.github.mikephil.charting.utils.ColorTemplate
2624import info.appdev.chartexample.DataTools.Companion.getValues
25+ import info.appdev.chartexample.databinding.ActivityLinechartBinding
2726import info.appdev.chartexample.notimportant.DemoBase
2827import timber.log.Timber
2928
3029class MultiLineChartActivity : DemoBase (), OnSeekBarChangeListener, OnChartGestureListener, OnChartValueSelectedListener {
31- private var chart: LineChart ? = null
32- private var seekBarX: SeekBar ? = null
33- private var seekBarY: SeekBar ? = null
34- private var tvX: TextView ? = null
35- private var tvY: TextView ? = null
30+
31+ private lateinit var binding: ActivityLinechartBinding
3632
3733 override fun onCreate (savedInstanceState : Bundle ? ) {
3834 super .onCreate(savedInstanceState)
39- setContentView(R .layout.activity_linechart)
40-
41- tvX = findViewById(R .id.tvXMax)
42- tvY = findViewById(R .id.tvYMax)
43-
44- seekBarX = findViewById(R .id.seekBarX)
45- seekBarX!! .setOnSeekBarChangeListener(this )
46-
47- seekBarY = findViewById(R .id.seekBarY)
48- seekBarY!! .setOnSeekBarChangeListener(this )
49-
50- chart = findViewById(R .id.chart1)
51- chart!! .setOnChartValueSelectedListener(this )
52-
53- chart!! .setDrawGridBackground(false )
54- chart!! .description.isEnabled = false
55- chart!! .setDrawBorders(false )
56-
57- chart!! .axisLeft.isEnabled = false
58- chart!! .axisRight.setDrawAxisLine(false )
59- chart!! .axisRight.setDrawGridLines(false )
60- chart!! .xAxis.setDrawAxisLine(false )
61- chart!! .xAxis.setDrawGridLines(false )
35+ binding = ActivityLinechartBinding .inflate(layoutInflater)
36+ val view = binding.root
37+ setContentView(view)
38+
39+ binding.seekBarX.setOnSeekBarChangeListener(this )
40+ binding.seekBarY.setOnSeekBarChangeListener(this )
41+ binding.chart1.setOnChartValueSelectedListener(this )
42+ binding.chart1.setDrawGridBackground(false )
43+ binding.chart1.description.isEnabled = false
44+ binding.chart1.setDrawBorders(false )
45+
46+ binding.chart1.axisLeft.isEnabled = false
47+ binding.chart1.axisRight.setDrawAxisLine(false )
48+ binding.chart1.axisRight.setDrawGridLines(false )
49+ binding.chart1.xAxis.setDrawAxisLine(false )
50+ binding.chart1.xAxis.setDrawGridLines(false )
6251
6352 // enable touch gestures
64- chart !! .setTouchEnabled(true )
53+ binding.chart1 .setTouchEnabled(true )
6554
6655 // enable scaling and dragging
67- chart !! .setDragEnabled(true )
68- chart !! .setScaleEnabled(true )
56+ binding.chart1 .setDragEnabled(true )
57+ binding.chart1 .setScaleEnabled(true )
6958
7059 // if disabled, scaling can be done on x- and y-axis separately
71- chart !! .setPinchZoom(false )
60+ binding.chart1 .setPinchZoom(false )
7261
73- seekBarX!! .progress = 20
74- seekBarY!! .progress = 100
62+ binding. seekBarX.progress = 20
63+ binding. seekBarY.progress = 100
7564
76- val l = chart!! .legend
77- l.verticalAlignment = Legend .LegendVerticalAlignment .TOP
78- l.horizontalAlignment = Legend .LegendHorizontalAlignment .RIGHT
79- l.orientation = Legend .LegendOrientation .VERTICAL
80- l.setDrawInside(false )
65+ binding.chart1.legend.apply {
66+ verticalAlignment = Legend .LegendVerticalAlignment .TOP
67+ horizontalAlignment = Legend .LegendHorizontalAlignment .RIGHT
68+ orientation = Legend .LegendOrientation .VERTICAL
69+ setDrawInside(false )
70+ }
8171 }
8272
8373 private val colors = intArrayOf(
@@ -87,32 +77,32 @@ class MultiLineChartActivity : DemoBase(), OnSeekBarChangeListener, OnChartGestu
8777 )
8878
8979 override fun onProgressChanged (seekBar : SeekBar ? , progress : Int , fromUser : Boolean ) {
90- chart !! .resetTracking()
80+ binding.chart1 .resetTracking()
9181
92- val progress: Int = seekBarX!! .progress
82+ val progress: Int = binding. seekBarX.progress
9383
94- tvX !! . text = seekBarX!! .progress.toString()
95- tvY !! . text = seekBarY!! .progress.toString()
84+ binding.tvXMax. text = binding. seekBarX.progress.toString()
85+ binding.tvYMax. text = binding. seekBarY.progress.toString()
9686
97- val dataSets = ArrayList <ILineDataSet ? >()
87+ val dataSets = ArrayList <ILineDataSet >()
9888
99- for (z in 0 .. 2 ) {
89+ for (datasetNumber in 0 .. 2 ) {
10090 val values = ArrayList <Entry ?>()
10191 val sampleValues = getValues(100 )
10292
10393 for (i in 0 .. < progress) {
104- val ` val ` = ((sampleValues[i]!! .toFloat() * seekBarY!! .progress) + 3 ).toDouble()
105- values.add(Entry (i.toFloat(), ` val ` .toFloat()))
94+ val valuesY = ((sampleValues[i]!! .toFloat() * binding. seekBarY.progress) + 3 ).toDouble()
95+ values.add(Entry (i.toFloat(), valuesY .toFloat()))
10696 }
10797
108- val d = LineDataSet (values, " DataSet " + (z + 1 ))
109- d .setLineWidth(2.5f )
110- d .circleRadius = 4f
98+ val lineDataSet = LineDataSet (values, " DataSet " + (datasetNumber + 1 ))
99+ lineDataSet .setLineWidth(2.5f )
100+ lineDataSet .circleRadius = 4f
111101
112- val color = colors[z % colors.size]
113- d .setColor(color)
114- d .setCircleColor(color)
115- dataSets.add(d )
102+ val color = colors[datasetNumber % colors.size]
103+ lineDataSet .setColor(color)
104+ lineDataSet .setCircleColor(color)
105+ dataSets.add(lineDataSet )
116106 }
117107
118108 // make the first DataSet dashed
@@ -121,8 +111,8 @@ class MultiLineChartActivity : DemoBase(), OnSeekBarChangeListener, OnChartGestu
121111 (dataSets[0 ] as LineDataSet ).setCircleColors(* ColorTemplate .VORDIPLOM_COLORS )
122112
123113 val data = LineData (dataSets)
124- chart !! .setData(data)
125- chart !! .invalidate()
114+ binding.chart1 .setData(data)
115+ binding.chart1 .invalidate()
126116 }
127117
128118 override fun onCreateOptionsMenu (menu : Menu ): Boolean {
@@ -141,44 +131,44 @@ class MultiLineChartActivity : DemoBase(), OnSeekBarChangeListener, OnChartGestu
141131 }
142132
143133 R .id.actionToggleValues -> {
144- chart !! .data!! .dataSets.forEach { set ->
134+ binding.chart1 .data!! .dataSets.forEach { set ->
145135 set.setDrawValues(! set.isDrawValuesEnabled)
146136 }
147137
148- chart !! .invalidate()
138+ binding.chart1 .invalidate()
149139 }
150140
151141 R .id.actionTogglePinch -> {
152- if (chart !! . isPinchZoomEnabled) chart !! .setPinchZoom(false )
153- else chart !! .setPinchZoom(true )
142+ if (binding.chart1. isPinchZoomEnabled) binding.chart1 .setPinchZoom(false )
143+ else binding.chart1 .setPinchZoom(true )
154144
155- chart !! .invalidate()
145+ binding.chart1 .invalidate()
156146 }
157147
158148 R .id.actionToggleAutoScaleMinMax -> {
159- chart !! . isAutoScaleMinMaxEnabled = ! chart !! .isAutoScaleMinMaxEnabled
160- chart !! .notifyDataSetChanged()
149+ binding.chart1. isAutoScaleMinMaxEnabled = ! binding.chart1 .isAutoScaleMinMaxEnabled
150+ binding.chart1 .notifyDataSetChanged()
161151 }
162152
163153 R .id.actionToggleHighlight -> {
164- if (chart !! .data != null ) {
165- chart !! . data!! .isHighlightEnabled = ! chart !! .data!! .isHighlightEnabled()
166- chart !! .invalidate()
154+ if (binding.chart1 .data != null ) {
155+ binding.chart1. data!! .isHighlightEnabled = ! binding.chart1 .data!! .isHighlightEnabled()
156+ binding.chart1 .invalidate()
167157 }
168158 }
169159
170160 R .id.actionToggleFilled -> {
171- chart !! .data!! .dataSets.forEach { set ->
161+ binding.chart1 .data!! .dataSets.forEach { set ->
172162 if (set.isDrawFilledEnabled)
173163 set.setDrawFilled(false )
174164 else
175165 set.setDrawFilled(true )
176166 }
177- chart !! .invalidate()
167+ binding.chart1 .invalidate()
178168 }
179169
180170 R .id.actionToggleCircles -> {
181- val sets = chart !! .data!! .dataSets
171+ val sets = binding.chart1 .data!! .dataSets
182172
183173 for (iSet in sets) {
184174 val set = iSet as LineDataSet
@@ -187,11 +177,11 @@ class MultiLineChartActivity : DemoBase(), OnSeekBarChangeListener, OnChartGestu
187177 else
188178 set.setDrawCircles(true )
189179 }
190- chart !! .invalidate()
180+ binding.chart1 .invalidate()
191181 }
192182
193183 R .id.actionToggleCubic -> {
194- val sets = chart !! .data!! .dataSets
184+ val sets = binding.chart1 .data!! .dataSets
195185
196186 for (iSet in sets) {
197187 val set = iSet as LineDataSet
@@ -200,11 +190,11 @@ class MultiLineChartActivity : DemoBase(), OnSeekBarChangeListener, OnChartGestu
200190 else
201191 LineDataSet .Mode .CUBIC_BEZIER
202192 }
203- chart !! .invalidate()
193+ binding.chart1 .invalidate()
204194 }
205195
206196 R .id.actionToggleStepped -> {
207- val sets = chart !! .data!! .dataSets
197+ val sets = binding.chart1 .data!! .dataSets
208198
209199 for (iSet in sets) {
210200 val set = iSet as LineDataSet
@@ -213,11 +203,11 @@ class MultiLineChartActivity : DemoBase(), OnSeekBarChangeListener, OnChartGestu
213203 else
214204 LineDataSet .Mode .STEPPED
215205 }
216- chart !! .invalidate()
206+ binding.chart1 .invalidate()
217207 }
218208
219209 R .id.actionToggleHorizontalCubic -> {
220- val sets = chart !! .data!! .dataSets
210+ val sets = binding.chart1 .data!! .dataSets
221211
222212 for (iSet in sets) {
223213 val set = iSet as LineDataSet
@@ -226,34 +216,34 @@ class MultiLineChartActivity : DemoBase(), OnSeekBarChangeListener, OnChartGestu
226216 else
227217 LineDataSet .Mode .HORIZONTAL_BEZIER
228218 }
229- chart !! .invalidate()
219+ binding.chart1 .invalidate()
230220 }
231221
232222 R .id.actionSave -> {
233223 if (ContextCompat .checkSelfPermission(this , Manifest .permission.WRITE_EXTERNAL_STORAGE ) == PackageManager .PERMISSION_GRANTED ) {
234224 saveToGallery()
235225 } else {
236- requestStoragePermission(chart )
226+ requestStoragePermission(binding.chart1 )
237227 }
238228 }
239229
240230 R .id.animateX -> {
241- chart !! .animateX(2000 )
231+ binding.chart1 .animateX(2000 )
242232 }
243233
244234 R .id.animateY -> {
245- chart !! .animateY(2000 )
235+ binding.chart1 .animateY(2000 )
246236 }
247237
248238 R .id.animateXY -> {
249- chart !! .animateXY(2000 , 2000 )
239+ binding.chart1 .animateXY(2000 , 2000 )
250240 }
251241 }
252242 return true
253243 }
254244
255245 override fun saveToGallery () {
256- saveToGallery(chart , " MultiLineChartActivity" )
246+ saveToGallery(binding.chart1 , " MultiLineChartActivity" )
257247 }
258248
259249 override fun onChartGestureStart (me : MotionEvent , lastPerformedGesture : ChartGesture ? ) {
@@ -264,7 +254,7 @@ class MultiLineChartActivity : DemoBase(), OnSeekBarChangeListener, OnChartGestu
264254 Timber .i(" END, lastGesture: $lastPerformedGesture " )
265255
266256 // un-highlight values after the gesture is finished and no single-tap
267- if (lastPerformedGesture != ChartGesture .SINGLE_TAP ) chart !! .highlightValues(null ) // or highlightTouch(null) for callback to onNothingSelected(...)
257+ if (lastPerformedGesture != ChartGesture .SINGLE_TAP ) binding.chart1 .highlightValues(null ) // or highlightTouch(null) for callback to onNothingSelected(...)
268258 }
269259
270260 override fun onChartLongPressed (me : MotionEvent ) {
0 commit comments