diff --git a/vico/compose/src/main/java/com/patrykandpatrick/vico/compose/extension/ModifierExtensions.kt b/vico/compose/src/main/java/com/patrykandpatrick/vico/compose/extension/ModifierExtensions.kt index a3669b878..641ba52e0 100644 --- a/vico/compose/src/main/java/com/patrykandpatrick/vico/compose/extension/ModifierExtensions.kt +++ b/vico/compose/src/main/java/com/patrykandpatrick/vico/compose/extension/ModifierExtensions.kt @@ -32,8 +32,9 @@ internal fun Modifier.chartTouchEvent( isScrollEnabled: Boolean, scrollableState: ChartScrollState, onZoom: OnZoom?, -): Modifier = - scrollable( +): Modifier { + var isDragStart = false + return scrollable( state = scrollableState, orientation = Orientation.Horizontal, reverseDirection = true, @@ -46,8 +47,10 @@ internal fun Modifier.chartTouchEvent( while (true) { val event = awaitPointerEvent() when (event.type) { - PointerEventType.Press -> setTouchPoint(event.changes.first().position.point) - PointerEventType.Release -> setTouchPoint(null) + PointerEventType.Press -> setTouchPoint(null) + PointerEventType.Release -> setTouchPoint( + if (!isDragStart) event.changes.first().position.point else null + ) } } } @@ -60,9 +63,9 @@ internal fun Modifier.chartTouchEvent( if (!isScrollEnabled && setTouchPoint != null) { pointerInput(setTouchPoint) { detectHorizontalDragGestures( - onDragStart = { setTouchPoint(it.point) }, - onDragEnd = { setTouchPoint(null) }, - onDragCancel = { setTouchPoint(null) }, + onDragStart = { isDragStart = true; setTouchPoint(it.point) }, + onDragEnd = { isDragStart = false; setTouchPoint(null) }, + onDragCancel = { isDragStart = false; setTouchPoint(null) }, ) { change, _ -> setTouchPoint(change.position.point) } } } else { @@ -81,6 +84,7 @@ internal fun Modifier.chartTouchEvent( Modifier }, ) +} private val Offset.point: Point get() = Point(x, y) diff --git a/vico/core/src/main/java/com/patrykandpatrick/vico/core/component/marker/MarkerComponent.kt b/vico/core/src/main/java/com/patrykandpatrick/vico/core/component/marker/MarkerComponent.kt index d4d54f061..23aab3ea5 100644 --- a/vico/core/src/main/java/com/patrykandpatrick/vico/core/component/marker/MarkerComponent.kt +++ b/vico/core/src/main/java/com/patrykandpatrick/vico/core/component/marker/MarkerComponent.kt @@ -46,7 +46,7 @@ import com.patrykandpatrick.vico.core.marker.MarkerLabelFormatter * @param guideline an optional line drawn from the bottom of the chart to the bottom edge of the [label]. */ public open class MarkerComponent( - public val label: TextComponent, + public val label: TextComponent?, public val indicator: Component?, public val guideline: LineComponent?, ) : Marker { @@ -100,22 +100,24 @@ public open class MarkerComponent( markedEntries: List, chartValues: ChartValues, ): Unit = with(context) { - val text = labelFormatter.getLabel(markedEntries, chartValues) - val entryX = markedEntries.averageOf { it.location.x } - val labelBounds = - label.getTextBounds(context = context, text = text, width = bounds.width().toInt(), outRect = tempBounds) - val halfOfTextWidth = labelBounds.width().half - val x = overrideXPositionToFit(entryX, bounds, halfOfTextWidth) - this[MarkerCorneredShape.tickXKey] = entryX + if (label != null) { + val text = labelFormatter.getLabel(markedEntries, chartValues) + val entryX = markedEntries.averageOf { it.location.x } + val labelBounds = + label.getTextBounds(context = context, text = text, width = bounds.width().toInt(), outRect = tempBounds) + val halfOfTextWidth = labelBounds.width().half + val x = overrideXPositionToFit(entryX, bounds, halfOfTextWidth) + this[MarkerCorneredShape.tickXKey] = entryX - label.drawText( - context = context, - text = text, - textX = x, - textY = bounds.top - labelBounds.height() - label.tickSizeDp.pixels, - verticalPosition = VerticalPosition.Bottom, - maxTextWidth = minOf(bounds.right - x, x - bounds.left).doubled.ceil.toInt(), - ) + label.drawText( + context = context, + text = text, + textX = x, + textY = bounds.top - labelBounds.height() - label.tickSizeDp.pixels, + verticalPosition = VerticalPosition.Bottom, + maxTextWidth = minOf(bounds.right - x, x - bounds.left).doubled.ceil.toInt(), + ) + } } private fun overrideXPositionToFit( @@ -151,6 +153,7 @@ public open class MarkerComponent( outInsets: Insets, horizontalDimensions: HorizontalDimensions, ): Unit = with(context) { - outInsets.top = label.getHeight(context) + label.tickSizeDp.pixels + outInsets.top = if (label != null) label.getHeight(context) + label.tickSizeDp.pixels + else 0F } }