Skip to content

Commit

Permalink
Rework selected core APIs (WIP)
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 Dec 2, 2023
1 parent 6de3863 commit e02c303
Show file tree
Hide file tree
Showing 153 changed files with 4,211 additions and 5,527 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@ import com.patrykandpatrick.vico.compose.axis.axisLabelComponent
import com.patrykandpatrick.vico.compose.axis.horizontal.rememberBottomAxis
import com.patrykandpatrick.vico.compose.axis.vertical.rememberEndAxis
import com.patrykandpatrick.vico.compose.axis.vertical.rememberStartAxis
import com.patrykandpatrick.vico.compose.chart.Chart
import com.patrykandpatrick.vico.compose.chart.column.columnChart
import com.patrykandpatrick.vico.compose.chart.CartesianChartHost
import com.patrykandpatrick.vico.compose.chart.layer.rememberColumnCartesianLayer
import com.patrykandpatrick.vico.compose.chart.rememberCartesianChart
import com.patrykandpatrick.vico.compose.component.lineComponent
import com.patrykandpatrick.vico.compose.component.shapeComponent
import com.patrykandpatrick.vico.compose.style.LocalChartStyle
Expand All @@ -36,9 +37,10 @@ import com.patrykandpatrick.vico.core.component.shape.cornered.Corner
import com.patrykandpatrick.vico.core.component.shape.cornered.CorneredShape
import com.patrykandpatrick.vico.core.component.shape.cornered.CutCornerTreatment
import com.patrykandpatrick.vico.core.component.shape.cornered.RoundedCornerTreatment
import com.patrykandpatrick.vico.core.entry.entryModelOf
import com.patrykandpatrick.vico.core.model.CartesianChartModel
import com.patrykandpatrick.vico.core.model.ColumnCartesianLayerModel

private val model = entryModelOf(1, 2, 3, 4)
private val model = CartesianChartModel(ColumnCartesianLayerModel.build { series(1, 2, 3, 4) })

