Skip to content

Commit

Permalink
Manually provide vertical insets to LineCartesianLayer
Browse files Browse the repository at this point in the history
  • Loading branch information
Tyler-Lopez committed Oct 31, 2024
1 parent 7066e4b commit 3186703
Show file tree
Hide file tree
Showing 6 changed files with 169 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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) },
)
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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),
Expand Down
Original file line number Diff line number Diff line change
@@ -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()
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 @@ -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 ||
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
}
}
Expand All @@ -598,15 +598,10 @@ protected constructor(
insets: Insets,
) {
with(context) {
val verticalInset =
(0..<model.series.size)
.mapNotNull { lineProvider.getLine(it, model.extraStore) }
.maxOf {
max(it.thicknessDp, it.pointProvider?.getLargestPoint(model.extraStore)?.sizeDp.orZero)
}
.half
.pixels
insets.ensureValuesAtLeast(top = verticalInset, bottom = verticalInset)
insets.ensureValuesAtLeast(
top = layerPadding.unscalableTopDp,
bottom = layerPadding.unscalableBottomDp,
)
}
}

Expand Down

0 comments on commit 3186703

Please sign in to comment.