Skip to content

Commit

Permalink
Rename selected API elements, fix CartesianChartModelProducer concu…
Browse files Browse the repository at this point in the history
…rrency issues, and more (WIP)

Co-authored-by: Patryk Goworowski <[email protected]>
  • Loading branch information
patrickmichalik and Gowsky committed Dec 28, 2024
1 parent b1fd697 commit 28d214e
Show file tree
Hide file tree
Showing 121 changed files with 1,787 additions and 2,211 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,41 +28,40 @@ import com.patrykandpatrick.vico.compose.common.component.rememberLayeredCompone
import com.patrykandpatrick.vico.compose.common.component.rememberShapeComponent
import com.patrykandpatrick.vico.compose.common.component.rememberTextComponent
import com.patrykandpatrick.vico.compose.common.component.shadow
import com.patrykandpatrick.vico.compose.common.dimensions
import com.patrykandpatrick.vico.compose.common.fill
import com.patrykandpatrick.vico.compose.common.insets
import com.patrykandpatrick.vico.compose.common.shape.markerCorneredShape
import com.patrykandpatrick.vico.core.cartesian.CartesianLayerDimensions
import com.patrykandpatrick.vico.core.cartesian.CartesianMeasuringContext
import com.patrykandpatrick.vico.core.cartesian.HorizontalDimensions
import com.patrykandpatrick.vico.core.cartesian.data.CartesianChartModel
import com.patrykandpatrick.vico.core.cartesian.layer.CartesianLayerMargins
import com.patrykandpatrick.vico.core.cartesian.marker.CartesianMarker
import com.patrykandpatrick.vico.core.cartesian.marker.DefaultCartesianMarker
import com.patrykandpatrick.vico.core.common.Fill
import com.patrykandpatrick.vico.core.common.Insets
import com.patrykandpatrick.vico.core.common.LayeredComponent
import com.patrykandpatrick.vico.core.common.component.Shadow
import com.patrykandpatrick.vico.core.common.component.ShapeComponent
import com.patrykandpatrick.vico.core.common.component.TextComponent
import com.patrykandpatrick.vico.core.common.shape.Corner
import com.patrykandpatrick.vico.core.common.shape.CorneredShape

