Skip to content

Commit

Permalink
Merge pull request #22 from Taewan-P/feat/point-label
Browse files Browse the repository at this point in the history
Update touch point label text
  • Loading branch information
Taewan-P authored Dec 5, 2023
2 parents dd25dc3 + 80a2045 commit a0d0e31
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 7 deletions.
41 changes: 35 additions & 6 deletions src/main/java/app/priceguard/materialchart/Chart.kt
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ class Chart @JvmOverloads constructor(
private val zeroDp = Dp(1F)

private var pointX = 0f
private var pointY = 0f
private var isDragging = false

// Use Android theme
Expand Down Expand Up @@ -193,10 +194,20 @@ class Chart @JvmOverloads constructor(
}

override fun onTouchEvent(event: MotionEvent?): Boolean {
if (dataset?.isInteractive != true) {
if (dataset?.isInteractive != true || event == null) {
return false
}
when (event?.action) {
pointX = event.x
pointY = event.y

if (pointX < xAxisMarginStart.toPx(context).value
|| pointX > width.toFloat() - xAxisMarginStart.toPx(context).value
|| pointY < yAxisMarginEnd.toPx(context).value
|| pointY > height.toFloat() - yAxisMarginEnd.toPx(context).value
) {
return true
}
when (event.action) {
MotionEvent.ACTION_DOWN -> {
parent.requestDisallowInterceptTouchEvent(true)
isDragging = true
Expand Down Expand Up @@ -656,6 +667,8 @@ class Chart @JvmOverloads constructor(
val graphWidth = graphSpaceEndX - graphSpaceStartX
val graphHeight = graphSpaceEndY - graphSpaceStartY

val pointXData = spaceX * ((pointX - graphSpaceStartX.value) / graphWidth.value) + minX

chartData.forEachIndexed { index, data ->
if (index < size - 1) {
val next = chartData[index + 1]
Expand All @@ -671,7 +684,15 @@ class Chart @JvmOverloads constructor(
circlePaint.setCirclePaint()
canvas.drawCircle(pointX, startY.value, circleSize.value / 2, circlePaint)

val text = convertToText(data.y)
val text =
"${dataset?.xLabel ?: "x"} : ${
convertTimeStampToDate(
pointXData,
dataset?.graphMode ?: GraphMode.DAY
)
}," +
" ${dataset?.yLabel ?: "y"} : ${convertToText(data.y)}"


textLabelPaint.setTextLabelPaint()
textLabelPaint.getTextBounds(text, 0, text.length, bounds)
Expand All @@ -682,6 +703,15 @@ class Chart @JvmOverloads constructor(

val rectWidth = bounds.width()
val rectHeight = bounds.height()

// Fix point label position when position is out of range
if (pointX - rectWidth / 2 - labelRectPaddingHorizontal.value < 0) {
pointX = rectWidth / 2 + labelRectPaddingHorizontal.value
}
if (pointX + rectWidth / 2 + labelRectPaddingHorizontal.value > width.toFloat()) {
pointX = width.toFloat() - rectWidth / 2 - labelRectPaddingHorizontal.value
}

val rect = RectF(
pointX - rectWidth / 2 - labelRectPaddingHorizontal.value,
startY.value - rectHeight - distanceTextAndPoint.value - labelRectPaddingVertical.value,
Expand All @@ -692,6 +722,7 @@ class Chart @JvmOverloads constructor(
textRectPaint.color = colorPrimaryContainer

canvas.drawRoundRect(rect, 10f, 10f, textRectPaint)

canvas.drawText(
text,
pointX - bounds.width() / 2 - 4F,
Expand Down Expand Up @@ -908,9 +939,7 @@ class Chart @JvmOverloads constructor(
}

private fun Paint.setTextLabelPaint() {
style = Paint.Style.FILL
typeface = Typeface.DEFAULT
textSize = 48F
textSize = 36F
color = colorOnPrimaryContainer
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ interface ChartDataset {
val showYAxis: Boolean
val isInteractive: Boolean
val graphMode: GraphMode
val xLabel: String
val yLabel: String
val data: List<ChartData>
val gridLines: List<GridLine>
}
}

0 comments on commit a0d0e31

Please sign in to comment.