Skip to content

Commit

Permalink
Add initial version of ColumnCartesianLayer.ColumnProvider
Browse files Browse the repository at this point in the history
Co-authored-by: Patryk Goworowski <[email protected]>
  • Loading branch information
patrickmichalik and Gowsky committed Mar 29, 2024
1 parent 1f13594 commit 01f721d
Show file tree
Hide file tree
Showing 11 changed files with 225 additions and 92 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import com.patrykandpatrick.vico.core.axis.AxisItemPlacer
import com.patrykandpatrick.vico.core.axis.AxisPosition.Vertical
import com.patrykandpatrick.vico.core.axis.AxisPosition.Vertical.End
import com.patrykandpatrick.vico.core.axis.AxisPosition.Vertical.Start
import com.patrykandpatrick.vico.core.chart.layer.ColumnCartesianLayer
import com.patrykandpatrick.vico.core.component.shape.Shapes
import com.patrykandpatrick.vico.core.component.shape.shader.DynamicShaders
import com.patrykandpatrick.vico.core.marker.Marker
Expand All @@ -59,8 +60,8 @@ private val markerMap: Map<Float, Marker>
@Composable
private fun getColumnLayer(verticalAxisPosition: Vertical? = null) =
rememberColumnCartesianLayer(
columns =
listOf(
columnProvider =
ColumnCartesianLayer.ColumnProvider.series(
rememberLineComponent(
color = Color.Black,
thickness = 8.dp,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ import com.patrykandpatrick.vico.compose.component.shape.shader.verticalGradient
import com.patrykandpatrick.vico.compose.component.shape.toVicoShape
import com.patrykandpatrick.vico.compose.dimensions.dimensionsOf
import com.patrykandpatrick.vico.core.axis.vertical.VerticalAxis
import com.patrykandpatrick.vico.core.chart.layer.ColumnCartesianLayer
import com.patrykandpatrick.vico.core.chart.values.AxisValueOverrider
import com.patrykandpatrick.vico.core.component.shape.DashedShape
import com.patrykandpatrick.vico.core.component.shape.Shapes.pillShape
Expand All @@ -73,7 +74,7 @@ public fun ColumnChartCard(): Unit =
chart =
rememberCartesianChart(
rememberColumnCartesianLayer(
listOf(
ColumnCartesianLayer.ColumnProvider.series(
rememberLineComponent(
color = colors.primary,
thickness = 8.dp,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ import com.patrykandpatrick.vico.compose.component.rememberTextComponent
import com.patrykandpatrick.vico.core.axis.AxisItemPlacer
import com.patrykandpatrick.vico.core.chart.layer.ColumnCartesianLayer
import com.patrykandpatrick.vico.core.chart.values.AxisValueOverrider
import com.patrykandpatrick.vico.core.component.shape.LineComponent
import com.patrykandpatrick.vico.core.model.CartesianChartModel
import com.patrykandpatrick.vico.core.model.ColumnCartesianLayerModel
import com.patrykandpatrick.vico.sample.showcase.rememberMarker
Expand All @@ -48,10 +47,10 @@ private val model =
},
)

private val columns: List<LineComponent>
private val columnProvider
@Composable
get() =
listOf(
ColumnCartesianLayer.ColumnProvider.series(
rememberLineComponent(color = Color(0xFF494949), thickness = 8.dp),
rememberLineComponent(color = Color(0xFF7C7A7A), thickness = 8.dp),
rememberLineComponent(color = Color(0xFFFF5D73), thickness = 8.dp),
Expand All @@ -67,7 +66,7 @@ public fun StackedColumnChartWithNegativeValues() {
chart =
rememberCartesianChart(
rememberColumnCartesianLayer(
columns = columns,
columnProvider = columnProvider,
mergeMode = { ColumnCartesianLayer.MergeMode.Stacked },
),
startAxis =
Expand All @@ -90,7 +89,7 @@ public fun StackedColumnChartWithNegativeValuesAndDataLabels() {
chart =
rememberCartesianChart(
rememberColumnCartesianLayer(
columns = columns,
columnProvider = columnProvider,
dataLabel = rememberTextComponent(),
mergeMode = { ColumnCartesianLayer.MergeMode.Stacked },
),
Expand All @@ -113,7 +112,7 @@ public fun StackedColumnChartWithNegativeValuesAndAxisValuesOverridden() {
chart =
rememberCartesianChart(
rememberColumnCartesianLayer(
columns = columns,
columnProvider = columnProvider,
axisValueOverrider = AxisValueOverrider.fixed(minY = 1f, maxY = 4f),
mergeMode = { ColumnCartesianLayer.MergeMode.Stacked },
),
Expand All @@ -136,7 +135,7 @@ public fun StackedColumnChartWithNegativeValuesAndAxisValuesOverridden2() {
chart =
rememberCartesianChart(
rememberColumnCartesianLayer(
columns = columns,
columnProvider = columnProvider,
axisValueOverrider = AxisValueOverrider.fixed(minY = -2f, maxY = 0f),
mergeMode = { ColumnCartesianLayer.MergeMode.Stacked },
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import com.patrykandpatrick.vico.core.axis.AxisPosition
import com.patrykandpatrick.vico.core.axis.BaseAxis
import com.patrykandpatrick.vico.core.axis.formatter.AxisValueFormatter
import com.patrykandpatrick.vico.core.chart.decoration.HorizontalLine
import com.patrykandpatrick.vico.core.chart.layer.ColumnCartesianLayer
import com.patrykandpatrick.vico.core.chart.layout.HorizontalLayout
import com.patrykandpatrick.vico.core.component.shape.LineComponent
import com.patrykandpatrick.vico.core.component.shape.ShapeComponent
Expand Down Expand Up @@ -89,7 +90,7 @@ private fun ComposeChart2(
chart =
rememberCartesianChart(
rememberColumnCartesianLayer(
listOf(
ColumnCartesianLayer.ColumnProvider.series(
rememberLineComponent(
color = Color(0xffff5500),
thickness = 16.dp,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import com.patrykandpatrick.vico.compose.component.rememberLineComponent
import com.patrykandpatrick.vico.compose.component.shape.roundedCornerShape
import com.patrykandpatrick.vico.compose.component.shape.shader.color
import com.patrykandpatrick.vico.core.chart.DefaultPointConnector
import com.patrykandpatrick.vico.core.chart.layer.ColumnCartesianLayer
import com.patrykandpatrick.vico.core.component.shape.Shapes
import com.patrykandpatrick.vico.core.component.shape.shader.DynamicShaders
import com.patrykandpatrick.vico.core.model.CartesianChartModelProducer
Expand Down Expand Up @@ -91,10 +92,16 @@ private fun ComposeChart4(
chart =
rememberCartesianChart(
rememberColumnCartesianLayer(
columns =
columnColors.map {
rememberLineComponent(color = it, thickness = 8.dp, shape = Shapes.roundedCornerShape(2.dp))
},
columnProvider =
ColumnCartesianLayer.ColumnProvider.series(
columnColors.map { color ->
rememberLineComponent(
color = color,
thickness = 8.dp,
shape = Shapes.roundedCornerShape(2.dp),
)
},
),
),
rememberLineCartesianLayer(
lines =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,8 @@ private fun ComposeChart5(
chart =
rememberCartesianChart(
rememberColumnCartesianLayer(
columns =
listOf(
columnProvider =
ColumnCartesianLayer.ColumnProvider.series(
rememberLineComponent(
color = color1,
thickness = COLUMN_THICKNESS_DP.dp,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import com.patrykandpatrick.vico.compose.dimensions.dimensionsOf
import com.patrykandpatrick.vico.core.axis.AxisPosition
import com.patrykandpatrick.vico.core.axis.formatter.AxisValueFormatter
import com.patrykandpatrick.vico.core.axis.horizontal.HorizontalAxis
import com.patrykandpatrick.vico.core.chart.layer.ColumnCartesianLayer
import com.patrykandpatrick.vico.core.component.shape.Shapes
import com.patrykandpatrick.vico.core.model.CartesianChartModelProducer
import com.patrykandpatrick.vico.core.model.columnSeries
Expand Down Expand Up @@ -94,7 +95,9 @@ private fun ComposeChart6(
chart =
rememberCartesianChart(
rememberColumnCartesianLayer(
columnColors.map { rememberLineComponent(color = it, thickness = 8.dp, shape = shape) },
ColumnCartesianLayer.ColumnProvider.series(
columnColors.map { rememberLineComponent(color = it, thickness = 8.dp, shape = shape) },
),
),
startAxis = rememberStartAxis(),
bottomAxis = rememberBottomAxis(valueFormatter = bottomAxisValueFormatter),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,14 +95,16 @@ private fun ComposeChart8(
chart =
rememberCartesianChart(
rememberColumnCartesianLayer(
columns =
columnChartColors.map { color ->
rememberLineComponent(
color = color,
thickness = 8.dp,
shape = Shapes.roundedCornerShape(40),
)
},
columnProvider =
ColumnCartesianLayer.ColumnProvider.series(
columnChartColors.map { color ->
rememberLineComponent(
color = color,
thickness = 8.dp,
shape = Shapes.roundedCornerShape(40),
)
},
),
mergeMode = { ColumnCartesianLayer.MergeMode.Stacked },
verticalAxisPosition = AxisPosition.Vertical.Start,
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ import com.patrykandpatrick.vico.compose.component.rememberLineComponent
import com.patrykandpatrick.vico.compose.theme.vicoTheme
import com.patrykandpatrick.vico.core.Defaults
import com.patrykandpatrick.vico.core.axis.AxisPosition
import com.patrykandpatrick.vico.core.axis.vertical.VerticalAxis
import com.patrykandpatrick.vico.core.chart.layer.CartesianLayer
import com.patrykandpatrick.vico.core.chart.layer.ColumnCartesianLayer
import com.patrykandpatrick.vico.core.chart.layer.ColumnCartesianLayer.MergeMode
import com.patrykandpatrick.vico.core.chart.values.AxisValueOverrider
Expand All @@ -40,34 +38,19 @@ import com.patrykandpatrick.vico.core.model.drawing.ColumnCartesianLayerDrawingM
import com.patrykandpatrick.vico.core.model.drawing.DefaultDrawingModelInterpolator
import com.patrykandpatrick.vico.core.model.drawing.DrawingModelInterpolator

/**
* Creates a [ColumnCartesianLayer].
*
* @param columns the [LineComponent] instances to use for columns. This list is iterated through as many times
* as necessary for each column collection. If the list contains a single element, all columns have the same appearance.
* @param spacing the distance between neighboring column collections.
* @param innerSpacing the distance between neighboring grouped columns.
* @param mergeMode defines how columns should be drawn in column collections.
* @param dataLabel an optional [TextComponent] to use for data labels.
* @param dataLabelVerticalPosition the vertical position of data labels relative to the top of their
* respective columns.
* @param dataLabelValueFormatter the [ValueFormatter] to use for data labels.
* @param dataLabelRotationDegrees the rotation of data labels (in degrees).
* @param axisValueOverrider overrides the _x_ and _y_ ranges.
* @param verticalAxisPosition the position of the [VerticalAxis] with which the [ColumnCartesianLayer] should be
* associated. Use this for independent [CartesianLayer] scaling.
* @param drawingModelInterpolator interpolates the [ColumnCartesianLayer]’s [ColumnCartesianLayerDrawingModel]s.
*/
/** Creates and remembers a [ColumnCartesianLayer]. */
@Composable
public fun rememberColumnCartesianLayer(
columns: List<LineComponent> =
vicoTheme.cartesianLayerColors.map { color ->
rememberLineComponent(
color,
Defaults.COLUMN_WIDTH.dp,
Shapes.roundedCornerShape(Defaults.COLUMN_ROUNDNESS_PERCENT),
)
},
columnProvider: ColumnCartesianLayer.ColumnProvider =
ColumnCartesianLayer.ColumnProvider.series(
vicoTheme.cartesianLayerColors.map { color ->
rememberLineComponent(
color,
Defaults.COLUMN_WIDTH.dp,
Shapes.roundedCornerShape(Defaults.COLUMN_ROUNDNESS_PERCENT),
)
},
),
spacing: Dp = Defaults.COLUMN_OUTSIDE_SPACING.dp,
innerSpacing: Dp = Defaults.COLUMN_INSIDE_SPACING.dp,
mergeMode: (ExtraStore) -> MergeMode = { MergeMode.Grouped },
Expand All @@ -81,8 +64,8 @@ public fun rememberColumnCartesianLayer(
DrawingModelInterpolator<ColumnCartesianLayerDrawingModel.ColumnInfo, ColumnCartesianLayerDrawingModel> =
remember { DefaultDrawingModelInterpolator() },
): ColumnCartesianLayer =
remember { ColumnCartesianLayer() }.apply {
this.columns = columns
remember { ColumnCartesianLayer(columnProvider) }.apply {
this.columnProvider = columnProvider
this.spacingDp = spacing.value
this.innerSpacingDp = innerSpacing.value
this.mergeMode = mergeMode
Expand All @@ -94,3 +77,40 @@ public fun rememberColumnCartesianLayer(
this.verticalAxisPosition = verticalAxisPosition
this.drawingModelInterpolator = drawingModelInterpolator
}

/**
* Creates and remembers a [ColumnCartesianLayer] with the provided column [LineComponent]s ([columns]). One
* [LineComponent] is used per series. The [LineComponent]s and series are associated by index. If there are more series
* than [LineComponent]s, [columns] is iterated multiple times.
*/
@Composable
@Suppress("DeprecatedCallableAddReplaceWith")
@Deprecated("Replace `columns = ...` with `columnProvider = ColumnCartesianLayer.ColumnProvider.series(...)`.")
public fun rememberColumnCartesianLayer(
columns: List<LineComponent>,
spacing: Dp = Defaults.COLUMN_OUTSIDE_SPACING.dp,
innerSpacing: Dp = Defaults.COLUMN_INSIDE_SPACING.dp,
mergeMode: (ExtraStore) -> MergeMode = { MergeMode.Grouped },
verticalAxisPosition: AxisPosition.Vertical? = null,
dataLabel: TextComponent? = null,
dataLabelVerticalPosition: VerticalPosition = VerticalPosition.Top,
dataLabelValueFormatter: ValueFormatter = remember { DecimalFormatValueFormatter() },
dataLabelRotationDegrees: Float = 0f,
axisValueOverrider: AxisValueOverrider = remember { AxisValueOverrider.auto() },
drawingModelInterpolator:
DrawingModelInterpolator<ColumnCartesianLayerDrawingModel.ColumnInfo, ColumnCartesianLayerDrawingModel> =
remember { DefaultDrawingModelInterpolator() },
): ColumnCartesianLayer =
rememberColumnCartesianLayer(
ColumnCartesianLayer.ColumnProvider.series(columns),
spacing,
innerSpacing,
mergeMode,
verticalAxisPosition,
dataLabel,
dataLabelVerticalPosition,
dataLabelValueFormatter,
dataLabelRotationDegrees,
axisValueOverrider,
drawingModelInterpolator,
)
Loading

0 comments on commit 01f721d

Please sign in to comment.