From 31867038b475d2a98cb3a0d74f71d096e9586166 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] Manually provide vertical insets to LineCartesianLayer --- .../vico/sample/showcase/Charts.kt | 2 + .../vico/sample/showcase/charts/Chart1.kt | 40 +++--- .../vico/sample/showcase/charts/Chart11.kt | 126 ++++++++++++++++++ .../cartesian/CartesianLayerPadding.kt | 12 +- .../core/cartesian/CartesianLayerPadding.kt | 2 + .../cartesian/layer/LineCartesianLayer.kt | 17 +-- 6 files changed, 169 insertions(+), 30 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..17e6830ce 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,35 @@ 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/CartesianLayerPadding.kt b/vico/core/src/main/java/com/patrykandpatrick/vico/core/cartesian/CartesianLayerPadding.kt index 49c832486..d1a7b8984 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 @@ -29,6 +29,8 @@ 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 || 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..