@@ -100,6 +100,7 @@ class Chart @JvmOverloads constructor(
100100 private val zeroDp = Dp (1F )
101101
102102 private var pointX = 0f
103+ private var pointY = 0f
103104 private var isDragging = false
104105
105106 // Use Android theme
@@ -193,10 +194,20 @@ class Chart @JvmOverloads constructor(
193194 }
194195
195196 override fun onTouchEvent (event : MotionEvent ? ): Boolean {
196- if (dataset?.isInteractive != true ) {
197+ if (dataset?.isInteractive != true || event == null ) {
197198 return false
198199 }
199- when (event?.action) {
200+ pointX = event.x
201+ pointY = event.y
202+
203+ if (pointX < xAxisMarginStart.toPx(context).value
204+ || pointX > width.toFloat() - xAxisMarginStart.toPx(context).value
205+ || pointY < yAxisMarginEnd.toPx(context).value
206+ || pointY > height.toFloat() - yAxisMarginEnd.toPx(context).value
207+ ) {
208+ return true
209+ }
210+ when (event.action) {
200211 MotionEvent .ACTION_DOWN -> {
201212 parent.requestDisallowInterceptTouchEvent(true )
202213 isDragging = true
@@ -656,6 +667,8 @@ class Chart @JvmOverloads constructor(
656667 val graphWidth = graphSpaceEndX - graphSpaceStartX
657668 val graphHeight = graphSpaceEndY - graphSpaceStartY
658669
670+ val pointXData = spaceX * ((pointX - graphSpaceStartX.value) / graphWidth.value) + minX
671+
659672 chartData.forEachIndexed { index, data ->
660673 if (index < size - 1 ) {
661674 val next = chartData[index + 1 ]
@@ -671,7 +684,15 @@ class Chart @JvmOverloads constructor(
671684 circlePaint.setCirclePaint()
672685 canvas.drawCircle(pointX, startY.value, circleSize.value / 2 , circlePaint)
673686
674- val text = convertToText(data.y)
687+ val text =
688+ " ${dataset?.xLabel ? : " x" } : ${
689+ convertTimeStampToDate(
690+ pointXData,
691+ dataset?.graphMode ? : GraphMode .DAY
692+ )
693+ } ," +
694+ " ${dataset?.yLabel ? : " y" } : ${convertToText(data.y)} "
695+
675696
676697 textLabelPaint.setTextLabelPaint()
677698 textLabelPaint.getTextBounds(text, 0 , text.length, bounds)
@@ -682,6 +703,15 @@ class Chart @JvmOverloads constructor(
682703
683704 val rectWidth = bounds.width()
684705 val rectHeight = bounds.height()
706+
707+ // Fix point label position when position is out of range
708+ if (pointX - rectWidth / 2 - labelRectPaddingHorizontal.value < 0 ) {
709+ pointX = rectWidth / 2 + labelRectPaddingHorizontal.value
710+ }
711+ if (pointX + rectWidth / 2 + labelRectPaddingHorizontal.value > width.toFloat()) {
712+ pointX = width.toFloat() - rectWidth / 2 - labelRectPaddingHorizontal.value
713+ }
714+
685715 val rect = RectF (
686716 pointX - rectWidth / 2 - labelRectPaddingHorizontal.value,
687717 startY.value - rectHeight - distanceTextAndPoint.value - labelRectPaddingVertical.value,
@@ -692,6 +722,7 @@ class Chart @JvmOverloads constructor(
692722 textRectPaint.color = colorPrimaryContainer
693723
694724 canvas.drawRoundRect(rect, 10f , 10f , textRectPaint)
725+
695726 canvas.drawText(
696727 text,
697728 pointX - bounds.width() / 2 - 4F ,
@@ -908,9 +939,7 @@ class Chart @JvmOverloads constructor(
908939 }
909940
910941 private fun Paint.setTextLabelPaint () {
911- style = Paint .Style .FILL
912- typeface = Typeface .DEFAULT
913- textSize = 48F
942+ textSize = 36F
914943 color = colorOnPrimaryContainer
915944 }
916945
0 commit comments