@Composable
private fun ProvidePreviewChartStyle(content: @Composable () -> Unit) {
Expand All @@ -50,10 +52,10 @@ private fun ProvidePreviewChartStyle(content: @Composable () -> Unit) {
axisLineColor = Color.Black.copy(alpha = 0.5f),
axisGuidelineColor = Color.Black.copy(alpha = 0.2f),
),
columnChart =
LocalChartStyle.current.columnChart.copy(
columnLayer =
LocalChartStyle.current.columnLayer.copy(
columns =
LocalChartStyle.current.columnChart.columns.map {
LocalChartStyle.current.columnLayer.columns.map {
lineComponent(
color = Color.Gray,
thickness = it.thicknessDp.dp,
Expand Down Expand Up @@ -97,8 +99,8 @@ public fun HorizontalAxisTextInside() {
verticalMargin = 4.dp,
horizontalMargin = 4.dp,
)
Chart(
chart = columnChart(),
CartesianChartHost(
chart = rememberCartesianChart(rememberColumnCartesianLayer()),
model = model,
startAxis =
rememberStartAxis(
Expand Down Expand Up @@ -131,8 +133,8 @@ public fun HorizontalAxisTextInsideAndBottomAxis() {
verticalMargin = 4.dp,
horizontalMargin = 4.dp,
)
Chart(
chart = columnChart(),
CartesianChartHost(
chart = rememberCartesianChart(rememberColumnCartesianLayer()),
model = model,
startAxis =
rememberStartAxis(
Expand All @@ -154,8 +156,8 @@ public fun HorizontalAxisTextInsideAndBottomAxis() {
@Preview(showBackground = true, widthDp = 250)
public fun HorizontalAxisTextOutside() {
ProvidePreviewChartStyle {
Chart(
chart = columnChart(),
CartesianChartHost(
chart = rememberCartesianChart(rememberColumnCartesianLayer()),
model = model,
startAxis =
rememberStartAxis(
Expand All @@ -174,8 +176,8 @@ public fun HorizontalAxisTextOutside() {
@Preview(showBackground = true, widthDp = 250)
public fun HorizontalAxisGuidelineDoesNotOverlayBottomAxisLine() {
ProvidePreviewChartStyle {
Chart(
chart = columnChart(),
CartesianChartHost(
chart = rememberCartesianChart(rememberColumnCartesianLayer()),
model = model,
startAxis =
rememberStartAxis(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,30 +25,29 @@ import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import com.patrykandpatrick.vico.compose.axis.horizontal.rememberBottomAxis
import com.patrykandpatrick.vico.compose.axis.vertical.rememberStartAxis
import com.patrykandpatrick.vico.compose.chart.Chart
import com.patrykandpatrick.vico.compose.chart.column.columnChart
import com.patrykandpatrick.vico.compose.chart.CartesianChartHost
import com.patrykandpatrick.vico.compose.chart.layer.rememberColumnCartesianLayer
import com.patrykandpatrick.vico.compose.chart.rememberCartesianChart
import com.patrykandpatrick.vico.core.axis.AxisItemPlacer
import com.patrykandpatrick.vico.core.chart.values.AxisValuesOverrider
import com.patrykandpatrick.vico.core.chart.values.AxisValueOverrider
import com.patrykandpatrick.vico.core.component.text.textComponent
import com.patrykandpatrick.vico.core.entry.entryModelOf
import com.patrykandpatrick.vico.core.model.CartesianChartModel
import com.patrykandpatrick.vico.core.model.LineCartesianLayerModel
import com.patrykandpatrick.vico.sample.showcase.rememberMarker

private val model = entryModelOf(2f, -1f, 4f, -2f, 1f, 5f, -3f)
private val model = CartesianChartModel(LineCartesianLayerModel.build { series(2, -1, 4, -2, 1, 5, -3) })

@Preview
@Composable
public fun SingleColumnChartWithNegativeValues() {
val marker = rememberMarker()
Surface {
Chart(
CartesianChartHost(
modifier = Modifier.height(250.dp),
chart =
columnChart(
persistentMarkers =
mapOf(
2f to marker,
3f to marker,
),
rememberCartesianChart(
rememberColumnCartesianLayer(),
persistentMarkers = mapOf(2f to marker, 3f to marker),
),
model = model,
startAxis = rememberStartAxis(itemPlacer = remember { AxisItemPlacer.Vertical.default(maxItemCount = 9) }),
Expand All @@ -61,11 +60,8 @@ public fun SingleColumnChartWithNegativeValues() {
@Composable
public fun SingleColumnChartWithNegativeValuesAndDataLabels() {
Surface {
Chart(
chart =
columnChart(
dataLabel = textComponent(),
),
CartesianChartHost(
chart = rememberCartesianChart(rememberColumnCartesianLayer(dataLabel = textComponent())),
model = model,
startAxis = rememberStartAxis(),
bottomAxis = rememberBottomAxis(),
Expand All @@ -77,14 +73,10 @@ public fun SingleColumnChartWithNegativeValuesAndDataLabels() {
@Composable
public fun SingleColumnChartWithNegativeValuesAndAxisValuesOverridden() {
Surface {
Chart(
CartesianChartHost(
chart =
columnChart(
axisValuesOverrider =
AxisValuesOverrider.fixed(
minY = 1f,
maxY = 4f,
),
rememberCartesianChart(
rememberColumnCartesianLayer(axisValueOverrider = AxisValueOverrider.fixed(minY = 1f, maxY = 4f)),
),
model = model,
startAxis = rememberStartAxis(itemPlacer = remember { AxisItemPlacer.Vertical.default(maxItemCount = 4) }),
Expand All @@ -97,14 +89,10 @@ public fun SingleColumnChartWithNegativeValuesAndAxisValuesOverridden() {
@Composable
public fun SingleColumnChartWithNegativeValuesAndAxisValuesOverridden2() {
Surface {
Chart(
CartesianChartHost(
chart =
columnChart(
axisValuesOverrider =
AxisValuesOverrider.fixed(
minY = -2f,
maxY = 0f,
),
rememberCartesianChart(
rememberColumnCartesianLayer(axisValueOverrider = AxisValueOverrider.fixed(minY = -2f, maxY = 0f)),
),
model = model,
startAxis = rememberStartAxis(itemPlacer = remember { AxisItemPlacer.Vertical.default(maxItemCount = 3) }),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,39 +25,40 @@ import androidx.compose.ui.unit.dp
import com.patrykandpatrick.vico.compose.axis.horizontal.rememberBottomAxis
import com.patrykandpatrick.vico.compose.axis.vertical.rememberEndAxis
import com.patrykandpatrick.vico.compose.axis.vertical.rememberStartAxis
import com.patrykandpatrick.vico.compose.chart.Chart
import com.patrykandpatrick.vico.compose.chart.column.columnChart
import com.patrykandpatrick.vico.compose.chart.line.lineChart
import com.patrykandpatrick.vico.compose.chart.line.lineSpec
import com.patrykandpatrick.vico.compose.chart.CartesianChartHost
import com.patrykandpatrick.vico.compose.chart.layer.lineSpec
import com.patrykandpatrick.vico.compose.chart.layer.rememberColumnCartesianLayer
import com.patrykandpatrick.vico.compose.chart.layer.rememberLineCartesianLayer
import com.patrykandpatrick.vico.compose.chart.rememberCartesianChart
import com.patrykandpatrick.vico.compose.component.lineComponent
import com.patrykandpatrick.vico.compose.component.shape.shader.color
import com.patrykandpatrick.vico.compose.component.shape.shader.verticalGradient
import com.patrykandpatrick.vico.compose.component.textComponent
import com.patrykandpatrick.vico.core.axis.Axis
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.column.ColumnChart
import com.patrykandpatrick.vico.core.chart.composed.plus
import com.patrykandpatrick.vico.core.chart.line.LineChart
import com.patrykandpatrick.vico.core.component.shape.Shapes
import com.patrykandpatrick.vico.core.entry.composed.plus
import com.patrykandpatrick.vico.core.entry.entryModelOf
import com.patrykandpatrick.vico.core.component.shape.shader.DynamicShaders
import com.patrykandpatrick.vico.core.marker.Marker
import com.patrykandpatrick.vico.core.model.CartesianChartModel
import com.patrykandpatrick.vico.core.model.ColumnCartesianLayerModel
import com.patrykandpatrick.vico.core.model.LineCartesianLayerModel
import com.patrykandpatrick.vico.sample.showcase.rememberMarker

private val model1 = entryModelOf(0 to 1, 1 to 2, 2 to 4, 3 to 1, 4 to 4)
private val model2 = entryModelOf(1 to 4, 2 to 1, 3 to 8, 4 to 12, 5 to 5)
private val model =
CartesianChartModel(
ColumnCartesianLayerModel.build { series(1, 2, 4, 1, 4) },
LineCartesianLayerModel.build { series(4, 1, 8, 12, 5) },
)

private val markerMap: Map<Float, Marker>
@Composable get() = mapOf(4f to rememberMarker())

@Composable
private fun getColumnChart(
markerMap: Map<Float, Marker> = emptyMap(),
targetVerticalAxisPosition: Vertical? = null,
): ColumnChart =
columnChart(
private fun getColumnLayer(targetVerticalAxisPosition: Vertical? = null) =
rememberColumnCartesianLayer(
columns =
listOf(
lineComponent(
Expand All @@ -66,27 +67,22 @@ private fun getColumnChart(
shape = Shapes.pillShape,
),
),
persistentMarkers = markerMap,
targetVerticalAxisPosition = targetVerticalAxisPosition,
)

@Composable
private fun getLineChart(
markerMap: Map<Float, Marker> = emptyMap(),
targetVerticalAxisPosition: Vertical? = null,
): LineChart =
lineChart(
private fun getLineLayer(targetVerticalAxisPosition: Vertical? = null) =
rememberLineCartesianLayer(
lines =
listOf(
lineSpec(
lineColor = Color.DarkGray,
lineBackgroundShader =
shader = DynamicShaders.color(Color.DarkGray),
backgroundShader =
verticalGradient(
arrayOf(Color.DarkGray, Color.DarkGray.copy(alpha = 0f)),
),
),
),
persistentMarkers = markerMap,
targetVerticalAxisPosition = targetVerticalAxisPosition,
)

Expand All @@ -107,15 +103,9 @@ private val endAxis: Axis<End>
@Composable
@Preview("Chart with independent axes", widthDp = 350)
public fun ChartWithIndependentAxes(modifier: Modifier = Modifier) {
val composedChart =
getColumnChart(targetVerticalAxisPosition = Start) +
getLineChart(targetVerticalAxisPosition = End)

composedChart.setPersistentMarkers(markerMap)

Chart(
chart = composedChart,
model = model1 + model2,
CartesianChartHost(
chart = rememberCartesianChart(getColumnLayer(Start), getLineLayer(End)),
model = model,
startAxis = startAxis,
bottomAxis = rememberBottomAxis(),
endAxis = endAxis,
Expand All @@ -126,13 +116,9 @@ public fun ChartWithIndependentAxes(modifier: Modifier = Modifier) {
@Composable
@Preview("Chart with dependent axes", widthDp = 350)
public fun ChartWithDependentAxes(modifier: Modifier = Modifier) {
val composedChart = getColumnChart() + getLineChart()

composedChart.setPersistentMarkers(markerMap)

Chart(
chart = composedChart,
model = model1 + model2,
CartesianChartHost(
chart = rememberCartesianChart(getColumnLayer(), getLineLayer(), persistentMarkers = markerMap),
model = model,
startAxis = startAxis,
bottomAxis = rememberBottomAxis(),
endAxis = endAxis,
Expand All @@ -143,9 +129,9 @@ public fun ChartWithDependentAxes(modifier: Modifier = Modifier) {
@Composable
@Preview("Column chart", widthDp = 350)
public fun ColumnChart(modifier: Modifier = Modifier) {
Chart(
chart = getColumnChart(markerMap = markerMap),
model = model1,
CartesianChartHost(
chart = rememberCartesianChart(getColumnLayer(), persistentMarkers = markerMap),
model = model,
startAxis = startAxis,
bottomAxis = rememberBottomAxis(),
modifier = modifier,
Expand All @@ -155,9 +141,9 @@ public fun ColumnChart(modifier: Modifier = Modifier) {
@Composable
@Preview("Line chart", widthDp = 350)
public fun LineChart(modifier: Modifier = Modifier) {
Chart(
chart = getLineChart(markerMap = markerMap),
model = model2,
CartesianChartHost(
chart = rememberCartesianChart(getLineLayer(), persistentMarkers = markerMap),
model = model,
startAxis = startAxis,
bottomAxis = rememberBottomAxis(),
modifier = modifier,
Expand Down
Loading

0 comments on commit e02c303

Please sign in to comment.