Skip to content

Commit a0d0e31

Browse files
authored
Merge pull request #22 from Taewan-P/feat/point-label
Update touch point label text
2 parents dd25dc3 + 80a2045 commit a0d0e31

2 files changed

Lines changed: 38 additions & 7 deletions

File tree

src/main/java/app/priceguard/materialchart/Chart.kt

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -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

src/main/java/app/priceguard/materialchart/data/ChartDataset.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ interface ChartDataset {
55
val showYAxis: Boolean
66
val isInteractive: Boolean
77
val graphMode: GraphMode
8+
val xLabel: String
9+
val yLabel: String
810
val data: List<ChartData>
911
val gridLines: List<GridLine>
10-
}
12+
}

0 commit comments

Comments
 (0)