Skip to content

Commit

Permalink
Add CartesianLayerPaddingProvider
Browse files Browse the repository at this point in the history
Add CartesianLayerPaddingProvider

This commit introduces CartesianLayerPaddingProvider, a means to retrieve CartesianLayerPadding with an `ExtraStore`. This commit does not substantially conflict with pre-existing functionality as `CartesianLayerPaddingProvider.fixed` is identical.
  • Loading branch information
Tyler-Lopez committed Nov 1, 2024
1 parent 7066e4b commit bc94a52
Show file tree
Hide file tree
Showing 12 changed files with 87 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import com.patrykandpatrick.vico.compose.cartesian.rememberCartesianChart
import com.patrykandpatrick.vico.compose.cartesian.rememberVicoZoomState
import com.patrykandpatrick.vico.compose.common.data.rememberExtraLambda
import com.patrykandpatrick.vico.compose.common.fill
import com.patrykandpatrick.vico.core.cartesian.CartesianLayerPaddingProvider
import com.patrykandpatrick.vico.core.cartesian.axis.HorizontalAxis
import com.patrykandpatrick.vico.core.cartesian.axis.VerticalAxis
import com.patrykandpatrick.vico.core.cartesian.data.CartesianChartModelProducer
Expand Down Expand Up @@ -83,7 +84,9 @@ private fun ComposeChart1(modelProducer: CartesianChartModelProducer, modifier:
itemPlacer = remember { HorizontalAxis.ItemPlacer.segmented() },
),
marker = marker,
layerPadding = cartesianLayerPadding(scalableStart = 16.dp, scalableEnd = 16.dp),
layerPaddingProvider = CartesianLayerPaddingProvider.fixed(
cartesianLayerPadding(scalableStart = 16.dp, scalableEnd = 16.dp)
),
persistentMarkers = rememberExtraLambda(marker) { marker at PERSISTENT_MARKER_X },
),
modelProducer = modelProducer,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import com.patrykandpatrick.vico.compose.cartesian.rememberVicoZoomState
import com.patrykandpatrick.vico.compose.common.component.rememberLineComponent
import com.patrykandpatrick.vico.compose.common.fill
import com.patrykandpatrick.vico.compose.common.shape.rounded
import com.patrykandpatrick.vico.core.cartesian.CartesianLayerPaddingProvider
import com.patrykandpatrick.vico.core.cartesian.axis.HorizontalAxis
import com.patrykandpatrick.vico.core.cartesian.axis.VerticalAxis
import com.patrykandpatrick.vico.core.cartesian.data.CartesianChartModelProducer
Expand Down Expand Up @@ -117,7 +118,9 @@ private fun ComposeChart4(modelProducer: CartesianChartModelProducer, modifier:
),
endAxis = VerticalAxis.rememberEnd(),
marker = rememberMarker(),
layerPadding = cartesianLayerPadding(scalableStart = 16.dp, scalableEnd = 16.dp),
layerPaddingProvider = CartesianLayerPaddingProvider.fixed(
cartesianLayerPadding(scalableStart = 16.dp, scalableEnd = 16.dp),
)
),
modelProducer = modelProducer,
modifier = modifier,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import com.patrykandpatrick.vico.compose.cartesian.layer.rememberColumnCartesian
import com.patrykandpatrick.vico.compose.cartesian.rememberCartesianChart
import com.patrykandpatrick.vico.compose.cartesian.rememberVicoZoomState
import com.patrykandpatrick.vico.compose.common.component.rememberLineComponent
import com.patrykandpatrick.vico.core.cartesian.CartesianLayerPaddingProvider
import com.patrykandpatrick.vico.core.cartesian.axis.HorizontalAxis
import com.patrykandpatrick.vico.core.cartesian.axis.VerticalAxis
import com.patrykandpatrick.vico.core.cartesian.data.CartesianChartModelProducer
Expand Down Expand Up @@ -119,7 +120,9 @@ private fun ComposeChart5(modelProducer: CartesianChartModelProducer, modifier:
itemPlacer = remember { HorizontalAxis.ItemPlacer.segmented() },
),
marker = rememberMarker(),
layerPadding = cartesianLayerPadding(scalableStart = 16.dp, scalableEnd = 16.dp),
layerPaddingProvider = CartesianLayerPaddingProvider.fixed(
cartesianLayerPadding(scalableStart = 16.dp, scalableEnd = 16.dp),
)
),
modelProducer = modelProducer,
modifier = modifier,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import com.patrykandpatrick.vico.compose.common.component.rememberShapeComponent
import com.patrykandpatrick.vico.compose.common.component.rememberTextComponent
import com.patrykandpatrick.vico.compose.common.component.shapeComponent
import com.patrykandpatrick.vico.compose.common.dimensions
import com.patrykandpatrick.vico.core.cartesian.CartesianLayerPaddingProvider
import com.patrykandpatrick.vico.core.cartesian.axis.HorizontalAxis
import com.patrykandpatrick.vico.core.cartesian.axis.VerticalAxis
import com.patrykandpatrick.vico.core.cartesian.data.CartesianChartModelProducer
Expand Down Expand Up @@ -109,7 +110,9 @@ private fun ComposeChart6(modelProducer: CartesianChartModelProducer, modifier:
itemPlacer = remember { HorizontalAxis.ItemPlacer.segmented() },
),
marker = rememberMarker(),
layerPadding = cartesianLayerPadding(scalableStart = 16.dp, scalableEnd = 16.dp),
layerPaddingProvider = CartesianLayerPaddingProvider.fixed(
cartesianLayerPadding(scalableStart = 16.dp, scalableEnd = 16.dp),
),
decorations = listOf(rememberComposeHorizontalBox()),
),
modelProducer = modelProducer,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ import com.patrykandpatrick.vico.compose.common.rememberVerticalLegend
import com.patrykandpatrick.vico.compose.common.shape.rounded
import com.patrykandpatrick.vico.compose.common.vicoTheme
import com.patrykandpatrick.vico.core.cartesian.CartesianDrawingContext
import com.patrykandpatrick.vico.core.cartesian.CartesianLayerPaddingProvider
import com.patrykandpatrick.vico.core.cartesian.CartesianMeasuringContext
import com.patrykandpatrick.vico.core.cartesian.axis.HorizontalAxis
import com.patrykandpatrick.vico.core.cartesian.axis.VerticalAxis
Expand Down Expand Up @@ -117,7 +118,9 @@ private fun ComposeChart7(modelProducer: CartesianChartModelProducer, modifier:
bottomAxis =
HorizontalAxis.rememberBottom(itemPlacer = HorizontalAxis.ItemPlacer.segmented()),
marker = rememberMarker(),
layerPadding = cartesianLayerPadding(scalableStart = 16.dp, scalableEnd = 16.dp),
layerPaddingProvider = CartesianLayerPaddingProvider.fixed(
cartesianLayerPadding(scalableStart = 16.dp, scalableEnd = 16.dp),
),
legend = rememberLegend(),
),
modelProducer = modelProducer,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import com.patrykandpatrick.vico.compose.cartesian.rememberCartesianChart
import com.patrykandpatrick.vico.compose.cartesian.rememberVicoZoomState
import com.patrykandpatrick.vico.compose.common.component.rememberLineComponent
import com.patrykandpatrick.vico.compose.common.fill
import com.patrykandpatrick.vico.core.cartesian.CartesianLayerPaddingProvider
import com.patrykandpatrick.vico.core.cartesian.axis.Axis
import com.patrykandpatrick.vico.core.cartesian.axis.HorizontalAxis
import com.patrykandpatrick.vico.core.cartesian.axis.VerticalAxis
Expand Down Expand Up @@ -123,7 +124,9 @@ private fun ComposeChart8(modelProducer: CartesianChartModelProducer, modifier:
itemPlacer = remember { HorizontalAxis.ItemPlacer.segmented() }
),
marker = rememberMarker(),
layerPadding = cartesianLayerPadding(scalableStart = 16.dp, scalableEnd = 16.dp),
layerPaddingProvider = CartesianLayerPaddingProvider.fixed(
cartesianLayerPadding(scalableStart = 16.dp, scalableEnd = 16.dp),
),
),
modelProducer = modelProducer,
modifier = modifier,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import com.patrykandpatrick.vico.compose.cartesian.layer.rememberLineCartesianLa
import com.patrykandpatrick.vico.core.cartesian.CartesianChart
import com.patrykandpatrick.vico.core.cartesian.CartesianDrawingContext
import com.patrykandpatrick.vico.core.cartesian.CartesianLayerPadding
import com.patrykandpatrick.vico.core.cartesian.CartesianLayerPaddingProvider
import com.patrykandpatrick.vico.core.cartesian.CartesianMeasuringContext
import com.patrykandpatrick.vico.core.cartesian.FadingEdges
import com.patrykandpatrick.vico.core.cartesian.axis.Axis
Expand Down Expand Up @@ -52,7 +53,7 @@ public fun rememberCartesianChart(
bottomAxis: Axis<Axis.Position.Horizontal.Bottom>? = null,
marker: CartesianMarker? = null,
markerVisibilityListener: CartesianMarkerVisibilityListener? = null,
layerPadding: CartesianLayerPadding = cartesianLayerPadding(),
layerPaddingProvider: CartesianLayerPaddingProvider = CartesianLayerPaddingProvider.fixed(),
legend: Legend<CartesianMeasuringContext, CartesianDrawingContext>? = null,
fadingEdges: FadingEdges? = null,
decorations: List<Decoration> = emptyList(),
Expand All @@ -68,7 +69,7 @@ public fun rememberCartesianChart(
bottomAxis,
marker,
markerVisibilityListener,
layerPadding,
layerPaddingProvider,
legend,
fadingEdges,
decorations,
Expand All @@ -84,7 +85,7 @@ public fun rememberCartesianChart(
bottomAxis = bottomAxis,
marker = marker,
markerVisibilityListener = markerVisibilityListener,
layerPadding = layerPadding,
layerPaddingProvider = layerPaddingProvider,
legend = legend,
fadingEdges = fadingEdges,
decorations = decorations,
Expand All @@ -99,7 +100,7 @@ public fun rememberCartesianChart(
bottomAxis = bottomAxis,
marker = marker,
markerVisibilityListener = markerVisibilityListener,
layerPadding = layerPadding,
layerPaddingProvider = layerPaddingProvider,
legend = legend,
fadingEdges = fadingEdges,
decorations = decorations,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ internal fun CartesianChartHostImpl(
ranges = ranges,
scrollEnabled = scrollState.scrollEnabled,
zoomEnabled = scrollState.scrollEnabled && zoomState.zoomEnabled,
layerPadding = chart.layerPadding,
layerPadding = chart.layerPaddingProvider.getPadding(extraStore = model.extraStore),
spToPx = with(LocalContext.current) { ::spToPx },
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,8 @@ public open class CartesianChart(
@get:RestrictTo(RestrictTo.Scope.LIBRARY_GROUP) public val marker: CartesianMarker? = null,
protected val markerVisibilityListener: CartesianMarkerVisibilityListener? = null,
@get:RestrictTo(RestrictTo.Scope.LIBRARY_GROUP)
public val layerPadding: CartesianLayerPadding = CartesianLayerPadding(),
public val layerPaddingProvider: CartesianLayerPaddingProvider =
CartesianLayerPaddingProvider.fixed(),
protected val legend: Legend<CartesianMeasuringContext, CartesianDrawingContext>? = null,
protected val fadingEdges: FadingEdges? = null,
protected val decorations: List<Decoration> = emptyList(),
Expand Down Expand Up @@ -387,7 +388,7 @@ public open class CartesianChart(
bottomAxis: Axis<Axis.Position.Horizontal.Bottom>? = this.bottomAxis,
marker: CartesianMarker? = this.marker,
markerVisibilityListener: CartesianMarkerVisibilityListener? = this.markerVisibilityListener,
layerPadding: CartesianLayerPadding = this.layerPadding,
layerPaddingProvider: CartesianLayerPaddingProvider = this.layerPaddingProvider,
legend: Legend<CartesianMeasuringContext, CartesianDrawingContext>? = this.legend,
fadingEdges: FadingEdges? = this.fadingEdges,
decorations: List<Decoration> = this.decorations,
Expand All @@ -402,7 +403,7 @@ public open class CartesianChart(
bottomAxis = bottomAxis,
marker = marker,
markerVisibilityListener = markerVisibilityListener,
layerPadding = layerPadding,
layerPaddingProvider = layerPaddingProvider,
legend = legend,
fadingEdges = fadingEdges,
decorations = decorations,
Expand All @@ -417,7 +418,7 @@ public open class CartesianChart(
id == other.id &&
marker == other.marker &&
markerVisibilityListener == other.markerVisibilityListener &&
layerPadding == other.layerPadding &&
layerPaddingProvider == other.layerPaddingProvider &&
legend == other.legend &&
fadingEdges == other.fadingEdges &&
decorations == other.decorations &&
Expand All @@ -430,7 +431,7 @@ public open class CartesianChart(
id,
marker,
markerVisibilityListener,
layerPadding,
layerPaddingProvider,
legend,
fadingEdges,
decorations,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,40 @@ package com.patrykandpatrick.vico.core.cartesian

import androidx.compose.runtime.Immutable
import com.patrykandpatrick.vico.core.cartesian.layer.CartesianLayer
import com.patrykandpatrick.vico.core.common.data.ExtraStore

/** Provides [CartesianLayerPadding] to a [CartesianChart]. */
@Immutable
public fun interface CartesianLayerPaddingProvider {
/** Returns the [CartesianLayerPadding] for the specified [ExtraStore]. */
public fun getPadding(extraStore: ExtraStore): CartesianLayerPadding

public companion object {
/** Constructs a [CartesianLayerPaddingProvider] which always provides the same
* [CartesianLayerPadding] regardless of the stored values in the [ExtraStore]. */
public fun fixed(
scalableStartDp: Float = 0f,
scalableEndDp: Float = 0f,
unscalableStartDp: Float = 0f,
unscalableEndDp: Float = 0f,
): CartesianLayerPaddingProvider = object : CartesianLayerPaddingProvider {
val padding = CartesianLayerPadding(
scalableStartDp = scalableStartDp,
scalableEndDp = scalableEndDp,
unscalableStartDp = unscalableStartDp,
unscalableEndDp = unscalableEndDp,
)

override fun getPadding(extraStore: ExtraStore): CartesianLayerPadding = padding
}

/** Constructs a [CartesianLayerPaddingProvider] which always provides the same
* [CartesianLayerPadding] regardless of the stored values in the [ExtraStore]. */
public fun fixed(
layerPadding: CartesianLayerPadding,
): CartesianLayerPaddingProvider = CartesianLayerPaddingProvider { layerPadding }
}
}

/**
* Stores [CartesianLayer] padding values. [scalableStartDp] and [scalableEndDp] are multiplied by
Expand All @@ -33,10 +67,10 @@ public class CartesianLayerPadding(
override fun equals(other: Any?): Boolean =
this === other ||
other is CartesianLayerPadding &&
scalableStartDp == other.scalableStartDp &&
scalableEndDp == other.scalableEndDp &&
unscalableStartDp == other.unscalableStartDp &&
unscalableEndDp == other.unscalableEndDp
scalableStartDp == other.scalableStartDp &&
scalableEndDp == other.scalableEndDp &&
unscalableStartDp == other.unscalableStartDp &&
unscalableEndDp == other.unscalableEndDp

override fun hashCode(): Int {
var result = scalableStartDp.hashCode()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import com.patrykandpatrick.vico.core.cartesian.data.toImmutable
import com.patrykandpatrick.vico.core.common.Defaults
import com.patrykandpatrick.vico.core.common.NEW_PRODUCER_ERROR_MESSAGE
import com.patrykandpatrick.vico.core.common.Point
import com.patrykandpatrick.vico.core.common.data.ExtraStore
import com.patrykandpatrick.vico.core.common.spToPx
import com.patrykandpatrick.vico.views.R
import com.patrykandpatrick.vico.views.common.ChartView
Expand Down Expand Up @@ -73,7 +74,8 @@ constructor(context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
ranges = CartesianChartRanges.Empty,
scrollEnabled = false,
zoomEnabled = false,
layerPadding = themeHandler.chart?.layerPadding ?: CartesianLayerPadding(),
layerPadding = themeHandler.chart?.layerPaddingProvider?.getPadding(extraStore = extraStore)
?: CartesianLayerPadding(),
spToPx = context::spToPx,
)

Expand Down Expand Up @@ -120,7 +122,8 @@ constructor(context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
/** The [CartesianChart] displayed by this [View]. */
public var chart: CartesianChart? by
observable(themeHandler.chart) { _, _, newValue ->
if (newValue != null) measuringContext.layerPadding = newValue.layerPadding
if (newValue != null) measuringContext.layerPadding =
newValue.layerPaddingProvider.getPadding(extraStore)
tryInvalidate(chart = newValue, model = model, updateRanges = true)
}

Expand Down Expand Up @@ -350,3 +353,7 @@ internal val MotionEvent.movedXDistance: Float

internal val MotionEvent.movedYDistance: Float
get() = if (historySize > 0) abs(y - getHistoricalY(historySize - 1)) else 0f

private fun CartesianChart.getLayerPadding(extraStore: ExtraStore?) = extraStore?.let {
layerPaddingProvider.getPadding(extraStore = it)
} ?: CartesianLayerPadding()
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import android.util.Log
import android.view.animation.AccelerateInterpolator
import com.patrykandpatrick.vico.core.cartesian.CartesianChart
import com.patrykandpatrick.vico.core.cartesian.CartesianLayerPadding
import com.patrykandpatrick.vico.core.cartesian.CartesianLayerPaddingProvider
import com.patrykandpatrick.vico.core.cartesian.FadingEdges
import com.patrykandpatrick.vico.core.cartesian.axis.Axis
import com.patrykandpatrick.vico.core.cartesian.axis.HorizontalAxis
Expand Down Expand Up @@ -227,7 +228,9 @@ internal class ThemeHandler(private val context: Context, attrs: AttributeSet?)
endAxis = baseTypedArray.getAxis(Axis.Position.Vertical.End),
bottomAxis = baseTypedArray.getAxis(Axis.Position.Horizontal.Bottom),
fadingEdges = baseTypedArray.getFadingEdges(),
layerPadding = baseTypedArray.getLayerPadding(),
layerPaddingProvider = CartesianLayerPaddingProvider.fixed(
layerPadding = baseTypedArray.getLayerPadding(),
)
)
}

Expand Down

0 comments on commit bc94a52

Please sign in to comment.