Skip to content

Commit

Permalink
Rework 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 Nov 27, 2023
1 parent c3ee8aa commit 899a3f4
Show file tree
Hide file tree
Showing 125 changed files with 2,715 additions and 3,546 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,22 @@ 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.cartesian.rememberCartesianChart
import com.patrykandpatrick.vico.compose.chart.column.rememberColumnCartesianLayer
import com.patrykandpatrick.vico.compose.component.lineComponent
import com.patrykandpatrick.vico.compose.component.shapeComponent
import com.patrykandpatrick.vico.compose.style.LocalChartStyle
import com.patrykandpatrick.vico.core.axis.vertical.VerticalAxis
import com.patrykandpatrick.vico.core.chart.column.ColumnCartesianLayerModel
import com.patrykandpatrick.vico.core.chart.composed.CartesianChartModel
import com.patrykandpatrick.vico.core.component.shape.Shapes
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

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 @@ -48,8 +50,8 @@ 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(
columns = LocalChartStyle.current.columnChart.columns.map {
columnLayer = LocalChartStyle.current.columnLayer.copy(
columns = LocalChartStyle.current.columnLayer.columns.map {
lineComponent(
color = Color.Gray,
thickness = it.thicknessDp.dp,
Expand Down Expand Up @@ -88,8 +90,8 @@ public fun HorizontalAxisTextInside() {
verticalMargin = 4.dp,
horizontalMargin = 4.dp,
)
Chart(
chart = columnChart(),
CartesianChartHost(
chart = rememberCartesianChart(rememberColumnCartesianLayer()),
model = model,
startAxis = rememberStartAxis(
horizontalLabelPosition = VerticalAxis.HorizontalLabelPosition.Inside,
Expand Down Expand Up @@ -118,8 +120,8 @@ public fun HorizontalAxisTextInsideAndBottomAxis() {
verticalMargin = 4.dp,
horizontalMargin = 4.dp,
)
Chart(
chart = columnChart(),
CartesianChartHost(
chart = rememberCartesianChart(rememberColumnCartesianLayer()),
model = model,
startAxis = rememberStartAxis(
horizontalLabelPosition = VerticalAxis.HorizontalLabelPosition.Inside,
Expand All @@ -139,8 +141,8 @@ public fun HorizontalAxisTextInsideAndBottomAxis() {
@Preview(showBackground = true, widthDp = 250)
public fun HorizontalAxisTextOutside() {
ProvidePreviewChartStyle {
Chart(
chart = columnChart(),
CartesianChartHost(
chart = rememberCartesianChart(rememberColumnCartesianLayer()),
model = model,
startAxis = rememberStartAxis(
horizontalLabelPosition = VerticalAxis.HorizontalLabelPosition.Outside,
Expand All @@ -157,8 +159,8 @@ public fun HorizontalAxisTextOutside() {
@Preview(showBackground = true, widthDp = 250)
public fun HorizontalAxisGuidelineDoesNotOverlayBottomAxisLine() {
ProvidePreviewChartStyle {
Chart(
chart = columnChart(),
CartesianChartHost(
chart = rememberCartesianChart(rememberColumnCartesianLayer()),
model = model,
startAxis = rememberStartAxis(
horizontalLabelPosition = VerticalAxis.HorizontalLabelPosition.Outside,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,28 +25,28 @@ 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.cartesian.rememberCartesianChart
import com.patrykandpatrick.vico.compose.chart.column.rememberColumnCartesianLayer
import com.patrykandpatrick.vico.core.axis.AxisItemPlacer
import com.patrykandpatrick.vico.core.chart.values.AxisValuesOverrider
import com.patrykandpatrick.vico.core.chart.composed.CartesianChartModel
import com.patrykandpatrick.vico.core.chart.line.LineCartesianLayerModel
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.sample.showcase.rememberMarker

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

@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,
),
chart = rememberCartesianChart(
rememberColumnCartesianLayer(),
persistentMarkers = mapOf(2f to marker, 3f to marker),
),
model = model,
startAxis = rememberStartAxis(itemPlacer = remember { AxisItemPlacer.Vertical.default(maxItemCount = 9) }),
Expand All @@ -59,10 +59,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 @@ -74,12 +72,9 @@ public fun SingleColumnChartWithNegativeValuesAndDataLabels() {
@Composable
public fun SingleColumnChartWithNegativeValuesAndAxisValuesOverridden() {
Surface {
Chart(
chart = columnChart(
axisValuesOverrider = AxisValuesOverrider.fixed(
minY = 1f,
maxY = 4f,
),
CartesianChartHost(
chart = rememberCartesianChart(
rememberColumnCartesianLayer(axisValueOverrider = AxisValueOverrider.fixed(minY = 1f, maxY = 4f)),
),
model = model,
startAxis = rememberStartAxis(itemPlacer = remember { AxisItemPlacer.Vertical.default(maxItemCount = 4) }),
Expand All @@ -92,12 +87,9 @@ public fun SingleColumnChartWithNegativeValuesAndAxisValuesOverridden() {
@Composable
public fun SingleColumnChartWithNegativeValuesAndAxisValuesOverridden2() {
Surface {
Chart(
chart = columnChart(
axisValuesOverrider = AxisValuesOverrider.fixed(
minY = -2f,
maxY = 0f,
),
CartesianChartHost(
chart = 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,10 +25,11 @@ 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.CartesianChartHost
import com.patrykandpatrick.vico.compose.chart.cartesian.rememberCartesianChart
import com.patrykandpatrick.vico.compose.chart.column.rememberColumnCartesianLayer
import com.patrykandpatrick.vico.compose.chart.line.lineSpec
import com.patrykandpatrick.vico.compose.chart.line.rememberLineCartesianLayer
import com.patrykandpatrick.vico.compose.component.lineComponent
import com.patrykandpatrick.vico.compose.component.shape.shader.verticalGradient
import com.patrykandpatrick.vico.compose.component.textComponent
Expand All @@ -37,42 +38,35 @@ 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.chart.column.ColumnCartesianLayerModel
import com.patrykandpatrick.vico.core.chart.composed.CartesianChartModel
import com.patrykandpatrick.vico.core.chart.line.LineCartesianLayerModel
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.marker.Marker
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(
color = Color.Black,
thickness = 8.dp,
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,
Expand All @@ -81,7 +75,6 @@ private fun getLineChart(
),
),
),
persistentMarkers = markerMap,
targetVerticalAxisPosition = targetVerticalAxisPosition,
)

Expand All @@ -100,14 +93,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 @@ -118,13 +106,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 @@ -135,9 +119,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 @@ -147,9 +131,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 899a3f4

Please sign in to comment.