From 43b39c11a3f77fc590488e8ea6886194581ef6a0 Mon Sep 17 00:00:00 2001 From: Tyler-Lopez <77797048+Tyler-Lopez@users.noreply.github.com> Date: Thu, 31 Oct 2024 16:51:50 -0600 Subject: [PATCH 1/3] Manually provide vertical insets to LineCartesianLayer --- .../vico/sample/showcase/Charts.kt | 2 + .../vico/sample/showcase/charts/Chart1.kt | 41 +++--- .../vico/sample/showcase/charts/Chart11.kt | 126 ++++++++++++++++++ .../cartesian/CartesianLayerPadding.kt | 12 +- .../vico/core/cartesian/CartesianChart.kt | 10 +- .../core/cartesian/CartesianLayerPadding.kt | 39 +++++- .../cartesian/layer/LineCartesianLayer.kt | 17 +-- 7 files changed, 207 insertions(+), 40 deletions(-) create mode 100644 sample/src/main/java/com/patrykandpatrick/vico/sample/showcase/charts/Chart11.kt diff --git a/sample/src/main/java/com/patrykandpatrick/vico/sample/showcase/Charts.kt b/sample/src/main/java/com/patrykandpatrick/vico/sample/showcase/Charts.kt index 930af767b..13fcf8e5f 100644 --- a/sample/src/main/java/com/patrykandpatrick/vico/sample/showcase/Charts.kt +++ b/sample/src/main/java/com/patrykandpatrick/vico/sample/showcase/Charts.kt @@ -20,6 +20,7 @@ import androidx.compose.runtime.Composable import androidx.compose.ui.Modifier import com.patrykandpatrick.vico.sample.showcase.charts.Chart1 import com.patrykandpatrick.vico.sample.showcase.charts.Chart10 +import com.patrykandpatrick.vico.sample.showcase.charts.Chart11 import com.patrykandpatrick.vico.sample.showcase.charts.Chart2 import com.patrykandpatrick.vico.sample.showcase.charts.Chart3 import com.patrykandpatrick.vico.sample.showcase.charts.Chart4 @@ -41,4 +42,5 @@ internal val charts = { uiFramework, modifier -> Chart8(uiFramework, modifier) }, { uiFramework, modifier -> Chart9(uiFramework, modifier) }, { uiFramework, modifier -> Chart10(uiFramework, modifier) }, + { uiFramework, modifier -> Chart11(uiFramework, modifier) }, ) diff --git a/sample/src/main/java/com/patrykandpatrick/vico/sample/showcase/charts/Chart1.kt b/sample/src/main/java/com/patrykandpatrick/vico/sample/showcase/charts/Chart1.kt index c04c1d508..33e1a64b9 100644 --- a/sample/src/main/java/com/patrykandpatrick/vico/sample/showcase/charts/Chart1.kt +++ b/sample/src/main/java/com/patrykandpatrick/vico/sample/showcase/charts/Chart1.kt @@ -38,6 +38,7 @@ 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 @@ -66,26 +67,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, + layerPadding = 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), diff --git a/sample/src/main/java/com/patrykandpatrick/vico/sample/showcase/charts/Chart11.kt b/sample/src/main/java/com/patrykandpatrick/vico/sample/showcase/charts/Chart11.kt new file mode 100644 index 000000000..d1deaf581 --- /dev/null +++ b/sample/src/main/java/com/patrykandpatrick/vico/sample/showcase/charts/Chart11.kt @@ -0,0 +1,126 @@ +/* + * Copyright 2024 by Patryk Goworowski and Patrick Michalik. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.patrykandpatrick.vico.sample.showcase.charts + +import androidx.compose.runtime.Composable +import androidx.compose.runtime.LaunchedEffect +import androidx.compose.runtime.remember +import androidx.compose.ui.Modifier +import androidx.compose.ui.graphics.Color +import androidx.compose.ui.unit.dp +import androidx.compose.ui.viewinterop.AndroidViewBinding +import com.patrykandpatrick.vico.compose.cartesian.CartesianChartHost +import com.patrykandpatrick.vico.compose.cartesian.axis.rememberBottom +import com.patrykandpatrick.vico.compose.cartesian.axis.rememberStart +import com.patrykandpatrick.vico.compose.cartesian.cartesianLayerPadding +import com.patrykandpatrick.vico.compose.cartesian.layer.rememberLine +import com.patrykandpatrick.vico.compose.cartesian.layer.rememberLineCartesianLayer +import com.patrykandpatrick.vico.compose.cartesian.rememberCartesianChart +import com.patrykandpatrick.vico.compose.cartesian.rememberVicoZoomState +import com.patrykandpatrick.vico.compose.common.fill +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.component.ShapeComponent +import com.patrykandpatrick.vico.databinding.Chart1Binding +import com.patrykandpatrick.vico.sample.showcase.UIFramework +import com.patrykandpatrick.vico.sample.showcase.rememberMarker +import kotlin.random.Random +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.withContext + +@Composable +internal fun Chart11(uiFramework: UIFramework, modifier: Modifier) { + val modelProducer = remember { CartesianChartModelProducer() } + LaunchedEffect(Unit) { + withContext(Dispatchers.Default) { + modelProducer.runTransaction { + /* Learn more: + https://patrykandpatrick.com/vico/wiki/cartesian-charts/layers/line-layer#data. */ + lineSeries { series(x, x.map { Random.nextFloat() * 15 }) } + } + } + } + when (uiFramework) { + UIFramework.Compose -> ComposeChart11(modelProducer, modifier) + UIFramework.Views -> ViewChart11(modelProducer, modifier) + } +} + +@Composable +private fun ComposeChart11(modelProducer: CartesianChartModelProducer, modifier: Modifier) { + val marker = rememberMarker() + val lineThickness = 16F + val requiredPaddingForLine = lineThickness / 2F + val pointSize = 32F + val requiredPaddingForPoint = pointSize / 2F + val requiredUnscalableLayerPadding = maxOf(requiredPaddingForPoint, requiredPaddingForLine).dp + + CartesianChartHost( + chart = + rememberCartesianChart( + rememberLineCartesianLayer( + LineCartesianLayer.LineProvider.series( + LineCartesianLayer.rememberLine( + remember { LineCartesianLayer.LineFill.single(fill(Color(0xffa485e0))) }, + pointProvider = LineCartesianLayer.PointProvider.single( + point = LineCartesianLayer.Point( + component = ShapeComponent(), + sizeDp = pointSize, + ) + ) + ), + ), + ), + startAxis = VerticalAxis.rememberStart(), + bottomAxis = + HorizontalAxis.rememberBottom( + guideline = null, + itemPlacer = remember { HorizontalAxis.ItemPlacer.segmented() }, + ), + marker = marker, + layerPadding = cartesianLayerPadding( + unscalableStart = requiredUnscalableLayerPadding, + unscalableEnd = requiredUnscalableLayerPadding, + unscalableTop = requiredUnscalableLayerPadding, + unscalableBottom = requiredUnscalableLayerPadding, + ), + ), + modelProducer = modelProducer, + modifier = modifier, + zoomState = rememberVicoZoomState(zoomEnabled = false), + ) +} + +@Composable +private fun ViewChart11(modelProducer: CartesianChartModelProducer, modifier: Modifier) { + val marker = rememberMarker() + AndroidViewBinding( + { inflater, parent, attachToParent -> + Chart1Binding.inflate(inflater, parent, attachToParent).apply { + with(chartView) { + this.modelProducer = modelProducer + } + } + }, + modifier, + ) +} + +private val x = (1..10).toList() diff --git a/vico/compose/src/main/java/com/patrykandpatrick/vico/compose/cartesian/CartesianLayerPadding.kt b/vico/compose/src/main/java/com/patrykandpatrick/vico/compose/cartesian/CartesianLayerPadding.kt index e87b86825..54b9afb3d 100644 --- a/vico/compose/src/main/java/com/patrykandpatrick/vico/compose/cartesian/CartesianLayerPadding.kt +++ b/vico/compose/src/main/java/com/patrykandpatrick/vico/compose/cartesian/CartesianLayerPadding.kt @@ -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, ) diff --git a/vico/core/src/main/java/com/patrykandpatrick/vico/core/cartesian/CartesianChart.kt b/vico/core/src/main/java/com/patrykandpatrick/vico/core/cartesian/CartesianChart.kt index 62b97eab8..d9925e4e6 100644 --- a/vico/core/src/main/java/com/patrykandpatrick/vico/core/cartesian/CartesianChart.kt +++ b/vico/core/src/main/java/com/patrykandpatrick/vico/core/cartesian/CartesianChart.kt @@ -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? = null, protected val fadingEdges: FadingEdges? = null, protected val decorations: List = emptyList(), @@ -387,7 +387,7 @@ public open class CartesianChart( bottomAxis: Axis? = this.bottomAxis, marker: CartesianMarker? = this.marker, markerVisibilityListener: CartesianMarkerVisibilityListener? = this.markerVisibilityListener, - layerPadding: CartesianLayerPadding = this.layerPadding, + paddingProvider: PaddingProvider = this.layerPaddingProvider, legend: Legend? = this.legend, fadingEdges: FadingEdges? = this.fadingEdges, decorations: List = this.decorations, @@ -402,7 +402,7 @@ public open class CartesianChart( bottomAxis = bottomAxis, marker = marker, markerVisibilityListener = markerVisibilityListener, - layerPadding = layerPadding, + layerPaddingProvider = paddingProvider, legend = legend, fadingEdges = fadingEdges, decorations = decorations, @@ -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 && @@ -430,7 +430,7 @@ public open class CartesianChart( id, marker, markerVisibilityListener, - layerPadding, + layerPaddingProvider, legend, fadingEdges, decorations, diff --git a/vico/core/src/main/java/com/patrykandpatrick/vico/core/cartesian/CartesianLayerPadding.kt b/vico/core/src/main/java/com/patrykandpatrick/vico/core/cartesian/CartesianLayerPadding.kt index 49c832486..fc8a8716f 100644 --- a/vico/core/src/main/java/com/patrykandpatrick/vico/core/cartesian/CartesianLayerPadding.kt +++ b/vico/core/src/main/java/com/patrykandpatrick/vico/core/cartesian/CartesianLayerPadding.kt @@ -18,6 +18,35 @@ 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 + + +@Immutable +public fun interface PaddingProvider { + public fun getCartesianLayerPadding( + extraStore: ExtraStore, + ): CartesianLayerPadding + + public companion object { + 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 @@ -29,14 +58,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() diff --git a/vico/core/src/main/java/com/patrykandpatrick/vico/core/cartesian/layer/LineCartesianLayer.kt b/vico/core/src/main/java/com/patrykandpatrick/vico/core/cartesian/layer/LineCartesianLayer.kt index ca9476c19..6eae02f9a 100644 --- a/vico/core/src/main/java/com/patrykandpatrick/vico/core/cartesian/layer/LineCartesianLayer.kt +++ b/vico/core/src/main/java/com/patrykandpatrick/vico/core/cartesian/layer/LineCartesianLayer.kt @@ -575,8 +575,8 @@ protected constructor( xSpacing = xSpacing, scalableStartPadding = layerPadding.scalableStartDp.pixels, scalableEndPadding = layerPadding.scalableEndDp.pixels, - unscalableStartPadding = maxPointSize.half + layerPadding.unscalableStartDp.pixels, - unscalableEndPadding = maxPointSize.half + layerPadding.unscalableEndDp.pixels, + unscalableStartPadding = layerPadding.unscalableStartDp.pixels, + unscalableEndPadding = layerPadding.unscalableEndDp.pixels, ) } } @@ -598,15 +598,10 @@ protected constructor( insets: Insets, ) { with(context) { - val verticalInset = - (0.. Date: Thu, 31 Oct 2024 22:31:19 -0600 Subject: [PATCH 2/3] Explore an extra store alternative --- .../vico/sample/showcase/charts/Chart1.kt | 5 +++-- .../vico/sample/showcase/charts/Chart11.kt | 5 +++-- .../vico/sample/showcase/charts/Chart4.kt | 5 +++-- .../vico/sample/showcase/charts/Chart5.kt | 3 ++- .../vico/sample/showcase/charts/Chart6.kt | 3 ++- .../vico/sample/showcase/charts/Chart7.kt | 3 ++- .../vico/sample/showcase/charts/Chart8.kt | 3 ++- .../vico/compose/cartesian/CartesianChart.kt | 9 +++++---- .../vico/compose/cartesian/CartesianChartHost.kt | 2 +- .../cartesian/CartesianMeasuringContext.kt | 7 ++++--- .../vico/core/cartesian/CartesianLayerPadding.kt | 5 +++++ .../core/cartesian/CartesianMeasuringContext.kt | 2 +- .../cartesian/MutableCartesianMeasuringContext.kt | 2 +- .../cartesian/layer/CandlestickCartesianLayer.kt | 11 +++++++---- .../core/cartesian/layer/ColumnCartesianLayer.kt | 11 +++++++---- .../core/cartesian/layer/LineCartesianLayer.kt | 15 +++++++++------ .../vico/views/cartesian/CartesianChartView.kt | 5 +++-- .../vico/views/common/theme/ThemeHandler.kt | 3 ++- 18 files changed, 62 insertions(+), 37 deletions(-) diff --git a/sample/src/main/java/com/patrykandpatrick/vico/sample/showcase/charts/Chart1.kt b/sample/src/main/java/com/patrykandpatrick/vico/sample/showcase/charts/Chart1.kt index 33e1a64b9..249a41339 100644 --- a/sample/src/main/java/com/patrykandpatrick/vico/sample/showcase/charts/Chart1.kt +++ b/sample/src/main/java/com/patrykandpatrick/vico/sample/showcase/charts/Chart1.kt @@ -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.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 @@ -87,12 +88,12 @@ private fun ComposeChart1(modelProducer: CartesianChartModelProducer, modifier: itemPlacer = remember { HorizontalAxis.ItemPlacer.segmented() }, ), marker = marker, - layerPadding = cartesianLayerPadding( + paddingProvider = PaddingProvider.fixed(cartesianLayerPadding( unscalableStart = requiredPaddingForLine, unscalableEnd = requiredPaddingForLine, unscalableTop = requiredPaddingForLine, unscalableBottom = requiredPaddingForLine, - ), + )), persistentMarkers = rememberExtraLambda(marker) { marker at PERSISTENT_MARKER_X }, ), modelProducer = modelProducer, diff --git a/sample/src/main/java/com/patrykandpatrick/vico/sample/showcase/charts/Chart11.kt b/sample/src/main/java/com/patrykandpatrick/vico/sample/showcase/charts/Chart11.kt index d1deaf581..e2931ac78 100644 --- a/sample/src/main/java/com/patrykandpatrick/vico/sample/showcase/charts/Chart11.kt +++ b/sample/src/main/java/com/patrykandpatrick/vico/sample/showcase/charts/Chart11.kt @@ -32,6 +32,7 @@ import com.patrykandpatrick.vico.compose.cartesian.layer.rememberLineCartesianLa import com.patrykandpatrick.vico.compose.cartesian.rememberCartesianChart import com.patrykandpatrick.vico.compose.cartesian.rememberVicoZoomState 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 @@ -95,12 +96,12 @@ private fun ComposeChart11(modelProducer: CartesianChartModelProducer, modifier: itemPlacer = remember { HorizontalAxis.ItemPlacer.segmented() }, ), marker = marker, - layerPadding = cartesianLayerPadding( + paddingProvider = PaddingProvider.fixed(cartesianLayerPadding( unscalableStart = requiredUnscalableLayerPadding, unscalableEnd = requiredUnscalableLayerPadding, unscalableTop = requiredUnscalableLayerPadding, unscalableBottom = requiredUnscalableLayerPadding, - ), + )), ), modelProducer = modelProducer, modifier = modifier, diff --git a/sample/src/main/java/com/patrykandpatrick/vico/sample/showcase/charts/Chart4.kt b/sample/src/main/java/com/patrykandpatrick/vico/sample/showcase/charts/Chart4.kt index d6543b724..eeba8f112 100644 --- a/sample/src/main/java/com/patrykandpatrick/vico/sample/showcase/charts/Chart4.kt +++ b/sample/src/main/java/com/patrykandpatrick/vico/sample/showcase/charts/Chart4.kt @@ -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 @@ -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), diff --git a/sample/src/main/java/com/patrykandpatrick/vico/sample/showcase/charts/Chart5.kt b/sample/src/main/java/com/patrykandpatrick/vico/sample/showcase/charts/Chart5.kt index 21fdc24d2..949fd8507 100644 --- a/sample/src/main/java/com/patrykandpatrick/vico/sample/showcase/charts/Chart5.kt +++ b/sample/src/main/java/com/patrykandpatrick/vico/sample/showcase/charts/Chart5.kt @@ -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 @@ -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, diff --git a/sample/src/main/java/com/patrykandpatrick/vico/sample/showcase/charts/Chart6.kt b/sample/src/main/java/com/patrykandpatrick/vico/sample/showcase/charts/Chart6.kt index 67130a1bb..a529cf912 100644 --- a/sample/src/main/java/com/patrykandpatrick/vico/sample/showcase/charts/Chart6.kt +++ b/sample/src/main/java/com/patrykandpatrick/vico/sample/showcase/charts/Chart6.kt @@ -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 @@ -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, diff --git a/sample/src/main/java/com/patrykandpatrick/vico/sample/showcase/charts/Chart7.kt b/sample/src/main/java/com/patrykandpatrick/vico/sample/showcase/charts/Chart7.kt index 3781e51e2..fa5b36b73 100644 --- a/sample/src/main/java/com/patrykandpatrick/vico/sample/showcase/charts/Chart7.kt +++ b/sample/src/main/java/com/patrykandpatrick/vico/sample/showcase/charts/Chart7.kt @@ -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 @@ -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, diff --git a/sample/src/main/java/com/patrykandpatrick/vico/sample/showcase/charts/Chart8.kt b/sample/src/main/java/com/patrykandpatrick/vico/sample/showcase/charts/Chart8.kt index b37dcd149..ec7e7a4b1 100644 --- a/sample/src/main/java/com/patrykandpatrick/vico/sample/showcase/charts/Chart8.kt +++ b/sample/src/main/java/com/patrykandpatrick/vico/sample/showcase/charts/Chart8.kt @@ -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 @@ -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, diff --git a/vico/compose/src/main/java/com/patrykandpatrick/vico/compose/cartesian/CartesianChart.kt b/vico/compose/src/main/java/com/patrykandpatrick/vico/compose/cartesian/CartesianChart.kt index 64f5a5e23..1ef4f10fa 100644 --- a/vico/compose/src/main/java/com/patrykandpatrick/vico/compose/cartesian/CartesianChart.kt +++ b/vico/compose/src/main/java/com/patrykandpatrick/vico/compose/cartesian/CartesianChart.kt @@ -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 @@ -52,7 +53,7 @@ public fun rememberCartesianChart( bottomAxis: Axis? = null, marker: CartesianMarker? = null, markerVisibilityListener: CartesianMarkerVisibilityListener? = null, - layerPadding: CartesianLayerPadding = cartesianLayerPadding(), + paddingProvider: PaddingProvider = PaddingProvider.fixed(), legend: Legend? = null, fadingEdges: FadingEdges? = null, decorations: List = emptyList(), @@ -68,7 +69,7 @@ public fun rememberCartesianChart( bottomAxis, marker, markerVisibilityListener, - layerPadding, + paddingProvider, legend, fadingEdges, decorations, @@ -84,7 +85,7 @@ public fun rememberCartesianChart( bottomAxis = bottomAxis, marker = marker, markerVisibilityListener = markerVisibilityListener, - layerPadding = layerPadding, + paddingProvider = paddingProvider, legend = legend, fadingEdges = fadingEdges, decorations = decorations, @@ -99,7 +100,7 @@ public fun rememberCartesianChart( bottomAxis = bottomAxis, marker = marker, markerVisibilityListener = markerVisibilityListener, - layerPadding = layerPadding, + layerPaddingProvider = paddingProvider, legend = legend, fadingEdges = fadingEdges, decorations = decorations, diff --git a/vico/compose/src/main/java/com/patrykandpatrick/vico/compose/cartesian/CartesianChartHost.kt b/vico/compose/src/main/java/com/patrykandpatrick/vico/compose/cartesian/CartesianChartHost.kt index f6372c077..889c89f33 100644 --- a/vico/compose/src/main/java/com/patrykandpatrick/vico/compose/cartesian/CartesianChartHost.kt +++ b/vico/compose/src/main/java/com/patrykandpatrick/vico/compose/cartesian/CartesianChartHost.kt @@ -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 }, ) diff --git a/vico/compose/src/main/java/com/patrykandpatrick/vico/compose/cartesian/CartesianMeasuringContext.kt b/vico/compose/src/main/java/com/patrykandpatrick/vico/compose/cartesian/CartesianMeasuringContext.kt index 2c9581998..7cdfd1ab7 100644 --- a/vico/compose/src/main/java/com/patrykandpatrick/vico/compose/cartesian/CartesianMeasuringContext.kt +++ b/vico/compose/src/main/java/com/patrykandpatrick/vico/compose/cartesian/CartesianMeasuringContext.kt @@ -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 @@ -34,7 +35,7 @@ internal fun rememberCartesianMeasuringContext( ranges: CartesianChartRanges, scrollEnabled: Boolean, zoomEnabled: Boolean, - layerPadding: CartesianLayerPadding, + paddingProvider: PaddingProvider, spToPx: (Float) -> Float, ): MutableCartesianMeasuringContext = remember { @@ -46,7 +47,7 @@ internal fun rememberCartesianMeasuringContext( ranges = ranges, scrollEnabled = scrollEnabled, zoomEnabled = zoomEnabled, - layerPadding = layerPadding, + paddingProvider = paddingProvider, spToPx = spToPx, ) } @@ -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 } diff --git a/vico/core/src/main/java/com/patrykandpatrick/vico/core/cartesian/CartesianLayerPadding.kt b/vico/core/src/main/java/com/patrykandpatrick/vico/core/cartesian/CartesianLayerPadding.kt index fc8a8716f..c7049bb71 100644 --- a/vico/core/src/main/java/com/patrykandpatrick/vico/core/cartesian/CartesianLayerPadding.kt +++ b/vico/core/src/main/java/com/patrykandpatrick/vico/core/cartesian/CartesianLayerPadding.kt @@ -16,6 +16,7 @@ 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 @@ -28,6 +29,10 @@ public fun interface PaddingProvider { ): CartesianLayerPadding public companion object { + public fun fixed(layerPadding: CartesianLayerPadding): PaddingProvider = PaddingProvider { + layerPadding + } + public fun fixed( scalableStartDp: Float = 0f, scalableEndDp: Float = 0f, diff --git a/vico/core/src/main/java/com/patrykandpatrick/vico/core/cartesian/CartesianMeasuringContext.kt b/vico/core/src/main/java/com/patrykandpatrick/vico/core/cartesian/CartesianMeasuringContext.kt index 0d85ed69e..5257c3114 100644 --- a/vico/core/src/main/java/com/patrykandpatrick/vico/core/cartesian/CartesianMeasuringContext.kt +++ b/vico/core/src/main/java/com/patrykandpatrick/vico/core/cartesian/CartesianMeasuringContext.kt @@ -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 } diff --git a/vico/core/src/main/java/com/patrykandpatrick/vico/core/cartesian/MutableCartesianMeasuringContext.kt b/vico/core/src/main/java/com/patrykandpatrick/vico/core/cartesian/MutableCartesianMeasuringContext.kt index faae42808..eb8499e6f 100644 --- a/vico/core/src/main/java/com/patrykandpatrick/vico/core/cartesian/MutableCartesianMeasuringContext.kt +++ b/vico/core/src/main/java/com/patrykandpatrick/vico/core/cartesian/MutableCartesianMeasuringContext.kt @@ -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( diff --git a/vico/core/src/main/java/com/patrykandpatrick/vico/core/cartesian/layer/CandlestickCartesianLayer.kt b/vico/core/src/main/java/com/patrykandpatrick/vico/core/cartesian/layer/CandlestickCartesianLayer.kt index 9702ce5ff..eb21e7a68 100644 --- a/vico/core/src/main/java/com/patrykandpatrick/vico/core/cartesian/layer/CandlestickCartesianLayer.kt +++ b/vico/core/src/main/java/com/patrykandpatrick/vico/core/cartesian/layer/CandlestickCartesianLayer.kt @@ -266,12 +266,15 @@ protected constructor( with(context) { val candleWidth = candles.getWidestCandle(model.extraStore).widthDp.pixels val xSpacing = candleWidth + candleSpacingDp.pixels + + val padding = paddingProvider.getCartesianLayerPadding(extraStore = model.extraStore) + horizontalDimensions.ensureValuesAtLeast( xSpacing = xSpacing, - scalableStartPadding = candleWidth.half + layerPadding.scalableStartDp.pixels, - scalableEndPadding = candleWidth.half + layerPadding.scalableEndDp.pixels, - unscalableStartPadding = layerPadding.unscalableStartDp.pixels, - unscalableEndPadding = layerPadding.unscalableEndDp.pixels, + scalableStartPadding = padding.scalableStartDp.pixels, + scalableEndPadding = padding.scalableEndDp.pixels, + unscalableStartPadding = padding.unscalableStartDp.pixels, + unscalableEndPadding = padding.unscalableEndDp.pixels, ) } } diff --git a/vico/core/src/main/java/com/patrykandpatrick/vico/core/cartesian/layer/ColumnCartesianLayer.kt b/vico/core/src/main/java/com/patrykandpatrick/vico/core/cartesian/layer/ColumnCartesianLayer.kt index d8c502f44..f54edf3de 100644 --- a/vico/core/src/main/java/com/patrykandpatrick/vico/core/cartesian/layer/ColumnCartesianLayer.kt +++ b/vico/core/src/main/java/com/patrykandpatrick/vico/core/cartesian/layer/ColumnCartesianLayer.kt @@ -396,12 +396,15 @@ protected constructor( mergeMode(model.extraStore), ) val xSpacing = columnCollectionWidth + columnCollectionSpacingDp.pixels + + val padding = paddingProvider.getCartesianLayerPadding(extraStore = model.extraStore) + horizontalDimensions.ensureValuesAtLeast( xSpacing = xSpacing, - scalableStartPadding = columnCollectionWidth.half + layerPadding.scalableStartDp.pixels, - scalableEndPadding = columnCollectionWidth.half + layerPadding.scalableEndDp.pixels, - unscalableStartPadding = layerPadding.unscalableStartDp.pixels, - unscalableEndPadding = layerPadding.unscalableEndDp.pixels, + scalableStartPadding = padding.scalableStartDp.pixels, + scalableEndPadding = padding.scalableEndDp.pixels, + unscalableStartPadding = padding.unscalableStartDp.pixels, + unscalableEndPadding = padding.unscalableEndDp.pixels, ) } } diff --git a/vico/core/src/main/java/com/patrykandpatrick/vico/core/cartesian/layer/LineCartesianLayer.kt b/vico/core/src/main/java/com/patrykandpatrick/vico/core/cartesian/layer/LineCartesianLayer.kt index 6eae02f9a..06b7c727e 100644 --- a/vico/core/src/main/java/com/patrykandpatrick/vico/core/cartesian/layer/LineCartesianLayer.kt +++ b/vico/core/src/main/java/com/patrykandpatrick/vico/core/cartesian/layer/LineCartesianLayer.kt @@ -571,12 +571,14 @@ protected constructor( } .pixels val xSpacing = maxPointSize + pointSpacingDp.pixels + + val padding = paddingProvider.getCartesianLayerPadding(model.extraStore) horizontalDimensions.ensureValuesAtLeast( xSpacing = xSpacing, - scalableStartPadding = layerPadding.scalableStartDp.pixels, - scalableEndPadding = layerPadding.scalableEndDp.pixels, - unscalableStartPadding = layerPadding.unscalableStartDp.pixels, - unscalableEndPadding = layerPadding.unscalableEndDp.pixels, + scalableStartPadding = padding.scalableStartDp.pixels, + scalableEndPadding = padding.scalableEndDp.pixels, + unscalableStartPadding = padding.unscalableStartDp.pixels, + unscalableEndPadding = padding.unscalableEndDp.pixels, ) } } @@ -598,9 +600,10 @@ protected constructor( insets: Insets, ) { with(context) { + val padding = paddingProvider.getCartesianLayerPadding(model.extraStore) insets.ensureValuesAtLeast( - top = layerPadding.unscalableTopDp, - bottom = layerPadding.unscalableBottomDp, + top = padding.unscalableTopDp, + bottom = padding.unscalableBottomDp, ) } } diff --git a/vico/views/src/main/java/com/patrykandpatrick/vico/views/cartesian/CartesianChartView.kt b/vico/views/src/main/java/com/patrykandpatrick/vico/views/cartesian/CartesianChartView.kt index ffd2f21a3..cbe8f1341 100644 --- a/vico/views/src/main/java/com/patrykandpatrick/vico/views/cartesian/CartesianChartView.kt +++ b/vico/views/src/main/java/com/patrykandpatrick/vico/views/cartesian/CartesianChartView.kt @@ -28,6 +28,7 @@ import com.patrykandpatrick.vico.core.cartesian.CartesianDrawingContext import com.patrykandpatrick.vico.core.cartesian.CartesianLayerPadding import com.patrykandpatrick.vico.core.cartesian.MutableCartesianMeasuringContext import com.patrykandpatrick.vico.core.cartesian.MutableHorizontalDimensions +import com.patrykandpatrick.vico.core.cartesian.PaddingProvider import com.patrykandpatrick.vico.core.cartesian.Scroll import com.patrykandpatrick.vico.core.cartesian.data.CartesianChartModel import com.patrykandpatrick.vico.core.cartesian.data.CartesianChartModelProducer @@ -73,7 +74,7 @@ constructor(context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0 ranges = CartesianChartRanges.Empty, scrollEnabled = false, zoomEnabled = false, - layerPadding = themeHandler.chart?.layerPadding ?: CartesianLayerPadding(), + paddingProvider = themeHandler.chart?.layerPaddingProvider ?: PaddingProvider.fixed(), spToPx = context::spToPx, ) @@ -120,7 +121,7 @@ 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.paddingProvider = newValue.layerPaddingProvider tryInvalidate(chart = newValue, model = model, updateRanges = true) } diff --git a/vico/views/src/main/java/com/patrykandpatrick/vico/views/common/theme/ThemeHandler.kt b/vico/views/src/main/java/com/patrykandpatrick/vico/views/common/theme/ThemeHandler.kt index 3b08c5243..75663219a 100644 --- a/vico/views/src/main/java/com/patrykandpatrick/vico/views/common/theme/ThemeHandler.kt +++ b/vico/views/src/main/java/com/patrykandpatrick/vico/views/common/theme/ThemeHandler.kt @@ -25,6 +25,7 @@ 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.FadingEdges +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 @@ -227,7 +228,7 @@ 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 = PaddingProvider.fixed(baseTypedArray.getLayerPadding()), ) } From 43474c44ae0b0e06c0625863fad8392eac69ff79 Mon Sep 17 00:00:00 2001 From: Tyler-Lopez <77797048+Tyler-Lopez@users.noreply.github.com> Date: Thu, 31 Oct 2024 22:32:56 -0600 Subject: [PATCH 3/3] Remove chart 11 --- .../vico/sample/showcase/Charts.kt | 2 - .../vico/sample/showcase/charts/Chart11.kt | 127 ------------------ 2 files changed, 129 deletions(-) delete mode 100644 sample/src/main/java/com/patrykandpatrick/vico/sample/showcase/charts/Chart11.kt diff --git a/sample/src/main/java/com/patrykandpatrick/vico/sample/showcase/Charts.kt b/sample/src/main/java/com/patrykandpatrick/vico/sample/showcase/Charts.kt index 13fcf8e5f..930af767b 100644 --- a/sample/src/main/java/com/patrykandpatrick/vico/sample/showcase/Charts.kt +++ b/sample/src/main/java/com/patrykandpatrick/vico/sample/showcase/Charts.kt @@ -20,7 +20,6 @@ import androidx.compose.runtime.Composable import androidx.compose.ui.Modifier import com.patrykandpatrick.vico.sample.showcase.charts.Chart1 import com.patrykandpatrick.vico.sample.showcase.charts.Chart10 -import com.patrykandpatrick.vico.sample.showcase.charts.Chart11 import com.patrykandpatrick.vico.sample.showcase.charts.Chart2 import com.patrykandpatrick.vico.sample.showcase.charts.Chart3 import com.patrykandpatrick.vico.sample.showcase.charts.Chart4 @@ -42,5 +41,4 @@ internal val charts = { uiFramework, modifier -> Chart8(uiFramework, modifier) }, { uiFramework, modifier -> Chart9(uiFramework, modifier) }, { uiFramework, modifier -> Chart10(uiFramework, modifier) }, - { uiFramework, modifier -> Chart11(uiFramework, modifier) }, ) diff --git a/sample/src/main/java/com/patrykandpatrick/vico/sample/showcase/charts/Chart11.kt b/sample/src/main/java/com/patrykandpatrick/vico/sample/showcase/charts/Chart11.kt deleted file mode 100644 index e2931ac78..000000000 --- a/sample/src/main/java/com/patrykandpatrick/vico/sample/showcase/charts/Chart11.kt +++ /dev/null @@ -1,127 +0,0 @@ -/* - * Copyright 2024 by Patryk Goworowski and Patrick Michalik. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.patrykandpatrick.vico.sample.showcase.charts - -import androidx.compose.runtime.Composable -import androidx.compose.runtime.LaunchedEffect -import androidx.compose.runtime.remember -import androidx.compose.ui.Modifier -import androidx.compose.ui.graphics.Color -import androidx.compose.ui.unit.dp -import androidx.compose.ui.viewinterop.AndroidViewBinding -import com.patrykandpatrick.vico.compose.cartesian.CartesianChartHost -import com.patrykandpatrick.vico.compose.cartesian.axis.rememberBottom -import com.patrykandpatrick.vico.compose.cartesian.axis.rememberStart -import com.patrykandpatrick.vico.compose.cartesian.cartesianLayerPadding -import com.patrykandpatrick.vico.compose.cartesian.layer.rememberLine -import com.patrykandpatrick.vico.compose.cartesian.layer.rememberLineCartesianLayer -import com.patrykandpatrick.vico.compose.cartesian.rememberCartesianChart -import com.patrykandpatrick.vico.compose.cartesian.rememberVicoZoomState -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.component.ShapeComponent -import com.patrykandpatrick.vico.databinding.Chart1Binding -import com.patrykandpatrick.vico.sample.showcase.UIFramework -import com.patrykandpatrick.vico.sample.showcase.rememberMarker -import kotlin.random.Random -import kotlinx.coroutines.Dispatchers -import kotlinx.coroutines.withContext - -@Composable -internal fun Chart11(uiFramework: UIFramework, modifier: Modifier) { - val modelProducer = remember { CartesianChartModelProducer() } - LaunchedEffect(Unit) { - withContext(Dispatchers.Default) { - modelProducer.runTransaction { - /* Learn more: - https://patrykandpatrick.com/vico/wiki/cartesian-charts/layers/line-layer#data. */ - lineSeries { series(x, x.map { Random.nextFloat() * 15 }) } - } - } - } - when (uiFramework) { - UIFramework.Compose -> ComposeChart11(modelProducer, modifier) - UIFramework.Views -> ViewChart11(modelProducer, modifier) - } -} - -@Composable -private fun ComposeChart11(modelProducer: CartesianChartModelProducer, modifier: Modifier) { - val marker = rememberMarker() - val lineThickness = 16F - val requiredPaddingForLine = lineThickness / 2F - val pointSize = 32F - val requiredPaddingForPoint = pointSize / 2F - val requiredUnscalableLayerPadding = maxOf(requiredPaddingForPoint, requiredPaddingForLine).dp - - CartesianChartHost( - chart = - rememberCartesianChart( - rememberLineCartesianLayer( - LineCartesianLayer.LineProvider.series( - LineCartesianLayer.rememberLine( - remember { LineCartesianLayer.LineFill.single(fill(Color(0xffa485e0))) }, - pointProvider = LineCartesianLayer.PointProvider.single( - point = LineCartesianLayer.Point( - component = ShapeComponent(), - sizeDp = pointSize, - ) - ) - ), - ), - ), - startAxis = VerticalAxis.rememberStart(), - bottomAxis = - HorizontalAxis.rememberBottom( - guideline = null, - itemPlacer = remember { HorizontalAxis.ItemPlacer.segmented() }, - ), - marker = marker, - paddingProvider = PaddingProvider.fixed(cartesianLayerPadding( - unscalableStart = requiredUnscalableLayerPadding, - unscalableEnd = requiredUnscalableLayerPadding, - unscalableTop = requiredUnscalableLayerPadding, - unscalableBottom = requiredUnscalableLayerPadding, - )), - ), - modelProducer = modelProducer, - modifier = modifier, - zoomState = rememberVicoZoomState(zoomEnabled = false), - ) -} - -@Composable -private fun ViewChart11(modelProducer: CartesianChartModelProducer, modifier: Modifier) { - val marker = rememberMarker() - AndroidViewBinding( - { inflater, parent, attachToParent -> - Chart1Binding.inflate(inflater, parent, attachToParent).apply { - with(chartView) { - this.modelProducer = modelProducer - } - } - }, - modifier, - ) -} - -private val x = (1..10).toList()