Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Manually provide vertical insets to LineCartesianLayer #940

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,13 @@ 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.PaddingProvider
import com.patrykandpatrick.vico.core.cartesian.axis.HorizontalAxis
import com.patrykandpatrick.vico.core.cartesian.axis.VerticalAxis
import com.patrykandpatrick.vico.core.cartesian.data.CartesianChartModelProducer
import com.patrykandpatrick.vico.core.cartesian.data.lineSeries
import com.patrykandpatrick.vico.core.cartesian.layer.LineCartesianLayer
import com.patrykandpatrick.vico.core.common.Defaults
import com.patrykandpatrick.vico.databinding.Chart1Binding
import com.patrykandpatrick.vico.sample.showcase.UIFramework
import com.patrykandpatrick.vico.sample.showcase.rememberMarker
Expand Down Expand Up @@ -66,26 +68,34 @@ internal fun Chart1(uiFramework: UIFramework, modifier: Modifier) {
@Composable
private fun ComposeChart1(modelProducer: CartesianChartModelProducer, modifier: Modifier) {
val marker = rememberMarker()
val lineThickness = Defaults.LINE_SPEC_THICKNESS_DP.dp
val requiredPaddingForLine = lineThickness.div(other = 2F)

CartesianChartHost(
chart =
rememberCartesianChart(
rememberLineCartesianLayer(
LineCartesianLayer.LineProvider.series(
LineCartesianLayer.rememberLine(
remember { LineCartesianLayer.LineFill.single(fill(Color(0xffa485e0))) }
)
)
),
startAxis = VerticalAxis.rememberStart(),
bottomAxis =
HorizontalAxis.rememberBottom(
guideline = null,
itemPlacer = remember { HorizontalAxis.ItemPlacer.segmented() },
rememberCartesianChart(
rememberLineCartesianLayer(
LineCartesianLayer.LineProvider.series(
LineCartesianLayer.rememberLine(
remember { LineCartesianLayer.LineFill.single(fill(Color(0xffa485e0))) },
),
marker = marker,
layerPadding = cartesianLayerPadding(scalableStart = 16.dp, scalableEnd = 16.dp),
persistentMarkers = rememberExtraLambda(marker) { marker at PERSISTENT_MARKER_X },
),
),
startAxis = VerticalAxis.rememberStart(),
bottomAxis =
HorizontalAxis.rememberBottom(
guideline = null,
itemPlacer = remember { HorizontalAxis.ItemPlacer.segmented() },
),
marker = marker,
paddingProvider = PaddingProvider.fixed(cartesianLayerPadding(
unscalableStart = requiredPaddingForLine,
unscalableEnd = requiredPaddingForLine,
unscalableTop = requiredPaddingForLine,
unscalableBottom = requiredPaddingForLine,
)),
persistentMarkers = rememberExtraLambda(marker) { marker at PERSISTENT_MARKER_X },
),
modelProducer = modelProducer,
modifier = modifier,
zoomState = rememberVicoZoomState(zoomEnabled = false),
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.PaddingProvider
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,8 +118,8 @@ private fun ComposeChart4(modelProducer: CartesianChartModelProducer, modifier:
),
endAxis = VerticalAxis.rememberEnd(),
marker = rememberMarker(),
layerPadding = cartesianLayerPadding(scalableStart = 16.dp, scalableEnd = 16.dp),
),
paddingProvider = PaddingProvider.fixed(cartesianLayerPadding(scalableStart = 16.dp, scalableEnd = 16.dp),
)),
modelProducer = modelProducer,
modifier = modifier,
zoomState = rememberVicoZoomState(zoomEnabled = false),
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.PaddingProvider
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,7 @@ private fun ComposeChart5(modelProducer: CartesianChartModelProducer, modifier:
itemPlacer = remember { HorizontalAxis.ItemPlacer.segmented() },
),
marker = rememberMarker(),
layerPadding = cartesianLayerPadding(scalableStart = 16.dp, scalableEnd = 16.dp),
paddingProvider = PaddingProvider.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.PaddingProvider
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,7 @@ private fun ComposeChart6(modelProducer: CartesianChartModelProducer, modifier:
itemPlacer = remember { HorizontalAxis.ItemPlacer.segmented() },
),
marker = rememberMarker(),
layerPadding = cartesianLayerPadding(scalableStart = 16.dp, scalableEnd = 16.dp),
paddingProvider = PaddingProvider.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 @@ -45,6 +45,7 @@ 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.CartesianMeasuringContext
import com.patrykandpatrick.vico.core.cartesian.PaddingProvider
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,7 @@ private fun ComposeChart7(modelProducer: CartesianChartModelProducer, modifier:
bottomAxis =
HorizontalAxis.rememberBottom(itemPlacer = HorizontalAxis.ItemPlacer.segmented()),
marker = rememberMarker(),
layerPadding = cartesianLayerPadding(scalableStart = 16.dp, scalableEnd = 16.dp),
paddingProvider = PaddingProvider.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.PaddingProvider
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,7 @@ private fun ComposeChart8(modelProducer: CartesianChartModelProducer, modifier:
itemPlacer = remember { HorizontalAxis.ItemPlacer.segmented() }
),
marker = rememberMarker(),
layerPadding = cartesianLayerPadding(scalableStart = 16.dp, scalableEnd = 16.dp),
paddingProvider = PaddingProvider.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 @@ -26,6 +26,7 @@ import com.patrykandpatrick.vico.core.cartesian.CartesianDrawingContext
import com.patrykandpatrick.vico.core.cartesian.CartesianLayerPadding
import com.patrykandpatrick.vico.core.cartesian.CartesianMeasuringContext
import com.patrykandpatrick.vico.core.cartesian.FadingEdges
import com.patrykandpatrick.vico.core.cartesian.PaddingProvider
import com.patrykandpatrick.vico.core.cartesian.axis.Axis
import com.patrykandpatrick.vico.core.cartesian.data.CartesianChartModel
import com.patrykandpatrick.vico.core.cartesian.decoration.Decoration
Expand All @@ -52,7 +53,7 @@ public fun rememberCartesianChart(
bottomAxis: Axis<Axis.Position.Horizontal.Bottom>? = null,
marker: CartesianMarker? = null,
markerVisibilityListener: CartesianMarkerVisibilityListener? = null,
layerPadding: CartesianLayerPadding = cartesianLayerPadding(),
paddingProvider: PaddingProvider = PaddingProvider.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,
paddingProvider,
legend,
fadingEdges,
decorations,
Expand All @@ -84,7 +85,7 @@ public fun rememberCartesianChart(
bottomAxis = bottomAxis,
marker = marker,
markerVisibilityListener = markerVisibilityListener,
layerPadding = layerPadding,
paddingProvider = paddingProvider,
legend = legend,
fadingEdges = fadingEdges,
decorations = decorations,
Expand All @@ -99,7 +100,7 @@ public fun rememberCartesianChart(
bottomAxis = bottomAxis,
marker = marker,
markerVisibilityListener = markerVisibilityListener,
layerPadding = layerPadding,
layerPaddingProvider = paddingProvider,
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,
paddingProvider = chart.layerPaddingProvider,
spToPx = with(LocalContext.current) { ::spToPx },
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,14 @@ public fun cartesianLayerPadding(
scalableEnd: Dp = 0.dp,
unscalableStart: Dp = 0.dp,
unscalableEnd: Dp = 0.dp,
unscalableTop: Dp = 0.dp,
unscalableBottom: Dp = 0.dp,
): CartesianLayerPadding =
CartesianLayerPadding(
scalableStart.value,
scalableEnd.value,
unscalableStart.value,
unscalableEnd.value,
scalableStartDp = scalableStart.value,
scalableEndDp = scalableEnd.value,
unscalableStartDp = unscalableStart.value,
unscalableEndDp = unscalableEnd.value,
unscalableTopDp = unscalableTop.value,
unscalableBottomDp = unscalableBottom.value,
)
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import androidx.compose.ui.platform.LocalLayoutDirection
import androidx.compose.ui.unit.LayoutDirection
import com.patrykandpatrick.vico.core.cartesian.CartesianLayerPadding
import com.patrykandpatrick.vico.core.cartesian.MutableCartesianMeasuringContext
import com.patrykandpatrick.vico.core.cartesian.PaddingProvider
import com.patrykandpatrick.vico.core.cartesian.data.CartesianChartModel
import com.patrykandpatrick.vico.core.cartesian.data.CartesianChartRanges

Expand All @@ -34,7 +35,7 @@ internal fun rememberCartesianMeasuringContext(
ranges: CartesianChartRanges,
scrollEnabled: Boolean,
zoomEnabled: Boolean,
layerPadding: CartesianLayerPadding,
paddingProvider: PaddingProvider,
spToPx: (Float) -> Float,
): MutableCartesianMeasuringContext =
remember {
Expand All @@ -46,7 +47,7 @@ internal fun rememberCartesianMeasuringContext(
ranges = ranges,
scrollEnabled = scrollEnabled,
zoomEnabled = zoomEnabled,
layerPadding = layerPadding,
paddingProvider = paddingProvider,
spToPx = spToPx,
)
}
Expand All @@ -57,6 +58,6 @@ internal fun rememberCartesianMeasuringContext(
this.ranges = ranges
this.scrollEnabled = scrollEnabled
this.zoomEnabled = zoomEnabled
this.layerPadding = layerPadding
this.paddingProvider = paddingProvider
this.spToPx = spToPx
}
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ 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: PaddingProvider = PaddingProvider.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 +387,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,
paddingProvider: PaddingProvider = this.layerPaddingProvider,
legend: Legend<CartesianMeasuringContext, CartesianDrawingContext>? = this.legend,
fadingEdges: FadingEdges? = this.fadingEdges,
decorations: List<Decoration> = this.decorations,
Expand All @@ -402,7 +402,7 @@ public open class CartesianChart(
bottomAxis = bottomAxis,
marker = marker,
markerVisibilityListener = markerVisibilityListener,
layerPadding = layerPadding,
layerPaddingProvider = paddingProvider,
legend = legend,
fadingEdges = fadingEdges,
decorations = decorations,
Expand All @@ -417,7 +417,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 +430,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 @@ -16,8 +16,42 @@

package com.patrykandpatrick.vico.core.cartesian

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


@Immutable
public fun interface PaddingProvider {
public fun getCartesianLayerPadding(
extraStore: ExtraStore,
): CartesianLayerPadding

public companion object {
public fun fixed(layerPadding: CartesianLayerPadding): PaddingProvider = PaddingProvider {
layerPadding
}

public fun fixed(
scalableStartDp: Float = 0f,
scalableEndDp: Float = 0f,
unscalableStartDp: Float = 0f,
unscalableEndDp: Float = 0f,
unscalableTopDp: Float = 0f,
unscalableBottomDp: Float = 0f,
): PaddingProvider = PaddingProvider { _ ->
CartesianLayerPadding(
scalableStartDp = scalableStartDp,
scalableEndDp = scalableEndDp,
unscalableStartDp = unscalableStartDp,
unscalableEndDp = unscalableEndDp,
unscalableTopDp = unscalableTopDp,
unscalableBottomDp = unscalableBottomDp,
)
}
}
}

/**
* Stores [CartesianLayer] padding values. [scalableStartDp] and [scalableEndDp] are multiplied by
Expand All @@ -29,14 +63,16 @@ public class CartesianLayerPadding(
internal val scalableEndDp: Float = 0f,
internal val unscalableStartDp: Float = 0f,
internal val unscalableEndDp: Float = 0f,
internal val unscalableTopDp: Float = 0f,
internal val unscalableBottomDp: Float = 0f,
) {
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 @@ -36,5 +36,5 @@ public interface CartesianMeasuringContext : MeasuringContext {
public val zoomEnabled: Boolean

/** Stores the [CartesianLayer] padding values. */
public val layerPadding: CartesianLayerPadding
public val paddingProvider: PaddingProvider
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public class MutableCartesianMeasuringContext(
override var ranges: CartesianChartRanges,
override var scrollEnabled: Boolean,
override var zoomEnabled: Boolean,
override var layerPadding: CartesianLayerPadding,
override var paddingProvider: PaddingProvider,
spToPx: (Float) -> Float,
) :
MutableMeasuringContext(
Expand Down
Loading