diff --git a/app/src/main/kotlin/info/appdev/chartexample/fragments/BarChartFrag.kt b/app/src/main/kotlin/info/appdev/chartexample/fragments/BarChartFrag.kt index a1feade99..3318a7def 100644 --- a/app/src/main/kotlin/info/appdev/chartexample/fragments/BarChartFrag.kt +++ b/app/src/main/kotlin/info/appdev/chartexample/fragments/BarChartFrag.kt @@ -6,55 +6,68 @@ import android.view.LayoutInflater import android.view.MotionEvent import android.view.View import android.view.ViewGroup -import android.widget.FrameLayout import androidx.fragment.app.Fragment +import info.appdev.chartexample.R +import info.appdev.chartexample.custom.MyMarkerView +import info.appdev.chartexample.databinding.FragSimpleBarBinding import info.appdev.charting.charts.BarChart import info.appdev.charting.listener.ChartTouchListener.ChartGesture import info.appdev.charting.listener.OnChartGestureListener -import info.appdev.chartexample.R -import info.appdev.chartexample.custom.MyMarkerView import timber.log.Timber class BarChartFrag : SimpleFragment(), OnChartGestureListener { - private var chart: BarChart? = null + private lateinit var chart: BarChart - override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View { - val v = inflater.inflate(R.layout.frag_simple_bar, container, false) + private var _binding: FragSimpleBarBinding? = null + // This property is only valid between onCreateView and onDestroyView. + private val binding get() = _binding!! + + override fun onCreateView( + inflater: LayoutInflater, + container: ViewGroup?, + savedInstanceState: Bundle? + ): View { + _binding = FragSimpleBarBinding.inflate(inflater, container, false) + val view = binding.root // create a new chart object chart = BarChart(requireActivity()) - chart!!.description.isEnabled = false - chart!!.onChartGestureListener = this + chart.description.isEnabled = false + chart.onChartGestureListener = this val mv = MyMarkerView(activity, R.layout.custom_marker_view) mv.chartView = chart // For bounds control - chart!!.setMarker(mv) + chart.setMarker(mv) - chart!!.setDrawGridBackground(false) - chart!!.setDrawBarShadow(false) + chart.setDrawGridBackground(false) + chart.setDrawBarShadow(false) val tf = Typeface.createFromAsset(requireContext().assets, "OpenSans-Light.ttf") - chart!!.setData(generateBarData(1, 20000f)) + chart.setData(generateBarData(1, 20000f)) - chart!!.legend.apply { + chart.legend.apply { typeface = tf } - val leftAxis = chart!!.axisLeft + val leftAxis = chart.axisLeft leftAxis.typeface = tf leftAxis.axisMinimum = 0f // this replaces setStartAtZero(true) - chart!!.axisRight.isEnabled = false + chart.axisRight.isEnabled = false - val xAxis = chart!!.xAxis + val xAxis = chart.xAxis xAxis.isEnabled = false // programmatically add the chart - val parent = v.findViewById(R.id.parentLayout) - parent.addView(chart) + binding.parentLayout.addView(chart) + + return view + } - return v + override fun onDestroyView() { + super.onDestroyView() + _binding = null } override fun onChartGestureStart(me: MotionEvent, lastPerformedGesture: ChartGesture?) { @@ -63,7 +76,7 @@ class BarChartFrag : SimpleFragment(), OnChartGestureListener { override fun onChartGestureEnd(me: MotionEvent, lastPerformedGesture: ChartGesture?) { Timber.i("END") - chart!!.highlightValues(null) + chart.highlightValues(null) } override fun onChartLongPressed(me: MotionEvent) { diff --git a/app/src/main/kotlin/info/appdev/chartexample/fragments/ComplexityFragment.kt b/app/src/main/kotlin/info/appdev/chartexample/fragments/ComplexityFragment.kt index 8b01d8880..33c86fd91 100644 --- a/app/src/main/kotlin/info/appdev/chartexample/fragments/ComplexityFragment.kt +++ b/app/src/main/kotlin/info/appdev/chartexample/fragments/ComplexityFragment.kt @@ -6,39 +6,50 @@ import android.view.LayoutInflater import android.view.View import android.view.ViewGroup import androidx.fragment.app.Fragment -import info.appdev.charting.charts.LineChart -import info.appdev.chartexample.R +import info.appdev.chartexample.databinding.FragSimpleLineBinding class ComplexityFragment : SimpleFragment() { - private var chart: LineChart? = null - override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View { - val v = inflater.inflate(R.layout.frag_simple_line, container, false) + private var _binding: FragSimpleLineBinding? = null - chart = v.findViewById(R.id.lineChart1) + // This property is only valid between onCreateView and onDestroyView. + private val binding get() = _binding!! - chart!!.description.isEnabled = false + override fun onCreateView( + inflater: LayoutInflater, + container: ViewGroup?, + savedInstanceState: Bundle? + ): View { + _binding = FragSimpleLineBinding.inflate(inflater, container, false) + val view = binding.root - chart!!.setDrawGridBackground(false) + binding.lineChart1.description.isEnabled = false - chart!!.setData(complexity) - chart!!.animateX(3000) + binding.lineChart1.setDrawGridBackground(false) + + binding.lineChart1.setData(complexity) + binding.lineChart1.animateX(3000) val tf = Typeface.createFromAsset(requireContext().assets, "OpenSans-Light.ttf") - chart!!.legend.apply { + binding.lineChart1.legend.apply { typeface = tf } - val leftAxis = chart!!.axisLeft + val leftAxis = binding.lineChart1.axisLeft leftAxis.typeface = tf - chart!!.axisRight.isEnabled = false + binding.lineChart1.axisRight.isEnabled = false - val xAxis = chart!!.xAxis + val xAxis = binding.lineChart1.xAxis xAxis.isEnabled = false - return v + return view + } + + override fun onDestroyView() { + super.onDestroyView() + _binding = null } companion object { diff --git a/app/src/main/kotlin/info/appdev/chartexample/fragments/PieChartFrag.kt b/app/src/main/kotlin/info/appdev/chartexample/fragments/PieChartFrag.kt index 4bef9518c..c6ec28c92 100644 --- a/app/src/main/kotlin/info/appdev/chartexample/fragments/PieChartFrag.kt +++ b/app/src/main/kotlin/info/appdev/chartexample/fragments/PieChartFrag.kt @@ -10,40 +10,52 @@ import android.view.LayoutInflater import android.view.View import android.view.ViewGroup import androidx.fragment.app.Fragment -import info.appdev.charting.charts.PieChart +import info.appdev.chartexample.databinding.FragSimplePieBinding import info.appdev.charting.components.Legend -import info.appdev.chartexample.R class PieChartFrag : SimpleFragment() { - private var chart: PieChart? = null - override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View { - val v = inflater.inflate(R.layout.frag_simple_pie, container, false) + private var _binding: FragSimplePieBinding? = null - chart = v.findViewById(R.id.pieChart1) - chart!!.description.isEnabled = false + // This property is only valid between onCreateView and onDestroyView. + private val binding get() = _binding!! + + override fun onCreateView( + inflater: LayoutInflater, + container: ViewGroup?, + savedInstanceState: Bundle? + ): View { + _binding = FragSimplePieBinding.inflate(inflater, container, false) + val view = binding.root + + binding.pieChart1.description.isEnabled = false val tf = Typeface.createFromAsset(requireContext().assets, "OpenSans-Light.ttf") - chart!!.setCenterTextTypeface(tf) - chart!!.centerText = generateCenterText() - chart!!.setCenterTextSize(10f) - chart!!.setCenterTextTypeface(tf) + binding.pieChart1.setCenterTextTypeface(tf) + binding.pieChart1.centerText = generateCenterText() + binding.pieChart1.setCenterTextSize(10f) + binding.pieChart1.setCenterTextTypeface(tf) // radius of the center hole in percent of maximum radius - chart!!.holeRadius = 45f - chart!!.transparentCircleRadius = 50f + binding.pieChart1.holeRadius = 45f + binding.pieChart1.transparentCircleRadius = 50f - chart!!.legend.apply { + binding.pieChart1.legend.apply { verticalAlignment = Legend.LegendVerticalAlignment.TOP horizontalAlignment = Legend.LegendHorizontalAlignment.RIGHT orientation = Legend.LegendOrientation.VERTICAL setDrawInside(false) } - chart!!.setData(generatePieData()) + binding.pieChart1.setData(generatePieData()) + + return view + } - return v + override fun onDestroyView() { + super.onDestroyView() + _binding = null } private fun generateCenterText(): SpannableString { diff --git a/app/src/main/kotlin/info/appdev/chartexample/fragments/ScatterChartFrag.kt b/app/src/main/kotlin/info/appdev/chartexample/fragments/ScatterChartFrag.kt index 00998f303..d57ab5095 100644 --- a/app/src/main/kotlin/info/appdev/chartexample/fragments/ScatterChartFrag.kt +++ b/app/src/main/kotlin/info/appdev/chartexample/fragments/ScatterChartFrag.kt @@ -6,41 +6,48 @@ import android.view.LayoutInflater import android.view.View import android.view.ViewGroup import androidx.fragment.app.Fragment -import info.appdev.charting.charts.ScatterChart import info.appdev.charting.components.XAxis.XAxisPosition import info.appdev.chartexample.R import info.appdev.chartexample.custom.MyMarkerView +import info.appdev.chartexample.databinding.FragSimpleScatterBinding class ScatterChartFrag : SimpleFragment() { - private var chart: ScatterChart? = null + private var _binding: FragSimpleScatterBinding? = null - override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View { - val v = inflater.inflate(R.layout.frag_simple_scatter, container, false) + // This property is only valid between onCreateView and onDestroyView. + private val binding get() = _binding!! - chart = v.findViewById(R.id.scatterChart1) - chart!!.description.isEnabled = false + override fun onCreateView( + inflater: LayoutInflater, + container: ViewGroup?, + savedInstanceState: Bundle? + ): View { + _binding = FragSimpleScatterBinding.inflate(inflater, container, false) + val view = binding.root + + binding.scatterChart1.description.isEnabled = false val tf = Typeface.createFromAsset(requireContext().assets, "OpenSans-Light.ttf") - val mv = MyMarkerView(activity, R.layout.custom_marker_view) - mv.chartView = chart // For bounds control - chart!!.setMarker(mv) + val markerView = MyMarkerView(activity, R.layout.custom_marker_view) + markerView.chartView = binding.scatterChart1 // For bounds control + binding.scatterChart1.setMarker(markerView) - chart!!.setDrawGridBackground(false) - chart!!.setData(generateScatterData(6, 10000f)) + binding.scatterChart1.setDrawGridBackground(false) + binding.scatterChart1.setData(generateScatterData(6, 10000f)) - val xAxis = chart!!.xAxis + val xAxis = binding.scatterChart1.xAxis xAxis.isEnabled = true xAxis.position = XAxisPosition.BOTTOM - val leftAxis = chart!!.axisLeft + val leftAxis = binding.scatterChart1.axisLeft leftAxis.typeface = tf - val rightAxis = chart!!.axisRight + val rightAxis = binding.scatterChart1.axisRight rightAxis.typeface = tf rightAxis.setDrawGridLines(false) - chart!!.legend.apply { + binding.scatterChart1.legend.apply { isWordWrapEnabled = true typeface = tf formSize = 14f @@ -49,9 +56,14 @@ class ScatterChartFrag : SimpleFragment() { yOffset = 13f } - chart!!.extraBottomOffset = 16f + binding.scatterChart1.extraBottomOffset = 16f + + return view + } - return v + override fun onDestroyView() { + super.onDestroyView() + _binding = null } companion object { diff --git a/app/src/main/kotlin/info/appdev/chartexample/fragments/SineCosineFragment.kt b/app/src/main/kotlin/info/appdev/chartexample/fragments/SineCosineFragment.kt index fd127fa6e..1a9d9a787 100644 --- a/app/src/main/kotlin/info/appdev/chartexample/fragments/SineCosineFragment.kt +++ b/app/src/main/kotlin/info/appdev/chartexample/fragments/SineCosineFragment.kt @@ -6,41 +6,52 @@ import android.view.LayoutInflater import android.view.View import android.view.ViewGroup import androidx.fragment.app.Fragment -import info.appdev.charting.charts.LineChart -import info.appdev.chartexample.R +import info.appdev.chartexample.databinding.FragSimpleLineBinding class SineCosineFragment : SimpleFragment() { - private var chart: LineChart? = null - override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View { - val v = inflater.inflate(R.layout.frag_simple_line, container, false) + private var _binding: FragSimpleLineBinding? = null - chart = v.findViewById(R.id.lineChart1) + // This property is only valid between onCreateView and onDestroyView. + private val binding get() = _binding!! - chart!!.description.isEnabled = false + override fun onCreateView( + inflater: LayoutInflater, + container: ViewGroup?, + savedInstanceState: Bundle? + ): View { + _binding = FragSimpleLineBinding.inflate(inflater, container, false) + val view = binding.root - chart!!.setDrawGridBackground(false) + binding.lineChart1.description.isEnabled = false - chart!!.setData(generateLineData()) - chart!!.animateX(3000) + binding.lineChart1.setDrawGridBackground(false) + + binding.lineChart1.setData(generateLineData()) + binding.lineChart1.animateX(3000) val tf = Typeface.createFromAsset(requireContext().assets, "OpenSans-Light.ttf") - chart!!.legend.apply { + binding.lineChart1.legend.apply { typeface = tf } - val leftAxis = chart!!.axisLeft + val leftAxis = binding.lineChart1.axisLeft leftAxis.typeface = tf leftAxis.axisMaximum = 1.2f leftAxis.axisMinimum = -1.2f - chart!!.axisRight.isEnabled = false + binding.lineChart1.axisRight.isEnabled = false - val xAxis = chart!!.xAxis + val xAxis = binding.lineChart1.xAxis xAxis.isEnabled = false - return v + return view + } + + override fun onDestroyView() { + super.onDestroyView() + _binding = null } companion object {