@Composable
internal fun rememberMarker(
labelPosition: DefaultCartesianMarker.LabelPosition = DefaultCartesianMarker.LabelPosition.Top,
showIndicator: Boolean = true,
): CartesianMarker {
val labelBackgroundShape = markerCorneredShape(Corner.FullyRounded)
val labelBackgroundShape = markerCorneredShape(CorneredShape.Corner.Rounded)
val labelBackground =
rememberShapeComponent(
fill = fill(MaterialTheme.colorScheme.surfaceBright),
shape = labelBackgroundShape,
shadow =
shadow(radius = LABEL_BACKGROUND_SHADOW_RADIUS_DP.dp, dy = LABEL_BACKGROUND_SHADOW_DY_DP.dp),
shadow(radius = LABEL_BACKGROUND_SHADOW_RADIUS_DP.dp, y = LABEL_BACKGROUND_SHADOW_DY_DP.dp),
)
val label =
rememberTextComponent(
color = MaterialTheme.colorScheme.onSurface,
textAlignment = Layout.Alignment.ALIGN_CENTER,
padding = dimensions(8.dp, 4.dp),
padding = insets(8.dp, 4.dp),
background = labelBackground,
minWidth = TextComponent.MinWidth.fixed(40.dp),
)
Expand All @@ -72,14 +71,14 @@ internal fun rememberMarker(
val indicatorRearComponent = rememberShapeComponent(shape = CorneredShape.Pill)
val indicator =
rememberLayeredComponent(
rear = indicatorRearComponent,
back = indicatorRearComponent,
front =
rememberLayeredComponent(
rear = indicatorCenterComponent,
back = indicatorCenterComponent,
front = indicatorFrontComponent,
padding = dimensions(5.dp),
padding = insets(5.dp),
),
padding = dimensions(10.dp),
padding = insets(10.dp),
)
val guideline = rememberAxisGuidelineComponent()
return remember(label, labelPosition, indicator, showIndicator, guideline) {
Expand All @@ -91,20 +90,20 @@ internal fun rememberMarker(
if (showIndicator) {
{ color ->
LayeredComponent(
rear =
back =
ShapeComponent(Fill(ColorUtils.setAlphaComponent(color, 38)), CorneredShape.Pill),
front =
LayeredComponent(
rear =
back =
ShapeComponent(
fill = Fill(color),
shape = CorneredShape.Pill,
shadow = Shadow(radiusDp = 12f, color = color),
),
front = indicatorFrontComponent,
padding = dimensions(5.dp),
padding = insets(5.dp),
),
padding = dimensions(10.dp),
padding = insets(10.dp),
)
}
} else {
Expand All @@ -113,24 +112,24 @@ internal fun rememberMarker(
indicatorSizeDp = 36f,
guideline = guideline,
) {
override fun updateInsets(
override fun updateLayerMargins(
context: CartesianMeasuringContext,
horizontalDimensions: HorizontalDimensions,
layerDimensions: CartesianLayerDimensions,
model: CartesianChartModel,
insets: Insets,
margins: CartesianLayerMargins,
) {
with(context) {
val baseShadowInsetDp =
val baseShadowMarginDp =
CLIPPING_FREE_SHADOW_RADIUS_MULTIPLIER * LABEL_BACKGROUND_SHADOW_RADIUS_DP
var topInset = (baseShadowInsetDp - LABEL_BACKGROUND_SHADOW_DY_DP).pixels
var bottomInset = (baseShadowInsetDp + LABEL_BACKGROUND_SHADOW_DY_DP).pixels
var topMargin = (baseShadowMarginDp - LABEL_BACKGROUND_SHADOW_DY_DP).pixels
var bottomMargin = (baseShadowMarginDp + LABEL_BACKGROUND_SHADOW_DY_DP).pixels
when (labelPosition) {
LabelPosition.Top,
LabelPosition.AbovePoint -> topInset += label.getHeight(context) + tickSizeDp.pixels
LabelPosition.Bottom -> bottomInset += label.getHeight(context) + tickSizeDp.pixels
LabelPosition.AbovePoint -> topMargin += label.getHeight(context) + tickSizeDp.pixels
LabelPosition.Bottom -> bottomMargin += label.getHeight(context) + tickSizeDp.pixels
LabelPosition.AroundPoint -> {}
}
insets.ensureValuesAtLeast(top = topInset, bottom = bottomInset)
margins.ensureValuesAtLeast(top = topMargin, bottom = bottomMargin)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ 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.data.rememberExtraLambda
import com.patrykandpatrick.vico.compose.common.fill
import com.patrykandpatrick.vico.core.cartesian.axis.HorizontalAxis
import com.patrykandpatrick.vico.core.cartesian.axis.VerticalAxis
Expand Down Expand Up @@ -84,7 +83,7 @@ private fun ComposeChart1(modelProducer: CartesianChartModelProducer, modifier:
),
marker = marker,
layerPadding = { cartesianLayerPadding(scalableStart = 16.dp, scalableEnd = 16.dp) },
persistentMarkers = rememberExtraLambda(marker) { marker at PERSISTENT_MARKER_X },
persistentMarkers = { marker at PERSISTENT_MARKER_X },
),
modelProducer = modelProducer,
modifier = modifier,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,17 @@ import com.patrykandpatrick.vico.compose.cartesian.rememberCartesianChart
import com.patrykandpatrick.vico.compose.common.component.rememberLineComponent
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.compose.common.fill
import com.patrykandpatrick.vico.compose.common.insets
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.CartesianValueFormatter
import com.patrykandpatrick.vico.core.cartesian.data.columnSeries
import com.patrykandpatrick.vico.core.cartesian.decoration.HorizontalLine
import com.patrykandpatrick.vico.core.cartesian.layer.ColumnCartesianLayer
import com.patrykandpatrick.vico.core.common.Dimensions
import com.patrykandpatrick.vico.core.common.Fill
import com.patrykandpatrick.vico.core.common.Insets
import com.patrykandpatrick.vico.core.common.component.LineComponent
import com.patrykandpatrick.vico.core.common.component.ShapeComponent
import com.patrykandpatrick.vico.core.common.component.TextComponent
Expand Down Expand Up @@ -140,9 +140,9 @@ private fun rememberComposeHorizontalLine(): HorizontalLine {
val line = rememberLineComponent(fill, HORIZONTAL_LINE_THICKNESS_DP.dp)
val labelComponent =
rememberTextComponent(
margins = dimensions(HORIZONTAL_LINE_LABEL_MARGIN_DP.dp),
margins = insets(HORIZONTAL_LINE_LABEL_MARGIN_DP.dp),
padding =
dimensions(
insets(
HORIZONTAL_LINE_LABEL_HORIZONTAL_PADDING_DP.dp,
HORIZONTAL_LINE_LABEL_VERTICAL_PADDING_DP.dp,
),
Expand All @@ -157,9 +157,9 @@ private fun getViewHorizontalLine() =
line = LineComponent(Fill(HORIZONTAL_LINE_COLOR), HORIZONTAL_LINE_THICKNESS_DP),
labelComponent =
TextComponent(
margins = Dimensions(HORIZONTAL_LINE_LABEL_MARGIN_DP),
margins = Insets(HORIZONTAL_LINE_LABEL_MARGIN_DP),
padding =
Dimensions(
Insets(
HORIZONTAL_LINE_LABEL_HORIZONTAL_PADDING_DP,
HORIZONTAL_LINE_LABEL_VERTICAL_PADDING_DP,
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ import com.patrykandpatrick.vico.compose.cartesian.rememberVicoZoomState
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.compose.common.fill
import com.patrykandpatrick.vico.compose.common.insets
import com.patrykandpatrick.vico.compose.common.shader.horizontalGradient
import com.patrykandpatrick.vico.core.cartesian.axis.HorizontalAxis
import com.patrykandpatrick.vico.core.cartesian.axis.VerticalAxis
Expand All @@ -48,7 +48,7 @@ import com.patrykandpatrick.vico.core.cartesian.data.lineSeries
import com.patrykandpatrick.vico.core.cartesian.layer.LineCartesianLayer
import com.patrykandpatrick.vico.core.cartesian.marker.DefaultCartesianMarker
import com.patrykandpatrick.vico.core.common.data.ExtraStore
import com.patrykandpatrick.vico.core.common.shader.DynamicShader
import com.patrykandpatrick.vico.core.common.shader.ShaderProvider
import com.patrykandpatrick.vico.core.common.shape.CorneredShape
import com.patrykandpatrick.vico.databinding.Chart3Binding
import com.patrykandpatrick.vico.sample.showcase.Defaults
Expand Down Expand Up @@ -94,7 +94,7 @@ private fun ComposeChart3(modelProducer: CartesianChartModelProducer, modifier:
fill =
LineCartesianLayer.LineFill.single(
fill(
DynamicShader.horizontalGradient(
ShaderProvider.horizontalGradient(
arrayOf(Color(LINE_COLOR_1), Color(LINE_COLOR_2))
)
)
Expand All @@ -111,8 +111,8 @@ private fun ComposeChart3(modelProducer: CartesianChartModelProducer, modifier:
titleComponent =
rememberTextComponent(
color = Color.Black,
margins = dimensions(end = 4.dp),
padding = dimensions(8.dp, 2.dp),
margins = insets(end = 4.dp),
padding = insets(8.dp, 2.dp),
background = rememberShapeComponent(fill(Color(LINE_COLOR_1)), CorneredShape.Pill),
),
title = stringResource(R.string.y_axis),
Expand All @@ -124,8 +124,8 @@ private fun ComposeChart3(modelProducer: CartesianChartModelProducer, modifier:
titleComponent =
rememberTextComponent(
color = Color.White,
margins = dimensions(top = 4.dp),
padding = dimensions(8.dp, 2.dp),
margins = insets(top = 4.dp),
padding = insets(8.dp, 2.dp),
background =
shapeComponent(fill(bottomAxisLabelBackgroundColor), CorneredShape.Pill),
),
Expand Down Expand Up @@ -155,7 +155,7 @@ private fun ViewChart3(modelProducer: CartesianChartModelProducer, modifier: Mod
LineCartesianLayer.Line(
fill =
LineCartesianLayer.LineFill.single(
fill(DynamicShader.horizontalGradient(LINE_COLOR_1, LINE_COLOR_2))
fill(ShaderProvider.horizontalGradient(LINE_COLOR_1, LINE_COLOR_2))
),
stroke = LineCartesianLayer.LineStroke.Dashed(),
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,16 @@ import com.patrykandpatrick.vico.compose.common.component.rememberLineComponent
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.compose.common.fill
import com.patrykandpatrick.vico.compose.common.insets
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.CartesianValueFormatter
import com.patrykandpatrick.vico.core.cartesian.data.columnSeries
import com.patrykandpatrick.vico.core.cartesian.decoration.HorizontalBox
import com.patrykandpatrick.vico.core.cartesian.layer.ColumnCartesianLayer
import com.patrykandpatrick.vico.core.common.Dimensions
import com.patrykandpatrick.vico.core.common.Insets
import com.patrykandpatrick.vico.core.common.component.ShapeComponent
import com.patrykandpatrick.vico.core.common.component.TextComponent
import com.patrykandpatrick.vico.core.common.shape.CorneredShape
Expand Down Expand Up @@ -140,9 +140,9 @@ private fun rememberComposeHorizontalBox(): HorizontalBox {
val box = rememberShapeComponent(fill(color.copy(HORIZONTAL_BOX_ALPHA)))
val labelComponent =
rememberTextComponent(
margins = dimensions(HORIZONTAL_BOX_LABEL_MARGIN_DP.dp),
margins = insets(HORIZONTAL_BOX_LABEL_MARGIN_DP.dp),
padding =
dimensions(
insets(
HORIZONTAL_BOX_LABEL_HORIZONTAL_PADDING_DP.dp,
HORIZONTAL_BOX_LABEL_VERTICAL_PADDING_DP.dp,
),
Expand All @@ -158,9 +158,9 @@ private fun getViewHorizontalBox(): HorizontalBox {
box = ShapeComponent(fill(color.copy(HORIZONTAL_BOX_ALPHA))),
labelComponent =
TextComponent(
margins = Dimensions(HORIZONTAL_BOX_LABEL_MARGIN_DP),
margins = Insets(HORIZONTAL_BOX_LABEL_MARGIN_DP),
padding =
Dimensions(
Insets(
HORIZONTAL_BOX_LABEL_HORIZONTAL_PADDING_DP,
HORIZONTAL_BOX_LABEL_VERTICAL_PADDING_DP,
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,8 @@ import com.patrykandpatrick.vico.compose.cartesian.rememberVicoZoomState
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.data.rememberExtraLambda
import com.patrykandpatrick.vico.compose.common.dimensions
import com.patrykandpatrick.vico.compose.common.fill
import com.patrykandpatrick.vico.compose.common.insets
import com.patrykandpatrick.vico.compose.common.rememberVerticalLegend
import com.patrykandpatrick.vico.compose.common.shape.rounded
import com.patrykandpatrick.vico.compose.common.vicoTheme
Expand Down Expand Up @@ -151,8 +150,8 @@ private fun ViewChart7(modelProducer: CartesianChartModelProducer, modifier: Mod
private fun rememberStartAxisLabel() =
rememberAxisLabelComponent(
color = Color.Black,
margins = dimensions(4.dp),
padding = dimensions(8.dp, 2.dp),
margins = insets(4.dp),
padding = insets(8.dp, 2.dp),
background = rememberShapeComponent(fill(Color(0xfffab94d)), CorneredShape.rounded(4.dp)),
)

Expand All @@ -161,19 +160,18 @@ private fun rememberLegend(): Legend<CartesianMeasuringContext, CartesianDrawing
val labelComponent = rememberTextComponent(vicoTheme.textColor)
val resources = LocalContext.current.resources
return rememberVerticalLegend(
items =
rememberExtraLambda {
chartColors.forEachIndexed { index, color ->
add(
LegendItem(
icon = shapeComponent(fill(color), CorneredShape.Pill),
labelComponent = labelComponent,
label = resources.getString(R.string.series_x, index + 1),
)
items = {
chartColors.forEachIndexed { index, color ->
add(
LegendItem(
icon = shapeComponent(fill(color), CorneredShape.Pill),
labelComponent = labelComponent,
label = resources.getString(R.string.series_x, index + 1),
)
}
},
padding = dimensions(top = 8.dp),
)
}
},
padding = insets(top = 8.dp),
)
}

Expand Down
Loading

0 comments on commit 28d214e

Please sign in to comment.