diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/charts/PieRadarChartBase.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/charts/PieRadarChartBase.kt index ed4a3a95f..2425565f5 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/charts/PieRadarChartBase.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/charts/PieRadarChartBase.kt @@ -89,7 +89,7 @@ abstract class PieRadarChartBase>> } override fun getMaxVisibleCount(): Int { - return mData!!.getEntryCount() + return mData!!.entryCount } override fun onTouchEvent(event: MotionEvent?): Boolean { diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/listener/BarLineChartTouchListener.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/listener/BarLineChartTouchListener.kt index 3d530c805..a1e688577 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/listener/BarLineChartTouchListener.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/listener/BarLineChartTouchListener.kt @@ -29,7 +29,7 @@ class BarLineChartTouchListener( touchMatrix: Matrix, dragTriggerDistance: Float ) : - ChartTouchListener>?>?>(chart) { + ChartTouchListener>>>(chart) { /** * the original touch-matrix from the chart */ @@ -110,11 +110,11 @@ class BarLineChartTouchListener( } } - if (touchMode == NONE || chart!!.isFlingEnabled) { + if (touchMode == NONE || chart.isFlingEnabled) { gestureDetector?.onTouchEvent(event) } - if (!chart!!.isDragEnabled && (!chart!!.isScaleXEnabled && !chart!!.isScaleYEnabled)) return true + if (!chart.isDragEnabled && (!chart.isScaleXEnabled && !chart.isScaleYEnabled)) return true // Handle touch events here... when (event.action and MotionEvent.ACTION_MASK) { @@ -127,7 +127,7 @@ class BarLineChartTouchListener( } MotionEvent.ACTION_POINTER_DOWN -> if (event.pointerCount >= 2) { - chart!!.disableScroll() + chart.disableScroll() saveTouchStart(event) @@ -141,11 +141,11 @@ class BarLineChartTouchListener( savedDist = spacing(event) if (savedDist > 10f) { - touchMode = if (chart!!.isPinchZoomEnabled) { + touchMode = if (chart.isPinchZoomEnabled) { PINCH_ZOOM } else { - if (chart!!.isScaleXEnabled != chart!!.isScaleYEnabled) { - if (chart!!.isScaleXEnabled) X_ZOOM else Y_ZOOM + if (chart.isScaleXEnabled != chart.isScaleYEnabled) { + if (chart.isScaleXEnabled) X_ZOOM else Y_ZOOM } else { if (savedXDist > savedYDist) X_ZOOM else Y_ZOOM } @@ -157,16 +157,16 @@ class BarLineChartTouchListener( } MotionEvent.ACTION_MOVE -> if (touchMode == DRAG) { - chart!!.disableScroll() + chart.disableScroll() - val x = if (chart!!.isDragXEnabled) event.x - touchStartPoint.x else 0f - val y = if (chart!!.isDragYEnabled) event.y - touchStartPoint.y else 0f + val x = if (chart.isDragXEnabled) event.x - touchStartPoint.x else 0f + val y = if (chart.isDragYEnabled) event.y - touchStartPoint.y else 0f performDrag(event, x, y) } else if (touchMode == X_ZOOM || touchMode == Y_ZOOM || touchMode == PINCH_ZOOM) { - chart!!.disableScroll() + chart.disableScroll() - if (chart!!.isScaleXEnabled || chart!!.isScaleYEnabled) performZoom(event) + if (chart.isScaleXEnabled || chart.isScaleYEnabled) performZoom(event) } else if (touchMode == NONE && abs( distance( @@ -175,26 +175,26 @@ class BarLineChartTouchListener( ).toDouble() ) > dragTriggerDist ) { - if (chart!!.isDragEnabled) { - val shouldPan = !chart!!.isFullyZoomedOut || - !chart!!.hasNoDragOffset() + if (chart.isDragEnabled) { + val shouldPan = !chart.isFullyZoomedOut || + !chart.hasNoDragOffset() if (shouldPan) { val distanceX = abs((event.x - touchStartPoint.x).toDouble()).toFloat() val distanceY = abs((event.y - touchStartPoint.y).toDouble()).toFloat() // Disable dragging in a direction that's disallowed - if ((chart!!.isDragXEnabled || distanceY >= distanceX) && - (chart!!.isDragYEnabled || distanceY <= distanceX) + if ((chart.isDragXEnabled || distanceY >= distanceX) && + (chart.isDragYEnabled || distanceY <= distanceX) ) { lastGesture = ChartGesture.DRAG touchMode = DRAG } } else { - if (chart!!.isHighlightPerDragEnabled) { + if (chart.isHighlightPerDragEnabled) { lastGesture = ChartGesture.DRAG - if (chart!!.isHighlightPerDragEnabled) performHighlightDrag(event) + if (chart.isHighlightPerDragEnabled) performHighlightDrag(event) } } } @@ -210,7 +210,7 @@ class BarLineChartTouchListener( if (abs(velocityX.toDouble()) > Utils.minimumFlingVelocity || abs(velocityY.toDouble()) > Utils.minimumFlingVelocity ) { - if (touchMode == DRAG && chart!!.isDragDecelerationEnabled) { + if (touchMode == DRAG && chart.isDragDecelerationEnabled) { stopDeceleration() decelerationLastTime = AnimationUtils.currentAnimationTimeMillis() @@ -222,7 +222,7 @@ class BarLineChartTouchListener( decelerationVelocity.y = velocityY // This causes computeScroll to fire, recommended for this by Google - chart?.postInvalidateOnAnimation() + chart.postInvalidateOnAnimation() } } @@ -231,12 +231,12 @@ class BarLineChartTouchListener( // could have changed in size, affecting Y-axis size. // So we need to recalculate offsets. - chart!!.calculateOffsets() - chart!!.postInvalidate() + chart.calculateOffsets() + chart.postInvalidate() } touchMode = NONE - chart!!.enableScroll() + chart.enableScroll() it.recycle() velocityTracker = null @@ -258,7 +258,7 @@ class BarLineChartTouchListener( } // perform the transformation, update the chart - matrix = chart!!.viewPortHandler.refresh(matrix, chart!!, true) + matrix = chart.viewPortHandler.refresh(matrix, chart, true) return true // indicate event was handled } @@ -274,7 +274,7 @@ class BarLineChartTouchListener( touchStartPoint.x = event.x touchStartPoint.y = event.y - closestDataSetToTouch = chart!!.getDataSetByTouchPoint(event.x, event.y) + closestDataSetToTouch = chart.getDataSetByTouchPoint(event.x, event.y) } /** @@ -289,7 +289,7 @@ class BarLineChartTouchListener( matrix.set(savedMatrix) - val l = chart!!.onChartGestureListener + val l = chart.onChartGestureListener // check if axis is inverted if (inverted()) { @@ -315,7 +315,7 @@ class BarLineChartTouchListener( private fun performZoom(event: MotionEvent) { if (event.pointerCount >= 2) { // two finger zoom - val l = chart!!.onChartGestureListener + val l = chart.onChartGestureListener // get the distance between the pointers of the touch event val totalDist = spacing(event) @@ -324,7 +324,7 @@ class BarLineChartTouchListener( // get the translation val t = getTrans(touchPointCenter.x, touchPointCenter.y) - val h = chart!!.viewPortHandler + val h = chart.viewPortHandler // take actions depending on the activated touch mode if (touchMode == PINCH_ZOOM) { @@ -338,8 +338,8 @@ class BarLineChartTouchListener( val canZoomMoreY = if (isZoomingOut) h.canZoomOutMoreY() else h.canZoomInMoreY() - val scaleX = if (chart!!.isScaleXEnabled) scale else 1f - val scaleY = if (chart!!.isScaleYEnabled) scale else 1f + val scaleX = if (chart.isScaleXEnabled) scale else 1f + val scaleY = if (chart.isScaleYEnabled) scale else 1f if (canZoomMoreY || canZoomMoreX) { matrix.set(savedMatrix) @@ -347,7 +347,7 @@ class BarLineChartTouchListener( l?.onChartScale(event, scaleX, scaleY) } - } else if (touchMode == X_ZOOM && chart!!.isScaleXEnabled) { + } else if (touchMode == X_ZOOM && chart.isScaleXEnabled) { lastGesture = ChartGesture.X_ZOOM val xDist = getXDist(event) @@ -362,7 +362,7 @@ class BarLineChartTouchListener( l?.onChartScale(event, scaleX, 1f) } - } else if (touchMode == Y_ZOOM && chart!!.isScaleYEnabled) { + } else if (touchMode == Y_ZOOM && chart.isScaleYEnabled) { lastGesture = ChartGesture.Y_ZOOM val yDist = getYDist(event) @@ -388,7 +388,7 @@ class BarLineChartTouchListener( * limit scaleX range */ private fun getLimitedScaleX(scaleX: Float, t: MPPointF): Float { - val h = chart!!.viewPortHandler + val h = chart.viewPortHandler tempMatrix.set(savedMatrix) tempMatrix.postScale(scaleX, 1f, t.x, t.y) @@ -412,7 +412,7 @@ class BarLineChartTouchListener( * limit scaleY range */ private fun getLimitedScaleY(scaleY: Float, t: MPPointF): Float { - val h = chart!!.viewPortHandler + val h = chart.viewPortHandler tempMatrix.set(savedMatrix) tempMatrix.postScale(1f, scaleY, t.x, t.y) @@ -438,11 +438,11 @@ class BarLineChartTouchListener( * @param motionEvent */ private fun performHighlightDrag(motionEvent: MotionEvent) { - val highlight = chart!!.getHighlightByTouchPoint(motionEvent.x, motionEvent.y) + val highlight = chart.getHighlightByTouchPoint(motionEvent.x, motionEvent.y) if (highlight != null && !highlight.equalTo(mLastHighlighted)) { mLastHighlighted = highlight - chart!!.highlightValue(highlight, true) + chart.highlightValue(highlight, true) } } @@ -453,7 +453,7 @@ class BarLineChartTouchListener( * points */ fun getTrans(x: Float, y: Float): MPPointF { - val vph = chart!!.viewPortHandler + val vph = chart.viewPortHandler val xTrans = x - vph.offsetLeft() @@ -461,7 +461,7 @@ class BarLineChartTouchListener( val yTrans: Float = if (inverted()) { -(y - vph.offsetTop()) } else { - -(chart!!.measuredHeight - y - vph.offsetBottom()) + -(chart.measuredHeight - y - vph.offsetBottom()) } return MPPointF.getInstance(xTrans, yTrans) @@ -471,8 +471,8 @@ class BarLineChartTouchListener( * Returns true if the current touch situation should be interpreted as inverted, false if not. */ private fun inverted(): Boolean { - return (closestDataSetToTouch == null && chart!!.isAnyAxisInverted) || (closestDataSetToTouch != null - && chart!!.isInverted(closestDataSetToTouch!!.axisDependency)) + return (closestDataSetToTouch == null && chart.isAnyAxisInverted) || (closestDataSetToTouch != null + && chart.isInverted(closestDataSetToTouch!!.axisDependency)) } /** @@ -488,20 +488,20 @@ class BarLineChartTouchListener( override fun onDoubleTap(e: MotionEvent): Boolean { lastGesture = ChartGesture.DOUBLE_TAP - val onChartGestureListener = chart!!.onChartGestureListener + val onChartGestureListener = chart.onChartGestureListener onChartGestureListener?.onChartDoubleTapped(e) // check if double-tap zooming is enabled - if (chart!!.isDoubleTapToZoomEnabled && chart!!.data!!.entryCount > 0) { + if (chart.isDoubleTapToZoomEnabled && chart.data!!.entryCount > 0) { val trans = getTrans(e.x, e.y) - val scaleX = if (chart!!.isScaleXEnabled) 1.4f else 1f - val scaleY = if (chart!!.isScaleYEnabled) 1.4f else 1f + val scaleX = if (chart.isScaleXEnabled) 1.4f else 1f + val scaleY = if (chart.isScaleYEnabled) 1.4f else 1f - chart!!.zoom(scaleX, scaleY, trans.x, trans.y) + chart.zoom(scaleX, scaleY, trans.x, trans.y) - if (chart!!.isLogEnabled) Log.i( + if (chart.isLogEnabled) Log.i( "BarlineChartTouch", ("Double-Tap, Zooming In, x: " + trans.x + ", y: " + trans.y) ) @@ -517,7 +517,7 @@ class BarLineChartTouchListener( override fun onLongPress(e: MotionEvent) { lastGesture = ChartGesture.LONG_PRESS - val l = chart!!.onChartGestureListener + val l = chart.onChartGestureListener l?.onChartLongPressed(e) } @@ -525,15 +525,15 @@ class BarLineChartTouchListener( override fun onSingleTapUp(e: MotionEvent): Boolean { lastGesture = ChartGesture.SINGLE_TAP - val l = chart!!.onChartGestureListener + val l = chart.onChartGestureListener l?.onChartSingleTapped(e) - if (!chart!!.isHighlightPerTapEnabled) { + if (!chart.isHighlightPerTapEnabled) { return false } - val h = chart!!.getHighlightByTouchPoint(e.x, e.y) + val h = chart.getHighlightByTouchPoint(e.x, e.y) performHighlight(h, e) return super.onSingleTapUp(e) @@ -542,7 +542,7 @@ class BarLineChartTouchListener( override fun onFling(e1: MotionEvent?, e2: MotionEvent, velocityX: Float, velocityY: Float): Boolean { lastGesture = ChartGesture.FLING - val chartGestureListener = chart!!.onChartGestureListener + val chartGestureListener = chart.onChartGestureListener chartGestureListener?.onChartFling(e1, e2, velocityX, velocityY) @@ -560,8 +560,8 @@ class BarLineChartTouchListener( val currentTime = AnimationUtils.currentAnimationTimeMillis() - decelerationVelocity.x *= chart!!.dragDecelerationFrictionCoef - decelerationVelocity.y *= chart!!.dragDecelerationFrictionCoef + decelerationVelocity.x *= chart.dragDecelerationFrictionCoef + decelerationVelocity.y *= chart.dragDecelerationFrictionCoef val timeInterval = (currentTime - decelerationLastTime).toFloat() / 1000f @@ -576,24 +576,24 @@ class BarLineChartTouchListener( decelerationCurrentPoint.y, 0 ) - val dragDistanceX = if (chart!!.isDragXEnabled) decelerationCurrentPoint.x - touchStartPoint.x else 0f - val dragDistanceY = if (chart!!.isDragYEnabled) decelerationCurrentPoint.y - touchStartPoint.y else 0f + val dragDistanceX = if (chart.isDragXEnabled) decelerationCurrentPoint.x - touchStartPoint.x else 0f + val dragDistanceY = if (chart.isDragYEnabled) decelerationCurrentPoint.y - touchStartPoint.y else 0f performDrag(event, dragDistanceX, dragDistanceY) event.recycle() - matrix = chart!!.viewPortHandler.refresh(matrix, chart!!, false) + matrix = chart.viewPortHandler.refresh(matrix, chart, false) decelerationLastTime = currentTime if (abs(decelerationVelocity.x.toDouble()) >= 0.01 || abs(decelerationVelocity.y.toDouble()) >= 0.01) - chart?.postInvalidateOnAnimation() // This causes computeScroll to fire, recommended for this by Google + chart.postInvalidateOnAnimation() // This causes computeScroll to fire, recommended for this by Google else { // Range might have changed, which means that Y-axis labels // could have changed in size, affecting Y-axis size. // So we need to recalculate offsets. - chart?.calculateOffsets() - chart?.postInvalidate() + chart.calculateOffsets() + chart.postInvalidate() stopDeceleration() } diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/listener/ChartTouchListener.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/listener/ChartTouchListener.kt index c0f5cf25f..f90b5e870 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/listener/ChartTouchListener.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/listener/ChartTouchListener.kt @@ -8,8 +8,8 @@ import com.github.mikephil.charting.charts.Chart import com.github.mikephil.charting.highlight.Highlight import kotlin.math.sqrt -abstract class ChartTouchListener?>( - @JvmField protected var chart: T?) : SimpleOnGestureListener(), OnTouchListener { +abstract class ChartTouchListener>( + @JvmField protected var chart: T) : SimpleOnGestureListener(), OnTouchListener { enum class ChartGesture { NONE, DRAG, X_ZOOM, Y_ZOOM, PINCH_ZOOM, ROTATE, SINGLE_TAP, DOUBLE_TAP, LONG_PRESS, FLING } @@ -35,7 +35,7 @@ abstract class ChartTouchListener?>( * the gesturedetector used for detecting taps and longpresses, ... */ @JvmField - protected var gestureDetector: GestureDetector? = GestureDetector(chart!!.getContext(), this) + protected var gestureDetector: GestureDetector? = GestureDetector(chart.context, this) /** * Calls the OnChartGestureListener to do the start callback @@ -43,7 +43,7 @@ abstract class ChartTouchListener?>( * @param me */ fun startAction(me: MotionEvent) { - val l = chart!!.getOnChartGestureListener() + val l = chart.onChartGestureListener if (l != null) l.onChartGestureStart(me, this.lastGesture) } @@ -54,7 +54,7 @@ abstract class ChartTouchListener?>( * @param me */ fun endAction(me: MotionEvent) { - val l = chart!!.getOnChartGestureListener() + val l = chart.onChartGestureListener if (l != null) l.onChartGestureEnd(me, this.lastGesture) } @@ -75,10 +75,10 @@ abstract class ChartTouchListener?>( */ protected fun performHighlight(highlight: Highlight?, motionEvent: MotionEvent?) { if (highlight == null || highlight.equalTo(mLastHighlighted)) { - chart!!.highlightValue(null, true) + chart.highlightValue(null, true) mLastHighlighted = null } else { - chart!!.highlightValue(highlight, true) + chart.highlightValue(highlight, true) mLastHighlighted = highlight } } diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/listener/PieRadarChartTouchListener.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/listener/PieRadarChartTouchListener.kt index 095563794..7c3952610 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/listener/PieRadarChartTouchListener.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/listener/PieRadarChartTouchListener.kt @@ -9,7 +9,7 @@ import com.github.mikephil.charting.utils.MPPointF import com.github.mikephil.charting.utils.convertDpToPixel import kotlin.math.abs -class PieRadarChartTouchListener(chart: PieRadarChartBase<*>?) : ChartTouchListener?>(chart) { +class PieRadarChartTouchListener(chart: PieRadarChartBase<*>) : ChartTouchListener>(chart) { private val touchStartPoint: MPPointF = MPPointF.getInstance(0f, 0f) /** @@ -30,7 +30,7 @@ class PieRadarChartTouchListener(chart: PieRadarChartBase<*>?) : ChartTouchListe // if rotation by touch is enabled // TODO: Also check if the pie itself is being touched, rather than the entire chart area - if (chart!!.isRotationEnabled) { + if (chart.isRotationEnabled) { val x = event.x val y = event.y @@ -42,7 +42,7 @@ class PieRadarChartTouchListener(chart: PieRadarChartBase<*>?) : ChartTouchListe resetVelocity() - if (chart!!.isDragDecelerationEnabled) { + if (chart.isDragDecelerationEnabled) { sampleVelocity(x, y) } @@ -52,7 +52,7 @@ class PieRadarChartTouchListener(chart: PieRadarChartBase<*>?) : ChartTouchListe } MotionEvent.ACTION_MOVE -> { - if (chart!!.isDragDecelerationEnabled) { + if (chart.isDragDecelerationEnabled) { sampleVelocity(x, y) } @@ -62,17 +62,17 @@ class PieRadarChartTouchListener(chart: PieRadarChartBase<*>?) : ChartTouchListe ) { lastGesture = ChartGesture.ROTATE touchMode = ROTATE - chart!!.disableScroll() + chart.disableScroll() } else if (touchMode == ROTATE) { updateGestureRotation(x, y) - chart?.invalidate() + chart.invalidate() } endAction(event) } MotionEvent.ACTION_UP -> { - if (chart!!.isDragDecelerationEnabled) { + if (chart.isDragDecelerationEnabled) { stopDeceleration() sampleVelocity(x, y) @@ -82,11 +82,11 @@ class PieRadarChartTouchListener(chart: PieRadarChartBase<*>?) : ChartTouchListe if (decelerationAngularVelocity != 0f) { decelerationLastTime = AnimationUtils.currentAnimationTimeMillis() - chart?.postInvalidateOnAnimation() // This causes computeScroll to fire, recommended for this by Google + chart.postInvalidateOnAnimation() // This causes computeScroll to fire, recommended for this by Google } } - chart!!.enableScroll() + chart.enableScroll() touchMode = NONE endAction(event) @@ -100,7 +100,7 @@ class PieRadarChartTouchListener(chart: PieRadarChartBase<*>?) : ChartTouchListe override fun onLongPress(me: MotionEvent) { lastGesture = ChartGesture.LONG_PRESS - val onChartGestureListener = chart!!.onChartGestureListener + val onChartGestureListener = chart.onChartGestureListener onChartGestureListener?.onChartLongPressed(me) } @@ -110,15 +110,15 @@ class PieRadarChartTouchListener(chart: PieRadarChartBase<*>?) : ChartTouchListe override fun onSingleTapUp(e: MotionEvent): Boolean { lastGesture = ChartGesture.SINGLE_TAP - val onChartGestureListener = chart!!.onChartGestureListener + val onChartGestureListener = chart.onChartGestureListener onChartGestureListener?.onChartSingleTapped(e) - if (!chart!!.isHighlightPerTapEnabled) { + if (!chart.isHighlightPerTapEnabled) { return false } - val high = chart!!.getHighlightByTouchPoint(e.x, e.y) + val high = chart.getHighlightByTouchPoint(e.x, e.y) performHighlight(high, e) return true @@ -131,7 +131,7 @@ class PieRadarChartTouchListener(chart: PieRadarChartBase<*>?) : ChartTouchListe private fun sampleVelocity(touchLocationX: Float, touchLocationY: Float) { val currentTime = AnimationUtils.currentAnimationTimeMillis() - velocitySamples.add(AngularVelocitySample(currentTime, chart!!.getAngleForPoint(touchLocationX, touchLocationY))) + velocitySamples.add(AngularVelocitySample(currentTime, chart.getAngleForPoint(touchLocationX, touchLocationY))) // Remove samples older than our sample time - 1 seconds var i = 0 @@ -204,7 +204,7 @@ class PieRadarChartTouchListener(chart: PieRadarChartBase<*>?) : ChartTouchListe * @param y */ fun setGestureStartAngle(x: Float, y: Float) { - startAngle = chart!!.getAngleForPoint(x, y) - chart!!.rawRotationAngle + startAngle = chart.getAngleForPoint(x, y) - chart.rawRotationAngle } /** @@ -215,7 +215,7 @@ class PieRadarChartTouchListener(chart: PieRadarChartBase<*>?) : ChartTouchListe * @param y */ fun updateGestureRotation(x: Float, y: Float) { - chart!!.rotationAngle = chart!!.getAngleForPoint(x, y) - startAngle + chart.rotationAngle = chart.getAngleForPoint(x, y) - startAngle } /** @@ -232,16 +232,16 @@ class PieRadarChartTouchListener(chart: PieRadarChartBase<*>?) : ChartTouchListe val currentTime = AnimationUtils.currentAnimationTimeMillis() - decelerationAngularVelocity *= chart!!.dragDecelerationFrictionCoef + decelerationAngularVelocity *= chart.dragDecelerationFrictionCoef val timeInterval = (currentTime - decelerationLastTime).toFloat() / 1000f - chart!!.rotationAngle += decelerationAngularVelocity * timeInterval + chart.rotationAngle += decelerationAngularVelocity * timeInterval decelerationLastTime = currentTime if (abs(decelerationAngularVelocity) >= 0.001) { - chart?.postInvalidateOnAnimation() // This causes computeScroll to fire, recommended for this by Google + chart.postInvalidateOnAnimation() // This causes computeScroll to fire, recommended for this by Google } else { stopDeceleration() } diff --git a/app/src/main/kotlin/info/appdev/chartexample/AnotherBarActivity.kt b/app/src/main/kotlin/info/appdev/chartexample/AnotherBarActivity.kt index 75c3e7c12..7791bf56e 100644 --- a/app/src/main/kotlin/info/appdev/chartexample/AnotherBarActivity.kt +++ b/app/src/main/kotlin/info/appdev/chartexample/AnotherBarActivity.kt @@ -8,10 +8,8 @@ import android.view.Menu import android.view.MenuItem import android.widget.SeekBar import android.widget.SeekBar.OnSeekBarChangeListener -import android.widget.TextView import androidx.core.content.ContextCompat import androidx.core.net.toUri -import com.github.mikephil.charting.charts.BarChart import com.github.mikephil.charting.components.XAxis.XAxisPosition import com.github.mikephil.charting.data.BarData import com.github.mikephil.charting.data.BarDataSet @@ -19,80 +17,69 @@ import com.github.mikephil.charting.data.BarEntry import com.github.mikephil.charting.interfaces.datasets.IBarDataSet import com.github.mikephil.charting.utils.ColorTemplate import info.appdev.chartexample.DataTools.Companion.getValues +import info.appdev.chartexample.databinding.ActivityBarchartBinding import info.appdev.chartexample.notimportant.DemoBase class AnotherBarActivity : DemoBase(), OnSeekBarChangeListener { - private var chart: BarChart? = null - private var seekBarX: SeekBar? = null - private var seekBarY: SeekBar? = null - private var tvX: TextView? = null - private var tvY: TextView? = null + + private lateinit var binding: ActivityBarchartBinding override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) - setContentView(R.layout.activity_barchart) - - tvX = findViewById(R.id.tvXMax) - tvY = findViewById(R.id.tvYMax) - - seekBarX = findViewById(R.id.seekBarX) - seekBarX!!.setOnSeekBarChangeListener(this) - - seekBarY = findViewById(R.id.seekBarY) - seekBarY!!.setOnSeekBarChangeListener(this) - - chart = findViewById(R.id.chart1) + binding = ActivityBarchartBinding.inflate(layoutInflater) + setContentView(binding.root) - chart!!.description.isEnabled = false + binding.seekBarX.setOnSeekBarChangeListener(this) + binding.seekBarY.setOnSeekBarChangeListener(this) + binding.chart1.description.isEnabled = false - // if more than 60 entries are displayed in the chart, no values will be - // drawn - chart!!.setMaxVisibleValueCount(60) + // if more than 60 entries are displayed in the chart, no values will be drawn + binding.chart1.setMaxVisibleValueCount(60) // scaling can now only be done on x- and y-axis separately - chart!!.setPinchZoom(false) + binding.chart1.setPinchZoom(false) - chart!!.setDrawBarShadow(false) - chart!!.setDrawGridBackground(false) + binding.chart1.setDrawBarShadow(false) + binding.chart1.setDrawGridBackground(false) - val xAxis = chart!!.xAxis + val xAxis = binding.chart1.xAxis xAxis.position = XAxisPosition.BOTTOM xAxis.setDrawGridLines(false) - chart!!.axisLeft.setDrawGridLines(false) + binding.chart1.axisLeft.setDrawGridLines(false) // setting data - seekBarX!!.progress = DEFAULT_VALUE - seekBarY!!.progress = 100 + binding.seekBarX.progress = DEFAULT_VALUE + binding.seekBarY.progress = 100 // add a nice and smooth animation - chart!!.animateY(1500) + binding.chart1.animateY(1500) - chart!!.legend.isEnabled = false + binding.chart1.legend.isEnabled = false } override fun onProgressChanged(seekBar: SeekBar?, progress: Int, fromUser: Boolean) { - tvX!!.text = seekBarX!!.progress.toString() - tvY!!.text = seekBarY!!.progress.toString() + binding.tvXMax.text = binding.seekBarX.progress.toString() + binding.tvYMax.text = binding.seekBarY.progress.toString() val values = ArrayList() val sampleValues = getValues(100) - for (i in 0.. 0 + if (binding.chart1.data != null && + binding.chart1.data!!.getDataSetCount() > 0 ) { - set1 = chart!!.data!!.getDataSetByIndex(0) as BarDataSet + set1 = binding.chart1.data!!.getDataSetByIndex(0) as BarDataSet set1.entries = values - chart!!.data!!.notifyDataChanged() - chart?.notifyDataSetChanged() + binding.chart1.data!!.notifyDataChanged() + binding.chart1.notifyDataSetChanged() } else { set1 = BarDataSet(values, "Data Set") set1.setColors(*ColorTemplate.VORDIPLOM_COLORS) @@ -102,11 +89,11 @@ class AnotherBarActivity : DemoBase(), OnSeekBarChangeListener { dataSets.add(set1) val data = BarData(dataSets) - chart!!.setData(data) - chart!!.setFitBars(true) + binding.chart1.setData(data) + binding.chart1.setFitBars(true) } - chart?.invalidate() + binding.chart1.invalidate() } override fun onCreateOptionsMenu(menu: Menu): Boolean { @@ -124,55 +111,58 @@ class AnotherBarActivity : DemoBase(), OnSeekBarChangeListener { } R.id.actionToggleValues -> { - chart!!.data!!.dataSets.forEach { + binding.chart1.data!!.dataSets.forEach { it?.isDrawValues = !it.isDrawValues } - chart?.invalidate() + binding.chart1.invalidate() } R.id.actionToggleHighlight -> { - if (chart!!.data != null) { - chart!!.data!!.isHighlightEnabled = !chart!!.data!!.isHighlightEnabled() - chart?.invalidate() + if (binding.chart1.data != null) { + binding.chart1.data!!.isHighlightEnabled = !binding.chart1.data!!.isHighlightEnabled() + binding.chart1.invalidate() } } R.id.actionTogglePinch -> { - if (chart!!.isPinchZoomEnabled) chart!!.setPinchZoom(false) - else chart!!.setPinchZoom(true) + if (binding.chart1.isPinchZoomEnabled) + binding.chart1.setPinchZoom(false) + else + binding.chart1.setPinchZoom(true) - chart?.invalidate() + binding.chart1.invalidate() } R.id.actionToggleAutoScaleMinMax -> { - chart!!.isAutoScaleMinMaxEnabled = !chart!!.isAutoScaleMinMaxEnabled - chart?.notifyDataSetChanged() + binding.chart1.isAutoScaleMinMaxEnabled = !binding.chart1.isAutoScaleMinMaxEnabled + binding.chart1.notifyDataSetChanged() } R.id.actionToggleBarBorders -> { - for (set in chart!!.data!!.dataSets) (set as BarDataSet).barBorderWidth = if (set.barBorderWidth == 1f) 0f else 1f + for (set in binding.chart1.data!!.dataSets) + (set as BarDataSet).barBorderWidth = if (set.barBorderWidth == 1f) 0f else 1f - chart?.invalidate() + binding.chart1.invalidate() } R.id.animateX -> { - chart!!.animateX(2000) + binding.chart1.animateX(2000) } R.id.animateY -> { - chart!!.animateY(2000) + binding.chart1.animateY(2000) } R.id.animateXY -> { - chart!!.animateXY(2000, 2000) + binding.chart1.animateXY(2000, 2000) } R.id.actionSave -> { if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) { saveToGallery() } else { - requestStoragePermission(chart) + requestStoragePermission(binding.chart1) } } } @@ -180,7 +170,7 @@ class AnotherBarActivity : DemoBase(), OnSeekBarChangeListener { } override fun saveToGallery() { - saveToGallery(chart, "AnotherBarActivity") + saveToGallery(binding.chart1, "AnotherBarActivity") } override fun onStartTrackingTouch(seekBar: SeekBar?) = Unit diff --git a/app/src/main/kotlin/info/appdev/chartexample/BarChartActivity.kt b/app/src/main/kotlin/info/appdev/chartexample/BarChartActivity.kt index 9cde1a234..cbe49ac12 100644 --- a/app/src/main/kotlin/info/appdev/chartexample/BarChartActivity.kt +++ b/app/src/main/kotlin/info/appdev/chartexample/BarChartActivity.kt @@ -9,11 +9,9 @@ import android.view.Menu import android.view.MenuItem import android.widget.SeekBar import android.widget.SeekBar.OnSeekBarChangeListener -import android.widget.TextView import androidx.core.content.ContextCompat import androidx.core.content.res.ResourcesCompat import androidx.core.net.toUri -import com.github.mikephil.charting.charts.BarChart import com.github.mikephil.charting.components.Legend import com.github.mikephil.charting.components.Legend.LegendForm import com.github.mikephil.charting.components.XAxis.XAxisPosition @@ -31,53 +29,45 @@ import com.github.mikephil.charting.utils.Fill import com.github.mikephil.charting.utils.MPPointF import info.appdev.chartexample.DataTools.Companion.getValues import info.appdev.chartexample.custom.XYMarkerView +import info.appdev.chartexample.databinding.ActivityBarchartBinding import info.appdev.chartexample.formatter.DayAxisValueFormatter import info.appdev.chartexample.formatter.MyAxisValueFormatter import info.appdev.chartexample.notimportant.DemoBase import timber.log.Timber class BarChartActivity : DemoBase(), OnSeekBarChangeListener, OnChartValueSelectedListener { - private var chart: BarChart? = null - private var seekBarX: SeekBar? = null - private var seekBarY: SeekBar? = null - private var tvX: TextView? = null - private var tvY: TextView? = null + + private lateinit var binding: ActivityBarchartBinding override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) - setContentView(R.layout.activity_barchart) - - tvX = findViewById(R.id.tvXMax) - tvY = findViewById(R.id.tvYMax) - - seekBarX = findViewById(R.id.seekBarX) - seekBarY = findViewById(R.id.seekBarY) + binding = ActivityBarchartBinding.inflate(layoutInflater) + setContentView(binding.root) - seekBarY!!.setOnSeekBarChangeListener(this) - seekBarX!!.setOnSeekBarChangeListener(this) + binding.seekBarY.setOnSeekBarChangeListener(this) + binding.seekBarX.setOnSeekBarChangeListener(this) - chart = findViewById(R.id.chart1) - chart!!.setOnChartValueSelectedListener(this) - chart!!.setRoundedBarRadius(50f) + binding.chart1.setOnChartValueSelectedListener(this) + binding.chart1.setRoundedBarRadius(50f) - chart!!.setDrawBarShadow(false) - chart!!.setDrawValueAboveBar(true) + binding.chart1.setDrawBarShadow(false) + binding.chart1.setDrawValueAboveBar(true) - chart!!.description.isEnabled = false + binding.chart1.description.isEnabled = false // if more than 60 entries are displayed in the chart, no values will be // drawn - chart!!.setMaxVisibleValueCount(60) + binding.chart1.setMaxVisibleValueCount(60) // scaling can now only be done on x- and y-axis separately - chart!!.setPinchZoom(false) + binding.chart1.setPinchZoom(false) - chart!!.setDrawGridBackground(false) + binding.chart1.setDrawGridBackground(false) // chart.setDrawYLabels(false); - val xAxisFormatter: IAxisValueFormatter = DayAxisValueFormatter(chart!!) + val xAxisFormatter: IAxisValueFormatter = DayAxisValueFormatter(binding.chart1) - val xAxis = chart!!.xAxis + val xAxis = binding.chart1.xAxis xAxis.position = XAxisPosition.BOTTOM xAxis.typeface = tfLight xAxis.setDrawGridLines(false) @@ -87,7 +77,7 @@ class BarChartActivity : DemoBase(), OnSeekBarChangeListener, OnChartValueSelect val custom: IAxisValueFormatter = MyAxisValueFormatter() - val leftAxis = chart!!.axisLeft + val leftAxis = binding.chart1.axisLeft leftAxis.typeface = tfLight leftAxis.setLabelCount(8, false) leftAxis.valueFormatter = custom @@ -95,7 +85,7 @@ class BarChartActivity : DemoBase(), OnSeekBarChangeListener, OnChartValueSelect leftAxis.spaceTop = 15f leftAxis.axisMinimum = 0f// this replaces setStartAtZero(true) - val rightAxis = chart!!.axisRight + val rightAxis = binding.chart1.axisRight rightAxis.setDrawGridLines(false) rightAxis.typeface = tfLight rightAxis.setLabelCount(8, false) @@ -103,7 +93,7 @@ class BarChartActivity : DemoBase(), OnSeekBarChangeListener, OnChartValueSelect rightAxis.spaceTop = 15f rightAxis.axisMinimum = 0f// this replaces setStartAtZero(true) - val l = chart!!.legend + val l = binding.chart1.legend l.verticalAlignment = Legend.LegendVerticalAlignment.BOTTOM l.horizontalAlignment = Legend.LegendHorizontalAlignment.LEFT l.orientation = Legend.LegendOrientation.HORIZONTAL @@ -114,12 +104,12 @@ class BarChartActivity : DemoBase(), OnSeekBarChangeListener, OnChartValueSelect l.xEntrySpace = 4f val mv = XYMarkerView(this, xAxisFormatter) - mv.chartView = chart // For bounds control - chart?.setMarker(mv) // Set the marker to the chart + mv.chartView = binding.chart1 // For bounds control + binding.chart1.setMarker(mv) // Set the marker to the chart // setting data - seekBarY!!.progress = 50 - seekBarX!!.progress = 12 + binding.seekBarY.progress = 50 + binding.seekBarX.progress = 12 // chart.setDrawLegend(false); } @@ -144,13 +134,13 @@ class BarChartActivity : DemoBase(), OnSeekBarChangeListener, OnChartValueSelect val set1: BarDataSet - if (chart!!.data != null && - chart!!.data!!.getDataSetCount() > 0 + if (binding.chart1.data != null && + binding.chart1.data!!.getDataSetCount() > 0 ) { - set1 = chart!!.data!!.getDataSetByIndex(0) as BarDataSet + set1 = binding.chart1.data!!.getDataSetByIndex(0) as BarDataSet set1.entries = values - chart!!.data!!.notifyDataChanged() - chart?.notifyDataSetChanged() + binding.chart1.data!!.notifyDataChanged() + binding.chart1.notifyDataSetChanged() } else { set1 = BarDataSet(values, "The year 2017") @@ -184,7 +174,7 @@ class BarChartActivity : DemoBase(), OnSeekBarChangeListener, OnChartValueSelect data.setValueTypeface(tfLight) data.barWidth = 0.9f - chart?.setData(data) + binding.chart1.setData(data) } } @@ -202,88 +192,88 @@ class BarChartActivity : DemoBase(), OnSeekBarChangeListener, OnChartValueSelect } R.id.actionToggleValues -> { - chart!!.data!!.dataSets.forEach { + binding.chart1.data!!.dataSets.forEach { it?.isDrawValues = !it.isDrawValues } - chart?.invalidate() + binding.chart1.invalidate() } R.id.actionToggleIcons -> { - for (set in chart!!.data!!.dataSets) + for (set in binding.chart1.data!!.dataSets) set?.isDrawIcons = !set.isDrawIcons - chart?.invalidate() + binding.chart1.invalidate() } R.id.actionToggleHighlight -> { - if (chart!!.data != null) { - chart!!.data!!.isHighlightEnabled = !chart!!.data!!.isHighlightEnabled() - chart?.invalidate() + if (binding.chart1.data != null) { + binding.chart1.data!!.isHighlightEnabled = !binding.chart1.data!!.isHighlightEnabled() + binding.chart1.invalidate() } } R.id.actionTogglePinch -> { - if (chart!!.isPinchZoomEnabled) chart!!.setPinchZoom(false) - else chart!!.setPinchZoom(true) + if (binding.chart1.isPinchZoomEnabled) binding.chart1.setPinchZoom(false) + else binding.chart1.setPinchZoom(true) - chart?.invalidate() + binding.chart1.invalidate() } R.id.actionToggleAutoScaleMinMax -> { - chart!!.isAutoScaleMinMaxEnabled = !chart!!.isAutoScaleMinMaxEnabled - chart?.notifyDataSetChanged() + binding.chart1.isAutoScaleMinMaxEnabled = !binding.chart1.isAutoScaleMinMaxEnabled + binding.chart1.notifyDataSetChanged() } R.id.actionToggleBarBorders -> { - for (set in chart!!.data!!.dataSets) + for (set in binding.chart1.data!!.dataSets) (set as BarDataSet).barBorderWidth = if (set.barBorderWidth == 1f) 0f else 1f - chart?.invalidate() + binding.chart1.invalidate() } R.id.animateX -> { - chart!!.animateX(2000) + binding.chart1.animateX(2000) } R.id.animateY -> { - chart!!.animateY(2000) + binding.chart1.animateY(2000) } R.id.animateXY -> { - chart!!.animateXY(2000, 2000) + binding.chart1.animateXY(2000, 2000) } R.id.actionSave -> { if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) { saveToGallery() } else { - requestStoragePermission(chart) + requestStoragePermission(binding.chart1) } } R.id.actionRotateXAxisLabelsBy45Deg -> { - chart!!.xAxis.labelRotationAngle = 45f - chart?.notifyDataSetChanged() - chart?.invalidate() + binding.chart1.xAxis.labelRotationAngle = 45f + binding.chart1.notifyDataSetChanged() + binding.chart1.invalidate() } } return true } override fun onProgressChanged(seekBar: SeekBar?, progress: Int, fromUser: Boolean) { - tvX!!.text = seekBarX!!.progress.toString() - tvY!!.text = seekBarY!!.progress.toString() + binding.tvXMax.text = binding.seekBarX.progress.toString() + binding.tvYMax.text = binding.seekBarY.progress.toString() - setData(seekBarX!!.progress, seekBarY!!.progress.toFloat()) - chart?.invalidate() + setData(binding.seekBarX.progress, binding.seekBarY.progress.toFloat()) + binding.chart1.invalidate() } override fun saveToGallery() { - saveToGallery(chart, "BarChartActivity") + saveToGallery(binding.chart1, "BarChartActivity") } override fun onStartTrackingTouch(seekBar: SeekBar?) = Unit @@ -294,12 +284,12 @@ class BarChartActivity : DemoBase(), OnSeekBarChangeListener, OnChartValueSelect override fun onValueSelected(entry: Entry, highlight: Highlight) { val bounds = onValueSelectedRectF - chart!!.getBarBounds(entry as BarEntry, bounds) - val position = chart!!.getPosition(entry, AxisDependency.LEFT) + binding.chart1.getBarBounds(entry as BarEntry, bounds) + val position = binding.chart1.getPosition(entry, AxisDependency.LEFT) Timber.i("bounds $bounds") Timber.i("position = $position") - Timber.i("x-index low: ${+chart!!.lowestVisibleX}, high: ${+chart!!.highestVisibleX}") + Timber.i("x-index low: ${+binding.chart1.lowestVisibleX}, high: ${+binding.chart1.highestVisibleX}") MPPointF.recycleInstance(position) } diff --git a/app/src/main/kotlin/info/appdev/chartexample/BarChartActivityMultiDataset.kt b/app/src/main/kotlin/info/appdev/chartexample/BarChartActivityMultiDataset.kt index 209f4c2b0..0f9be3ef5 100644 --- a/app/src/main/kotlin/info/appdev/chartexample/BarChartActivityMultiDataset.kt +++ b/app/src/main/kotlin/info/appdev/chartexample/BarChartActivityMultiDataset.kt @@ -9,10 +9,8 @@ import android.view.Menu import android.view.MenuItem import android.widget.SeekBar import android.widget.SeekBar.OnSeekBarChangeListener -import android.widget.TextView import androidx.core.content.ContextCompat import androidx.core.net.toUri -import com.github.mikephil.charting.charts.BarChart import com.github.mikephil.charting.components.AxisBase import com.github.mikephil.charting.components.Legend import com.github.mikephil.charting.data.BarData @@ -25,55 +23,49 @@ import com.github.mikephil.charting.highlight.Highlight import com.github.mikephil.charting.listener.OnChartValueSelectedListener import info.appdev.chartexample.DataTools.Companion.getValues import info.appdev.chartexample.custom.MyMarkerView +import info.appdev.chartexample.databinding.ActivityBarchartBinding import info.appdev.chartexample.notimportant.DemoBase import timber.log.Timber import java.util.Locale class BarChartActivityMultiDataset : DemoBase(), OnSeekBarChangeListener, OnChartValueSelectedListener { - private var chart: BarChart? = null - private var seekBarX: SeekBar? = null - private var seekBarY: SeekBar? = null - private var tvX: TextView? = null - private var tvY: TextView? = null + + private lateinit var binding: ActivityBarchartBinding override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) - setContentView(R.layout.activity_barchart) + binding = ActivityBarchartBinding.inflate(layoutInflater) + setContentView(binding.root) - tvX = findViewById(R.id.tvXMax) - tvX!!.textSize = 10f - tvY = findViewById(R.id.tvYMax) + binding.tvXMax.textSize = 10f - seekBarX = findViewById(R.id.seekBarX) - seekBarX!!.max = 50 - seekBarX!!.setOnSeekBarChangeListener(this) + binding.seekBarX.max = 50 + binding.seekBarX.setOnSeekBarChangeListener(this) - seekBarY = findViewById(R.id.seekBarY) - seekBarY!!.setOnSeekBarChangeListener(this) + binding.seekBarY.setOnSeekBarChangeListener(this) - chart = findViewById(R.id.chart1) - chart!!.setOnChartValueSelectedListener(this) - chart!!.description.isEnabled = false + binding.chart1.setOnChartValueSelectedListener(this) + binding.chart1.description.isEnabled = false // chart.setDrawBorders(true); // scaling can now only be done on x- and y-axis separately - chart!!.setPinchZoom(false) + binding.chart1.setPinchZoom(false) - chart!!.setDrawBarShadow(false) + binding.chart1.setDrawBarShadow(false) - chart!!.setDrawGridBackground(false) + binding.chart1.setDrawGridBackground(false) // create a custom MarkerView (extend MarkerView) and specify the layout // to use for it val mv = MyMarkerView(this, R.layout.custom_marker_view) - mv.chartView = chart // For bounds control - chart!!.setMarker(mv) // Set the marker to the chart + mv.chartView = binding.chart1 // For bounds control + binding.chart1.setMarker(mv) // Set the marker to the chart - seekBarX!!.progress = 10 - seekBarY!!.progress = 100 + binding.seekBarX.progress = 10 + binding.seekBarY.progress = 100 - val l = chart!!.legend + val l = binding.chart1.legend l.verticalAlignment = Legend.LegendVerticalAlignment.TOP l.horizontalAlignment = Legend.LegendHorizontalAlignment.RIGHT l.orientation = Legend.LegendOrientation.VERTICAL @@ -84,7 +76,7 @@ class BarChartActivityMultiDataset : DemoBase(), OnSeekBarChangeListener, OnChar l.yEntrySpace = 0f l.textSize = 8f - val xAxis = chart!!.xAxis + val xAxis = binding.chart1.xAxis xAxis.typeface = tfLight xAxis.granularity = 1f xAxis.setCenterAxisLabels(true) @@ -94,14 +86,14 @@ class BarChartActivityMultiDataset : DemoBase(), OnSeekBarChangeListener, OnChar } } - val leftAxis = chart!!.axisLeft + val leftAxis = binding.chart1.axisLeft leftAxis.typeface = tfLight leftAxis.valueFormatter = LargeValueFormatter() leftAxis.setDrawGridLines(false) leftAxis.spaceTop = 35f leftAxis.axisMinimum = 0f // this replaces setStartAtZero(true) - chart!!.axisRight.isEnabled = false + binding.chart1.axisRight.isEnabled = false } override fun onProgressChanged(seekBar: SeekBar?, progress: Int, fromUser: Boolean) { @@ -110,19 +102,19 @@ class BarChartActivityMultiDataset : DemoBase(), OnSeekBarChangeListener, OnChar val barWidth = 0.2f // x4 DataSet // (0.2 + 0.03) * 4 + 0.08 = 1.00 -> interval per "group" - val groupCount = seekBarX!!.progress + 1 + val groupCount = binding.seekBarX.progress + 1 val startYear = 1980 val endYear = startYear + groupCount - tvX!!.text = String.format(Locale.ENGLISH, "%d-%d", startYear, endYear) - tvY!!.text = seekBarY!!.progress.toString() + binding.tvXMax.text = String.format(Locale.ENGLISH, "%d-%d", startYear, endYear) + binding.tvYMax.text = binding.seekBarY.progress.toString() val values1 = ArrayList() val values2 = ArrayList() val values3 = ArrayList() val values4 = ArrayList() - val randomMultiplier = seekBarY!!.progress * 100000f + val randomMultiplier = binding.seekBarY.progress * 100000f val sampleValues = getValues(100 + 2) for (i in startYear.. 0) { - set1 = chart!!.data!!.getDataSetByIndex(0) as BarDataSet - set2 = chart!!.data!!.getDataSetByIndex(1) as BarDataSet - set3 = chart!!.data!!.getDataSetByIndex(2) as BarDataSet - set4 = chart!!.data!!.getDataSetByIndex(3) as BarDataSet + if (binding.chart1.data != null && binding.chart1.data!!.getDataSetCount() > 0) { + set1 = binding.chart1.data!!.getDataSetByIndex(0) as BarDataSet + set2 = binding.chart1.data!!.getDataSetByIndex(1) as BarDataSet + set3 = binding.chart1.data!!.getDataSetByIndex(2) as BarDataSet + set4 = binding.chart1.data!!.getDataSetByIndex(3) as BarDataSet set1.entries = values1 set2.entries = values2 set3.entries = values3 set4.entries = values4 - chart!!.data!!.notifyDataChanged() - chart?.notifyDataSetChanged() + binding.chart1.data!!.notifyDataChanged() + binding.chart1.notifyDataSetChanged() } else { // create 4 DataSets set1 = BarDataSet(values1, "Company A") @@ -163,19 +155,19 @@ class BarChartActivityMultiDataset : DemoBase(), OnSeekBarChangeListener, OnChar data.setValueFormatter(LargeValueFormatter()) data.setValueTypeface(tfLight) - chart!!.setData(data) + binding.chart1.setData(data) } // specify the width each bar should have - chart!!.barData.barWidth = barWidth + binding.chart1.barData.barWidth = barWidth // restrict the x-axis range - chart!!.xAxis.axisMinimum = startYear.toFloat() + binding.chart1.xAxis.axisMinimum = startYear.toFloat() // barData.getGroupWith(...) is a helper that calculates the width each group needs based on the provided parameters - chart!!.xAxis.axisMaximum = startYear + chart!!.barData.getGroupWidth(groupSpace, barSpace) * groupCount - chart!!.groupBars(startYear.toFloat(), groupSpace, barSpace) - chart?.invalidate() + binding.chart1.xAxis.axisMaximum = startYear + binding.chart1.barData.getGroupWidth(groupSpace, barSpace) * groupCount + binding.chart1.groupBars(startYear.toFloat(), groupSpace, barSpace) + binding.chart1.invalidate() } override fun onCreateOptionsMenu(menu: Menu?): Boolean { @@ -193,35 +185,35 @@ class BarChartActivityMultiDataset : DemoBase(), OnSeekBarChangeListener, OnChar } R.id.actionToggleValues -> { - chart!!.data!!.dataSets.forEach { + binding.chart1.data!!.dataSets.forEach { it?.isDrawValues = !it.isDrawValues } - chart?.invalidate() + binding.chart1.invalidate() } R.id.actionTogglePinch -> { - if (chart!!.isPinchZoomEnabled) chart!!.setPinchZoom(false) - else chart!!.setPinchZoom(true) + if (binding.chart1.isPinchZoomEnabled) binding.chart1.setPinchZoom(false) + else binding.chart1.setPinchZoom(true) - chart?.invalidate() + binding.chart1.invalidate() } R.id.actionToggleAutoScaleMinMax -> { - chart!!.isAutoScaleMinMaxEnabled = !chart!!.isAutoScaleMinMaxEnabled - chart?.notifyDataSetChanged() + binding.chart1.isAutoScaleMinMaxEnabled = !binding.chart1.isAutoScaleMinMaxEnabled + binding.chart1.notifyDataSetChanged() } R.id.actionToggleBarBorders -> { - for (set in chart!!.data!!.dataSets) (set as BarDataSet).barBorderWidth = if (set.barBorderWidth == 1f) 0f else 1f + for (set in binding.chart1.data!!.dataSets) (set as BarDataSet).barBorderWidth = if (set.barBorderWidth == 1f) 0f else 1f - chart?.invalidate() + binding.chart1.invalidate() } R.id.actionToggleHighlight -> { - if (chart!!.data != null) { - chart!!.data!!.isHighlightEnabled = !chart!!.data!!.isHighlightEnabled() - chart?.invalidate() + if (binding.chart1.data != null) { + binding.chart1.data!!.isHighlightEnabled = !binding.chart1.data!!.isHighlightEnabled() + binding.chart1.invalidate() } } @@ -229,27 +221,27 @@ class BarChartActivityMultiDataset : DemoBase(), OnSeekBarChangeListener, OnChar if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) { saveToGallery() } else { - requestStoragePermission(chart) + requestStoragePermission(binding.chart1) } } R.id.animateX -> { - chart!!.animateX(2000) + binding.chart1.animateX(2000) } R.id.animateY -> { - chart!!.animateY(2000) + binding.chart1.animateY(2000) } R.id.animateXY -> { - chart!!.animateXY(2000, 2000) + binding.chart1.animateXY(2000, 2000) } } return true } override fun saveToGallery() { - saveToGallery(chart, "BarChartActivityMultiDataset") + saveToGallery(binding.chart1, "BarChartActivityMultiDataset") } override fun onStartTrackingTouch(seekBar: SeekBar?) = Unit diff --git a/app/src/main/kotlin/info/appdev/chartexample/BarChartActivitySinus.kt b/app/src/main/kotlin/info/appdev/chartexample/BarChartActivitySinus.kt index 84a3df300..c09365abf 100644 --- a/app/src/main/kotlin/info/appdev/chartexample/BarChartActivitySinus.kt +++ b/app/src/main/kotlin/info/appdev/chartexample/BarChartActivitySinus.kt @@ -29,8 +29,7 @@ class BarChartActivitySinus : DemoBase(), OnSeekBarChangeListener { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) binding = ActivityBarchartSinusBinding.inflate(layoutInflater) - val view = binding.root - setContentView(view) + setContentView(binding.root) dataSinus = assets.loadBarEntriesFromAssets("sinus_values.txt") binding.chart1.setDrawBarShadow(false) diff --git a/app/src/main/kotlin/info/appdev/chartexample/BarChartPositiveNegative.kt b/app/src/main/kotlin/info/appdev/chartexample/BarChartPositiveNegative.kt index 78b511c1b..d025b80ea 100644 --- a/app/src/main/kotlin/info/appdev/chartexample/BarChartPositiveNegative.kt +++ b/app/src/main/kotlin/info/appdev/chartexample/BarChartPositiveNegative.kt @@ -6,7 +6,6 @@ import android.os.Bundle import android.view.Menu import android.view.MenuItem import androidx.core.net.toUri -import com.github.mikephil.charting.charts.BarChart import com.github.mikephil.charting.components.AxisBase import com.github.mikephil.charting.components.XAxis.XAxisPosition import com.github.mikephil.charting.data.BarData @@ -16,36 +15,38 @@ import com.github.mikephil.charting.data.Entry import com.github.mikephil.charting.formatter.IAxisValueFormatter import com.github.mikephil.charting.formatter.IValueFormatter import com.github.mikephil.charting.utils.ViewPortHandler +import info.appdev.chartexample.databinding.ActivityBarchartNoseekbarBinding import info.appdev.chartexample.notimportant.DemoBase import java.text.DecimalFormat import kotlin.math.max import kotlin.math.min class BarChartPositiveNegative : DemoBase() { - private var chart: BarChart? = null + + private lateinit var binding: ActivityBarchartNoseekbarBinding override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) - setContentView(R.layout.activity_barchart_noseekbar) + binding = ActivityBarchartNoseekbarBinding.inflate(layoutInflater) + setContentView(binding.root) - chart = findViewById(R.id.chart1) - chart!!.setBackgroundColor(Color.WHITE) - chart!!.extraTopOffset = -30f - chart!!.extraBottomOffset = 10f - chart!!.extraLeftOffset = 70f - chart!!.extraRightOffset = 70f + binding.chart1.setBackgroundColor(Color.WHITE) + binding.chart1.extraTopOffset = -30f + binding.chart1.extraBottomOffset = 10f + binding.chart1.extraLeftOffset = 70f + binding.chart1.extraRightOffset = 70f - chart!!.setDrawBarShadow(false) - chart!!.setDrawValueAboveBar(true) + binding.chart1.setDrawBarShadow(false) + binding.chart1.setDrawValueAboveBar(true) - chart!!.description.isEnabled = false + binding.chart1.description.isEnabled = false // scaling can now only be done on x- and y-axis separately - chart!!.setPinchZoom(false) + binding.chart1.setPinchZoom(false) - chart!!.setDrawGridBackground(false) + binding.chart1.setDrawGridBackground(false) - val xAxis = chart!!.xAxis + val xAxis = binding.chart1.xAxis xAxis.position = XAxisPosition.BOTTOM xAxis.typeface = tfRegular xAxis.setDrawGridLines(false) @@ -56,7 +57,7 @@ class BarChartPositiveNegative : DemoBase() { xAxis.setCenterAxisLabels(true) xAxis.granularity = 1f - val left = chart!!.axisLeft + val left = binding.chart1.axisLeft left.setDrawLabels(false) left.spaceTop = 25f left.spaceBottom = 25f @@ -65,8 +66,8 @@ class BarChartPositiveNegative : DemoBase() { left.setDrawZeroLine(true) // draw a zero line left.zeroLineColor = Color.GRAY left.zeroLineWidth = 0.7f - chart!!.axisRight.isEnabled = false - chart!!.legend.isEnabled = false + binding.chart1.axisRight.isEnabled = false + binding.chart1.legend.isEnabled = false // THIS IS THE ORIGINAL DATA YOU WANT TO PLOT val data: MutableList = ArrayList() @@ -104,13 +105,13 @@ class BarChartPositiveNegative : DemoBase() { val set: BarDataSet - if (chart!!.data != null && - chart!!.data!!.getDataSetCount() > 0 + if (binding.chart1.data != null && + binding.chart1.data!!.getDataSetCount() > 0 ) { - set = chart!!.data!!.getDataSetByIndex(0) as BarDataSet + set = binding.chart1.data!!.getDataSetByIndex(0) as BarDataSet set.entries = values - chart!!.data!!.notifyDataChanged() - chart?.notifyDataSetChanged() + binding.chart1.data!!.notifyDataChanged() + binding.chart1.notifyDataSetChanged() } else { set = BarDataSet(values, "Values") set.setColors(colors) @@ -122,8 +123,8 @@ class BarChartPositiveNegative : DemoBase() { data.setValueFormatter(ValueFormatter()) data.barWidth = 0.8f - chart!!.setData(data) - chart?.invalidate() + binding.chart1.setData(data) + binding.chart1.invalidate() } } diff --git a/app/src/main/kotlin/info/appdev/chartexample/BubbleChartActivity.kt b/app/src/main/kotlin/info/appdev/chartexample/BubbleChartActivity.kt index 9c0a70d42..f39148af2 100644 --- a/app/src/main/kotlin/info/appdev/chartexample/BubbleChartActivity.kt +++ b/app/src/main/kotlin/info/appdev/chartexample/BubbleChartActivity.kt @@ -9,11 +9,9 @@ import android.view.Menu import android.view.MenuItem import android.widget.SeekBar import android.widget.SeekBar.OnSeekBarChangeListener -import android.widget.TextView import androidx.core.content.ContextCompat import androidx.core.content.res.ResourcesCompat import androidx.core.net.toUri -import com.github.mikephil.charting.charts.BubbleChart import com.github.mikephil.charting.components.Legend import com.github.mikephil.charting.components.XAxis import com.github.mikephil.charting.data.BubbleData @@ -26,74 +24,62 @@ import com.github.mikephil.charting.listener.OnChartValueSelectedListener import com.github.mikephil.charting.utils.ColorTemplate import com.github.mikephil.charting.utils.MPPointF import info.appdev.chartexample.DataTools.Companion.getValues +import info.appdev.chartexample.databinding.ActivityBubblechartBinding import info.appdev.chartexample.notimportant.DemoBase import timber.log.Timber class BubbleChartActivity : DemoBase(), OnSeekBarChangeListener, OnChartValueSelectedListener { - private var chart: BubbleChart? = null - private var seekBarX: SeekBar? = null - private var seekBarY: SeekBar? = null - private var tvX: TextView? = null - private var tvY: TextView? = null + + private lateinit var binding: ActivityBubblechartBinding override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) - setContentView(R.layout.activity_bubblechart) - - tvX = findViewById(R.id.tvXMax) - tvY = findViewById(R.id.tvYMax) - - seekBarX = findViewById(R.id.seekBarX) - seekBarX!!.setOnSeekBarChangeListener(this) - - seekBarY = findViewById(R.id.seekBarY) - seekBarY!!.setOnSeekBarChangeListener(this) - - chart = findViewById(R.id.chart1) - chart!!.description.isEnabled = false - - chart!!.setOnChartValueSelectedListener(this) - - chart!!.setDrawGridBackground(false) + binding = ActivityBubblechartBinding.inflate(layoutInflater) + setContentView(binding.root) - chart!!.setTouchEnabled(true) + binding.seekBarX.setOnSeekBarChangeListener(this) + binding.seekBarY.setOnSeekBarChangeListener(this) + binding.chart1.description.isEnabled = false + binding.chart1.setOnChartValueSelectedListener(this) + binding.chart1.setDrawGridBackground(false) + binding.chart1.setTouchEnabled(true) // enable scaling and dragging - chart!!.setDragEnabled(true) - chart!!.setScaleEnabled(true) + binding.chart1.setDragEnabled(true) + binding.chart1.setScaleEnabled(true) - chart!!.setMaxVisibleValueCount(200) - chart!!.setPinchZoom(true) + binding.chart1.setMaxVisibleValueCount(200) + binding.chart1.setPinchZoom(true) - seekBarX!!.progress = 10 - seekBarY!!.progress = 50 + binding.seekBarX.progress = 10 + binding.seekBarY.progress = 50 - val l = chart!!.legend + val l = binding.chart1.legend l.verticalAlignment = Legend.LegendVerticalAlignment.TOP l.horizontalAlignment = Legend.LegendHorizontalAlignment.RIGHT l.orientation = Legend.LegendOrientation.VERTICAL l.setDrawInside(false) l.typeface = tfLight - val yl = chart!!.axisLeft + val yl = binding.chart1.axisLeft yl.typeface = tfLight yl.spaceTop = 30f yl.spaceBottom = 30f yl.setDrawZeroLine(false) - chart!!.axisRight.isEnabled = false + binding.chart1.axisRight.isEnabled = false - val xl = chart!!.xAxis + val xl = binding.chart1.xAxis xl.position = XAxis.XAxisPosition.BOTTOM xl.typeface = tfLight } override fun onProgressChanged(seekBar: SeekBar?, progress: Int, fromUser: Boolean) { - val count = seekBarX!!.progress - val range = seekBarY!!.progress + val count = binding.seekBarX.progress + val range = binding.seekBarY.progress - tvX!!.text = count.toString() - tvY!!.text = range.toString() + binding.tvXMax.text = count.toString() + binding.tvYMax.text = range.toString() val values1 = ArrayList() val values2 = ArrayList() @@ -149,8 +135,8 @@ class BubbleChartActivity : DemoBase(), OnSeekBarChangeListener, OnChartValueSel data.setValueTextColor(Color.WHITE) data.setHighlightCircleWidth(1.5f) - chart!!.setData(data) - chart?.invalidate() + binding.chart1.setData(data) + binding.chart1.invalidate() } override fun onCreateOptionsMenu(menu: Menu?): Boolean { @@ -167,62 +153,62 @@ class BubbleChartActivity : DemoBase(), OnSeekBarChangeListener, OnChartValueSel } R.id.actionToggleValues -> { - chart!!.data!!.dataSets.forEach { + binding.chart1.data!!.dataSets.forEach { it?.isDrawValues = !it.isDrawValues } - chart?.invalidate() + binding.chart1.invalidate() } R.id.actionToggleIcons -> { - for (set in chart!!.data!!.dataSets) + for (set in binding.chart1.data!!.dataSets) set?.isDrawIcons = !set.isDrawIcons - chart?.invalidate() + binding.chart1.invalidate() } R.id.actionToggleHighlight -> { - if (chart!!.data != null) { - chart!!.data!!.isHighlightEnabled = !chart!!.data!!.isHighlightEnabled() - chart?.invalidate() + if (binding.chart1.data != null) { + binding.chart1.data!!.isHighlightEnabled = !binding.chart1.data!!.isHighlightEnabled() + binding.chart1.invalidate() } } R.id.actionTogglePinch -> { - if (chart!!.isPinchZoomEnabled) chart!!.setPinchZoom(false) - else chart!!.setPinchZoom(true) + if (binding.chart1.isPinchZoomEnabled) binding.chart1.setPinchZoom(false) + else binding.chart1.setPinchZoom(true) - chart?.invalidate() + binding.chart1.invalidate() } R.id.actionToggleAutoScaleMinMax -> { - chart!!.isAutoScaleMinMaxEnabled = !chart!!.isAutoScaleMinMaxEnabled - chart?.notifyDataSetChanged() + binding.chart1.isAutoScaleMinMaxEnabled = !binding.chart1.isAutoScaleMinMaxEnabled + binding.chart1.notifyDataSetChanged() } R.id.actionSave -> { if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) { saveToGallery() } else { - requestStoragePermission(chart) + requestStoragePermission(binding.chart1) } } R.id.animateX -> { - chart!!.animateX(2000) + binding.chart1.animateX(2000) } R.id.animateY -> { - chart!!.animateY(2000) + binding.chart1.animateY(2000) } R.id.animateXY -> { - chart!!.animateXY(2000, 2000) + binding.chart1.animateXY(2000, 2000) } } return true } override fun saveToGallery() { - saveToGallery(chart, "BubbleChartActivity") + saveToGallery(binding.chart1, "BubbleChartActivity") } override fun onValueSelected(entry: Entry, highlight: Highlight) { diff --git a/app/src/main/kotlin/info/appdev/chartexample/CandleStickChartActivity.kt b/app/src/main/kotlin/info/appdev/chartexample/CandleStickChartActivity.kt index da76c3a35..deb217482 100644 --- a/app/src/main/kotlin/info/appdev/chartexample/CandleStickChartActivity.kt +++ b/app/src/main/kotlin/info/appdev/chartexample/CandleStickChartActivity.kt @@ -10,88 +10,75 @@ import android.view.Menu import android.view.MenuItem import android.widget.SeekBar import android.widget.SeekBar.OnSeekBarChangeListener -import android.widget.TextView import androidx.core.content.ContextCompat import androidx.core.content.res.ResourcesCompat import androidx.core.net.toUri -import com.github.mikephil.charting.charts.CandleStickChart import com.github.mikephil.charting.components.XAxis.XAxisPosition import com.github.mikephil.charting.components.YAxis.AxisDependency import com.github.mikephil.charting.data.CandleData import com.github.mikephil.charting.data.CandleDataSet import com.github.mikephil.charting.data.CandleEntry import info.appdev.chartexample.DataTools.Companion.getValues +import info.appdev.chartexample.databinding.ActivityCandlechartBinding import info.appdev.chartexample.notimportant.DemoBase class CandleStickChartActivity : DemoBase(), OnSeekBarChangeListener { - private var chart: CandleStickChart? = null - private var seekBarX: SeekBar? = null - private var seekBarY: SeekBar? = null - private var tvX: TextView? = null - private var tvY: TextView? = null + + private lateinit var binding: ActivityCandlechartBinding override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) - setContentView(R.layout.activity_candlechart) - - tvX = findViewById(R.id.tvXMax) - tvY = findViewById(R.id.tvYMax) - - seekBarX = findViewById(R.id.seekBarX) - seekBarX!!.setOnSeekBarChangeListener(this) - - seekBarY = findViewById(R.id.seekBarY) - seekBarY!!.setOnSeekBarChangeListener(this) - - chart = findViewById(R.id.chart1) - chart!!.setBackgroundColor(Color.WHITE) + binding = ActivityCandlechartBinding.inflate(layoutInflater) + setContentView(binding.root) - chart!!.description.isEnabled = false + binding.seekBarX.setOnSeekBarChangeListener(this) + binding.seekBarY.setOnSeekBarChangeListener(this) + binding.chart1.setBackgroundColor(Color.WHITE) + binding.chart1.description.isEnabled = false - // if more than 60 entries are displayed in the chart, no values will be - // drawn - chart!!.setMaxVisibleValueCount(60) + // if more than 60 entries are displayed in the chart, no values will be drawn + binding.chart1.setMaxVisibleValueCount(60) // scaling can now only be done on x- and y-axis separately - chart!!.setPinchZoom(false) + binding.chart1.setPinchZoom(false) - chart!!.setDrawGridBackground(false) + binding.chart1.setDrawGridBackground(false) - val xAxis = chart!!.xAxis + val xAxis = binding.chart1.xAxis xAxis.position = XAxisPosition.BOTTOM xAxis.setDrawGridLines(false) - val leftAxis = chart!!.axisLeft + val leftAxis = binding.chart1.axisLeft // leftAxis.setEnabled(false); leftAxis.setLabelCount(7, false) leftAxis.setDrawGridLines(false) leftAxis.setDrawAxisLine(false) - val rightAxis = chart!!.axisRight + val rightAxis = binding.chart1.axisRight rightAxis.isEnabled = false // rightAxis.setStartAtZero(false); // setting data - seekBarX!!.progress = 40 - seekBarY!!.progress = 100 + binding.seekBarX.progress = 40 + binding.seekBarY.progress = 100 - chart!!.legend.isEnabled = false + binding.chart1.legend.isEnabled = false } override fun onProgressChanged(seekBar: SeekBar?, progress: Int, fromUser: Boolean) { - val progress: Int = (seekBarX!!.progress) + val progress: Int = (binding.seekBarX.progress) - tvX!!.text = progress.toString() - tvY!!.text = seekBarY!!.progress.toString() + binding.tvXMax.text = progress.toString() + binding.tvYMax.text = binding.seekBarY.progress.toString() - chart!!.resetTracking() + binding.chart1.resetTracking() val values = ArrayList() val sampleValues = getValues(100) for (i in 0.. { - chart!!.data!!.dataSets.forEach { + binding.chart1.data!!.dataSets.forEach { it?.isDrawValues = !it.isDrawValues } - chart?.invalidate() + binding.chart1.invalidate() } R.id.actionToggleIcons -> { - for (set in chart!!.data!!.dataSets) + for (set in binding.chart1.data!!.dataSets) set?.isDrawIcons = !set.isDrawIcons - chart?.invalidate() + binding.chart1.invalidate() } R.id.actionToggleHighlight -> { - if (chart!!.data != null) { - chart!!.data!!.isHighlightEnabled = !chart!!.data!!.isHighlightEnabled() - chart?.invalidate() + if (binding.chart1.data != null) { + binding.chart1.data!!.isHighlightEnabled = !binding.chart1.data!!.isHighlightEnabled() + binding.chart1.invalidate() } } R.id.actionTogglePinch -> { - if (chart!!.isPinchZoomEnabled) chart!!.setPinchZoom(false) - else chart!!.setPinchZoom(true) + if (binding.chart1.isPinchZoomEnabled) binding.chart1.setPinchZoom(false) + else binding.chart1.setPinchZoom(true) - chart?.invalidate() + binding.chart1.invalidate() } R.id.actionToggleAutoScaleMinMax -> { - chart!!.isAutoScaleMinMaxEnabled = !chart!!.isAutoScaleMinMaxEnabled - chart?.notifyDataSetChanged() + binding.chart1.isAutoScaleMinMaxEnabled = !binding.chart1.isAutoScaleMinMaxEnabled + binding.chart1.notifyDataSetChanged() } R.id.actionToggleMakeShadowSameColorAsCandle -> { - for (set in chart!!.data!!.dataSets) { + for (set in binding.chart1.data!!.dataSets) { (set as CandleDataSet).shadowColorSameAsCandle = !set.shadowColorSameAsCandle } - chart?.invalidate() + binding.chart1.invalidate() } R.id.animateX -> { - chart!!.animateX(2000) + binding.chart1.animateX(2000) } R.id.animateY -> { - chart!!.animateY(2000) + binding.chart1.animateY(2000) } R.id.animateXY -> { - chart!!.animateXY(2000, 2000) + binding.chart1.animateXY(2000, 2000) } R.id.actionSave -> { if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) { saveToGallery() } else { - requestStoragePermission(chart) + requestStoragePermission(binding.chart1) } } } @@ -211,7 +198,7 @@ class CandleStickChartActivity : DemoBase(), OnSeekBarChangeListener { } override fun saveToGallery() { - saveToGallery(chart, "CandleStickChartActivity") + saveToGallery(binding.chart1, "CandleStickChartActivity") } override fun onStartTrackingTouch(seekBar: SeekBar?) = Unit diff --git a/app/src/main/kotlin/info/appdev/chartexample/CombinedChartActivity.kt b/app/src/main/kotlin/info/appdev/chartexample/CombinedChartActivity.kt index 83a5a6d27..2a49df921 100644 --- a/app/src/main/kotlin/info/appdev/chartexample/CombinedChartActivity.kt +++ b/app/src/main/kotlin/info/appdev/chartexample/CombinedChartActivity.kt @@ -6,7 +6,6 @@ import android.os.Bundle import android.view.Menu import android.view.MenuItem import androidx.core.net.toUri -import com.github.mikephil.charting.charts.CombinedChart import com.github.mikephil.charting.charts.CombinedChart.DrawOrder import com.github.mikephil.charting.components.AxisBase import com.github.mikephil.charting.components.Legend @@ -30,48 +29,50 @@ import com.github.mikephil.charting.data.ScatterDataSet import com.github.mikephil.charting.formatter.IAxisValueFormatter import com.github.mikephil.charting.utils.ColorTemplate import info.appdev.chartexample.DataTools.Companion.getValues +import info.appdev.chartexample.databinding.ActivityCombinedBinding import info.appdev.chartexample.notimportant.DemoBase import kotlin.math.roundToInt class CombinedChartActivity : DemoBase() { - private var chart: CombinedChart? = null private val sampleCount = 12 var values: Array = getValues(sampleCount * 2) + private lateinit var binding: ActivityCombinedBinding + override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) - setContentView(R.layout.activity_combined) + binding = ActivityCombinedBinding.inflate(layoutInflater) + setContentView(binding.root) - chart = findViewById(R.id.chart1) - chart!!.description.isEnabled = false - chart!!.setBackgroundColor(Color.WHITE) - chart!!.setDrawGridBackground(false) - chart!!.setDrawBarShadow(false) - chart!!.isHighlightFullBarEnabled = false + binding.chart1.description.isEnabled = false + binding.chart1.setBackgroundColor(Color.WHITE) + binding.chart1.setDrawGridBackground(false) + binding.chart1.setDrawBarShadow(false) + binding.chart1.isHighlightFullBarEnabled = false // draw bars behind lines - chart!!.setDrawOrder( + binding.chart1.setDrawOrder( arrayOf( DrawOrder.BAR, DrawOrder.BUBBLE, DrawOrder.CANDLE, DrawOrder.LINE, DrawOrder.SCATTER ) ) - val l = chart!!.legend + val l = binding.chart1.legend l.isWordWrapEnabled = true l.verticalAlignment = Legend.LegendVerticalAlignment.BOTTOM l.horizontalAlignment = Legend.LegendHorizontalAlignment.CENTER l.orientation = Legend.LegendOrientation.HORIZONTAL l.setDrawInside(false) - val rightAxis = chart!!.axisRight + val rightAxis = binding.chart1.axisRight rightAxis.setDrawGridLines(false) rightAxis.axisMinimum = 0f // this replaces setStartAtZero(true) - val leftAxis = chart!!.axisLeft + val leftAxis = binding.chart1.axisLeft leftAxis.setDrawGridLines(false) leftAxis.axisMinimum = 0f // this replaces setStartAtZero(true) - val xAxis = chart!!.xAxis + val xAxis = binding.chart1.xAxis xAxis.position = XAxisPosition.BOTH_SIDED xAxis.axisMinimum = 0f xAxis.granularity = 1f @@ -92,8 +93,8 @@ class CombinedChartActivity : DemoBase() { xAxis.axisMaximum = data.xMax + 0.25f - chart!!.setData(data) - chart?.invalidate() + binding.chart1.setData(data) + binding.chart1.invalidate() } private fun generateLineData(): LineData { @@ -238,27 +239,27 @@ class CombinedChartActivity : DemoBase() { } R.id.actionToggleLineValues -> { - chart!!.data!!.dataSets.forEach { + binding.chart1.data!!.dataSets.forEach { if (it is LineDataSet) it.isDrawValues = !it.isDrawValues } - chart?.invalidate() + binding.chart1.invalidate() } R.id.actionToggleBarValues -> { - chart!!.data!!.dataSets.forEach { + binding.chart1.data!!.dataSets.forEach { if (it is BarDataSet) it.isDrawValues = !it.isDrawValues } - chart?.invalidate() + binding.chart1.invalidate() } R.id.actionRemoveDataSet -> { - val rnd = values[sampleCount]!!.toFloat().toInt() * chart!!.data!!.getDataSetCount() - chart!!.data!!.removeDataSet(chart!!.data!!.getDataSetByIndex(rnd)) - chart!!.data!!.notifyDataChanged() - chart?.notifyDataSetChanged() - chart?.invalidate() + val rnd = values[sampleCount]!!.toFloat().toInt() * binding.chart1.data!!.getDataSetCount() + binding.chart1.data!!.removeDataSet(binding.chart1.data!!.getDataSetByIndex(rnd)) + binding.chart1.data!!.notifyDataChanged() + binding.chart1.notifyDataSetChanged() + binding.chart1.invalidate() } } return true diff --git a/app/src/main/kotlin/info/appdev/chartexample/CubicLineChartActivity.kt b/app/src/main/kotlin/info/appdev/chartexample/CubicLineChartActivity.kt index b5e5f96d2..5c14469db 100644 --- a/app/src/main/kotlin/info/appdev/chartexample/CubicLineChartActivity.kt +++ b/app/src/main/kotlin/info/appdev/chartexample/CubicLineChartActivity.kt @@ -10,7 +10,6 @@ import android.view.Menu import android.view.MenuItem import android.widget.SeekBar import android.widget.SeekBar.OnSeekBarChangeListener -import android.widget.TextView import androidx.core.content.ContextCompat import androidx.core.net.toUri import com.github.mikephil.charting.components.YAxis @@ -25,19 +24,13 @@ import info.appdev.chartexample.databinding.ActivityLinechartBinding import info.appdev.chartexample.notimportant.DemoBase class CubicLineChartActivity : DemoBase(), OnSeekBarChangeListener { - private var tvX: TextView? = null - private var tvY: TextView? = null private lateinit var binding: ActivityLinechartBinding override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) binding = ActivityLinechartBinding.inflate(layoutInflater) - val view = binding.root - setContentView(view) - - tvX = findViewById(R.id.tvXMax) - tvY = findViewById(R.id.tvYMax) + setContentView(binding.root) binding.chart1.setViewPortOffsets(0f, 0f, 0f, 0f) binding.chart1.setBackgroundColor(Color.rgb(104, 241, 175)) @@ -268,8 +261,8 @@ class CubicLineChartActivity : DemoBase(), OnSeekBarChangeListener { @SuppressLint("SetTextI18n") override fun onProgressChanged(seekBar: SeekBar, progress: Int, fromUser: Boolean) { - tvX!!.text = binding.seekBarX.progress.toString() - tvY!!.text = binding.seekBarY.progress.toString() + binding.tvXMax.text = binding.seekBarX.progress.toString() + binding.tvYMax.text = binding.seekBarY.progress.toString() setData(binding.seekBarX.progress, binding.seekBarY.progress.toFloat()) diff --git a/app/src/main/kotlin/info/appdev/chartexample/DrawChartActivity.kt b/app/src/main/kotlin/info/appdev/chartexample/DrawChartActivity.kt index c4b0d3020..87cca6905 100644 --- a/app/src/main/kotlin/info/appdev/chartexample/DrawChartActivity.kt +++ b/app/src/main/kotlin/info/appdev/chartexample/DrawChartActivity.kt @@ -28,8 +28,7 @@ class DrawChartActivity : DemoBase(), OnChartValueSelectedListener, OnDrawListen override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) binding = ActivityDrawChartBinding.inflate(layoutInflater) - val view = binding.root - setContentView(view) + setContentView(binding.root) // listener for selecting and drawing binding.chart1.setOnChartValueSelectedListener(this) diff --git a/app/src/main/kotlin/info/appdev/chartexample/DynamicalAddingActivity.kt b/app/src/main/kotlin/info/appdev/chartexample/DynamicalAddingActivity.kt index 7ac0a78e2..8c1c3fca7 100644 --- a/app/src/main/kotlin/info/appdev/chartexample/DynamicalAddingActivity.kt +++ b/app/src/main/kotlin/info/appdev/chartexample/DynamicalAddingActivity.kt @@ -9,7 +9,6 @@ import android.view.Menu import android.view.MenuItem import android.widget.Toast import androidx.core.content.ContextCompat -import com.github.mikephil.charting.charts.LineChart import com.github.mikephil.charting.components.YAxis.AxisDependency import com.github.mikephil.charting.data.Entry import com.github.mikephil.charting.data.LineData @@ -20,34 +19,36 @@ import com.github.mikephil.charting.utils.ColorTemplate import info.appdev.chartexample.DataTools.Companion.getValues import info.appdev.chartexample.notimportant.DemoBase import androidx.core.net.toUri +import info.appdev.chartexample.databinding.ActivityLinechartNoseekbarBinding class DynamicalAddingActivity : DemoBase(), OnChartValueSelectedListener { - private var chart: LineChart? = null var sampleValues: Array = getValues(102) + private lateinit var binding: ActivityLinechartNoseekbarBinding + override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) - setContentView(R.layout.activity_linechart_noseekbar) + binding = ActivityLinechartNoseekbarBinding.inflate(layoutInflater) + setContentView(binding.root) - chart = findViewById(R.id.chart1) - chart!!.setOnChartValueSelectedListener(this) - chart!!.setDrawGridBackground(false) - chart!!.description.isEnabled = false - chart!!.setNoDataText("No chart data available. Use the menu to add entries and data sets!") + binding.chart1.setOnChartValueSelectedListener(this) + binding.chart1.setDrawGridBackground(false) + binding.chart1.description.isEnabled = false + binding.chart1.setNoDataText("No chart data available. Use the menu to add entries and data sets!") // chart.getXAxis().setDrawLabels(false); // chart.getXAxis().setDrawGridLines(false); - chart?.invalidate() + binding.chart1.invalidate() } private val colors: IntArray = ColorTemplate.VORDIPLOM_COLORS private fun addEntry() { - var data = chart!!.data + var data = binding.chart1.data if (data == null) { data = LineData() - chart!!.setData(data) + binding.chart1.setData(data) } var set = data.getDataSetByIndex(0) @@ -69,18 +70,18 @@ class DynamicalAddingActivity : DemoBase(), OnChartValueSelectedListener { data.notifyDataChanged() // let the chart know it's data has changed - chart?.notifyDataSetChanged() + binding.chart1.notifyDataSetChanged() - chart!!.setVisibleXRangeMaximum(6f) + binding.chart1.setVisibleXRangeMaximum(6f) //chart.setVisibleYRangeMaximum(15, AxisDependency.LEFT); // // this automatically refreshes the chart (calls invalidate()) - chart!!.moveViewTo((data.getEntryCount() - 7).toFloat(), 50f, AxisDependency.LEFT) + binding.chart1.moveViewTo((data.getEntryCount() - 7).toFloat(), 50f, AxisDependency.LEFT) } } private fun removeLastEntry() { - val data = chart!!.data + val data = binding.chart1.data if (data != null) { val set = data.getDataSetByIndex(0) @@ -92,17 +93,17 @@ class DynamicalAddingActivity : DemoBase(), OnChartValueSelectedListener { // or remove by index // mData.removeEntryByXValue(xIndex, dataSetIndex); data.notifyDataChanged() - chart?.notifyDataSetChanged() - chart?.invalidate() + binding.chart1.notifyDataSetChanged() + binding.chart1.invalidate() } } } private fun addDataSet() { - val data = chart!!.data + val data = binding.chart1.data if (data == null) { - chart!!.setData(LineData()) + binding.chart1.setData(LineData()) } else { val count = (data.getDataSetCount() + 1) val amount = data.getDataSetByIndex(0)?.entryCount ?: 0 @@ -129,19 +130,19 @@ class DynamicalAddingActivity : DemoBase(), OnChartValueSelectedListener { data.addDataSet(set) data.notifyDataChanged() - chart?.notifyDataSetChanged() - chart?.invalidate() + binding.chart1.notifyDataSetChanged() + binding.chart1.invalidate() } } private fun removeDataSet() { - val data = chart!!.data + val data = binding.chart1.data if (data != null) { data.removeDataSet(data.getDataSetByIndex(data.getDataSetCount() - 1)) - chart?.notifyDataSetChanged() - chart?.invalidate() + binding.chart1.notifyDataSetChanged() + binding.chart1.invalidate() } } @@ -199,7 +200,7 @@ class DynamicalAddingActivity : DemoBase(), OnChartValueSelectedListener { } R.id.actionClear -> { - chart!!.clear() + binding.chart1.clear() Toast.makeText(this, "Chart cleared!", Toast.LENGTH_SHORT).show() } @@ -207,7 +208,7 @@ class DynamicalAddingActivity : DemoBase(), OnChartValueSelectedListener { if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) { saveToGallery() } else { - requestStoragePermission(chart) + requestStoragePermission(binding.chart1) } } } @@ -216,6 +217,6 @@ class DynamicalAddingActivity : DemoBase(), OnChartValueSelectedListener { } override fun saveToGallery() { - saveToGallery(chart, "DynamicalAddingActivity") + saveToGallery(binding.chart1, "DynamicalAddingActivity") } } diff --git a/app/src/main/kotlin/info/appdev/chartexample/FilledLineActivity.kt b/app/src/main/kotlin/info/appdev/chartexample/FilledLineActivity.kt index f51b204c8..44fbae8e1 100644 --- a/app/src/main/kotlin/info/appdev/chartexample/FilledLineActivity.kt +++ b/app/src/main/kotlin/info/appdev/chartexample/FilledLineActivity.kt @@ -32,8 +32,7 @@ class FilledLineActivity : DemoBase() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) binding = ActivityLinechartNoseekbarBinding.inflate(layoutInflater) - val view = binding.root - setContentView(view) + setContentView(binding.root) binding.chart1.setBackgroundColor(Color.WHITE) binding.chart1.setGridBackgroundColor(fillColor) diff --git a/app/src/main/kotlin/info/appdev/chartexample/HalfPieChartActivity.kt b/app/src/main/kotlin/info/appdev/chartexample/HalfPieChartActivity.kt index b85c39849..8090747bd 100644 --- a/app/src/main/kotlin/info/appdev/chartexample/HalfPieChartActivity.kt +++ b/app/src/main/kotlin/info/appdev/chartexample/HalfPieChartActivity.kt @@ -14,7 +14,6 @@ import android.widget.RelativeLayout import androidx.core.net.toUri import androidx.window.layout.WindowMetricsCalculator import com.github.mikephil.charting.animation.Easing -import com.github.mikephil.charting.charts.PieChart import com.github.mikephil.charting.components.Legend import com.github.mikephil.charting.data.PieData import com.github.mikephil.charting.data.PieDataSet @@ -22,49 +21,51 @@ import com.github.mikephil.charting.data.PieEntry import com.github.mikephil.charting.formatter.PercentFormatter import com.github.mikephil.charting.utils.ColorTemplate import info.appdev.chartexample.DataTools.Companion.getValues +import info.appdev.chartexample.databinding.ActivityPiechartHalfBinding import info.appdev.chartexample.notimportant.DemoBase class HalfPieChartActivity : DemoBase() { - private var chart: PieChart? = null + + private lateinit var binding: ActivityPiechartHalfBinding override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) - setContentView(R.layout.activity_piechart_half) + binding = ActivityPiechartHalfBinding.inflate(layoutInflater) + setContentView(binding.root) - chart = findViewById(R.id.chart1) - chart!!.setBackgroundColor(Color.WHITE) + binding.chart1.setBackgroundColor(Color.WHITE) moveOffScreen() - chart!!.setUsePercentValues(true) - chart!!.description.isEnabled = false + binding.chart1.setUsePercentValues(true) + binding.chart1.description.isEnabled = false - chart!!.setCenterTextTypeface(tfLight) - chart!!.centerText = generateCenterSpannableText() + binding.chart1.setCenterTextTypeface(tfLight) + binding.chart1.centerText = generateCenterSpannableText() - chart!!.isDrawHoleEnabled = true - chart!!.setHoleColor(Color.WHITE) + binding.chart1.isDrawHoleEnabled = true + binding.chart1.setHoleColor(Color.WHITE) - chart!!.setTransparentCircleColor(Color.WHITE) - chart!!.setTransparentCircleAlpha(110) + binding.chart1.setTransparentCircleColor(Color.WHITE) + binding.chart1.setTransparentCircleAlpha(110) - chart!!.holeRadius = 58f - chart!!.transparentCircleRadius = 61f + binding.chart1.holeRadius = 58f + binding.chart1.transparentCircleRadius = 61f - chart!!.setDrawCenterText(true) + binding.chart1.setDrawCenterText(true) - chart!!.isRotationEnabled = false - chart!!.isHighlightPerTapEnabled = true + binding.chart1.isRotationEnabled = false + binding.chart1.isHighlightPerTapEnabled = true - chart!!.setMaxAngle(180f) // HALF CHART - chart!!.rotationAngle = 180f - chart!!.setCenterTextOffset(0f, -20f) + binding.chart1.setMaxAngle(180f) // HALF CHART + binding.chart1.rotationAngle = 180f + binding.chart1.setCenterTextOffset(0f, -20f) setData(100f) - chart!!.animateY(1400, Easing.easeInOutQuad) + binding.chart1.animateY(1400, Easing.easeInOutQuad) - val l = chart!!.legend + val l = binding.chart1.legend l.verticalAlignment = Legend.LegendVerticalAlignment.TOP l.horizontalAlignment = Legend.LegendHorizontalAlignment.CENTER l.orientation = Legend.LegendOrientation.HORIZONTAL @@ -74,9 +75,9 @@ class HalfPieChartActivity : DemoBase() { l.yOffset = 0f // entry label styling - chart!!.setEntryLabelColor(Color.WHITE) - chart!!.setEntryLabelTypeface(tfRegular) - chart!!.setEntryLabelTextSize(12f) + binding.chart1.setEntryLabelColor(Color.WHITE) + binding.chart1.setEntryLabelTypeface(tfRegular) + binding.chart1.setEntryLabelTextSize(12f) } private fun setData(range: Float) { @@ -100,9 +101,9 @@ class HalfPieChartActivity : DemoBase() { data.setValueTextSize(11f) data.setValueTextColor(Color.WHITE) data.setValueTypeface(tfLight) - chart!!.setData(data) + binding.chart1.setData(data) - chart?.invalidate() + binding.chart1.invalidate() } private fun generateCenterSpannableText(): SpannableString { @@ -122,9 +123,9 @@ class HalfPieChartActivity : DemoBase() { val offset = (height * 0.65).toInt() /* percent to move */ - val rlParams = chart!!.layoutParams as RelativeLayout.LayoutParams + val rlParams = binding.chart1.layoutParams as RelativeLayout.LayoutParams rlParams.setMargins(0, 0, 0, -offset) - chart!!.layoutParams = rlParams + binding.chart1.layoutParams = rlParams } override fun onCreateOptionsMenu(menu: Menu?): Boolean { diff --git a/app/src/main/kotlin/info/appdev/chartexample/HorizontalBarChartActivity.kt b/app/src/main/kotlin/info/appdev/chartexample/HorizontalBarChartActivity.kt index 9865a3ffc..21d513283 100644 --- a/app/src/main/kotlin/info/appdev/chartexample/HorizontalBarChartActivity.kt +++ b/app/src/main/kotlin/info/appdev/chartexample/HorizontalBarChartActivity.kt @@ -34,8 +34,7 @@ class HorizontalBarChartActivity : DemoBase(), OnSeekBarChangeListener, OnChartV override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) binding = ActivityHorizontalbarchartBinding.inflate(layoutInflater) - val view = binding.root - setContentView(view) + setContentView(binding.root) binding.seekBarY.setOnSeekBarChangeListener(this) binding.seekBarX.setOnSeekBarChangeListener(this) diff --git a/app/src/main/kotlin/info/appdev/chartexample/HorizontalBarNegativeChartActivity.kt b/app/src/main/kotlin/info/appdev/chartexample/HorizontalBarNegativeChartActivity.kt index 0b6171ddc..5fc79bd0b 100644 --- a/app/src/main/kotlin/info/appdev/chartexample/HorizontalBarNegativeChartActivity.kt +++ b/app/src/main/kotlin/info/appdev/chartexample/HorizontalBarNegativeChartActivity.kt @@ -9,11 +9,9 @@ import android.view.Menu import android.view.MenuItem import android.widget.SeekBar import android.widget.SeekBar.OnSeekBarChangeListener -import android.widget.TextView import androidx.core.content.ContextCompat import androidx.core.content.res.ResourcesCompat import androidx.core.net.toUri -import com.github.mikephil.charting.charts.HorizontalBarChart import com.github.mikephil.charting.components.Legend import com.github.mikephil.charting.components.XAxis.XAxisPosition import com.github.mikephil.charting.data.BarData @@ -25,77 +23,68 @@ import com.github.mikephil.charting.interfaces.datasets.IBarDataSet import com.github.mikephil.charting.listener.OnChartValueSelectedListener import com.github.mikephil.charting.utils.MPPointF import info.appdev.chartexample.DataTools.Companion.getValues +import info.appdev.chartexample.databinding.ActivityHorizontalbarchartBinding import info.appdev.chartexample.notimportant.DemoBase import timber.log.Timber class HorizontalBarNegativeChartActivity : DemoBase(), OnSeekBarChangeListener, OnChartValueSelectedListener { - private var chart: HorizontalBarChart? = null - private var seekBarX: SeekBar? = null - private var seekBarY: SeekBar? = null - private var tvX: TextView? = null - private var tvY: TextView? = null + + private lateinit var binding: ActivityHorizontalbarchartBinding override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) - setContentView(R.layout.activity_horizontalbarchart) - - tvX = findViewById(R.id.tvXMax) - tvY = findViewById(R.id.tvYMax) + binding = ActivityHorizontalbarchartBinding.inflate(layoutInflater) + setContentView(binding.root) - seekBarX = findViewById(R.id.seekBarX) - seekBarY = findViewById(R.id.seekBarY) + binding.seekBarY.setOnSeekBarChangeListener(this) + binding.seekBarX.setOnSeekBarChangeListener(this) - seekBarY!!.setOnSeekBarChangeListener(this) - seekBarX!!.setOnSeekBarChangeListener(this) - - chart = findViewById(R.id.chart1) - chart!!.setOnChartValueSelectedListener(this) + binding.chart1.setOnChartValueSelectedListener(this) // chart.setHighlightEnabled(false); - chart!!.setDrawBarShadow(false) - - chart!!.setDrawValueAboveBar(true) + binding.chart1.setDrawBarShadow(false) + binding.chart1.setDrawValueAboveBar(true) - chart!!.description.isEnabled = false + binding.chart1.description.isEnabled = false // if more than 60 entries are displayed in the chart, no values will be // drawn - chart!!.setMaxVisibleValueCount(60) + binding.chart1.setMaxVisibleValueCount(60) // scaling can now only be done on x- and y-axis separately - chart!!.setPinchZoom(false) + binding.chart1.setPinchZoom(false) // draw shadows for each bar that show the maximum value // chart.setDrawBarShadow(true); - chart!!.setDrawGridBackground(false) + binding.chart1.setDrawGridBackground(false) - val xl = chart!!.xAxis + val xl = binding.chart1.xAxis xl.position = XAxisPosition.BOTTOM xl.typeface = tfLight xl.setDrawAxisLine(true) xl.setDrawGridLines(false) xl.granularity = 10f - val yl = chart!!.axisLeft + val yl = binding.chart1.axisLeft yl.typeface = tfLight yl.setDrawAxisLine(true) yl.setDrawGridLines(true) // yl.setInverted(true); - val yr = chart!!.axisRight + val yr = binding.chart1.axisRight yr.typeface = tfLight yr.setDrawAxisLine(true) yr.setDrawGridLines(false) // yr.setInverted(true); - chart!!.setFitBars(true) - chart!!.animateY(2500) + binding.chart1.setFitBars(true) + binding.chart1.animateY(2500) // setting data - seekBarY!!.progress = 50 - seekBarX!!.progress = 12 + binding.seekBarY.progress = 50 + binding.seekBarX.progress = 12 - val l = chart!!.legend + val l = binding.chart1.legend l.verticalAlignment = Legend.LegendVerticalAlignment.BOTTOM l.horizontalAlignment = Legend.LegendHorizontalAlignment.LEFT l.orientation = Legend.LegendOrientation.HORIZONTAL @@ -122,13 +111,13 @@ class HorizontalBarNegativeChartActivity : DemoBase(), OnSeekBarChangeListener, val set1: BarDataSet - if (chart!!.data != null && - chart!!.data!!.getDataSetCount() > 0 + if (binding.chart1.data != null && + binding.chart1.data!!.getDataSetCount() > 0 ) { - set1 = chart!!.data!!.getDataSetByIndex(0) as BarDataSet + set1 = binding.chart1.data!!.getDataSetByIndex(0) as BarDataSet set1.entries = values - chart!!.data!!.notifyDataChanged() - chart?.notifyDataSetChanged() + binding.chart1.data!!.notifyDataChanged() + binding.chart1.notifyDataSetChanged() } else { set1 = BarDataSet(values, "DataSet 1") @@ -141,7 +130,7 @@ class HorizontalBarNegativeChartActivity : DemoBase(), OnSeekBarChangeListener, data.setValueTextSize(10f) data.setValueTypeface(tfLight) data.barWidth = barWidth - chart!!.setData(data) + binding.chart1.setData(data) } } @@ -160,61 +149,61 @@ class HorizontalBarNegativeChartActivity : DemoBase(), OnSeekBarChangeListener, } R.id.actionToggleValues -> { - chart!!.data!!.dataSets.forEach { + binding.chart1.data!!.dataSets.forEach { it?.isDrawValues = !it.isDrawValues } - chart?.invalidate() + binding.chart1.invalidate() } R.id.actionToggleIcons -> { - chart!!.data!!.dataSets.forEach { iSet -> + binding.chart1.data!!.dataSets.forEach { iSet -> iSet.isDrawIcons = !iSet.isDrawIcons } - chart?.invalidate() + binding.chart1.invalidate() } R.id.actionToggleHighlight -> { - if (chart!!.data != null) { - chart!!.data!!.isHighlightEnabled = !chart!!.data!!.isHighlightEnabled() - chart?.invalidate() + if (binding.chart1.data != null) { + binding.chart1.data!!.isHighlightEnabled = !binding.chart1.data!!.isHighlightEnabled() + binding.chart1.invalidate() } } R.id.actionTogglePinch -> { - chart!!.setPinchZoom(!chart!!.isPinchZoomEnabled) + binding.chart1.setPinchZoom(!binding.chart1.isPinchZoomEnabled) - chart?.invalidate() + binding.chart1.invalidate() } R.id.actionToggleAutoScaleMinMax -> { - chart!!.isAutoScaleMinMaxEnabled = !chart!!.isAutoScaleMinMaxEnabled - chart?.notifyDataSetChanged() + binding.chart1.isAutoScaleMinMaxEnabled = !binding.chart1.isAutoScaleMinMaxEnabled + binding.chart1.notifyDataSetChanged() } R.id.actionToggleBarBorders -> { - for (set in chart!!.data!!.dataSets) (set as BarDataSet).barBorderWidth = if (set.barBorderWidth == 1f) 0f else 1f + for (set in binding.chart1.data!!.dataSets) (set as BarDataSet).barBorderWidth = if (set.barBorderWidth == 1f) 0f else 1f - chart?.invalidate() + binding.chart1.invalidate() } R.id.animateX -> { - chart!!.animateX(2000) + binding.chart1.animateX(2000) } R.id.animateY -> { - chart!!.animateY(2000) + binding.chart1.animateY(2000) } R.id.animateXY -> { - chart!!.animateXY(2000, 2000) + binding.chart1.animateXY(2000, 2000) } R.id.actionSave -> { if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) { saveToGallery() } else { - requestStoragePermission(chart) + requestStoragePermission(binding.chart1) } } } @@ -222,16 +211,16 @@ class HorizontalBarNegativeChartActivity : DemoBase(), OnSeekBarChangeListener, } override fun onProgressChanged(seekBar: SeekBar?, progress: Int, fromUser: Boolean) { - tvX!!.text = seekBarX!!.progress.toString() - tvY!!.text = seekBarY!!.progress.toString() + binding.tvXMax.text = binding.seekBarX.progress.toString() + binding.tvYMax.text = binding.seekBarY.progress.toString() - setData(seekBarX!!.progress, seekBarY!!.progress.toFloat()) - chart!!.setFitBars(true) - chart?.invalidate() + setData(binding.seekBarX.progress, binding.seekBarY.progress.toFloat()) + binding.chart1.setFitBars(true) + binding.chart1.invalidate() } override fun saveToGallery() { - saveToGallery(chart, "HorizontalBarChartActivity") + saveToGallery(binding.chart1, "HorizontalBarChartActivity") } override fun onStartTrackingTouch(seekBar: SeekBar?) = Unit @@ -242,10 +231,10 @@ class HorizontalBarNegativeChartActivity : DemoBase(), OnSeekBarChangeListener, override fun onValueSelected(entry: Entry, highlight: Highlight) { val bounds = mOnValueSelectedRectF - chart!!.getBarBounds(entry as BarEntry, bounds) + binding.chart1.getBarBounds(entry as BarEntry, bounds) - val position = chart!!.getPosition( - entry, chart!!.data!!.getDataSetByIndex(highlight.dataSetIndex) + val position = binding.chart1.getPosition( + entry, binding.chart1.data!!.getDataSetByIndex(highlight.dataSetIndex) .axisDependency ) diff --git a/app/src/main/kotlin/info/appdev/chartexample/InvertedLineChartActivity.kt b/app/src/main/kotlin/info/appdev/chartexample/InvertedLineChartActivity.kt index 72635d44b..17c11803a 100644 --- a/app/src/main/kotlin/info/appdev/chartexample/InvertedLineChartActivity.kt +++ b/app/src/main/kotlin/info/appdev/chartexample/InvertedLineChartActivity.kt @@ -8,10 +8,8 @@ import android.view.Menu import android.view.MenuItem import android.widget.SeekBar import android.widget.SeekBar.OnSeekBarChangeListener -import android.widget.TextView import androidx.core.content.ContextCompat import androidx.core.net.toUri -import com.github.mikephil.charting.charts.LineChart import com.github.mikephil.charting.components.Legend.LegendForm import com.github.mikephil.charting.data.Entry import com.github.mikephil.charting.data.LineData @@ -21,46 +19,38 @@ import com.github.mikephil.charting.listener.OnChartValueSelectedListener import com.github.mikephil.charting.utils.EntryXComparator import info.appdev.chartexample.DataTools.Companion.getValues import info.appdev.chartexample.custom.MyMarkerView +import info.appdev.chartexample.databinding.ActivityLinechartBinding import info.appdev.chartexample.notimportant.DemoBase import timber.log.Timber import java.util.Collections class InvertedLineChartActivity : DemoBase(), OnSeekBarChangeListener, OnChartValueSelectedListener { - private var chart: LineChart? = null - private var seekBarX: SeekBar? = null - private var seekBarY: SeekBar? = null - private var tvX: TextView? = null - private var tvY: TextView? = null + + private lateinit var binding: ActivityLinechartBinding override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) - setContentView(R.layout.activity_linechart) - - tvX = findViewById(R.id.tvXMax) - tvY = findViewById(R.id.tvYMax) - - seekBarX = findViewById(R.id.seekBarX) - seekBarY = findViewById(R.id.seekBarY) + binding = ActivityLinechartBinding.inflate(layoutInflater) + setContentView(binding.root) - seekBarY!!.setOnSeekBarChangeListener(this) - seekBarX!!.setOnSeekBarChangeListener(this) + binding.seekBarY.setOnSeekBarChangeListener(this) + binding.seekBarX.setOnSeekBarChangeListener(this) - chart = findViewById(R.id.chart1) - chart!!.setOnChartValueSelectedListener(this) - chart!!.setDrawGridBackground(false) + binding.chart1.setOnChartValueSelectedListener(this) + binding.chart1.setDrawGridBackground(false) // no description text - chart!!.description.isEnabled = false + binding.chart1.description.isEnabled = false // enable touch gestures - chart!!.setTouchEnabled(true) + binding.chart1.setTouchEnabled(true) // enable scaling and dragging - chart!!.setDragEnabled(true) - chart!!.setScaleEnabled(true) + binding.chart1.setDragEnabled(true) + binding.chart1.setScaleEnabled(true) // if disabled, scaling can be done on x- and y-axis separately - chart!!.setPinchZoom(true) + binding.chart1.setPinchZoom(true) // set an alternative background color // chart.setBackgroundColor(Color.GRAY); @@ -68,23 +58,23 @@ class InvertedLineChartActivity : DemoBase(), OnSeekBarChangeListener, OnChartVa // create a custom MarkerView (extend MarkerView) and specify the layout // to use for it val mv = MyMarkerView(this, R.layout.custom_marker_view) - mv.chartView = chart // For bounds control - chart!!.setMarker(mv) // Set the marker to the chart + mv.chartView = binding.chart1 // For bounds control + binding.chart1.setMarker(mv) // Set the marker to the chart - val xl = chart!!.xAxis + val xl = binding.chart1.xAxis xl.setAvoidFirstLastClipping(true) xl.axisMinimum = 0f - val leftAxis = chart!!.axisLeft + val leftAxis = binding.chart1.axisLeft leftAxis.isInverted = true leftAxis.axisMinimum = 0f // this replaces setStartAtZero(true) - val rightAxis = chart!!.axisRight + val rightAxis = binding.chart1.axisRight rightAxis.isEnabled = false // add data - seekBarX!!.progress = 25 - seekBarY!!.progress = 50 + binding.seekBarX.progress = 25 + binding.seekBarY.progress = 50 // // restrain the maximum scale-out factor // chart.setScaleMinima(3f, 3f); @@ -93,13 +83,13 @@ class InvertedLineChartActivity : DemoBase(), OnSeekBarChangeListener, OnChartVa // chart.centerViewPort(10, 50); // get the legend (only possible after setting data) - val l = chart!!.legend + val l = binding.chart1.legend // modify the legend ... l.form = LegendForm.LINE // don't forget to refresh the drawing - chart?.invalidate() + binding.chart1.invalidate() } private fun setData(count: Int, range: Float) { @@ -125,7 +115,7 @@ class InvertedLineChartActivity : DemoBase(), OnSeekBarChangeListener, OnChartVa val data = LineData(set1) // set data - chart!!.setData(data) + binding.chart1.setData(data) } override fun onCreateOptionsMenu(menu: Menu?): Boolean { @@ -143,64 +133,64 @@ class InvertedLineChartActivity : DemoBase(), OnSeekBarChangeListener, OnChartVa } R.id.actionToggleValues -> { - chart!!.data!!.dataSets.forEach { + binding.chart1.data!!.dataSets.forEach { it?.isDrawValues = !it.isDrawValues } - chart?.invalidate() + binding.chart1.invalidate() } R.id.actionToggleHighlight -> { - if (chart!!.data != null) { - chart!!.data!!.isHighlightEnabled = !chart!!.data!!.isHighlightEnabled() - chart?.invalidate() + if (binding.chart1.data != null) { + binding.chart1.data!!.isHighlightEnabled = !binding.chart1.data!!.isHighlightEnabled() + binding.chart1.invalidate() } } R.id.actionToggleFilled -> { - chart!!.data!!.dataSets.forEach { set -> + binding.chart1.data!!.dataSets.forEach { set -> set?.setDrawFilled(!set.isDrawFilledEnabled) } - chart?.invalidate() + binding.chart1.invalidate() } R.id.actionToggleCircles -> { - val sets = chart!!.data!!.dataSets + val sets = binding.chart1.data!!.dataSets for (iSet in sets) { val set = iSet as LineDataSet set.isDrawCirclesEnabled = !set.isDrawCirclesEnabled } - chart?.invalidate() + binding.chart1.invalidate() } R.id.animateX -> { - chart!!.animateX(2000) + binding.chart1.animateX(2000) } R.id.animateY -> { - chart!!.animateY(2000) + binding.chart1.animateY(2000) } R.id.animateXY -> { - chart!!.animateXY(2000, 2000) + binding.chart1.animateXY(2000, 2000) } R.id.actionTogglePinch -> { - chart!!.setPinchZoom(!chart!!.isPinchZoomEnabled) + binding.chart1.setPinchZoom(!binding.chart1.isPinchZoomEnabled) - chart?.invalidate() + binding.chart1.invalidate() } R.id.actionToggleAutoScaleMinMax -> { - chart!!.isAutoScaleMinMaxEnabled = !chart!!.isAutoScaleMinMaxEnabled - chart?.notifyDataSetChanged() + binding.chart1.isAutoScaleMinMaxEnabled = !binding.chart1.isAutoScaleMinMaxEnabled + binding.chart1.notifyDataSetChanged() } R.id.actionSave -> { if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) { saveToGallery() } else { - requestStoragePermission(chart) + requestStoragePermission(binding.chart1) } } } @@ -208,17 +198,17 @@ class InvertedLineChartActivity : DemoBase(), OnSeekBarChangeListener, OnChartVa } override fun onProgressChanged(seekBar: SeekBar?, progress: Int, fromUser: Boolean) { - tvX!!.text = seekBarX!!.progress.toString() - tvY!!.text = seekBarY!!.progress.toString() + binding.tvXMax.text = binding.seekBarX.progress.toString() + binding.tvYMax.text = binding.seekBarY.progress.toString() - setData(seekBarX!!.progress, seekBarY!!.progress.toFloat()) + setData(binding.seekBarX.progress, binding.seekBarY.progress.toFloat()) // redraw - chart?.invalidate() + binding.chart1.invalidate() } override fun saveToGallery() { - saveToGallery(chart, "InvertedLineChartActivity") + saveToGallery(binding.chart1, "InvertedLineChartActivity") } override fun onValueSelected(entry: Entry, highlight: Highlight) { diff --git a/app/src/main/kotlin/info/appdev/chartexample/LineChartActivity.kt b/app/src/main/kotlin/info/appdev/chartexample/LineChartActivity.kt index 8cfaef382..a5d9a38b5 100644 --- a/app/src/main/kotlin/info/appdev/chartexample/LineChartActivity.kt +++ b/app/src/main/kotlin/info/appdev/chartexample/LineChartActivity.kt @@ -37,6 +37,7 @@ class LineChartActivity : DemoBase(), OnSeekBarChangeListener, OnChartValueSelec super.onCreate(savedInstanceState) binding = ActivityLinechartBinding.inflate(layoutInflater) setContentView(binding.root) + binding.seekBarX.setOnSeekBarChangeListener(this) binding.seekBarY.max = 180 binding.seekBarY.setOnSeekBarChangeListener(this) diff --git a/app/src/main/kotlin/info/appdev/chartexample/LineChartDualAxisActivity.kt b/app/src/main/kotlin/info/appdev/chartexample/LineChartDualAxisActivity.kt index 2a161d1e6..1ea907a96 100644 --- a/app/src/main/kotlin/info/appdev/chartexample/LineChartDualAxisActivity.kt +++ b/app/src/main/kotlin/info/appdev/chartexample/LineChartDualAxisActivity.kt @@ -9,7 +9,6 @@ import android.view.Menu import android.view.MenuItem import android.widget.SeekBar import android.widget.SeekBar.OnSeekBarChangeListener -import android.widget.TextView import androidx.core.content.ContextCompat import androidx.core.net.toUri import com.github.mikephil.charting.charts.LineChart @@ -23,6 +22,7 @@ import com.github.mikephil.charting.highlight.Highlight import com.github.mikephil.charting.listener.OnChartValueSelectedListener import com.github.mikephil.charting.utils.ColorTemplate import info.appdev.chartexample.DataTools.Companion.getValues +import info.appdev.chartexample.databinding.ActivityLinechartBinding import info.appdev.chartexample.notimportant.DemoBase import timber.log.Timber @@ -33,56 +33,47 @@ import timber.log.Timber * @version 3.1.0 */ class LineChartDualAxisActivity : DemoBase(), OnSeekBarChangeListener, OnChartValueSelectedListener { - private var chart: LineChart? = null - private var seekBarX: SeekBar? = null - private var seekBarY: SeekBar? = null - private var tvX: TextView? = null - private var tvY: TextView? = null + + private lateinit var binding: ActivityLinechartBinding override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) - setContentView(R.layout.activity_linechart) - - tvX = findViewById(R.id.tvXMax) - tvY = findViewById(R.id.tvYMax) - - seekBarX = findViewById(R.id.seekBarX) - seekBarX!!.setOnSeekBarChangeListener(this) + binding = ActivityLinechartBinding.inflate(layoutInflater) + setContentView(binding.root) - seekBarY = findViewById(R.id.seekBarY) - seekBarY!!.setOnSeekBarChangeListener(this) + binding.seekBarX.setOnSeekBarChangeListener(this) + binding.seekBarY.setOnSeekBarChangeListener(this) - chart = findViewById(R.id.chart1) - chart!!.setOnChartValueSelectedListener(this) + binding.chart1.setOnChartValueSelectedListener(this) // no description text - chart!!.description.isEnabled = false + binding.chart1.description.isEnabled = false // enable touch gestures - chart!!.setTouchEnabled(true) + binding.chart1.setTouchEnabled(true) - chart!!.setDragDecelerationFrictionCoef(0.9f) + binding.chart1.setDragDecelerationFrictionCoef(0.9f) // enable scaling and dragging - chart!!.setDragEnabled(true) - chart!!.setScaleEnabled(true) - chart!!.setDrawGridBackground(false) - chart!!.isHighlightPerDragEnabled = true + binding.chart1.setDragEnabled(true) + binding.chart1.setScaleEnabled(true) + binding.chart1.setDrawGridBackground(false) + binding.chart1.isHighlightPerDragEnabled = true // if disabled, scaling can be done on x- and y-axis separately - chart!!.setPinchZoom(true) + binding.chart1.setPinchZoom(true) // set an alternative background color - chart!!.setBackgroundColor(Color.LTGRAY) + binding.chart1.setBackgroundColor(Color.LTGRAY) // add data - seekBarX!!.progress = 20 - seekBarY!!.progress = 30 + binding.seekBarX.progress = 20 + binding.seekBarY.progress = 30 - chart!!.animateX(1500) + binding.chart1.animateX(1500) // get the legend (only possible after setting data) - val l = chart!!.legend + val l = binding.chart1.legend // modify the legend ... l.form = LegendForm.LINE @@ -95,14 +86,14 @@ class LineChartDualAxisActivity : DemoBase(), OnSeekBarChangeListener, OnChartVa l.setDrawInside(false) // l.setYOffset(11f); - val xAxis = chart!!.xAxis + val xAxis = binding.chart1.xAxis xAxis.typeface = tfLight xAxis.textSize = 11f xAxis.textColor = Color.WHITE xAxis.setDrawGridLines(false) xAxis.setDrawAxisLine(false) - val leftAxis = chart!!.axisLeft + val leftAxis = binding.chart1.axisLeft leftAxis.typeface = tfLight leftAxis.textColor = ColorTemplate.holoBlue leftAxis.axisMaximum = 200f @@ -110,7 +101,7 @@ class LineChartDualAxisActivity : DemoBase(), OnSeekBarChangeListener, OnChartVa leftAxis.setDrawGridLines(true) leftAxis.isGranularityEnabled = true - val rightAxis = chart!!.axisRight + val rightAxis = binding.chart1.axisRight rightAxis.typeface = tfLight rightAxis.textColor = Color.MAGENTA rightAxis.axisMaximum = 900f @@ -147,17 +138,17 @@ class LineChartDualAxisActivity : DemoBase(), OnSeekBarChangeListener, OnChartVa val set2: LineDataSet val set3: LineDataSet - if (chart!!.data != null && - chart!!.data!!.getDataSetCount() > 0 + if (binding.chart1.data != null && + binding.chart1.data!!.getDataSetCount() > 0 ) { - set1 = chart!!.data!!.getDataSetByIndex(0) as LineDataSet - set2 = chart!!.data!!.getDataSetByIndex(1) as LineDataSet - set3 = chart!!.data!!.getDataSetByIndex(2) as LineDataSet + set1 = binding.chart1.data!!.getDataSetByIndex(0) as LineDataSet + set2 = binding.chart1.data!!.getDataSetByIndex(1) as LineDataSet + set3 = binding.chart1.data!!.getDataSetByIndex(2) as LineDataSet set1.entries = values1 set2.entries = values2 set3.entries = values3 - chart!!.data!!.notifyDataChanged() - chart?.notifyDataSetChanged() + binding.chart1.data!!.notifyDataChanged() + binding.chart1.notifyDataSetChanged() } else { // create a dataset and give it a type set1 = LineDataSet(values1, "DataSet 1") @@ -207,7 +198,7 @@ class LineChartDualAxisActivity : DemoBase(), OnSeekBarChangeListener, OnChartVa data.setValueTextSize(9f) // set data - chart!!.setData(data) + binding.chart1.setData(data) } } @@ -226,38 +217,38 @@ class LineChartDualAxisActivity : DemoBase(), OnSeekBarChangeListener, OnChartVa } R.id.actionToggleValues -> { - chart!!.data!!.dataSets.forEach { set -> + binding.chart1.data!!.dataSets.forEach { set -> set?.isDrawValues = !set.isDrawValues } - chart?.invalidate() + binding.chart1.invalidate() } R.id.actionToggleHighlight -> { - if (chart!!.data != null) { - chart!!.data!!.isHighlightEnabled = !chart!!.data!!.isHighlightEnabled() - chart?.invalidate() + if (binding.chart1.data != null) { + binding.chart1.data!!.isHighlightEnabled = !binding.chart1.data!!.isHighlightEnabled() + binding.chart1.invalidate() } } R.id.actionToggleFilled -> { - chart!!.data!!.dataSets.forEach { set -> + binding.chart1.data!!.dataSets.forEach { set -> set?.setDrawFilled(!set.isDrawFilledEnabled) } - chart?.invalidate() + binding.chart1.invalidate() } R.id.actionToggleCircles -> { - val sets = chart!!.data!!.dataSets + val sets = binding.chart1.data!!.dataSets for (iSet in sets) { val set = iSet as LineDataSet set.isDrawCirclesEnabled = !set.isDrawCirclesEnabled } - chart?.invalidate() + binding.chart1.invalidate() } R.id.actionToggleCubic -> { - val sets = chart!!.data!!.dataSets + val sets = binding.chart1.data!!.dataSets for (iSet in sets) { val set = iSet as LineDataSet @@ -266,11 +257,11 @@ class LineChartDualAxisActivity : DemoBase(), OnSeekBarChangeListener, OnChartVa else LineDataSet.Mode.CUBIC_BEZIER } - chart?.invalidate() + binding.chart1.invalidate() } R.id.actionToggleStepped -> { - val sets = chart!!.data!!.dataSets + val sets = binding.chart1.data!!.dataSets for (iSet in sets) { val set = iSet as LineDataSet @@ -279,11 +270,11 @@ class LineChartDualAxisActivity : DemoBase(), OnSeekBarChangeListener, OnChartVa else LineDataSet.Mode.STEPPED } - chart?.invalidate() + binding.chart1.invalidate() } R.id.actionToggleHorizontalCubic -> { - val sets = chart!!.data!!.dataSets + val sets = binding.chart1.data!!.dataSets for (iSet in sets) { val set = iSet as LineDataSet @@ -292,37 +283,37 @@ class LineChartDualAxisActivity : DemoBase(), OnSeekBarChangeListener, OnChartVa else LineDataSet.Mode.HORIZONTAL_BEZIER } - chart?.invalidate() + binding.chart1.invalidate() } R.id.actionTogglePinch -> { - chart!!.setPinchZoom(!chart!!.isPinchZoomEnabled) + binding.chart1.setPinchZoom(!binding.chart1.isPinchZoomEnabled) - chart?.invalidate() + binding.chart1.invalidate() } R.id.actionToggleAutoScaleMinMax -> { - chart!!.isAutoScaleMinMaxEnabled = !chart!!.isAutoScaleMinMaxEnabled - chart?.notifyDataSetChanged() + binding.chart1.isAutoScaleMinMaxEnabled = !binding.chart1.isAutoScaleMinMaxEnabled + binding.chart1.notifyDataSetChanged() } R.id.animateX -> { - chart!!.animateX(2000) + binding.chart1.animateX(2000) } R.id.animateY -> { - chart!!.animateY(2000) + binding.chart1.animateY(2000) } R.id.animateXY -> { - chart!!.animateXY(2000, 2000) + binding.chart1.animateXY(2000, 2000) } R.id.actionSave -> { if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) { saveToGallery() } else { - requestStoragePermission(chart) + requestStoragePermission(binding.chart1) } } } @@ -330,24 +321,24 @@ class LineChartDualAxisActivity : DemoBase(), OnSeekBarChangeListener, OnChartVa } override fun onProgressChanged(seekBar: SeekBar?, progress: Int, fromUser: Boolean) { - tvX!!.text = seekBarX!!.progress.toString() - tvY!!.text = seekBarY!!.progress.toString() + binding.tvXMax.text = binding.seekBarX.progress.toString() + binding.tvYMax.text = binding.seekBarY.progress.toString() - setData(seekBarX!!.progress, seekBarY!!.progress.toFloat()) + setData(binding.seekBarX.progress, binding.seekBarY.progress.toFloat()) // redraw - chart?.invalidate() + binding.chart1.invalidate() } override fun saveToGallery() { - saveToGallery(chart, "LineChartActivity2") + saveToGallery(binding.chart1, "LineChartActivity2") } override fun onValueSelected(entry: Entry, highlight: Highlight) { Timber.i(entry.toString()) - chart!!.data!!.getDataSetByIndex(highlight.dataSetIndex)?.let { - chart!!.centerViewToAnimated(entry.x, entry.y, it.axisDependency, 500) + binding.chart1.data!!.getDataSetByIndex(highlight.dataSetIndex)?.let { + binding.chart1.centerViewToAnimated(entry.x, entry.y, it.axisDependency, 500) //chart.zoomAndCenterAnimated(2.5f, 2.5f, e.getX(), e.getY(), chart.getData().getDataSetByIndex(dataSetIndex) // .getAxisDependency(), 1000); //chart.zoomAndCenterAnimated(1.8f, 1.8f, e.getX(), e.getY(), chart.getData().getDataSetByIndex(dataSetIndex) diff --git a/app/src/main/kotlin/info/appdev/chartexample/LineChartTimeActivity.kt b/app/src/main/kotlin/info/appdev/chartexample/LineChartTimeActivity.kt index b4fe5ef94..8c95cfe30 100644 --- a/app/src/main/kotlin/info/appdev/chartexample/LineChartTimeActivity.kt +++ b/app/src/main/kotlin/info/appdev/chartexample/LineChartTimeActivity.kt @@ -9,10 +9,8 @@ import android.view.Menu import android.view.MenuItem import android.widget.SeekBar import android.widget.SeekBar.OnSeekBarChangeListener -import android.widget.TextView import androidx.core.content.ContextCompat import androidx.core.net.toUri -import com.github.mikephil.charting.charts.LineChart import com.github.mikephil.charting.components.AxisBase import com.github.mikephil.charting.components.XAxis import com.github.mikephil.charting.components.YAxis @@ -23,6 +21,7 @@ import com.github.mikephil.charting.data.LineDataSet import com.github.mikephil.charting.formatter.IAxisValueFormatter import com.github.mikephil.charting.utils.ColorTemplate.holoBlue import info.appdev.chartexample.DataTools.Companion.getValues +import info.appdev.chartexample.databinding.ActivityLinechartTimeBinding import info.appdev.chartexample.notimportant.DemoBase import java.text.SimpleDateFormat import java.util.Date @@ -31,46 +30,42 @@ import java.util.concurrent.TimeUnit import kotlin.math.roundToInt class LineChartTimeActivity : DemoBase(), OnSeekBarChangeListener { - private var chart: LineChart? = null - private var seekBarX: SeekBar? = null - private var tvX: TextView? = null + + private lateinit var binding: ActivityLinechartTimeBinding override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) - setContentView(R.layout.activity_linechart_time) - - tvX = findViewById(R.id.tvXMax) - seekBarX = findViewById(R.id.seekBarX) - seekBarX!!.setOnSeekBarChangeListener(this) + binding = ActivityLinechartTimeBinding.inflate(layoutInflater) + setContentView(binding.root) - chart = findViewById(R.id.chart1) + binding.seekBarX.setOnSeekBarChangeListener(this) // no description text - chart!!.description.isEnabled = false + binding.chart1.description.isEnabled = false // enable touch gestures - chart!!.setTouchEnabled(true) + binding.chart1.setTouchEnabled(true) - chart!!.setDragDecelerationFrictionCoef(0.9f) + binding.chart1.setDragDecelerationFrictionCoef(0.9f) // enable scaling and dragging - chart!!.setDragEnabled(true) - chart!!.setScaleEnabled(true) - chart!!.setDrawGridBackground(false) - chart!!.isHighlightPerDragEnabled = true + binding.chart1.setDragEnabled(true) + binding.chart1.setScaleEnabled(true) + binding.chart1.setDrawGridBackground(false) + binding.chart1.isHighlightPerDragEnabled = true // set an alternative background color - chart!!.setBackgroundColor(Color.WHITE) - chart!!.setViewPortOffsets(0f, 0f, 0f, 0f) + binding.chart1.setBackgroundColor(Color.WHITE) + binding.chart1.setViewPortOffsets(0f, 0f, 0f, 0f) // add data - seekBarX!!.progress = 100 + binding.seekBarX.progress = 100 // get the legend (only possible after setting data) - val l = chart!!.legend + val l = binding.chart1.legend l.isEnabled = false - val xAxis = chart!!.xAxis + val xAxis = binding.chart1.xAxis xAxis.position = XAxis.XAxisPosition.TOP_INSIDE xAxis.typeface = tfLight xAxis.textSize = 10f @@ -89,7 +84,7 @@ class LineChartTimeActivity : DemoBase(), OnSeekBarChangeListener { } } - val leftAxis = chart!!.axisLeft + val leftAxis = binding.chart1.axisLeft leftAxis.setPosition(YAxis.YAxisLabelPosition.INSIDE_CHART) leftAxis.typeface = tfLight leftAxis.textColor = holoBlue @@ -100,7 +95,7 @@ class LineChartTimeActivity : DemoBase(), OnSeekBarChangeListener { leftAxis.yOffset = -9f leftAxis.textColor = Color.rgb(255, 192, 56) - val rightAxis = chart!!.axisRight + val rightAxis = binding.chart1.axisRight rightAxis.isEnabled = false } @@ -145,7 +140,7 @@ class LineChartTimeActivity : DemoBase(), OnSeekBarChangeListener { data.setValueTextSize(9f) // set data - chart!!.setData(data) + binding.chart1.setData(data) } override fun onCreateOptionsMenu(menu: Menu?): Boolean { @@ -163,21 +158,21 @@ class LineChartTimeActivity : DemoBase(), OnSeekBarChangeListener { } R.id.actionToggleValues -> { - chart!!.data!!.dataSets.forEach { set -> + binding.chart1.data!!.dataSets.forEach { set -> set?.isDrawValues = !set.isDrawValues } - chart?.invalidate() + binding.chart1.invalidate() } R.id.actionToggleHighlight -> { - if (chart!!.data != null) { - chart!!.data!!.isHighlightEnabled = !chart!!.data!!.isHighlightEnabled() - chart?.invalidate() + if (binding.chart1.data != null) { + binding.chart1.data!!.isHighlightEnabled = !binding.chart1.data!!.isHighlightEnabled() + binding.chart1.invalidate() } } R.id.actionToggleFilled -> { - chart!!.data!!.dataSets.forEach { set -> + binding.chart1.data!!.dataSets.forEach { set -> set?.let { if (set.isDrawFilledEnabled) set.setDrawFilled(false) @@ -185,21 +180,21 @@ class LineChartTimeActivity : DemoBase(), OnSeekBarChangeListener { set.setDrawFilled(true) } } - chart?.invalidate() + binding.chart1.invalidate() } R.id.actionToggleCircles -> { - val sets = chart!!.data!!.dataSets + val sets = binding.chart1.data!!.dataSets for (iSet in sets) { val set = iSet as LineDataSet set.isDrawCirclesEnabled = !set.isDrawCirclesEnabled } - chart?.invalidate() + binding.chart1.invalidate() } R.id.actionToggleCubic -> { - val sets = chart!!.data!!.dataSets + val sets = binding.chart1.data!!.dataSets for (iSet in sets) { val set = iSet as LineDataSet @@ -208,11 +203,11 @@ class LineChartTimeActivity : DemoBase(), OnSeekBarChangeListener { else set.lineMode = LineDataSet.Mode.CUBIC_BEZIER } - chart?.invalidate() + binding.chart1.invalidate() } R.id.actionToggleStepped -> { - val sets = chart!!.data!!.dataSets + val sets = binding.chart1.data!!.dataSets for (iSet in sets) { val set = iSet as LineDataSet @@ -221,38 +216,38 @@ class LineChartTimeActivity : DemoBase(), OnSeekBarChangeListener { else set.lineMode = LineDataSet.Mode.STEPPED } - chart?.invalidate() + binding.chart1.invalidate() } R.id.actionTogglePinch -> { - if (chart!!.isPinchZoomEnabled) chart!!.setPinchZoom(false) - else chart!!.setPinchZoom(true) + if (binding.chart1.isPinchZoomEnabled) binding.chart1.setPinchZoom(false) + else binding.chart1.setPinchZoom(true) - chart?.invalidate() + binding.chart1.invalidate() } R.id.actionToggleAutoScaleMinMax -> { - chart!!.isAutoScaleMinMaxEnabled = !chart!!.isAutoScaleMinMaxEnabled - chart?.notifyDataSetChanged() + binding.chart1.isAutoScaleMinMaxEnabled = !binding.chart1.isAutoScaleMinMaxEnabled + binding.chart1.notifyDataSetChanged() } R.id.animateX -> { - chart!!.animateX(2000) + binding.chart1.animateX(2000) } R.id.animateY -> { - chart!!.animateY(2000) + binding.chart1.animateY(2000) } R.id.animateXY -> { - chart!!.animateXY(2000, 2000) + binding.chart1.animateXY(2000, 2000) } R.id.actionSave -> { if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) { saveToGallery() } else { - requestStoragePermission(chart) + requestStoragePermission(binding.chart1) } } } @@ -260,16 +255,16 @@ class LineChartTimeActivity : DemoBase(), OnSeekBarChangeListener { } override fun onProgressChanged(seekBar: SeekBar?, progress: Int, fromUser: Boolean) { - tvX!!.text = seekBarX!!.progress.toString() + binding.tvXMax.text = binding.seekBarX.progress.toString() - setData(seekBarX!!.progress) + setData(binding.seekBarX.progress) // redraw - chart?.invalidate() + binding.chart1.invalidate() } override fun saveToGallery() { - saveToGallery(chart, "LineChartTime") + saveToGallery(binding.chart1, "LineChartTime") } override fun onStartTrackingTouch(seekBar: SeekBar?) = Unit diff --git a/app/src/main/kotlin/info/appdev/chartexample/ListViewBarChartActivity.kt b/app/src/main/kotlin/info/appdev/chartexample/ListViewBarChartActivity.kt index 13d5daec9..176be1c78 100644 --- a/app/src/main/kotlin/info/appdev/chartexample/ListViewBarChartActivity.kt +++ b/app/src/main/kotlin/info/appdev/chartexample/ListViewBarChartActivity.kt @@ -11,7 +11,6 @@ import android.view.MenuItem import android.view.View import android.view.ViewGroup import android.widget.ArrayAdapter -import android.widget.ListView import com.github.mikephil.charting.charts.BarChart import com.github.mikephil.charting.components.XAxis.XAxisPosition import com.github.mikephil.charting.data.BarData @@ -22,19 +21,22 @@ import com.github.mikephil.charting.utils.ColorTemplate import info.appdev.chartexample.DataTools.Companion.getValues import info.appdev.chartexample.notimportant.DemoBase import androidx.core.net.toUri +import info.appdev.chartexample.databinding.ActivityListviewChartBinding /** * Demonstrates the use of charts inside a ListView. IMPORTANT: provide a * specific height attribute for the chart inside your ListView item */ class ListViewBarChartActivity : DemoBase() { + + private lateinit var binding: ActivityListviewChartBinding + override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) - setContentView(R.layout.activity_listview_chart) - - val lv = findViewById(R.id.listViewMain) + binding = ActivityListviewChartBinding.inflate(layoutInflater) + setContentView(binding.root) - val list = ArrayList() + val list = ArrayList() // 20 items for (i in 0..19) { @@ -42,10 +44,10 @@ class ListViewBarChartActivity : DemoBase() { } val chartDataAdapter = ChartDataAdapter(applicationContext, list) - lv.adapter = chartDataAdapter + binding.listViewMain.adapter = chartDataAdapter } - private inner class ChartDataAdapter(context: Context, objects: MutableList) : ArrayAdapter(context, 0, objects) { + private inner class ChartDataAdapter(context: Context, objects: MutableList) : ArrayAdapter(context, 0, objects) { @SuppressLint("InflateParams") override fun getView(position: Int, convertView: View?, parent: ViewGroup): View { var convertView = convertView diff --git a/app/src/main/kotlin/info/appdev/chartexample/ListViewMultiChartActivity.kt b/app/src/main/kotlin/info/appdev/chartexample/ListViewMultiChartActivity.kt index 062fd2650..54e611c50 100644 --- a/app/src/main/kotlin/info/appdev/chartexample/ListViewMultiChartActivity.kt +++ b/app/src/main/kotlin/info/appdev/chartexample/ListViewMultiChartActivity.kt @@ -9,7 +9,6 @@ import android.view.MenuItem import android.view.View import android.view.ViewGroup import android.widget.ArrayAdapter -import android.widget.ListView import com.github.mikephil.charting.data.BarData import com.github.mikephil.charting.data.BarDataSet import com.github.mikephil.charting.data.BarEntry @@ -28,19 +27,21 @@ import info.appdev.chartexample.listviewitems.LineChartItem import info.appdev.chartexample.listviewitems.PieChartItem import info.appdev.chartexample.notimportant.DemoBase import androidx.core.net.toUri +import info.appdev.chartexample.databinding.ActivityListviewChartBinding /** * Demonstrates the use of charts inside a ListView. IMPORTANT: provide a * specific height attribute for the chart inside your ListView item */ class ListViewMultiChartActivity : DemoBase() { + private lateinit var binding: ActivityListviewChartBinding + override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) - setContentView(R.layout.activity_listview_chart) - - val lv = findViewById(R.id.listViewMain) + binding = ActivityListviewChartBinding.inflate(layoutInflater) + setContentView(binding.root) - val list = ArrayList() + val list = ArrayList() // 30 items for (i in 0..29) { @@ -54,11 +55,11 @@ class ListViewMultiChartActivity : DemoBase() { } val chartDataAdapter = ChartDataAdapter(applicationContext, list) - lv.adapter = chartDataAdapter + binding.listViewMain.adapter = chartDataAdapter } /** adapter that supports 3 different item types */ - private class ChartDataAdapter(context: Context, objects: MutableList) : ArrayAdapter(context, 0, objects) { + private class ChartDataAdapter(context: Context, objects: MutableList) : ArrayAdapter(context, 0, objects) { override fun getView(position: Int, convertView: View?, parent: ViewGroup): View { return getItem(position)!!.getView(position, convertView, context)!! } diff --git a/app/src/main/kotlin/info/appdev/chartexample/MultiLineChartActivity.kt b/app/src/main/kotlin/info/appdev/chartexample/MultiLineChartActivity.kt index 911cd9b68..900cc5472 100644 --- a/app/src/main/kotlin/info/appdev/chartexample/MultiLineChartActivity.kt +++ b/app/src/main/kotlin/info/appdev/chartexample/MultiLineChartActivity.kt @@ -33,8 +33,7 @@ class MultiLineChartActivity : DemoBase(), OnSeekBarChangeListener, OnChartGestu override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) binding = ActivityLinechartBinding.inflate(layoutInflater) - val view = binding.root - setContentView(view) + setContentView(binding.root) binding.seekBarX.setOnSeekBarChangeListener(this) binding.seekBarY.setOnSeekBarChangeListener(this) diff --git a/app/src/main/kotlin/info/appdev/chartexample/PerformanceLineChart.kt b/app/src/main/kotlin/info/appdev/chartexample/PerformanceLineChart.kt index 77f5f73e6..9b188fb6a 100644 --- a/app/src/main/kotlin/info/appdev/chartexample/PerformanceLineChart.kt +++ b/app/src/main/kotlin/info/appdev/chartexample/PerformanceLineChart.kt @@ -7,53 +7,49 @@ import android.view.Menu import android.view.MenuItem import android.widget.SeekBar import android.widget.SeekBar.OnSeekBarChangeListener -import android.widget.TextView -import com.github.mikephil.charting.charts.LineChart import com.github.mikephil.charting.data.Entry import com.github.mikephil.charting.data.LineData import com.github.mikephil.charting.data.LineDataSet import info.appdev.chartexample.DataTools.Companion.getMuchValues import info.appdev.chartexample.notimportant.DemoBase import androidx.core.net.toUri +import info.appdev.chartexample.databinding.ActivityPerformanceLinechartBinding class PerformanceLineChart : DemoBase(), OnSeekBarChangeListener { - private var chart: LineChart? = null - private var seekBarValues: SeekBar? = null - private var tvCount: TextView? = null + + private lateinit var binding: ActivityPerformanceLinechartBinding override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) - setContentView(R.layout.activity_performance_linechart) + binding = ActivityPerformanceLinechartBinding.inflate(layoutInflater) + setContentView(binding.root) - tvCount = findViewById(R.id.tvValueCount) - seekBarValues = findViewById(R.id.seekbarValues) - seekBarValues!!.setOnSeekBarChangeListener(this) + binding.seekbarValues.setOnSeekBarChangeListener(this) - chart = findViewById(R.id.chart1) - chart!!.setDrawGridBackground(false) + binding.chart1.setDrawGridBackground(false) // no description text - chart!!.description.isEnabled = false + binding.chart1.description.isEnabled = false // enable touch gestures - chart!!.setTouchEnabled(true) + binding.chart1.setTouchEnabled(true) // enable scaling and dragging - chart!!.setDragEnabled(true) - chart!!.setScaleEnabled(true) + binding.chart1.setDragEnabled(true) + binding.chart1.setScaleEnabled(true) // if disabled, scaling can be done on x- and y-axis separately - chart!!.setPinchZoom(false) + binding.chart1.setPinchZoom(false) - chart!!.axisLeft.setDrawGridLines(false) - chart!!.axisRight.isEnabled = false - chart!!.xAxis.setDrawGridLines(true) - chart!!.xAxis.setDrawAxisLine(false) + binding.chart1.axisLeft.setDrawGridLines(false) + binding.chart1.axisRight.isEnabled = false + binding.chart1.xAxis.setDrawGridLines(true) + binding.chart1.xAxis.setDrawAxisLine(false) - seekBarValues!!.progress = 9000 + binding.seekbarValues.progress = 9000 // don't forget to refresh the drawing - chart?.invalidate() + binding.chart1.invalidate() } private fun setData(count: Int, range: Float) { @@ -79,10 +75,10 @@ class PerformanceLineChart : DemoBase(), OnSeekBarChangeListener { val data = LineData(set1) // set data - chart!!.setData(data) + binding.chart1.setData(data) // get the legend (only possible after setting data) - val l = chart!!.legend + val l = binding.chart1.legend l.isEnabled = false } @@ -105,15 +101,15 @@ class PerformanceLineChart : DemoBase(), OnSeekBarChangeListener { } override fun onProgressChanged(seekBar: SeekBar?, progress: Int, fromUser: Boolean) { - val count = seekBarValues!!.progress + 1000 - tvCount!!.text = count.toString() + val count = binding.seekbarValues.progress + 1000 + binding.tvValueCount.text = count.toString() - chart!!.resetTracking() + binding.chart1.resetTracking() setData(count, 500f) // redraw - chart?.invalidate() + binding.chart1.invalidate() } public override fun saveToGallery() { /* Intentionally left empty */ diff --git a/app/src/main/kotlin/info/appdev/chartexample/PieChartActivity.kt b/app/src/main/kotlin/info/appdev/chartexample/PieChartActivity.kt index 649754eac..935c677e2 100644 --- a/app/src/main/kotlin/info/appdev/chartexample/PieChartActivity.kt +++ b/app/src/main/kotlin/info/appdev/chartexample/PieChartActivity.kt @@ -14,12 +14,10 @@ import android.view.Menu import android.view.MenuItem import android.widget.SeekBar import android.widget.SeekBar.OnSeekBarChangeListener -import android.widget.TextView import androidx.core.content.ContextCompat import androidx.core.content.res.ResourcesCompat import androidx.core.net.toUri import com.github.mikephil.charting.animation.Easing -import com.github.mikephil.charting.charts.PieChart import com.github.mikephil.charting.components.Legend import com.github.mikephil.charting.data.Entry import com.github.mikephil.charting.data.PieData @@ -31,68 +29,60 @@ import com.github.mikephil.charting.listener.OnChartValueSelectedListener import com.github.mikephil.charting.utils.ColorTemplate import com.github.mikephil.charting.utils.MPPointF import info.appdev.chartexample.DataTools.Companion.getValues +import info.appdev.chartexample.databinding.ActivityPiechartBinding import info.appdev.chartexample.notimportant.DemoBase import timber.log.Timber class PieChartActivity : DemoBase(), OnSeekBarChangeListener, OnChartValueSelectedListener { - private var chart: PieChart? = null - private var seekBarX: SeekBar? = null - private var seekBarY: SeekBar? = null - private var tvX: TextView? = null - private var tvY: TextView? = null + + private lateinit var binding: ActivityPiechartBinding override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) - setContentView(R.layout.activity_piechart) - - tvX = findViewById(R.id.tvXMax) - tvY = findViewById(R.id.tvYMax) - - seekBarX = findViewById(R.id.seekBarX) - seekBarY = findViewById(R.id.seekBarY) + binding = ActivityPiechartBinding.inflate(layoutInflater) + setContentView(binding.root) - seekBarX!!.setOnSeekBarChangeListener(this) - seekBarY!!.setOnSeekBarChangeListener(this) + binding.seekBarX.setOnSeekBarChangeListener(this) + binding.seekBarY.setOnSeekBarChangeListener(this) - chart = findViewById(R.id.chart1) - chart!!.setUsePercentValues(true) - chart!!.description.isEnabled = false - chart!!.setExtraOffsets(5f, 10f, 5f, 5f) + binding.chart1.setUsePercentValues(true) + binding.chart1.description.isEnabled = false + binding.chart1.setExtraOffsets(5f, 10f, 5f, 5f) - chart!!.setDragDecelerationFrictionCoef(0.95f) + binding.chart1.setDragDecelerationFrictionCoef(0.95f) - chart!!.setCenterTextTypeface(tfLight) - chart!!.centerText = generateCenterSpannableText() + binding.chart1.setCenterTextTypeface(tfLight) + binding.chart1.centerText = generateCenterSpannableText() - chart!!.isDrawHoleEnabled = true - chart!!.setHoleColor(Color.WHITE) + binding.chart1.isDrawHoleEnabled = true + binding.chart1.setHoleColor(Color.WHITE) - chart!!.setTransparentCircleColor(Color.WHITE) - chart!!.setTransparentCircleAlpha(110) + binding.chart1.setTransparentCircleColor(Color.WHITE) + binding.chart1.setTransparentCircleAlpha(110) - chart!!.holeRadius = 58f - chart!!.transparentCircleRadius = 61f + binding.chart1.holeRadius = 58f + binding.chart1.transparentCircleRadius = 61f - chart!!.setDrawCenterText(true) + binding.chart1.setDrawCenterText(true) - chart!!.rotationAngle = 0f + binding.chart1.rotationAngle = 0f // enable rotation of the chart by touch - chart!!.isRotationEnabled = true - chart!!.isHighlightPerTapEnabled = true + binding.chart1.isRotationEnabled = true + binding.chart1.isHighlightPerTapEnabled = true // chart.setUnit(" €"); // chart.setDrawUnitsInChart(true); // add a selection listener - chart!!.setOnChartValueSelectedListener(this) + binding.chart1.setOnChartValueSelectedListener(this) - seekBarX!!.progress = 4 - seekBarY!!.progress = 10 + binding.seekBarX.progress = 4 + binding.seekBarY.progress = 10 - chart!!.animateY(1400, Easing.easeInOutQuad) + binding.chart1.animateY(1400, Easing.easeInOutQuad) // chart.spin(2000, 0, 360); - val l = chart!!.legend + val l = binding.chart1.legend l.verticalAlignment = Legend.LegendVerticalAlignment.TOP l.horizontalAlignment = Legend.LegendHorizontalAlignment.RIGHT l.orientation = Legend.LegendOrientation.VERTICAL @@ -102,9 +92,9 @@ class PieChartActivity : DemoBase(), OnSeekBarChangeListener, OnChartValueSelect l.yOffset = 0f // entry label styling - chart!!.setEntryLabelColor(Color.WHITE) - chart!!.setEntryLabelTypeface(tfRegular) - chart!!.setEntryLabelTextSize(12f) + binding.chart1.setEntryLabelColor(Color.WHITE) + binding.chart1.setEntryLabelTypeface(tfRegular) + binding.chart1.setEntryLabelTextSize(12f) } private fun setData(count: Int, range: Float) { @@ -154,12 +144,12 @@ class PieChartActivity : DemoBase(), OnSeekBarChangeListener, OnChartValueSelect data.setValueTextSize(11f) data.setValueTextColor(Color.WHITE) data.setValueTypeface(tfLight) - chart!!.setData(data) + binding.chart1.setData(data) // undo all highlights - chart!!.highlightValues(null) + binding.chart1.highlightValues(null) - chart?.invalidate() + binding.chart1.invalidate() } override fun onCreateOptionsMenu(menu: Menu?): Boolean { @@ -177,79 +167,79 @@ class PieChartActivity : DemoBase(), OnSeekBarChangeListener, OnChartValueSelect } R.id.actionToggleValues -> { - chart!!.data!!.dataSets.forEach { + binding.chart1.data!!.dataSets.forEach { it?.isDrawValues = !it.isDrawValues } - chart?.invalidate() + binding.chart1.invalidate() } R.id.actionToggleIcons -> { - for (set in chart!!.data!!.getDataSets()) + for (set in binding.chart1.data!!.getDataSets()) set?.isDrawIcons = !set.isDrawIcons - chart?.invalidate() + binding.chart1.invalidate() } R.id.actionToggleHole -> { - chart!!.isDrawHoleEnabled = !chart!!.isDrawHoleEnabled - chart?.invalidate() + binding.chart1.isDrawHoleEnabled = !binding.chart1.isDrawHoleEnabled + binding.chart1.invalidate() } R.id.actionToggleMinAngles -> { - if (chart!!.minAngleForSlices == 0f) chart!!.setMinAngleForSlices(36f) - else chart!!.setMinAngleForSlices(0f) - chart?.notifyDataSetChanged() - chart?.invalidate() + if (binding.chart1.minAngleForSlices == 0f) binding.chart1.setMinAngleForSlices(36f) + else binding.chart1.setMinAngleForSlices(0f) + binding.chart1.notifyDataSetChanged() + binding.chart1.invalidate() } R.id.actionToggleCurvedSlices -> { - val toSet = !chart!!.isDrawRoundedSlicesEnabled || !chart!!.isDrawHoleEnabled - chart!!.setDrawRoundedSlices(toSet) - if (toSet && !chart!!.isDrawHoleEnabled) { - chart!!.isDrawHoleEnabled = true + val toSet = !binding.chart1.isDrawRoundedSlicesEnabled || !binding.chart1.isDrawHoleEnabled + binding.chart1.setDrawRoundedSlices(toSet) + if (toSet && !binding.chart1.isDrawHoleEnabled) { + binding.chart1.isDrawHoleEnabled = true } - if (toSet && chart!!.isDrawSlicesUnderHoleEnabled) { - chart!!.setDrawSlicesUnderHole(false) + if (toSet && binding.chart1.isDrawSlicesUnderHoleEnabled) { + binding.chart1.setDrawSlicesUnderHole(false) } - chart?.invalidate() + binding.chart1.invalidate() } R.id.actionDrawCenter -> { - chart!!.setDrawCenterText(!chart!!.isDrawCenterTextEnabled) - chart?.invalidate() + binding.chart1.setDrawCenterText(!binding.chart1.isDrawCenterTextEnabled) + binding.chart1.invalidate() } R.id.actionToggleXValues -> { - chart!!.setDrawEntryLabels(!chart!!.isDrawEntryLabelsEnabled) - chart?.invalidate() + binding.chart1.setDrawEntryLabels(!binding.chart1.isDrawEntryLabelsEnabled) + binding.chart1.invalidate() } R.id.actionTogglePercent -> { - chart!!.setUsePercentValues(!chart!!.isUsePercentValuesEnabled) - chart?.invalidate() + binding.chart1.setUsePercentValues(!binding.chart1.isUsePercentValuesEnabled) + binding.chart1.invalidate() } R.id.animateX -> { - chart!!.animateX(1400) + binding.chart1.animateX(1400) } R.id.animateY -> { - chart!!.animateY(1400) + binding.chart1.animateY(1400) } R.id.animateXY -> { - chart!!.animateXY(1400, 1400) + binding.chart1.animateXY(1400, 1400) } R.id.actionToggleSpin -> { - chart!!.spin(1000, chart!!.rotationAngle, chart!!.rotationAngle + 360, Easing.easeInOutCubic) + binding.chart1.spin(1000, binding.chart1.rotationAngle, binding.chart1.rotationAngle + 360, Easing.easeInOutCubic) } R.id.actionSave -> { if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) { saveToGallery() } else { - requestStoragePermission(chart) + requestStoragePermission(binding.chart1) } } } @@ -257,14 +247,14 @@ class PieChartActivity : DemoBase(), OnSeekBarChangeListener, OnChartValueSelect } override fun onProgressChanged(seekBar: SeekBar?, progress: Int, fromUser: Boolean) { - tvX!!.text = seekBarX!!.progress.toString() - tvY!!.text = seekBarY!!.progress.toString() + binding.tvXMax.text = binding.seekBarX.progress.toString() + binding.tvYMax.text = binding.seekBarY.progress.toString() - setData(seekBarX!!.progress, seekBarY!!.progress.toFloat()) + setData(binding.seekBarX.progress, binding.seekBarY.progress.toFloat()) } override fun saveToGallery() { - saveToGallery(chart, "PieChartActivity") + saveToGallery(binding.chart1, "PieChartActivity") } private fun generateCenterSpannableText(): SpannableString { diff --git a/app/src/main/kotlin/info/appdev/chartexample/PieChartRoundedActivity.kt b/app/src/main/kotlin/info/appdev/chartexample/PieChartRoundedActivity.kt index 516cf7812..5398372d3 100644 --- a/app/src/main/kotlin/info/appdev/chartexample/PieChartRoundedActivity.kt +++ b/app/src/main/kotlin/info/appdev/chartexample/PieChartRoundedActivity.kt @@ -14,12 +14,10 @@ import android.view.Menu import android.view.MenuItem import android.widget.SeekBar import android.widget.SeekBar.OnSeekBarChangeListener -import android.widget.TextView import androidx.core.content.ContextCompat import androidx.core.content.res.ResourcesCompat import androidx.core.net.toUri import com.github.mikephil.charting.animation.Easing -import com.github.mikephil.charting.charts.PieChart import com.github.mikephil.charting.components.Legend import com.github.mikephil.charting.data.Entry import com.github.mikephil.charting.data.PieData @@ -32,69 +30,60 @@ import com.github.mikephil.charting.renderer.PieChartRenderer import com.github.mikephil.charting.utils.ColorTemplate import com.github.mikephil.charting.utils.MPPointF import info.appdev.chartexample.DataTools.Companion.getValues +import info.appdev.chartexample.databinding.ActivityPiechartBinding import info.appdev.chartexample.notimportant.DemoBase import timber.log.Timber class PieChartRoundedActivity : DemoBase(), OnSeekBarChangeListener, OnChartValueSelectedListener { - private var chart: PieChart? = null - private var seekBarX: SeekBar? = null - private var seekBarY: SeekBar? = null - private var tvX: TextView? = null - private var tvY: TextView? = null + + private lateinit var binding: ActivityPiechartBinding override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) - setContentView(R.layout.activity_piechart) - - tvX = findViewById(R.id.tvXMax) - tvY = this.findViewById(R.id.tvYMax) - - seekBarX = findViewById(R.id.seekBarX) - seekBarY = findViewById(R.id.seekBarY) - - seekBarX!!.setOnSeekBarChangeListener(this) - seekBarY!!.setOnSeekBarChangeListener(this) + binding = ActivityPiechartBinding.inflate(layoutInflater) + setContentView(binding.root) - chart = findViewById(R.id.chart1) - chart!!.setUsePercentValues(true) - chart!!.description.isEnabled = false - chart!!.setExtraOffsets(5f, 10f, 5f, 5f) + binding.seekBarX.setOnSeekBarChangeListener(this) + binding.seekBarY.setOnSeekBarChangeListener(this) + binding.chart1.setUsePercentValues(true) + binding.chart1.description.isEnabled = false + binding.chart1.setExtraOffsets(5f, 10f, 5f, 5f) - chart!!.setDragDecelerationFrictionCoef(0.95f) + binding.chart1.setDragDecelerationFrictionCoef(0.95f) - chart!!.setCenterTextTypeface(tfLight) - chart!!.centerText = generateCenterSpannableText() + binding.chart1.setCenterTextTypeface(tfLight) + binding.chart1.centerText = generateCenterSpannableText() - chart!!.isDrawHoleEnabled = true - chart!!.setHoleColor(Color.TRANSPARENT) + binding.chart1.isDrawHoleEnabled = true + binding.chart1.setHoleColor(Color.TRANSPARENT) - chart!!.setTransparentCircleColor(Color.TRANSPARENT) - chart!!.setTransparentCircleAlpha(110) + binding.chart1.setTransparentCircleColor(Color.TRANSPARENT) + binding.chart1.setTransparentCircleAlpha(110) - chart!!.holeRadius = 50f + binding.chart1.holeRadius = 50f - chart!!.transparentCircleRadius = 0f + binding.chart1.transparentCircleRadius = 0f - chart!!.setDrawCenterText(true) + binding.chart1.setDrawCenterText(true) - chart!!.rotationAngle = 0f + binding.chart1.rotationAngle = 0f // enable rotation of the chart by touch - chart!!.isRotationEnabled = true - chart!!.isHighlightPerTapEnabled = true + binding.chart1.isRotationEnabled = true + binding.chart1.isHighlightPerTapEnabled = true // chart.setUnit(" €"); // chart.setDrawUnitsInChart(true); // add a selection listener - chart!!.setOnChartValueSelectedListener(this) + binding.chart1.setOnChartValueSelectedListener(this) - seekBarX!!.progress = 4 - seekBarY!!.progress = 10 + binding.seekBarX.progress = 4 + binding.seekBarY.progress = 10 - chart!!.animateY(1400, Easing.easeInOutQuad) + binding.chart1.animateY(1400, Easing.easeInOutQuad) // chart.spin(2000, 0, 360); - val l = chart!!.legend + val l = binding.chart1.legend l.verticalAlignment = Legend.LegendVerticalAlignment.TOP l.horizontalAlignment = Legend.LegendHorizontalAlignment.RIGHT l.orientation = Legend.LegendOrientation.VERTICAL @@ -104,9 +93,9 @@ class PieChartRoundedActivity : DemoBase(), OnSeekBarChangeListener, OnChartValu l.yOffset = 0f // entry label styling - chart!!.setEntryLabelColor(Color.WHITE) - chart!!.setEntryLabelTypeface(tfRegular) - chart!!.setEntryLabelTextSize(12f) + binding.chart1.setEntryLabelColor(Color.WHITE) + binding.chart1.setEntryLabelTypeface(tfRegular) + binding.chart1.setEntryLabelTextSize(12f) } private fun setData(count: Int, range: Float) { @@ -156,16 +145,16 @@ class PieChartRoundedActivity : DemoBase(), OnSeekBarChangeListener, OnChartValu data.setValueTextSize(11f) data.setValueTextColor(Color.WHITE) data.setValueTypeface(tfLight) - chart!!.setData(data) + binding.chart1.setData(data) // undo all highlights - chart!!.highlightValues(null) + binding.chart1.highlightValues(null) - val renderer = chart!!.renderer as PieChartRenderer + val renderer = binding.chart1.renderer as PieChartRenderer renderer.roundedCornerRadius = 30f dataSet.sliceSpace = renderer.roundedCornerRadius / 2 - chart?.invalidate() + binding.chart1.invalidate() } override fun onCreateOptionsMenu(menu: Menu?): Boolean { @@ -182,79 +171,79 @@ class PieChartRoundedActivity : DemoBase(), OnSeekBarChangeListener, OnChartValu } R.id.actionToggleValues -> { - chart!!.data!!.dataSets.forEach { + binding.chart1.data!!.dataSets.forEach { it?.isDrawValues = !it.isDrawValues } - chart?.invalidate() + binding.chart1.invalidate() } R.id.actionToggleIcons -> { - for (set in chart!!.data!!.getDataSets()) + for (set in binding.chart1.data!!.getDataSets()) set?.isDrawIcons = !set.isDrawIcons - chart?.invalidate() + binding.chart1.invalidate() } R.id.actionToggleHole -> { - chart!!.isDrawHoleEnabled = !chart!!.isDrawHoleEnabled - chart?.invalidate() + binding.chart1.isDrawHoleEnabled = !binding.chart1.isDrawHoleEnabled + binding.chart1.invalidate() } R.id.actionToggleMinAngles -> { - if (chart!!.minAngleForSlices == 0f) chart!!.setMinAngleForSlices(36f) - else chart!!.setMinAngleForSlices(0f) - chart?.notifyDataSetChanged() - chart?.invalidate() + if (binding.chart1.minAngleForSlices == 0f) binding.chart1.setMinAngleForSlices(36f) + else binding.chart1.setMinAngleForSlices(0f) + binding.chart1.notifyDataSetChanged() + binding.chart1.invalidate() } R.id.actionToggleCurvedSlices -> { - val toSet = !chart!!.isDrawRoundedSlicesEnabled || !chart!!.isDrawHoleEnabled - chart!!.setDrawRoundedSlices(toSet) - if (toSet && !chart!!.isDrawHoleEnabled) { - chart!!.isDrawHoleEnabled = true + val toSet = !binding.chart1.isDrawRoundedSlicesEnabled || !binding.chart1.isDrawHoleEnabled + binding.chart1.setDrawRoundedSlices(toSet) + if (toSet && !binding.chart1.isDrawHoleEnabled) { + binding.chart1.isDrawHoleEnabled = true } - if (toSet && chart!!.isDrawSlicesUnderHoleEnabled) { - chart!!.setDrawSlicesUnderHole(false) + if (toSet && binding.chart1.isDrawSlicesUnderHoleEnabled) { + binding.chart1.setDrawSlicesUnderHole(false) } - chart?.invalidate() + binding.chart1.invalidate() } R.id.actionDrawCenter -> { - chart!!.setDrawCenterText(!chart!!.isDrawCenterTextEnabled) - chart?.invalidate() + binding.chart1.setDrawCenterText(!binding.chart1.isDrawCenterTextEnabled) + binding.chart1.invalidate() } R.id.actionToggleXValues -> { - chart!!.setDrawEntryLabels(!chart!!.isDrawEntryLabelsEnabled) - chart?.invalidate() + binding.chart1.setDrawEntryLabels(!binding.chart1.isDrawEntryLabelsEnabled) + binding.chart1.invalidate() } R.id.actionTogglePercent -> { - chart!!.setUsePercentValues(!chart!!.isUsePercentValuesEnabled) - chart?.invalidate() + binding.chart1.setUsePercentValues(!binding.chart1.isUsePercentValuesEnabled) + binding.chart1.invalidate() } R.id.animateX -> { - chart!!.animateX(1400) + binding.chart1.animateX(1400) } R.id.animateY -> { - chart!!.animateY(1400) + binding.chart1.animateY(1400) } R.id.animateXY -> { - chart!!.animateXY(1400, 1400) + binding.chart1.animateXY(1400, 1400) } R.id.actionToggleSpin -> { - chart!!.spin(1000, chart!!.rotationAngle, chart!!.rotationAngle + 360, Easing.easeInOutCubic) + binding.chart1.spin(1000, binding.chart1.rotationAngle, binding.chart1.rotationAngle + 360, Easing.easeInOutCubic) } R.id.actionSave -> { if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) { saveToGallery() } else { - requestStoragePermission(chart) + requestStoragePermission(binding.chart1) } } } @@ -262,14 +251,14 @@ class PieChartRoundedActivity : DemoBase(), OnSeekBarChangeListener, OnChartValu } override fun onProgressChanged(seekBar: SeekBar?, progress: Int, fromUser: Boolean) { - tvX!!.text = seekBarX!!.progress.toString() - tvY!!.text = seekBarY!!.progress.toString() + binding.tvXMax.text = binding.seekBarX.progress.toString() + binding.tvYMax.text = binding.seekBarY.progress.toString() - setData(seekBarX!!.progress, seekBarY!!.progress.toFloat()) + setData(binding.seekBarX.progress, binding.seekBarY.progress.toFloat()) } override fun saveToGallery() { - saveToGallery(chart, "PieChartActivity") + saveToGallery(binding.chart1, "PieChartActivity") } private fun generateCenterSpannableText(): SpannableString { diff --git a/app/src/main/kotlin/info/appdev/chartexample/PiePolylineChartActivity.kt b/app/src/main/kotlin/info/appdev/chartexample/PiePolylineChartActivity.kt index 94eaba0ad..c65028cd4 100644 --- a/app/src/main/kotlin/info/appdev/chartexample/PiePolylineChartActivity.kt +++ b/app/src/main/kotlin/info/appdev/chartexample/PiePolylineChartActivity.kt @@ -14,11 +14,9 @@ import android.view.Menu import android.view.MenuItem import android.widget.SeekBar import android.widget.SeekBar.OnSeekBarChangeListener -import android.widget.TextView import androidx.core.content.ContextCompat import androidx.core.net.toUri import com.github.mikephil.charting.animation.Easing -import com.github.mikephil.charting.charts.PieChart import com.github.mikephil.charting.components.Legend import com.github.mikephil.charting.data.Entry import com.github.mikephil.charting.data.PieData @@ -29,74 +27,65 @@ import com.github.mikephil.charting.highlight.Highlight import com.github.mikephil.charting.listener.OnChartValueSelectedListener import com.github.mikephil.charting.utils.ColorTemplate import info.appdev.chartexample.DataTools.Companion.getValues +import info.appdev.chartexample.databinding.ActivityPiechartBinding import info.appdev.chartexample.notimportant.DemoBase import timber.log.Timber class PiePolylineChartActivity : DemoBase(), OnSeekBarChangeListener, OnChartValueSelectedListener { - private var chart: PieChart? = null - private var seekBarX: SeekBar? = null - private var seekBarY: SeekBar? = null - private var tvX: TextView? = null - private var tvY: TextView? = null private var tf: Typeface? = null + private lateinit var binding: ActivityPiechartBinding + override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) - setContentView(R.layout.activity_piechart) - - tvX = findViewById(R.id.tvXMax) - tvY = findViewById(R.id.tvYMax) - - seekBarX = findViewById(R.id.seekBarX) - seekBarY = findViewById(R.id.seekBarY) - - seekBarX!!.setOnSeekBarChangeListener(this) - seekBarY!!.setOnSeekBarChangeListener(this) + binding = ActivityPiechartBinding.inflate(layoutInflater) + setContentView(binding.root) + binding.seekBarX.setOnSeekBarChangeListener(this) + binding.seekBarY.setOnSeekBarChangeListener(this) - chart = findViewById(R.id.chart1) - chart!!.setUsePercentValues(true) - chart!!.description.isEnabled = false - chart!!.setExtraOffsets(5f, 10f, 5f, 5f) + binding.chart1.setUsePercentValues(true) + binding.chart1.description.isEnabled = false + binding.chart1.setExtraOffsets(5f, 10f, 5f, 5f) - chart!!.setDragDecelerationFrictionCoef(0.95f) + binding.chart1.setDragDecelerationFrictionCoef(0.95f) tf = Typeface.createFromAsset(assets, "OpenSans-Regular.ttf") - chart!!.setCenterTextTypeface(Typeface.createFromAsset(assets, "OpenSans-Light.ttf")) - chart!!.centerText = generateCenterSpannableText() + binding.chart1.setCenterTextTypeface(Typeface.createFromAsset(assets, "OpenSans-Light.ttf")) + binding.chart1.centerText = generateCenterSpannableText() - chart!!.setExtraOffsets(20f, 0f, 20f, 0f) + binding.chart1.setExtraOffsets(20f, 0f, 20f, 0f) - chart!!.isDrawHoleEnabled = true - chart!!.setHoleColor(Color.WHITE) + binding.chart1.isDrawHoleEnabled = true + binding.chart1.setHoleColor(Color.WHITE) - chart!!.setTransparentCircleColor(Color.WHITE) - chart!!.setTransparentCircleAlpha(110) + binding.chart1.setTransparentCircleColor(Color.WHITE) + binding.chart1.setTransparentCircleAlpha(110) - chart!!.holeRadius = 58f - chart!!.transparentCircleRadius = 61f + binding.chart1.holeRadius = 58f + binding.chart1.transparentCircleRadius = 61f - chart!!.setDrawCenterText(true) + binding.chart1.setDrawCenterText(true) - chart!!.rotationAngle = 0f + binding.chart1.rotationAngle = 0f // enable rotation of the chart by touch - chart!!.isRotationEnabled = true - chart!!.isHighlightPerTapEnabled = true + binding.chart1.isRotationEnabled = true + binding.chart1.isHighlightPerTapEnabled = true // chart.setUnit(" €"); // chart.setDrawUnitsInChart(true); // add a selection listener - chart!!.setOnChartValueSelectedListener(this) + binding.chart1.setOnChartValueSelectedListener(this) - seekBarX!!.progress = 4 - seekBarY!!.progress = 100 + binding.seekBarX.progress = 4 + binding.seekBarY.progress = 100 - chart!!.animateY(1400, Easing.easeInOutQuad) + binding.chart1.animateY(1400, Easing.easeInOutQuad) // chart.spin(2000, 0, 360); - val l = chart!!.legend + val l = binding.chart1.legend l.verticalAlignment = Legend.LegendVerticalAlignment.TOP l.horizontalAlignment = Legend.LegendHorizontalAlignment.RIGHT l.orientation = Legend.LegendOrientation.VERTICAL @@ -149,12 +138,12 @@ class PiePolylineChartActivity : DemoBase(), OnSeekBarChangeListener, OnChartVal data.setValueTextSize(11f) data.setValueTextColor(Color.BLACK) data.setValueTypeface(tf) - chart!!.setData(data) + binding.chart1.setData(data) // undo all highlights - chart!!.highlightValues(null) + binding.chart1.highlightValues(null) - chart?.invalidate() + binding.chart1.invalidate() } override fun onCreateOptionsMenu(menu: Menu?): Boolean { @@ -171,72 +160,72 @@ class PiePolylineChartActivity : DemoBase(), OnSeekBarChangeListener, OnChartVal } R.id.actionToggleValues -> { - chart!!.data!!.dataSets.forEach { + binding.chart1.data!!.dataSets.forEach { it?.isDrawValues = !it.isDrawValues } - chart?.invalidate() + binding.chart1.invalidate() } R.id.actionToggleHole -> { - chart!!.isDrawHoleEnabled = !chart!!.isDrawHoleEnabled - chart?.invalidate() + binding.chart1.isDrawHoleEnabled = !binding.chart1.isDrawHoleEnabled + binding.chart1.invalidate() } R.id.actionToggleMinAngles -> { - if (chart!!.minAngleForSlices == 0f) chart!!.setMinAngleForSlices(36f) - else chart!!.setMinAngleForSlices(0f) - chart?.notifyDataSetChanged() - chart?.invalidate() + if (binding.chart1.minAngleForSlices == 0f) binding.chart1.setMinAngleForSlices(36f) + else binding.chart1.setMinAngleForSlices(0f) + binding.chart1.notifyDataSetChanged() + binding.chart1.invalidate() } R.id.actionToggleCurvedSlices -> { - val toSet = !chart!!.isDrawRoundedSlicesEnabled || !chart!!.isDrawHoleEnabled - chart!!.setDrawRoundedSlices(toSet) - if (toSet && !chart!!.isDrawHoleEnabled) { - chart!!.isDrawHoleEnabled = true + val toSet = !binding.chart1.isDrawRoundedSlicesEnabled || !binding.chart1.isDrawHoleEnabled + binding.chart1.setDrawRoundedSlices(toSet) + if (toSet && !binding.chart1.isDrawHoleEnabled) { + binding.chart1.isDrawHoleEnabled = true } - if (toSet && chart!!.isDrawSlicesUnderHoleEnabled) { - chart!!.setDrawSlicesUnderHole(false) + if (toSet && binding.chart1.isDrawSlicesUnderHoleEnabled) { + binding.chart1.setDrawSlicesUnderHole(false) } - chart?.invalidate() + binding.chart1.invalidate() } R.id.actionDrawCenter -> { - chart!!.setDrawCenterText(!chart!!.isDrawCenterTextEnabled) - chart?.invalidate() + binding.chart1.setDrawCenterText(!binding.chart1.isDrawCenterTextEnabled) + binding.chart1.invalidate() } R.id.actionToggleXValues -> { - chart!!.setDrawEntryLabels(!chart!!.isDrawEntryLabelsEnabled) - chart?.invalidate() + binding.chart1.setDrawEntryLabels(!binding.chart1.isDrawEntryLabelsEnabled) + binding.chart1.invalidate() } R.id.actionTogglePercent -> { - chart!!.setUsePercentValues(!chart!!.isUsePercentValuesEnabled) - chart?.invalidate() + binding.chart1.setUsePercentValues(!binding.chart1.isUsePercentValuesEnabled) + binding.chart1.invalidate() } R.id.animateX -> { - chart!!.animateX(1400) + binding.chart1.animateX(1400) } R.id.animateY -> { - chart!!.animateY(1400) + binding.chart1.animateY(1400) } R.id.animateXY -> { - chart!!.animateXY(1400, 1400) + binding.chart1.animateXY(1400, 1400) } R.id.actionToggleSpin -> { - chart!!.spin(1000, chart!!.rotationAngle, chart!!.rotationAngle + 360, Easing.easeInOutCubic) + binding.chart1.spin(1000, binding.chart1.rotationAngle, binding.chart1.rotationAngle + 360, Easing.easeInOutCubic) } R.id.actionSave -> { if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) { saveToGallery() } else { - requestStoragePermission(chart) + requestStoragePermission(binding.chart1) } } } @@ -244,14 +233,14 @@ class PiePolylineChartActivity : DemoBase(), OnSeekBarChangeListener, OnChartVal } override fun onProgressChanged(seekBar: SeekBar?, progress: Int, fromUser: Boolean) { - tvX!!.text = seekBarX!!.progress.toString() - tvY!!.text = seekBarY!!.progress.toString() + binding.tvXMax.text = binding.seekBarX.progress.toString() + binding.tvYMax.text = binding.seekBarY.progress.toString() - setData(seekBarX!!.progress, seekBarY!!.progress.toFloat()) + setData(binding.seekBarX.progress, binding.seekBarY.progress.toFloat()) } override fun saveToGallery() { - saveToGallery(chart, "PiePolylineChartActivity") + saveToGallery(binding.chart1, "PiePolylineChartActivity") } private fun generateCenterSpannableText(): SpannableString { diff --git a/app/src/main/kotlin/info/appdev/chartexample/RadarChartActivity.kt b/app/src/main/kotlin/info/appdev/chartexample/RadarChartActivity.kt index d72faf595..299fcf316 100644 --- a/app/src/main/kotlin/info/appdev/chartexample/RadarChartActivity.kt +++ b/app/src/main/kotlin/info/appdev/chartexample/RadarChartActivity.kt @@ -10,7 +10,6 @@ import android.view.MenuItem import androidx.core.content.ContextCompat import androidx.core.net.toUri import com.github.mikephil.charting.animation.Easing -import com.github.mikephil.charting.charts.RadarChart import com.github.mikephil.charting.components.AxisBase import com.github.mikephil.charting.components.Legend import com.github.mikephil.charting.components.MarkerView @@ -21,37 +20,38 @@ import com.github.mikephil.charting.formatter.IAxisValueFormatter import com.github.mikephil.charting.interfaces.datasets.IRadarDataSet import info.appdev.chartexample.DataTools.Companion.getValues import info.appdev.chartexample.custom.RadarMarkerView +import info.appdev.chartexample.databinding.ActivityRadarchartBinding import info.appdev.chartexample.notimportant.DemoBase class RadarChartActivity : DemoBase() { - private var chart: RadarChart? = null + + private lateinit var binding: ActivityRadarchartBinding override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) - setContentView(R.layout.activity_radarchart) - - chart = findViewById(R.id.chart1) - chart!!.setBackgroundColor(Color.rgb(60, 65, 82)) + binding = ActivityRadarchartBinding.inflate(layoutInflater) + setContentView(binding.root) - chart!!.description.isEnabled = false + binding.chart1.setBackgroundColor(Color.rgb(60, 65, 82)) + binding.chart1.description.isEnabled = false - chart!!.webLineWidth = 1f - chart!!.webColor = Color.LTGRAY - chart!!.webLineWidthInner = 1f - chart!!.webColorInner = Color.LTGRAY - chart!!.webAlpha = 100 + binding.chart1.webLineWidth = 1f + binding.chart1.webColor = Color.LTGRAY + binding.chart1.webLineWidthInner = 1f + binding.chart1.webColorInner = Color.LTGRAY + binding.chart1.webAlpha = 100 // create a custom MarkerView (extend MarkerView) and specify the layout // to use for it val mv: MarkerView = RadarMarkerView(this, R.layout.radar_markerview) - mv.chartView = chart // For bounds control - chart!!.setMarker(mv) // Set the marker to the chart + mv.chartView = binding.chart1 // For bounds control + binding.chart1.setMarker(mv) // Set the marker to the chart setData() - chart!!.animateXY(1400, 1400, Easing.easeInOutQuad) + binding.chart1.animateXY(1400, 1400, Easing.easeInOutQuad) - val xAxis = chart!!.xAxis + val xAxis = binding.chart1.xAxis xAxis.typeface = tfLight xAxis.textSize = 9f xAxis.yOffset = 0f @@ -65,7 +65,7 @@ class RadarChartActivity : DemoBase() { } xAxis.textColor = Color.WHITE - val yAxis = chart!!.yAxis + val yAxis = binding.chart1.yAxis yAxis.typeface = tfLight yAxis.setLabelCount(5, false) yAxis.textSize = 9f @@ -73,7 +73,7 @@ class RadarChartActivity : DemoBase() { yAxis.axisMaximum = 80f yAxis.setDrawLabels(false) - val l = chart!!.legend + val l = binding.chart1.legend l.verticalAlignment = Legend.LegendVerticalAlignment.TOP l.horizontalAlignment = Legend.LegendHorizontalAlignment.CENTER l.orientation = Legend.LegendOrientation.HORIZONTAL @@ -131,15 +131,15 @@ class RadarChartActivity : DemoBase() { data.setDrawValues(false) data.setValueTextColor(Color.WHITE) - chart!!.setData(data) + binding.chart1.setData(data) val colorList: MutableList = ArrayList() colorList.add(Color.rgb(222, 166, 111)) colorList.add(Color.rgb(220, 206, 138)) colorList.add(Color.rgb(243, 255, 192)) colorList.add(Color.rgb(240, 255, 240)) colorList.add(Color.rgb(250, 255, 250)) - chart!!.setLayerColorList(colorList) - chart?.invalidate() + binding.chart1.setLayerColorList(colorList) + binding.chart1.invalidate() } override fun onCreateOptionsMenu(menu: Menu?): Boolean { @@ -156,71 +156,71 @@ class RadarChartActivity : DemoBase() { } R.id.actionToggleValues -> { - chart!!.data!!.dataSets.forEach { + binding.chart1.data!!.dataSets.forEach { it?.isDrawValues = !it.isDrawValues } - chart?.invalidate() + binding.chart1.invalidate() } R.id.actionToggleHighlight -> { - if (chart!!.data != null) { - chart!!.data!!.isHighlightEnabled = !chart!!.data!!.isHighlightEnabled() - chart?.invalidate() + if (binding.chart1.data != null) { + binding.chart1.data!!.isHighlightEnabled = !binding.chart1.data!!.isHighlightEnabled() + binding.chart1.invalidate() } } R.id.actionToggleRotate -> { - chart!!.isRotationEnabled = !chart!!.isRotationEnabled - chart?.invalidate() + binding.chart1.isRotationEnabled = !binding.chart1.isRotationEnabled + binding.chart1.invalidate() } R.id.actionToggleFilled -> { - chart!!.data!!.dataSets.forEach { set -> + binding.chart1.data!!.dataSets.forEach { set -> set?.setDrawFilled(!set.isDrawFilledEnabled) } - chart?.invalidate() + binding.chart1.invalidate() } R.id.actionToggleHighlightCircle -> { - chart!!.data!!.dataSets.forEach { set -> + binding.chart1.data!!.dataSets.forEach { set -> set.isDrawHighlightCircleEnabled = !set.isDrawHighlightCircleEnabled } - chart?.invalidate() + binding.chart1.invalidate() } R.id.actionToggleXLabels -> { - chart!!.xAxis.isEnabled = !chart!!.xAxis.isEnabled - chart?.notifyDataSetChanged() - chart?.invalidate() + binding.chart1.xAxis.isEnabled = !binding.chart1.xAxis.isEnabled + binding.chart1.notifyDataSetChanged() + binding.chart1.invalidate() } R.id.actionToggleYLabels -> { - chart!!.yAxis.isEnabled = !chart!!.yAxis.isEnabled - chart?.invalidate() + binding.chart1.yAxis.isEnabled = !binding.chart1.yAxis.isEnabled + binding.chart1.invalidate() } R.id.animateX -> { - chart!!.animateX(1400) + binding.chart1.animateX(1400) } R.id.animateY -> { - chart!!.animateY(1400) + binding.chart1.animateY(1400) } R.id.animateXY -> { - chart!!.animateXY(1400, 1400) + binding.chart1.animateXY(1400, 1400) } R.id.actionToggleSpin -> { - chart!!.spin(2000, chart!!.rotationAngle, chart!!.rotationAngle + 360, Easing.easeInOutCubic) + binding.chart1.spin(2000, binding.chart1.rotationAngle, binding.chart1.rotationAngle + 360, Easing.easeInOutCubic) } R.id.actionSave -> { if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) { saveToGallery() } else { - requestStoragePermission(chart) + requestStoragePermission(binding.chart1) } } } @@ -228,6 +228,6 @@ class RadarChartActivity : DemoBase() { } override fun saveToGallery() { - saveToGallery(chart, "RadarChartActivity") + saveToGallery(binding.chart1, "RadarChartActivity") } } diff --git a/app/src/main/kotlin/info/appdev/chartexample/RealtimeLineChartActivity.kt b/app/src/main/kotlin/info/appdev/chartexample/RealtimeLineChartActivity.kt index 37ab52739..328643354 100644 --- a/app/src/main/kotlin/info/appdev/chartexample/RealtimeLineChartActivity.kt +++ b/app/src/main/kotlin/info/appdev/chartexample/RealtimeLineChartActivity.kt @@ -10,7 +10,6 @@ import android.view.MenuItem import android.widget.Toast import androidx.core.content.ContextCompat import androidx.core.net.toUri -import com.github.mikephil.charting.charts.LineChart import com.github.mikephil.charting.components.Legend.LegendForm import com.github.mikephil.charting.components.YAxis.AxisDependency import com.github.mikephil.charting.data.Entry @@ -20,71 +19,71 @@ import com.github.mikephil.charting.highlight.Highlight import com.github.mikephil.charting.listener.OnChartValueSelectedListener import com.github.mikephil.charting.utils.ColorTemplate import info.appdev.chartexample.DataTools.Companion.getValues +import info.appdev.chartexample.databinding.ActivityRealtimeLinechartBinding import info.appdev.chartexample.notimportant.DemoBase import timber.log.Timber class RealtimeLineChartActivity : DemoBase(), OnChartValueSelectedListener { - private var chart: LineChart? = null var sampleValues: Array = getValues(102) + private lateinit var binding: ActivityRealtimeLinechartBinding + override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) - setContentView(R.layout.activity_realtime_linechart) - - chart = findViewById(R.id.chart1) - chart!!.setOnChartValueSelectedListener(this) + binding = ActivityRealtimeLinechartBinding.inflate(layoutInflater) + setContentView(binding.root) - // enable description text - chart!!.description.isEnabled = true + binding.chart1.setOnChartValueSelectedListener(this) + binding.chart1.description.isEnabled = true // enable touch gestures - chart!!.setTouchEnabled(true) + binding.chart1.setTouchEnabled(true) // enable scaling and dragging - chart!!.setDragEnabled(true) - chart!!.setScaleEnabled(true) - chart!!.setDrawGridBackground(false) + binding.chart1.setDragEnabled(true) + binding.chart1.setScaleEnabled(true) + binding.chart1.setDrawGridBackground(false) // if disabled, scaling can be done on x- and y-axis separately - chart!!.setPinchZoom(true) + binding.chart1.setPinchZoom(true) // set an alternative background color - chart!!.setBackgroundColor(Color.LTGRAY) + binding.chart1.setBackgroundColor(Color.LTGRAY) val data = LineData() data.setValueTextColor(Color.WHITE) // add empty data - chart!!.setData(data) + binding.chart1.setData(data) // get the legend (only possible after setting data) - val l = chart!!.legend + val l = binding.chart1.legend // modify the legend ... l.form = LegendForm.LINE l.typeface = tfLight l.textColor = Color.WHITE - val xl = chart!!.xAxis + val xl = binding.chart1.xAxis xl.typeface = tfLight xl.textColor = Color.WHITE xl.setDrawGridLines(false) xl.setAvoidFirstLastClipping(true) xl.isEnabled = true - val leftAxis = chart!!.axisLeft + val leftAxis = binding.chart1.axisLeft leftAxis.typeface = tfLight leftAxis.textColor = Color.WHITE leftAxis.axisMaximum = 100f leftAxis.axisMinimum = 0f leftAxis.setDrawGridLines(true) - val rightAxis = chart!!.axisRight + val rightAxis = binding.chart1.axisRight rightAxis.isEnabled = false } private fun addEntry() { - val data = chart!!.data + val data = binding.chart1.data if (data != null) { var set = data.getDataSetByIndex(0) @@ -100,15 +99,15 @@ class RealtimeLineChartActivity : DemoBase(), OnChartValueSelectedListener { data.notifyDataChanged() // let the chart know it's data has changed - chart?.notifyDataSetChanged() + binding.chart1.notifyDataSetChanged() // limit the number of visible entries - chart!!.setVisibleXRangeMaximum(120f) + binding.chart1.setVisibleXRangeMaximum(120f) // chart.setVisibleYRange(30, AxisDependency.LEFT); // move to the latest entry - chart!!.moveViewToX(data.getEntryCount().toFloat()) + binding.chart1.moveViewToX(data.getEntryCount().toFloat()) // this automatically refreshes the chart (calls invalidate()) // chart.moveViewTo(data.getXValCount()-7, 55f, @@ -175,7 +174,7 @@ class RealtimeLineChartActivity : DemoBase(), OnChartValueSelectedListener { } R.id.actionClear -> { - chart!!.clearValues() + binding.chart1.clearValues() Toast.makeText(this, "Chart cleared!", Toast.LENGTH_SHORT).show() } @@ -187,7 +186,7 @@ class RealtimeLineChartActivity : DemoBase(), OnChartValueSelectedListener { if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) { saveToGallery() } else { - requestStoragePermission(chart) + requestStoragePermission(binding.chart1) } } } @@ -195,7 +194,7 @@ class RealtimeLineChartActivity : DemoBase(), OnChartValueSelectedListener { } override fun saveToGallery() { - saveToGallery(chart, "RealtimeLineChartActivity") + saveToGallery(binding.chart1, "RealtimeLineChartActivity") } override fun onValueSelected(entry: Entry, highlight: Highlight) { diff --git a/app/src/main/kotlin/info/appdev/chartexample/ScatterChartActivity.kt b/app/src/main/kotlin/info/appdev/chartexample/ScatterChartActivity.kt index 74f06d356..f6b6cf448 100644 --- a/app/src/main/kotlin/info/appdev/chartexample/ScatterChartActivity.kt +++ b/app/src/main/kotlin/info/appdev/chartexample/ScatterChartActivity.kt @@ -8,7 +8,6 @@ import android.view.Menu import android.view.MenuItem import android.widget.SeekBar import android.widget.SeekBar.OnSeekBarChangeListener -import android.widget.TextView import androidx.core.content.ContextCompat import androidx.core.net.toUri import com.github.mikephil.charting.charts.ScatterChart @@ -22,48 +21,39 @@ import com.github.mikephil.charting.listener.OnChartValueSelectedListener import com.github.mikephil.charting.utils.ColorTemplate import info.appdev.chartexample.DataTools.Companion.getValues import info.appdev.chartexample.custom.CustomScatterShapeRenderer +import info.appdev.chartexample.databinding.ActivityScatterchartBinding import info.appdev.chartexample.notimportant.DemoBase import timber.log.Timber class ScatterChartActivity : DemoBase(), OnSeekBarChangeListener, OnChartValueSelectedListener { - private var chart: ScatterChart? = null - private var seekBarX: SeekBar? = null - private var seekBarY: SeekBar? = null - private var tvX: TextView? = null - private var tvY: TextView? = null + + private lateinit var binding: ActivityScatterchartBinding override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) - setContentView(R.layout.activity_scatterchart) - - tvX = findViewById(R.id.tvXMax) - tvY = findViewById(R.id.tvYMax) - - seekBarX = findViewById(R.id.seekBarX) - seekBarX!!.setOnSeekBarChangeListener(this) - - seekBarY = findViewById(R.id.seekBarY) - seekBarY!!.setOnSeekBarChangeListener(this) + binding = ActivityScatterchartBinding.inflate(layoutInflater) + setContentView(binding.root) - chart = findViewById(R.id.chart1) - chart!!.description.isEnabled = false - chart!!.setOnChartValueSelectedListener(this) + binding.seekBarX.setOnSeekBarChangeListener(this) + binding.seekBarY.setOnSeekBarChangeListener(this) + binding.chart1.description.isEnabled = false + binding.chart1.setOnChartValueSelectedListener(this) - chart!!.setDrawGridBackground(false) - chart!!.setTouchEnabled(true) - chart!!.maxHighlightDistance = 50f + binding.chart1.setDrawGridBackground(false) + binding.chart1.setTouchEnabled(true) + binding.chart1.maxHighlightDistance = 50f // enable scaling and dragging - chart!!.setDragEnabled(true) - chart!!.setScaleEnabled(true) + binding.chart1.setDragEnabled(true) + binding.chart1.setScaleEnabled(true) - chart!!.setMaxVisibleValueCount(200) - chart!!.setPinchZoom(true) + binding.chart1.setMaxVisibleValueCount(200) + binding.chart1.setPinchZoom(true) - seekBarX!!.progress = 45 - seekBarY!!.progress = 100 + binding.seekBarX.progress = 45 + binding.seekBarY.progress = 100 - val l = chart!!.legend + val l = binding.chart1.legend l.verticalAlignment = Legend.LegendVerticalAlignment.TOP l.horizontalAlignment = Legend.LegendHorizontalAlignment.RIGHT l.orientation = Legend.LegendOrientation.VERTICAL @@ -71,38 +61,38 @@ class ScatterChartActivity : DemoBase(), OnSeekBarChangeListener, OnChartValueSe l.typeface = tfLight l.xOffset = 5f - val yl = chart!!.axisLeft + val yl = binding.chart1.axisLeft yl.typeface = tfLight yl.axisMinimum = 0f // this replaces setStartAtZero(true) - chart!!.axisRight.isEnabled = false + binding.chart1.axisRight.isEnabled = false - val xl = chart!!.xAxis + val xl = binding.chart1.xAxis xl.typeface = tfLight xl.setDrawGridLines(false) } override fun onProgressChanged(seekBar: SeekBar?, progress: Int, fromUser: Boolean) { - tvX!!.text = seekBarX!!.progress.toString() - tvY!!.text = seekBarY!!.progress.toString() + binding.tvXMax.text = binding.seekBarX.progress.toString() + binding.tvYMax.text = binding.seekBarY.progress.toString() val values1 = ArrayList() val values2 = ArrayList() val values3 = ArrayList() val sampleValues = getValues(100 + 2) - for (i in 0.. { - chart!!.data!!.dataSets.forEach { set -> + binding.chart1.data!!.dataSets.forEach { set -> set?.isDrawValues = !set.isDrawValues } - chart?.invalidate() + binding.chart1.invalidate() } R.id.actionToggleHighlight -> { - if (chart!!.data != null) { - chart!!.data!!.isHighlightEnabled = !chart!!.data!!.isHighlightEnabled() - chart?.invalidate() + if (binding.chart1.data != null) { + binding.chart1.data!!.isHighlightEnabled = !binding.chart1.data!!.isHighlightEnabled() + binding.chart1.invalidate() } } R.id.actionTogglePinch -> { - chart!!.setPinchZoom(!chart!!.isPinchZoomEnabled) + binding.chart1.setPinchZoom(!binding.chart1.isPinchZoomEnabled) - chart?.invalidate() + binding.chart1.invalidate() } R.id.actionToggleAutoScaleMinMax -> { - chart!!.isAutoScaleMinMaxEnabled = !chart!!.isAutoScaleMinMaxEnabled - chart?.notifyDataSetChanged() + binding.chart1.isAutoScaleMinMaxEnabled = !binding.chart1.isAutoScaleMinMaxEnabled + binding.chart1.notifyDataSetChanged() } R.id.animateX -> { - chart!!.animateX(3000) + binding.chart1.animateX(3000) } R.id.animateY -> { - chart!!.animateY(3000) + binding.chart1.animateY(3000) } R.id.animateXY -> { - chart!!.animateXY(3000, 3000) + binding.chart1.animateXY(3000, 3000) } R.id.actionSave -> { if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) { saveToGallery() } else { - requestStoragePermission(chart) + requestStoragePermission(binding.chart1) } } } @@ -198,7 +188,7 @@ class ScatterChartActivity : DemoBase(), OnSeekBarChangeListener, OnChartValueSe } override fun saveToGallery() { - saveToGallery(chart, "ScatterChartActivity") + saveToGallery(binding.chart1, "ScatterChartActivity") } override fun onValueSelected(entry: Entry, highlight: Highlight) { diff --git a/app/src/main/kotlin/info/appdev/chartexample/ScrollViewActivity.kt b/app/src/main/kotlin/info/appdev/chartexample/ScrollViewActivity.kt index dfde6c005..ab93670c2 100644 --- a/app/src/main/kotlin/info/appdev/chartexample/ScrollViewActivity.kt +++ b/app/src/main/kotlin/info/appdev/chartexample/ScrollViewActivity.kt @@ -5,42 +5,42 @@ import android.os.Bundle import android.view.Menu import android.view.MenuItem import androidx.core.net.toUri -import com.github.mikephil.charting.charts.BarChart import com.github.mikephil.charting.components.XAxis.XAxisPosition import com.github.mikephil.charting.data.BarData import com.github.mikephil.charting.data.BarDataSet import com.github.mikephil.charting.data.BarEntry import com.github.mikephil.charting.utils.ColorTemplate import info.appdev.chartexample.DataTools.Companion.getValues +import info.appdev.chartexample.databinding.ActivityScrollviewBinding import info.appdev.chartexample.notimportant.DemoBase class ScrollViewActivity : DemoBase() { - private var chart: BarChart? = null + + private lateinit var binding: ActivityScrollviewBinding override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) - setContentView(R.layout.activity_scrollview) - - chart = findViewById(R.id.chart1) + binding = ActivityScrollviewBinding.inflate(layoutInflater) + setContentView(binding.root) - chart!!.description.isEnabled = false + binding.chart1.description.isEnabled = false // scaling can now only be done on x- and y-axis separately - chart!!.setPinchZoom(false) + binding.chart1.setPinchZoom(false) - chart!!.setDrawBarShadow(false) - chart!!.setDrawGridBackground(false) + binding.chart1.setDrawBarShadow(false) + binding.chart1.setDrawGridBackground(false) - val xAxis = chart!!.xAxis + val xAxis = binding.chart1.xAxis xAxis.position = XAxisPosition.BOTTOM xAxis.setDrawGridLines(false) - chart!!.axisLeft.setDrawGridLines(false) + binding.chart1.axisLeft.setDrawGridLines(false) - chart!!.legend.isEnabled = false + binding.chart1.legend.isEnabled = false setData(10) - chart!!.setFitBars(true) + binding.chart1.setFitBars(true) } private fun setData(count: Int) { @@ -54,13 +54,13 @@ class ScrollViewActivity : DemoBase() { val set = BarDataSet(values, "Data Set") set.setColors(*ColorTemplate.VORDIPLOM_COLORS) - set?.isDrawValues = false + set.isDrawValues = false val data = BarData(set) - chart!!.setData(data) - chart?.invalidate() - chart!!.animateY(800) + binding.chart1.setData(data) + binding.chart1.invalidate() + binding.chart1.animateY(800) } override fun onCreateOptionsMenu(menu: Menu?): Boolean { diff --git a/app/src/main/kotlin/info/appdev/chartexample/SpecificPositionsLineChartActivity.kt b/app/src/main/kotlin/info/appdev/chartexample/SpecificPositionsLineChartActivity.kt index 0abda2646..0c6b9be0f 100644 --- a/app/src/main/kotlin/info/appdev/chartexample/SpecificPositionsLineChartActivity.kt +++ b/app/src/main/kotlin/info/appdev/chartexample/SpecificPositionsLineChartActivity.kt @@ -8,14 +8,11 @@ import android.os.Bundle import android.view.Menu import android.view.MenuItem import android.view.MotionEvent -import android.view.View import android.widget.SeekBar import android.widget.SeekBar.OnSeekBarChangeListener -import android.widget.TextView import android.widget.Toast import androidx.core.content.ContextCompat import com.github.mikephil.charting.animation.Easing -import com.github.mikephil.charting.charts.LineChart import com.github.mikephil.charting.components.Legend.LegendForm import com.github.mikephil.charting.components.LimitLine import com.github.mikephil.charting.components.LimitLine.LimitLabelPosition @@ -30,47 +27,41 @@ import com.github.mikephil.charting.listener.OnChartValueSelectedListener import com.github.mikephil.charting.utils.getSDKInt import info.appdev.chartexample.DataTools.Companion.getValues import info.appdev.chartexample.custom.MyMarkerView +import info.appdev.chartexample.databinding.ActivityLinechartBinding import info.appdev.chartexample.notimportant.DemoBase import timber.log.Timber class SpecificPositionsLineChartActivity : DemoBase(), OnSeekBarChangeListener, OnChartGestureListener, OnChartValueSelectedListener { - private var mChart: LineChart? = null - private var mSeekBarX: SeekBar? = null - private var mSeekBarY: SeekBar? = null - private var tvX: TextView? = null - private var tvY: TextView? = null + private lateinit var binding: ActivityLinechartBinding override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) - setContentView(R.layout.activity_linechart) - tvX = findViewById(R.id.tvXMax) as TextView - tvY = findViewById(R.id.tvYMax) as TextView - mSeekBarX = findViewById(R.id.seekBarX) as SeekBar - mSeekBarY = findViewById(R.id.seekBarY) as SeekBar - mSeekBarX!!.progress = 45 - mSeekBarY!!.progress = 100 - mSeekBarY!!.setOnSeekBarChangeListener(this) - mSeekBarX!!.setOnSeekBarChangeListener(this) - mChart = findViewById(R.id.chart1) as LineChart - mChart!!.onChartGestureListener = this - mChart!!.setOnChartValueSelectedListener(this) - mChart!!.setDrawGridBackground(false) + binding = ActivityLinechartBinding.inflate(layoutInflater) + setContentView(binding.root) + + binding.seekBarX.progress = 45 + binding.seekBarY.progress = 100 + binding.seekBarY.setOnSeekBarChangeListener(this) + binding.seekBarX.setOnSeekBarChangeListener(this) + binding.chart1.onChartGestureListener = this + binding.chart1.setOnChartValueSelectedListener(this) + binding.chart1.setDrawGridBackground(false) // no description text - mChart!!.description.isEnabled = false + binding.chart1.description.isEnabled = false // enable touch gestures - mChart!!.setTouchEnabled(true) + binding.chart1.setTouchEnabled(true) // enable scaling and dragging - mChart!!.isDragEnabled = true - mChart!!.setScaleEnabled(true) + binding.chart1.isDragEnabled = true + binding.chart1.setScaleEnabled(true) // mChart.setScaleXEnabled(true); // mChart.setScaleYEnabled(true); // if disabled, scaling can be done on x- and y-axis separately - mChart!!.setPinchZoom(true) + binding.chart1.setPinchZoom(true) // set an alternative background color // mChart.setBackgroundColor(Color.GRAY); @@ -78,8 +69,8 @@ class SpecificPositionsLineChartActivity : DemoBase(), OnSeekBarChangeListener, // create a custom MarkerView (extend MarkerView) and specify the layout // to use for it val mv = MyMarkerView(this, R.layout.custom_marker_view) - mv.chartView = mChart // For bounds control - mChart!!.marker.add(mv) // Set the marker to the chart + mv.chartView = binding.chart1 // For bounds control + binding.chart1.marker.add(mv) // Set the marker to the chart // x-axis limit line val llXAxis = LimitLine(10f, "Index 10") @@ -87,7 +78,7 @@ class SpecificPositionsLineChartActivity : DemoBase(), OnSeekBarChangeListener, llXAxis.enableDashedLine(10f, 10f, 0f) llXAxis.labelPosition = LimitLabelPosition.RIGHT_BOTTOM llXAxis.textSize = 10f - val xAxis = mChart!!.xAxis + val xAxis = binding.chart1.xAxis xAxis.enableGridDashedLine(10f, 10f, 0f) xAxis.isShowSpecificPositions = true xAxis.specificPositions = floatArrayOf(20f, 30f, 60f) @@ -104,7 +95,7 @@ class SpecificPositionsLineChartActivity : DemoBase(), OnSeekBarChangeListener, ll2.labelPosition = LimitLabelPosition.RIGHT_BOTTOM ll2.textSize = 10f ll2.typeface = tf - val leftAxis = mChart!!.axisLeft + val leftAxis = binding.chart1.axisLeft leftAxis.removeAllLimitLines() // reset all limit lines to avoid overlapping lines leftAxis.addLimitLine(ll1) leftAxis.addLimitLine(ll2) @@ -118,13 +109,13 @@ class SpecificPositionsLineChartActivity : DemoBase(), OnSeekBarChangeListener, // limit lines are drawn behind data (and not on top) leftAxis.setDrawLimitLinesBehindData(true) - mChart!!.axisRight.isEnabled = false + binding.chart1.axisRight.isEnabled = false setData(45, 100f) - mChart!!.animateX(2500) + binding.chart1.animateX(2500) //mChart.invalidate(); // get the legend (only possible after setting data) - val l = mChart!!.legend + val l = binding.chart1.legend // modify the legend ... l.form = LegendForm.LINE @@ -138,74 +129,74 @@ class SpecificPositionsLineChartActivity : DemoBase(), OnSeekBarChangeListener, override fun onOptionsItemSelected(item: MenuItem): Boolean { when (item.itemId) { R.id.actionToggleValues -> { - mChart!!.data?.dataSets?.forEach { + binding.chart1.data?.dataSets?.forEach { it?.isDrawValues = !it.isDrawValues } - mChart!!.invalidate() + binding.chart1.invalidate() } R.id.actionToggleHighlight -> { - mChart!!.data?.let { + binding.chart1.data?.let { it.isHighlightEnabled = !it.isHighlightEnabled - mChart!!.invalidate() + binding.chart1.invalidate() } } R.id.actionToggleFilled -> { - mChart!!.data?.dataSets?.forEach { + binding.chart1.data?.dataSets?.forEach { val set = it as LineDataSet - set?.setDrawFilled(!set.isDrawFilledEnabled) + set.setDrawFilled(!set.isDrawFilledEnabled) } - mChart!!.invalidate() + binding.chart1.invalidate() } R.id.actionToggleCircles -> { - mChart!!.data?.dataSets?.forEach { + binding.chart1.data?.dataSets?.forEach { val set = it as LineDataSet set.isDrawCirclesEnabled = !set.isDrawCirclesEnabled } - mChart!!.invalidate() + binding.chart1.invalidate() } R.id.actionToggleCubic -> { - mChart!!.data?.dataSets?.forEach { + binding.chart1.data?.dataSets?.forEach { val set = it as LineDataSet set.lineMode = if (set.lineMode == LineDataSet.Mode.CUBIC_BEZIER) LineDataSet.Mode.LINEAR else LineDataSet.Mode.CUBIC_BEZIER } - mChart!!.invalidate() + binding.chart1.invalidate() } R.id.actionToggleStepped -> { - mChart!!.data?.dataSets?.forEach { + binding.chart1.data?.dataSets?.forEach { val set = it as LineDataSet set.lineMode = if (set.lineMode == LineDataSet.Mode.STEPPED) LineDataSet.Mode.LINEAR else LineDataSet.Mode.STEPPED } - mChart!!.invalidate() + binding.chart1.invalidate() } R.id.actionToggleHorizontalCubic -> { - mChart!!.data?.dataSets?.forEach { + binding.chart1.data?.dataSets?.forEach { val set = it as LineDataSet set.lineMode = if (set.lineMode == LineDataSet.Mode.HORIZONTAL_BEZIER) LineDataSet.Mode.LINEAR else LineDataSet.Mode.HORIZONTAL_BEZIER } - mChart!!.invalidate() + binding.chart1.invalidate() } R.id.actionTogglePinch -> { - mChart!!.setPinchZoom(!mChart!!.isPinchZoomEnabled) - mChart!!.invalidate() + binding.chart1.setPinchZoom(!binding.chart1.isPinchZoomEnabled) + binding.chart1.invalidate() } R.id.actionToggleAutoScaleMinMax -> { - mChart!!.isAutoScaleMinMaxEnabled = !mChart!!.isAutoScaleMinMaxEnabled - mChart!!.notifyDataSetChanged() + binding.chart1.isAutoScaleMinMaxEnabled = !binding.chart1.isAutoScaleMinMaxEnabled + binding.chart1.notifyDataSetChanged() } - R.id.animateX -> mChart!!.animateX(3000) - R.id.animateY -> mChart!!.animateY(3000, Easing.easeInCubic) - R.id.animateXY -> mChart!!.animateXY(3000, 3000) + R.id.animateX -> binding.chart1.animateX(3000) + R.id.animateY -> binding.chart1.animateY(3000, Easing.easeInCubic) + R.id.animateXY -> binding.chart1.animateXY(3000, 3000) R.id.actionSave -> { - if (mChart!!.saveToPath("title" + System.currentTimeMillis(), "")) { + if (binding.chart1.saveToPath("title" + System.currentTimeMillis(), "")) { Toast.makeText(applicationContext, "Saving SUCCESSFUL!", Toast.LENGTH_SHORT).show() } else { Toast.makeText(applicationContext, "Saving FAILED!", Toast.LENGTH_SHORT).show() @@ -219,12 +210,12 @@ class SpecificPositionsLineChartActivity : DemoBase(), OnSeekBarChangeListener, @SuppressLint("SetTextI18n") override fun onProgressChanged(seekBar: SeekBar, progress: Int, fromUser: Boolean) { - tvX!!.text = "" + (mSeekBarX!!.progress + 1) - tvY!!.text = "" + mSeekBarY!!.progress - setData(mSeekBarX!!.progress + 1, mSeekBarY!!.progress.toFloat()) + binding.tvXMax.text = "" + (binding.seekBarX.progress + 1) + binding.tvXMax.text = "" + binding.seekBarY.progress + setData(binding.seekBarX.progress + 1, binding.seekBarY.progress.toFloat()) // redraw - mChart!!.invalidate() + binding.chart1.invalidate() } override fun onStartTrackingTouch(seekBar: SeekBar) { @@ -242,12 +233,12 @@ class SpecificPositionsLineChartActivity : DemoBase(), OnSeekBarChangeListener, val `val` = (sampleValues[i]!!.toFloat() * range) + 3 values.add(Entry(i.toFloat(), `val`)) } - mChart!!.data?.let { + binding.chart1.data?.let { if (it.dataSetCount > 0) { val set1 = it.getDataSetByIndex(0) as LineDataSet set1.entries = values it.notifyDataChanged() - mChart!!.notifyDataSetChanged() + binding.chart1.notifyDataSetChanged() } else createDataset(values) } ?: run { @@ -286,7 +277,7 @@ class SpecificPositionsLineChartActivity : DemoBase(), OnSeekBarChangeListener, val data = LineData(dataSets) // set data - mChart!!.data = data + binding.chart1.data = data } override fun onChartGestureStart(me: MotionEvent, lastPerformedGesture: ChartGesture?) { @@ -298,7 +289,7 @@ class SpecificPositionsLineChartActivity : DemoBase(), OnSeekBarChangeListener, // un-highlight values after the gesture is finished and no single-tap if (lastPerformedGesture != ChartGesture.SINGLE_TAP) { - mChart!!.highlightValues(null) // or highlightTouch(null) for callback to onNothingSelected(...) + binding.chart1.highlightValues(null) // or highlightTouch(null) for callback to onNothingSelected(...) } } @@ -328,13 +319,13 @@ class SpecificPositionsLineChartActivity : DemoBase(), OnSeekBarChangeListener, override fun onValueSelected(entry: Entry, highlight: Highlight) { Timber.i(entry.toString()) - Timber.i("LOWHIGH low: ${mChart!!.lowestVisibleX}, high: ${mChart!!.highestVisibleX}") - Timber.i("MIN MAX xmin: ${mChart!!.xChartMin}, xmax: ${mChart!!.xChartMax}, ymin: ${mChart!!.yChartMin}, ymax: ${mChart!!.yChartMax}") + Timber.i("LOWHIGH low: ${binding.chart1.lowestVisibleX}, high: ${binding.chart1.highestVisibleX}") + Timber.i("MIN MAX xmin: ${binding.chart1.xChartMin}, xmax: ${binding.chart1.xChartMax}, ymin: ${binding.chart1.yChartMin}, ymax: ${binding.chart1.yChartMax}") } override fun onNothingSelected() = Unit override fun saveToGallery() { - saveToGallery(mChart, "SpecificPositionsLineChartActivity") + saveToGallery(binding.chart1, "SpecificPositionsLineChartActivity") } } diff --git a/app/src/main/kotlin/info/appdev/chartexample/StackedBarActivity.kt b/app/src/main/kotlin/info/appdev/chartexample/StackedBarActivity.kt index 430a952aa..4a64fa4b4 100644 --- a/app/src/main/kotlin/info/appdev/chartexample/StackedBarActivity.kt +++ b/app/src/main/kotlin/info/appdev/chartexample/StackedBarActivity.kt @@ -9,11 +9,9 @@ import android.view.Menu import android.view.MenuItem import android.widget.SeekBar import android.widget.SeekBar.OnSeekBarChangeListener -import android.widget.TextView import androidx.core.content.ContextCompat import androidx.core.content.res.ResourcesCompat import androidx.core.net.toUri -import com.github.mikephil.charting.charts.BarChart import com.github.mikephil.charting.components.Legend import com.github.mikephil.charting.components.XAxis.XAxisPosition import com.github.mikephil.charting.data.BarData @@ -25,66 +23,58 @@ import com.github.mikephil.charting.interfaces.datasets.IBarDataSet import com.github.mikephil.charting.listener.OnChartValueSelectedListener import com.github.mikephil.charting.utils.ColorTemplate import info.appdev.chartexample.DataTools.Companion.getValues +import info.appdev.chartexample.databinding.ActivityBarchartBinding import info.appdev.chartexample.formatter.MyAxisValueFormatter import info.appdev.chartexample.formatter.MyValueFormatter import info.appdev.chartexample.notimportant.DemoBase import timber.log.Timber class StackedBarActivity : DemoBase(), OnSeekBarChangeListener, OnChartValueSelectedListener { - private var chart: BarChart? = null - private var seekBarX: SeekBar? = null - private var seekBarY: SeekBar? = null - private var tvX: TextView? = null - private var tvY: TextView? = null + + private lateinit var binding: ActivityBarchartBinding override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) - setContentView(R.layout.activity_barchart) - - tvX = findViewById(R.id.tvXMax) - tvY = findViewById(R.id.tvYMax) - - seekBarX = findViewById(R.id.seekBarX) - seekBarX!!.setOnSeekBarChangeListener(this) + binding = ActivityBarchartBinding.inflate(layoutInflater) + setContentView(binding.root) - seekBarY = findViewById(R.id.seekBarY) - seekBarY!!.setOnSeekBarChangeListener(this) + binding.seekBarX.setOnSeekBarChangeListener(this) + binding.seekBarY.setOnSeekBarChangeListener(this) - chart = findViewById(R.id.chart1) - chart!!.setOnChartValueSelectedListener(this) + binding.chart1.setOnChartValueSelectedListener(this) - chart!!.description.isEnabled = false + binding.chart1.description.isEnabled = false // if more than 60 entries are displayed in the chart, no values will be // drawn - chart!!.setMaxVisibleValueCount(40) + binding.chart1.setMaxVisibleValueCount(40) // scaling can now only be done on x- and y-axis separately - chart!!.setPinchZoom(false) + binding.chart1.setPinchZoom(false) - chart!!.setDrawGridBackground(false) - chart!!.setDrawBarShadow(false) + binding.chart1.setDrawGridBackground(false) + binding.chart1.setDrawBarShadow(false) - chart!!.setDrawValueAboveBar(false) - chart!!.isHighlightFullBarEnabled = false + binding.chart1.setDrawValueAboveBar(false) + binding.chart1.isHighlightFullBarEnabled = false // change the position of the y-labels - val leftAxis = chart!!.axisLeft + val leftAxis = binding.chart1.axisLeft leftAxis.valueFormatter = MyAxisValueFormatter() leftAxis.axisMinimum = 0f // this replaces setStartAtZero(true) - chart!!.axisRight.isEnabled = false + binding.chart1.axisRight.isEnabled = false - val xLabels = chart!!.xAxis + val xLabels = binding.chart1.xAxis xLabels.position = XAxisPosition.TOP // chart.setDrawXLabels(false); // chart.setDrawYLabels(false); // setting data - seekBarX!!.progress = 12 - seekBarY!!.progress = 100 + binding.seekBarX.progress = 12 + binding.seekBarY.progress = 100 - val l = chart!!.legend + val l = binding.chart1.legend l.verticalAlignment = Legend.LegendVerticalAlignment.BOTTOM l.horizontalAlignment = Legend.LegendHorizontalAlignment.RIGHT l.orientation = Legend.LegendOrientation.HORIZONTAL @@ -97,14 +87,14 @@ class StackedBarActivity : DemoBase(), OnSeekBarChangeListener, OnChartValueSele } override fun onProgressChanged(seekBar: SeekBar?, progress: Int, fromUser: Boolean) { - tvX!!.text = seekBarX!!.progress.toString() - tvY!!.text = seekBarY!!.progress.toString() + binding.tvXMax.text = binding.seekBarX.progress.toString() + binding.tvYMax.text = binding.seekBarY.progress.toString() val values = ArrayList() val sampleValues = getValues(100 + 2) - for (i in 0.. 0 + if (binding.chart1.data != null && + binding.chart1.data!!.getDataSetCount() > 0 ) { - set1 = chart!!.data!!.getDataSetByIndex(0) as BarDataSet + set1 = binding.chart1.data!!.getDataSetByIndex(0) as BarDataSet set1.entries = values - chart!!.data!!.notifyDataChanged() - chart?.notifyDataSetChanged() + binding.chart1.data!!.notifyDataChanged() + binding.chart1.notifyDataSetChanged() } else { set1 = BarDataSet(values, "Statistics Vienna 2014") set1.isDrawIcons = false @@ -139,11 +129,11 @@ class StackedBarActivity : DemoBase(), OnSeekBarChangeListener, OnChartValueSele data.setValueFormatter(MyValueFormatter()) data.setValueTextColor(Color.WHITE) - chart!!.setData(data) + binding.chart1.setData(data) } - chart!!.setFitBars(true) - chart?.invalidate() + binding.chart1.setFitBars(true) + binding.chart1.invalidate() } override fun onCreateOptionsMenu(menu: Menu?): Boolean { @@ -160,60 +150,60 @@ class StackedBarActivity : DemoBase(), OnSeekBarChangeListener, OnChartValueSele } R.id.actionToggleValues -> { - chart!!.data!!.dataSets.forEach { + binding.chart1.data!!.dataSets.forEach { it?.isDrawValues = !it.isDrawValues } - chart?.invalidate() + binding.chart1.invalidate() } R.id.actionToggleIcons -> { - chart!!.data!!.dataSets.forEach { set -> + binding.chart1.data!!.dataSets.forEach { set -> set?.isDrawIcons = !set.isDrawIcons } - chart?.invalidate() + binding.chart1.invalidate() } R.id.actionToggleHighlight -> { - if (chart!!.data != null) { - chart!!.data!!.isHighlightEnabled = !chart!!.data!!.isHighlightEnabled() - chart?.invalidate() + if (binding.chart1.data != null) { + binding.chart1.data!!.isHighlightEnabled = !binding.chart1.data!!.isHighlightEnabled() + binding.chart1.invalidate() } } R.id.actionTogglePinch -> { - chart!!.setPinchZoom(!chart!!.isPinchZoomEnabled) + binding.chart1.setPinchZoom(!binding.chart1.isPinchZoomEnabled) - chart?.invalidate() + binding.chart1.invalidate() } R.id.actionToggleAutoScaleMinMax -> { - chart!!.isAutoScaleMinMaxEnabled = !chart!!.isAutoScaleMinMaxEnabled - chart?.notifyDataSetChanged() + binding.chart1.isAutoScaleMinMaxEnabled = !binding.chart1.isAutoScaleMinMaxEnabled + binding.chart1.notifyDataSetChanged() } R.id.actionToggleBarBorders -> { - for (set in chart!!.data!!.dataSets) (set as BarDataSet).barBorderWidth = if (set.barBorderWidth == 1f) 0f else 1f + for (set in binding.chart1.data!!.dataSets) (set as BarDataSet).barBorderWidth = if (set.barBorderWidth == 1f) 0f else 1f - chart?.invalidate() + binding.chart1.invalidate() } R.id.animateX -> { - chart!!.animateX(2000) + binding.chart1.animateX(2000) } R.id.animateY -> { - chart!!.animateY(2000) + binding.chart1.animateY(2000) } R.id.animateXY -> { - chart!!.animateXY(2000, 2000) + binding.chart1.animateXY(2000, 2000) } R.id.actionSave -> { if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) { saveToGallery() } else { - requestStoragePermission(chart) + requestStoragePermission(binding.chart1) } } } @@ -221,7 +211,7 @@ class StackedBarActivity : DemoBase(), OnSeekBarChangeListener, OnChartValueSele } override fun saveToGallery() { - saveToGallery(chart, "StackedBarActivity") + saveToGallery(binding.chart1, "StackedBarActivity") } override fun onStartTrackingTouch(seekBar: SeekBar?) = Unit diff --git a/app/src/main/kotlin/info/appdev/chartexample/StackedBarActivityNegative.kt b/app/src/main/kotlin/info/appdev/chartexample/StackedBarActivityNegative.kt index e3d1d74b5..70b5be8cb 100644 --- a/app/src/main/kotlin/info/appdev/chartexample/StackedBarActivityNegative.kt +++ b/app/src/main/kotlin/info/appdev/chartexample/StackedBarActivityNegative.kt @@ -10,7 +10,6 @@ import android.view.MenuItem import androidx.core.content.ContextCompat import androidx.core.content.res.ResourcesCompat import androidx.core.net.toUri -import com.github.mikephil.charting.charts.HorizontalBarChart import com.github.mikephil.charting.components.AxisBase import com.github.mikephil.charting.components.Legend import com.github.mikephil.charting.components.XAxis.XAxisPosition @@ -24,40 +23,42 @@ import com.github.mikephil.charting.formatter.IValueFormatter import com.github.mikephil.charting.highlight.Highlight import com.github.mikephil.charting.listener.OnChartValueSelectedListener import com.github.mikephil.charting.utils.ViewPortHandler +import info.appdev.chartexample.databinding.ActivityAgeDistributionBinding import info.appdev.chartexample.notimportant.DemoBase import timber.log.Timber import java.text.DecimalFormat import kotlin.math.abs class StackedBarActivityNegative : DemoBase(), OnChartValueSelectedListener { - private var chart: HorizontalBarChart? = null + + private lateinit var binding: ActivityAgeDistributionBinding override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) - setContentView(R.layout.activity_age_distribution) + binding = ActivityAgeDistributionBinding.inflate(layoutInflater) + setContentView(binding.root) - chart = findViewById(R.id.chart1) - chart!!.setOnChartValueSelectedListener(this) - chart!!.setDrawGridBackground(false) - chart!!.description.isEnabled = false + binding.chart1.setOnChartValueSelectedListener(this) + binding.chart1.setDrawGridBackground(false) + binding.chart1.description.isEnabled = false // scaling can now only be done on x- and y-axis separately - chart!!.setPinchZoom(false) - - chart!!.setDrawBarShadow(false) - chart!!.setDrawValueAboveBar(true) - chart!!.isHighlightFullBarEnabled = false - - chart!!.axisLeft.isEnabled = false - chart!!.axisRight.axisMaximum = 25f - chart!!.axisRight.axisMinimum = -25f - chart!!.axisRight.setDrawGridLines(false) - chart!!.axisRight.setDrawZeroLine(true) - chart!!.axisRight.setLabelCount(7, false) - chart!!.axisRight.valueFormatter = CustomFormatter() - chart!!.axisRight.textSize = 9f - - val xAxis = chart!!.xAxis + binding.chart1.setPinchZoom(false) + + binding.chart1.setDrawBarShadow(false) + binding.chart1.setDrawValueAboveBar(true) + binding.chart1.isHighlightFullBarEnabled = false + + binding.chart1.axisLeft.isEnabled = false + binding.chart1.axisRight.axisMaximum = 25f + binding.chart1.axisRight.axisMinimum = -25f + binding.chart1.axisRight.setDrawGridLines(false) + binding.chart1.axisRight.setDrawZeroLine(true) + binding.chart1.axisRight.setLabelCount(7, false) + binding.chart1.axisRight.valueFormatter = CustomFormatter() + binding.chart1.axisRight.textSize = 9f + + val xAxis = binding.chart1.xAxis xAxis.position = XAxisPosition.BOTH_SIDED xAxis.setDrawGridLines(false) xAxis.setDrawAxisLine(false) @@ -75,7 +76,7 @@ class StackedBarActivityNegative : DemoBase(), OnChartValueSelectedListener { } } - val l = chart!!.legend + val l = binding.chart1.legend l.verticalAlignment = Legend.LegendVerticalAlignment.BOTTOM l.horizontalAlignment = Legend.LegendHorizontalAlignment.RIGHT l.orientation = Legend.LegendOrientation.HORIZONTAL @@ -100,7 +101,7 @@ class StackedBarActivityNegative : DemoBase(), OnChartValueSelectedListener { values.add(BarEntry(105f, floatArrayOf(-1f, 2f))) val set = BarDataSet(values, "Age Distribution") - set?.isDrawIcons = false + set.isDrawIcons = false set.valueFormatter = CustomFormatter() set.valueTextSize = 7f set.axisDependency = YAxis.AxisDependency.RIGHT @@ -109,8 +110,8 @@ class StackedBarActivityNegative : DemoBase(), OnChartValueSelectedListener { val data = BarData(set) data.barWidth = 8.5f - chart!!.setData(data) - chart?.invalidate() + binding.chart1.setData(data) + binding.chart1.invalidate() } override fun onCreateOptionsMenu(menu: Menu?): Boolean { @@ -128,67 +129,67 @@ class StackedBarActivityNegative : DemoBase(), OnChartValueSelectedListener { } R.id.actionToggleValues -> { - chart!!.data!!.dataSets.forEach { + binding.chart1.data!!.dataSets.forEach { it?.isDrawValues = !it.isDrawValues } - chart?.invalidate() + binding.chart1.invalidate() } R.id.actionToggleIcons -> { - chart!!.data!!.dataSets.forEach { set -> + binding.chart1.data!!.dataSets.forEach { set -> set?.isDrawIcons = !set.isDrawIcons } - chart?.invalidate() + binding.chart1.invalidate() } R.id.actionToggleHighlight -> { - if (chart!!.data != null) { - chart!!.data!!.isHighlightEnabled = !chart!!.data!!.isHighlightEnabled() - chart?.invalidate() + if (binding.chart1.data != null) { + binding.chart1.data!!.isHighlightEnabled = !binding.chart1.data!!.isHighlightEnabled() + binding.chart1.invalidate() } } R.id.actionTogglePinch -> { - if (chart!!.isPinchZoomEnabled) { - chart!!.setPinchZoom(false) + if (binding.chart1.isPinchZoomEnabled) { + binding.chart1.setPinchZoom(false) } else { - chart!!.setPinchZoom(true) + binding.chart1.setPinchZoom(true) } - chart?.invalidate() + binding.chart1.invalidate() } R.id.actionToggleAutoScaleMinMax -> { - chart!!.isAutoScaleMinMaxEnabled = !chart!!.isAutoScaleMinMaxEnabled - chart?.notifyDataSetChanged() + binding.chart1.isAutoScaleMinMaxEnabled = !binding.chart1.isAutoScaleMinMaxEnabled + binding.chart1.notifyDataSetChanged() } R.id.actionToggleBarBorders -> { - for (set in chart!!.data!!.dataSets) { + for (set in binding.chart1.data!!.dataSets) { (set as BarDataSet).barBorderWidth = if (set.barBorderWidth == 1f) 0f else 1f } - chart?.invalidate() + binding.chart1.invalidate() } R.id.animateX -> { - chart!!.animateX(3000) + binding.chart1.animateX(3000) } R.id.animateY -> { - chart!!.animateY(3000) + binding.chart1.animateY(3000) } R.id.animateXY -> { - chart!!.animateXY(3000, 3000) + binding.chart1.animateXY(3000, 3000) } R.id.actionSave -> { if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) { saveToGallery() } else { - requestStoragePermission(chart) + requestStoragePermission(binding.chart1) } } } @@ -196,7 +197,7 @@ class StackedBarActivityNegative : DemoBase(), OnChartValueSelectedListener { } override fun saveToGallery() { - saveToGallery(chart, "StackedBarActivityNegative") + saveToGallery(binding.chart1, "StackedBarActivityNegative") } override fun onValueSelected(entry: Entry, highlight: Highlight) { diff --git a/app/src/main/kotlin/info/appdev/chartexample/compose/HorizontalBarComposeActivity.kt b/app/src/main/kotlin/info/appdev/chartexample/compose/HorizontalBarComposeActivity.kt index d1484ea07..ac126dc4c 100644 --- a/app/src/main/kotlin/info/appdev/chartexample/compose/HorizontalBarComposeActivity.kt +++ b/app/src/main/kotlin/info/appdev/chartexample/compose/HorizontalBarComposeActivity.kt @@ -377,7 +377,7 @@ class HorizontalBarComposeActivity : DemoBaseCompose() { private fun toggleValues() { chart?.let { - chart!!.data!!.dataSets.forEach { + it.data!!.dataSets.forEach { it?.isDrawValues = !it.isDrawValues } it.invalidate()