From fe23983a99354e03e734c5420525124636a1531c Mon Sep 17 00:00:00 2001 From: Patrick Michalik <120058021+patrickmichalik@users.noreply.github.com> Date: Wed, 29 Nov 2023 21:03:49 +0100 Subject: [PATCH] Rework selected core APIs (WIP) Co-authored-by: Patryk Goworowski --- .../vico/sample/previews/AxisLinePreviews.kt | 32 +- .../ColumnChartsWithNegativeValuesPreviews.kt | 42 +- .../sample/previews/ContainedChartsPreview.kt | 79 ++-- .../vico/sample/previews/LineChartPreviews.kt | 124 +++--- .../LineChartsWithNegativeValuesPreviews.kt | 75 ++-- .../vico/sample/previews/Previews.kt | 45 ++- ...dColumnChartsWithNegativeValuesPreviews.kt | 70 ++-- .../sample/previews/ThresholdLinePreviews.kt | 299 +++++++------- .../composables/column/ColumnCharts.kt | 27 +- .../previews/composables/line/LineCharts.kt | 23 +- .../sample/previews/resource/SampleModels.kt | 21 +- .../vico/sample/showcase/ChartStyle.kt | 13 +- .../vico/sample/showcase/ShowcaseScreen.kt | 22 +- .../vico/sample/showcase/ShowcaseViewModel.kt | 90 ++--- .../vico/sample/showcase/charts/Chart1.kt | 28 +- .../vico/sample/showcase/charts/Chart2.kt | 40 +- .../vico/sample/showcase/charts/Chart3.kt | 40 +- .../vico/sample/showcase/charts/Chart4.kt | 70 ++-- .../vico/sample/showcase/charts/Chart5.kt | 82 ++-- .../vico/sample/showcase/charts/Chart6.kt | 36 +- .../vico/sample/showcase/charts/Chart7.kt | 37 +- .../vico/sample/showcase/charts/Chart8.kt | 55 ++- .../vico/sample/showcase/charts/Chart9.kt | 138 ++++--- sample/src/main/res/layout/chart_1.xml | 7 +- sample/src/main/res/layout/chart_2.xml | 6 +- sample/src/main/res/layout/chart_3.xml | 6 +- sample/src/main/res/layout/chart_4.xml | 8 +- sample/src/main/res/layout/chart_5.xml | 6 +- sample/src/main/res/layout/chart_6.xml | 6 +- sample/src/main/res/layout/chart_7.xml | 6 +- sample/src/main/res/layout/chart_8.xml | 8 +- sample/src/main/res/layout/chart_9.xml | 8 +- .../main/res/values-night/chart_9_styles.xml | 4 +- sample/src/main/res/values/chart_5_styles.xml | 1 + sample/src/main/res/values/chart_8_styles.xml | 1 + sample/src/main/res/values/chart_9_styles.xml | 1 + sample/src/main/res/values/strings.xml | 2 +- .../vico/compose/chart/CartesianChart.kt | 45 +++ .../{Charts.kt => CartesianChartHost.kt} | 120 +++--- ...ColumnChart.kt => ColumnCartesianLayer.kt} | 68 ++-- .../chart/entry/ChartEntryModelExtensions.kt | 55 ++- .../compose/chart/line/LineCartesianLayer.kt | 208 ++++++++++ .../vico/compose/chart/line/LineChart.kt | 266 ------------- .../compose/chart/scroll/ChartScrollSpec.kt | 18 +- .../compose/chart/scroll/ChartScrollState.kt | 6 +- .../component/shape/shader/DynamicShaders.kt | 18 +- .../layout/MeasureContextExtensions.kt | 11 +- .../state/CartesianChartModelWrapper.kt | 58 +++ .../compose/state/ChartEntryModelWrapper.kt | 63 --- .../vico/compose/style/ChartStyle.kt | 36 +- .../vico/core/axis/AxisItemPlacer.kt | 15 +- .../vico/core/axis/AxisManager.kt | 12 +- .../vico/core/axis/AxisPosition.kt | 36 +- .../vico/core/axis/AxisRenderer.kt | 12 +- .../DefaultHorizontalAxisItemPlacer.kt | 111 +++--- .../core/axis/horizontal/HorizontalAxis.kt | 15 +- .../vertical/DefaultVerticalAxisItemPlacer.kt | 34 +- .../vico/core/axis/vertical/VerticalAxis.kt | 27 +- .../vico/core/chart/BaseCartesianLayer.kt | 52 +++ .../vico/core/chart/BaseChart.kt | 129 ------ .../vico/core/chart/CartesianChart.kt | 219 +++++++++++ .../vico/core/chart/CartesianChartModel.kt | 101 +++++ .../vico/core/chart/CartesianLayer.kt | 64 +++ .../patrykandpatrick/vico/core/chart/Chart.kt | 193 --------- .../vico/core/chart/ChartExtensions.kt | 4 +- .../vico/core/chart/DefaultPointConnector.kt | 9 +- .../vico/core/chart/EntryModelExtensions.kt | 62 +-- .../vico/core/chart/LineSpecExtensions.kt | 28 +- ...ColumnChart.kt => ColumnCartesianLayer.kt} | 208 +++++----- ...kt => ColumnCartesianLayerDrawingModel.kt} | 14 +- .../chart/column/ColumnCartesianLayerModel.kt | 169 ++++++++ .../vico/core/chart/composed/ComposedChart.kt | 172 -------- .../chart/composed/ComposedChartEntryModel.kt | 29 -- .../chart/composed/ComposedChartExtensions.kt | 35 -- .../vico/core/chart/decoration/Decoration.kt | 18 +- .../core/chart/decoration/ThresholdLine.kt | 26 +- .../chart/dimensions/HorizontalDimensions.kt | 12 +- .../vico/core/chart/draw/ChartDrawContext.kt | 16 +- .../chart/draw/ChartDrawContextExtensions.kt | 15 +- .../vico/core/chart/insets/ChartInsetter.kt | 14 +- .../core/chart/insets/HorizontalInsets.kt | 6 +- .../core/chart/layout/HorizontalLayout.kt | 23 +- .../{LineChart.kt => LineCartesianLayer.kt} | 367 +++++++----------- ...l.kt => LineCartesianLayerDrawingModel.kt} | 18 +- ...ons.kt => LineCartesianLayerExtensions.kt} | 4 +- .../chart/line/LineCartesianLayerModel.kt | 145 +++++++ ...luesOverrider.kt => AxisValueOverrider.kt} | 50 ++- .../vico/core/chart/values/ChartValues.kt | 90 +++-- .../core/chart/values/ChartValuesManager.kt | 110 ------ .../core/chart/values/ChartValuesProvider.kt | 38 -- .../core/chart/values/MutableChartValues.kt | 105 +++-- .../core/component/marker/MarkerComponent.kt | 5 +- .../component/shape/shader/ColorShader.kt | 44 +++ .../component/shape/shader/DynamicShader.kt | 67 +--- .../component/shape/shader/DynamicShaders.kt | 19 +- .../shape/shader/HorizontalSplitShader.kt | 241 ------------ .../component/shape/shader/SolidShader.kt | 84 ---- .../component/shape/shader/TopBottomShader.kt | 89 +++++ .../vico/core/context/MeasureContext.kt | 9 +- .../core/context/MutableMeasureContext.kt | 4 +- .../vico/core/draw/DrawContextExtensions.kt | 4 +- .../vico/core/entry/CartesianLayerModel.kt | 63 +++ .../vico/core/entry/ChartEntry.kt | 41 -- .../vico/core/entry/ChartEntryExtensions.kt | 81 +--- .../vico/core/entry/ChartEntryModel.kt | 93 ----- .../core/entry/ChartEntryModelProducer.kt | 247 ------------ .../vico/core/entry/ChartModelProducer.kt | 71 ---- .../vico/core/entry/EntryListExtensions.kt | 49 --- .../vico/core/entry/FloatEntry.kt | 23 -- .../composed/CartesianChartModelProducer.kt | 232 +++++++++++ .../ComposedChartEntryModelProducer.kt | 342 ---------------- .../composed/ComposedEntryListExtensions.kt | 47 --- .../vico/core/entry/diff/DrawingModel.kt | 9 +- .../vico/core/extension/MapExtensions.kt | 8 +- .../vico/core/extension/NumberExtensions.kt | 6 + .../formatter/DecimalFormatValueFormatter.kt | 10 +- .../core/formatter/DefaultValueFormatter.kt | 10 +- .../PercentageFormatValueFormatter.kt | 11 +- .../vico/core/formatter/ValueFormatter.kt | 9 +- .../vico/core/layout/VirtualLayout.kt | 7 +- .../marker/DefaultMarkerLabelFormatter.kt | 14 + .../vico/core/marker/Marker.kt | 18 +- .../vico/core/scroll/AutoScrollCondition.kt | 35 +- .../util/RandomCartesianModelGenerator.kt | 75 ++++ .../vico/core/util/RandomEntriesGenerator.kt | 83 ---- .../vico/core/ChartEntryModelProducerTest.kt | 44 --- ...BaseChartView.kt => CartesianChartView.kt} | 166 +++++--- .../vico/views/chart/ChartView.kt | 63 --- .../vico/views/chart/ComposedChartView.kt | 69 ---- .../chart/column/ColumnChartExtensions.kt | 22 +- .../views/chart/line/LineChartExtensions.kt | 25 +- .../component/shape/shader/DynamicShaders.kt | 15 - .../vico/views/scroll/ChartScrollSpec.kt | 20 +- .../vico/views/theme/ChartStyleExtensions.kt | 63 +-- .../views/theme/ComponentStyleExtensions.kt | 20 +- .../vico/views/theme/ThemeHandler.kt | 146 +++---- vico/views/src/main/res/values/attrs.xml | 44 +-- .../main/res/values/column_chart_attrs.xml | 8 +- .../src/main/res/values/line_chart_attrs.xml | 4 +- 139 files changed, 3522 insertions(+), 4674 deletions(-) create mode 100644 vico/compose/src/main/java/com/patrykandpatrick/vico/compose/chart/CartesianChart.kt rename vico/compose/src/main/java/com/patrykandpatrick/vico/compose/chart/{Charts.kt => CartesianChartHost.kt} (84%) rename vico/compose/src/main/java/com/patrykandpatrick/vico/compose/chart/column/{ColumnChart.kt => ColumnCartesianLayer.kt} (56%) create mode 100644 vico/compose/src/main/java/com/patrykandpatrick/vico/compose/chart/line/LineCartesianLayer.kt delete mode 100644 vico/compose/src/main/java/com/patrykandpatrick/vico/compose/chart/line/LineChart.kt create mode 100644 vico/compose/src/main/java/com/patrykandpatrick/vico/compose/state/CartesianChartModelWrapper.kt delete mode 100644 vico/compose/src/main/java/com/patrykandpatrick/vico/compose/state/ChartEntryModelWrapper.kt create mode 100644 vico/core/src/main/java/com/patrykandpatrick/vico/core/chart/BaseCartesianLayer.kt delete mode 100644 vico/core/src/main/java/com/patrykandpatrick/vico/core/chart/BaseChart.kt create mode 100644 vico/core/src/main/java/com/patrykandpatrick/vico/core/chart/CartesianChart.kt create mode 100644 vico/core/src/main/java/com/patrykandpatrick/vico/core/chart/CartesianChartModel.kt create mode 100644 vico/core/src/main/java/com/patrykandpatrick/vico/core/chart/CartesianLayer.kt delete mode 100644 vico/core/src/main/java/com/patrykandpatrick/vico/core/chart/Chart.kt rename vico/core/src/main/java/com/patrykandpatrick/vico/core/chart/column/{ColumnChart.kt => ColumnCartesianLayer.kt} (72%) rename vico/core/src/main/java/com/patrykandpatrick/vico/core/chart/column/{ColumnChartDrawingModel.kt => ColumnCartesianLayerDrawingModel.kt} (70%) create mode 100644 vico/core/src/main/java/com/patrykandpatrick/vico/core/chart/column/ColumnCartesianLayerModel.kt delete mode 100644 vico/core/src/main/java/com/patrykandpatrick/vico/core/chart/composed/ComposedChart.kt delete mode 100644 vico/core/src/main/java/com/patrykandpatrick/vico/core/chart/composed/ComposedChartEntryModel.kt delete mode 100644 vico/core/src/main/java/com/patrykandpatrick/vico/core/chart/composed/ComposedChartExtensions.kt rename vico/core/src/main/java/com/patrykandpatrick/vico/core/chart/line/{LineChart.kt => LineCartesianLayer.kt} (64%) rename vico/core/src/main/java/com/patrykandpatrick/vico/core/chart/line/{LineChartDrawingModel.kt => LineCartesianLayerDrawingModel.kt} (68%) rename vico/core/src/main/java/com/patrykandpatrick/vico/core/chart/line/{LineChartExtensions.kt => LineCartesianLayerExtensions.kt} (88%) create mode 100644 vico/core/src/main/java/com/patrykandpatrick/vico/core/chart/line/LineCartesianLayerModel.kt rename vico/core/src/main/java/com/patrykandpatrick/vico/core/chart/values/{AxisValuesOverrider.kt => AxisValueOverrider.kt} (60%) delete mode 100644 vico/core/src/main/java/com/patrykandpatrick/vico/core/chart/values/ChartValuesManager.kt delete mode 100644 vico/core/src/main/java/com/patrykandpatrick/vico/core/chart/values/ChartValuesProvider.kt create mode 100644 vico/core/src/main/java/com/patrykandpatrick/vico/core/component/shape/shader/ColorShader.kt delete mode 100644 vico/core/src/main/java/com/patrykandpatrick/vico/core/component/shape/shader/HorizontalSplitShader.kt delete mode 100644 vico/core/src/main/java/com/patrykandpatrick/vico/core/component/shape/shader/SolidShader.kt create mode 100644 vico/core/src/main/java/com/patrykandpatrick/vico/core/component/shape/shader/TopBottomShader.kt create mode 100644 vico/core/src/main/java/com/patrykandpatrick/vico/core/entry/CartesianLayerModel.kt delete mode 100644 vico/core/src/main/java/com/patrykandpatrick/vico/core/entry/ChartEntry.kt delete mode 100644 vico/core/src/main/java/com/patrykandpatrick/vico/core/entry/ChartEntryModel.kt delete mode 100644 vico/core/src/main/java/com/patrykandpatrick/vico/core/entry/ChartEntryModelProducer.kt delete mode 100644 vico/core/src/main/java/com/patrykandpatrick/vico/core/entry/ChartModelProducer.kt delete mode 100644 vico/core/src/main/java/com/patrykandpatrick/vico/core/entry/EntryListExtensions.kt delete mode 100644 vico/core/src/main/java/com/patrykandpatrick/vico/core/entry/FloatEntry.kt create mode 100644 vico/core/src/main/java/com/patrykandpatrick/vico/core/entry/composed/CartesianChartModelProducer.kt delete mode 100644 vico/core/src/main/java/com/patrykandpatrick/vico/core/entry/composed/ComposedChartEntryModelProducer.kt delete mode 100644 vico/core/src/main/java/com/patrykandpatrick/vico/core/entry/composed/ComposedEntryListExtensions.kt create mode 100644 vico/core/src/main/java/com/patrykandpatrick/vico/core/util/RandomCartesianModelGenerator.kt delete mode 100644 vico/core/src/main/java/com/patrykandpatrick/vico/core/util/RandomEntriesGenerator.kt delete mode 100644 vico/core/src/test/java/com/patrykandpatrick/vico/core/ChartEntryModelProducerTest.kt rename vico/views/src/main/java/com/patrykandpatrick/vico/views/chart/{BaseChartView.kt => CartesianChartView.kt} (81%) delete mode 100644 vico/views/src/main/java/com/patrykandpatrick/vico/views/chart/ChartView.kt delete mode 100644 vico/views/src/main/java/com/patrykandpatrick/vico/views/chart/ComposedChartView.kt diff --git a/sample/src/main/java/com/patrykandpatrick/vico/sample/previews/AxisLinePreviews.kt b/sample/src/main/java/com/patrykandpatrick/vico/sample/previews/AxisLinePreviews.kt index df1ff82d2..a9fc92290 100644 --- a/sample/src/main/java/com/patrykandpatrick/vico/sample/previews/AxisLinePreviews.kt +++ b/sample/src/main/java/com/patrykandpatrick/vico/sample/previews/AxisLinePreviews.kt @@ -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.column.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 import com.patrykandpatrick.vico.core.axis.vertical.VerticalAxis +import com.patrykandpatrick.vico.core.chart.CartesianChartModel +import com.patrykandpatrick.vico.core.chart.column.ColumnCartesianLayerModel 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) { @@ -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, @@ -97,8 +99,8 @@ public fun HorizontalAxisTextInside() { verticalMargin = 4.dp, horizontalMargin = 4.dp ) - Chart( - chart = columnChart(), + CartesianChartHost( + chart = rememberCartesianChart(rememberColumnCartesianLayer()), model = model, startAxis = rememberStartAxis( @@ -127,8 +129,8 @@ public fun HorizontalAxisTextInsideAndBottomAxis() { verticalMargin = 4.dp, horizontalMargin = 4.dp ) - Chart( - chart = columnChart(), + CartesianChartHost( + chart = rememberCartesianChart(rememberColumnCartesianLayer()), model = model, startAxis = rememberStartAxis( @@ -150,8 +152,8 @@ public fun HorizontalAxisTextInsideAndBottomAxis() { @Preview(showBackground = true, widthDp = 250) public fun HorizontalAxisTextOutside() { ProvidePreviewChartStyle { - Chart( - chart = columnChart(), + CartesianChartHost( + chart = rememberCartesianChart(rememberColumnCartesianLayer()), model = model, startAxis = rememberStartAxis( @@ -170,8 +172,8 @@ public fun HorizontalAxisTextOutside() { @Preview(showBackground = true, widthDp = 250) public fun HorizontalAxisGuidelineDoesNotOverlayBottomAxisLine() { ProvidePreviewChartStyle { - Chart( - chart = columnChart(), + CartesianChartHost( + chart = rememberCartesianChart(rememberColumnCartesianLayer()), model = model, startAxis = rememberStartAxis( diff --git a/sample/src/main/java/com/patrykandpatrick/vico/sample/previews/ColumnChartsWithNegativeValuesPreviews.kt b/sample/src/main/java/com/patrykandpatrick/vico/sample/previews/ColumnChartsWithNegativeValuesPreviews.kt index 633f05527..a0a3902f6 100644 --- a/sample/src/main/java/com/patrykandpatrick/vico/sample/previews/ColumnChartsWithNegativeValuesPreviews.kt +++ b/sample/src/main/java/com/patrykandpatrick/vico/sample/previews/ColumnChartsWithNegativeValuesPreviews.kt @@ -25,24 +25,31 @@ 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.column.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.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(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)), + chart = + rememberCartesianChart( + rememberColumnCartesianLayer(), + persistentMarkers = mapOf(2f to marker, 3f to marker) + ), model = model, startAxis = rememberStartAxis( @@ -57,8 +64,9 @@ 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() @@ -70,9 +78,13 @@ 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( @@ -87,9 +99,13 @@ 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( diff --git a/sample/src/main/java/com/patrykandpatrick/vico/sample/previews/ContainedChartsPreview.kt b/sample/src/main/java/com/patrykandpatrick/vico/sample/previews/ContainedChartsPreview.kt index 9e7765a97..51baaf2ed 100644 --- a/sample/src/main/java/com/patrykandpatrick/vico/sample/previews/ContainedChartsPreview.kt +++ b/sample/src/main/java/com/patrykandpatrick/vico/sample/previews/ContainedChartsPreview.kt @@ -25,11 +25,13 @@ 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.column.rememberColumnCartesianLayer import com.patrykandpatrick.vico.compose.chart.line.lineSpec +import com.patrykandpatrick.vico.compose.chart.line.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 @@ -37,48 +39,42 @@ 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.CartesianChartModel +import com.patrykandpatrick.vico.core.chart.column.ColumnCartesianLayerModel +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.component.shape.shader.DynamicShaders 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 @Composable get() = mapOf(4f to rememberMarker()) @Composable -private fun getColumnChart( - markerMap: Map = 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 = 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 ) @@ -101,15 +97,9 @@ private val endAxis: Axis @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, @@ -120,13 +110,10 @@ 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, @@ -137,9 +124,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 @@ -149,9 +136,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 diff --git a/sample/src/main/java/com/patrykandpatrick/vico/sample/previews/LineChartPreviews.kt b/sample/src/main/java/com/patrykandpatrick/vico/sample/previews/LineChartPreviews.kt index 568672f07..5279e5188 100644 --- a/sample/src/main/java/com/patrykandpatrick/vico/sample/previews/LineChartPreviews.kt +++ b/sample/src/main/java/com/patrykandpatrick/vico/sample/previews/LineChartPreviews.kt @@ -25,19 +25,30 @@ import androidx.compose.ui.graphics.Color import androidx.compose.ui.tooling.preview.Preview import androidx.compose.ui.unit.dp import com.patrykandpatrick.vico.compose.axis.vertical.rememberStartAxis -import com.patrykandpatrick.vico.compose.chart.Chart -import com.patrykandpatrick.vico.compose.chart.line.lineChart +import com.patrykandpatrick.vico.compose.chart.CartesianChartHost import com.patrykandpatrick.vico.compose.chart.line.lineSpec +import com.patrykandpatrick.vico.compose.chart.line.rememberLineCartesianLayer +import com.patrykandpatrick.vico.compose.chart.rememberCartesianChart +import com.patrykandpatrick.vico.compose.component.shape.shader.color import com.patrykandpatrick.vico.compose.component.shape.shader.verticalGradient -import com.patrykandpatrick.vico.core.chart.composed.plus -import com.patrykandpatrick.vico.core.chart.values.AxisValuesOverrider -import com.patrykandpatrick.vico.core.entry.composed.plus -import com.patrykandpatrick.vico.core.entry.entriesOf -import com.patrykandpatrick.vico.core.entry.entryModelOf +import com.patrykandpatrick.vico.core.chart.CartesianChartModel +import com.patrykandpatrick.vico.core.chart.line.LineCartesianLayerModel +import com.patrykandpatrick.vico.core.chart.values.AxisValueOverrider +import com.patrykandpatrick.vico.core.component.shape.shader.DynamicShaders -private val model1 = entryModelOf(0, 2, 4, 0, 2) -private val model2 = entryModelOf(1, 3, 4, 1, 3) -private val model3 = entryModelOf(entriesOf(3, 2, 2, 3, 1), entriesOf(1, 3, 1, 2, 3)) +private val model1 = CartesianChartModel(LineCartesianLayerModel.build { series(0, 2, 4, 0, 2) }) +private val model2 = + CartesianChartModel( + LineCartesianLayerModel.build { series(0, 2, 4, 0, 2) }, + LineCartesianLayerModel.build { series(1, 3, 4, 1, 3) } + ) +private val model3 = + CartesianChartModel( + LineCartesianLayerModel.build { + series(3, 2, 2, 3, 1) + series(1, 3, 1, 2, 3) + } + ) @Preview("Line Chart Dark", widthDp = 200) @Composable @@ -46,28 +57,29 @@ public fun LineChartDark() { val yellow = Color(0xFFFFAA4A) val pink = Color(0xFFFF4AAA) - Chart( + CartesianChartHost( modifier = Modifier.padding(8.dp), chart = - lineChart( - lines = + rememberCartesianChart( + rememberLineCartesianLayer( listOf( lineSpec( - lineColor = yellow, - lineBackgroundShader = + shader = DynamicShaders.color(yellow), + backgroundShader = verticalGradient( - arrayOf(yellow.copy(0.5f), yellow.copy(alpha = 0f)) + arrayOf(yellow.copy(alpha = 0.5f), yellow.copy(alpha = 0f)) ) ), lineSpec( - lineColor = pink, - lineBackgroundShader = + shader = DynamicShaders.color(pink), + backgroundShader = verticalGradient( - arrayOf(pink.copy(0.5f), pink.copy(alpha = 0f)) + arrayOf(pink.copy(alpha = 0.5f), pink.copy(alpha = 0f)) ) ) ), - axisValuesOverrider = AxisValuesOverrider.fixed(maxY = 4f) + axisValueOverrider = AxisValueOverrider.fixed(maxY = 4f) + ) ), model = model3 ) @@ -77,14 +89,23 @@ public fun LineChartDark() { @Preview("Line Chart", widthDp = 200) @Composable public fun RegularLineChart() { - Chart(chart = lineChart(), model = model1, startAxis = rememberStartAxis()) + CartesianChartHost( + chart = rememberCartesianChart(rememberLineCartesianLayer()), + model = model1, + startAxis = rememberStartAxis() + ) } @Preview("Line Chart Expanded", widthDp = 200) @Composable public fun RegularLineChartExpanded() { - Chart( - chart = lineChart(axisValuesOverrider = AxisValuesOverrider.fixed(minY = -1f, maxY = 5f)), + CartesianChartHost( + chart = + rememberCartesianChart( + rememberLineCartesianLayer( + axisValueOverrider = AxisValueOverrider.fixed(minY = -1f, maxY = 5f) + ) + ), model = model1, startAxis = rememberStartAxis() ) @@ -93,8 +114,13 @@ public fun RegularLineChartExpanded() { @Preview("Line Chart Collapsed", widthDp = 200) @Composable public fun RegularLineChartCollapsed() { - Chart( - chart = lineChart(axisValuesOverrider = AxisValuesOverrider.fixed(minY = 1f, maxY = 3f)), + CartesianChartHost( + chart = + rememberCartesianChart( + rememberLineCartesianLayer( + axisValueOverrider = AxisValueOverrider.fixed(minY = 1f, maxY = 3f) + ) + ), model = model1, startAxis = rememberStartAxis() ) @@ -103,26 +129,26 @@ public fun RegularLineChartCollapsed() { @Preview("Composed Chart", widthDp = 200) @Composable public fun ComposedLineChart() { - Chart( + CartesianChartHost( chart = - lineChart() + - lineChart( - lines = - listOf( - lineSpec( - lineColor = Color.Blue, - lineBackgroundShader = - verticalGradient( - colors = - arrayOf( - Color.Blue.copy(alpha = 0.4f), - Color.Blue.copy(alpha = 0f) - ) + rememberCartesianChart( + rememberLineCartesianLayer(), + rememberLineCartesianLayer( + listOf( + lineSpec( + shader = DynamicShaders.color(Color.Blue), + backgroundShader = + verticalGradient( + arrayOf( + Color.Blue.copy(alpha = 0.4f), + Color.Blue.copy(alpha = 0f) ) - ) + ) ) - ), - model = model1 + model2, + ) + ) + ), + model = model2, startAxis = rememberStartAxis() ) } @@ -130,11 +156,17 @@ public fun ComposedLineChart() { @Preview("Composed Chart Collapsed", widthDp = 200) @Composable public fun ComposedLineChartCollapsed() { - Chart( + CartesianChartHost( chart = - lineChart(axisValuesOverrider = AxisValuesOverrider.fixed(minY = 1f, maxY = 3f)) + - lineChart(axisValuesOverrider = AxisValuesOverrider.fixed(minY = 1f, maxY = 3f)), - model = model1 + model2, + rememberCartesianChart( + rememberLineCartesianLayer( + axisValueOverrider = AxisValueOverrider.fixed(minY = 1f, maxY = 3f) + ), + rememberLineCartesianLayer( + axisValueOverrider = AxisValueOverrider.fixed(minY = 1f, maxY = 3f) + ) + ), + model = model2, startAxis = rememberStartAxis() ) } diff --git a/sample/src/main/java/com/patrykandpatrick/vico/sample/previews/LineChartsWithNegativeValuesPreviews.kt b/sample/src/main/java/com/patrykandpatrick/vico/sample/previews/LineChartsWithNegativeValuesPreviews.kt index ce902beb4..227c3c787 100644 --- a/sample/src/main/java/com/patrykandpatrick/vico/sample/previews/LineChartsWithNegativeValuesPreviews.kt +++ b/sample/src/main/java/com/patrykandpatrick/vico/sample/previews/LineChartsWithNegativeValuesPreviews.kt @@ -27,41 +27,46 @@ import androidx.compose.ui.unit.dp import com.patrykandpatrick.vico.compose.axis.axisLineComponent 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.CartesianChartHost import com.patrykandpatrick.vico.compose.chart.layout.fullWidth -import com.patrykandpatrick.vico.compose.chart.line.lineChart import com.patrykandpatrick.vico.compose.chart.line.lineSpec -import com.patrykandpatrick.vico.compose.component.shape.shader.solid -import com.patrykandpatrick.vico.compose.component.shape.shader.split +import com.patrykandpatrick.vico.compose.chart.line.rememberLineCartesianLayer +import com.patrykandpatrick.vico.compose.chart.rememberCartesianChart +import com.patrykandpatrick.vico.compose.component.shape.shader.color import com.patrykandpatrick.vico.compose.component.textComponent import com.patrykandpatrick.vico.core.axis.AxisItemPlacer +import com.patrykandpatrick.vico.core.chart.CartesianChartModel import com.patrykandpatrick.vico.core.chart.layout.HorizontalLayout -import com.patrykandpatrick.vico.core.chart.values.AxisValuesOverrider +import com.patrykandpatrick.vico.core.chart.line.LineCartesianLayerModel +import com.patrykandpatrick.vico.core.chart.values.AxisValueOverrider import com.patrykandpatrick.vico.core.component.shape.shader.DynamicShaders -import com.patrykandpatrick.vico.core.entry.entryModelOf +import com.patrykandpatrick.vico.core.component.shape.shader.TopBottomShader 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 SingleLineChartWithNegativeValues() { val marker = rememberMarker() Surface { - Chart( + CartesianChartHost( modifier = Modifier.height(250.dp), chart = - lineChart( - lines = - listOf( - lineSpec( - lineShader = - DynamicShaders.split( - positiveColor = Color(0xFF25BE53), - negativeColor = Color(0xFFE73B3B) - ) + rememberCartesianChart( + rememberLineCartesianLayer( + lines = + listOf( + lineSpec( + shader = + TopBottomShader( + DynamicShaders.color(Color(0xFF25BE53)), + DynamicShaders.color(Color(0xFFE73B3B)) + ) + ) ) - ), + ), persistentMarkers = mapOf(2f to marker, 3f to marker) ), model = model, @@ -84,16 +89,18 @@ public fun SingleLineChartWithNegativeValues() { @Composable public fun SingleLineChartWithNegativeValuesAndDataLabels() { Surface { - Chart( + CartesianChartHost( chart = - lineChart( - lines = - listOf( - lineSpec( - lineShader = DynamicShaders.solid(Color.DarkGray), - dataLabel = textComponent() + rememberCartesianChart( + rememberLineCartesianLayer( + lines = + listOf( + lineSpec( + shader = DynamicShaders.color(Color.DarkGray), + dataLabel = textComponent() + ) ) - ) + ) ), model = model, startAxis = rememberStartAxis(), @@ -106,9 +113,13 @@ public fun SingleLineChartWithNegativeValuesAndDataLabels() { @Composable public fun SingleLineChartWithNegativeValuesAndAxisValuesOverridden() { Surface { - Chart( + CartesianChartHost( chart = - lineChart(axisValuesOverrider = AxisValuesOverrider.fixed(minY = 1f, maxY = 4f)), + rememberCartesianChart( + rememberLineCartesianLayer( + axisValueOverrider = AxisValueOverrider.fixed(minY = 1f, maxY = 4f) + ) + ), model = model, startAxis = rememberStartAxis( @@ -123,9 +134,13 @@ public fun SingleLineChartWithNegativeValuesAndAxisValuesOverridden() { @Composable public fun SingleLineChartWithNegativeValuesAndAxisValuesOverridden2() { Surface { - Chart( + CartesianChartHost( chart = - lineChart(axisValuesOverrider = AxisValuesOverrider.fixed(minY = -2f, maxY = 0f)), + rememberCartesianChart( + rememberLineCartesianLayer( + axisValueOverrider = AxisValueOverrider.fixed(minY = -2f, maxY = 0f) + ) + ), model = model, startAxis = rememberStartAxis( diff --git a/sample/src/main/java/com/patrykandpatrick/vico/sample/previews/Previews.kt b/sample/src/main/java/com/patrykandpatrick/vico/sample/previews/Previews.kt index d8ac6dcf3..3d3616cff 100644 --- a/sample/src/main/java/com/patrykandpatrick/vico/sample/previews/Previews.kt +++ b/sample/src/main/java/com/patrykandpatrick/vico/sample/previews/Previews.kt @@ -34,11 +34,13 @@ import androidx.compose.ui.graphics.toArgb import androidx.compose.ui.tooling.preview.Preview import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.sp -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.column.rememberColumnCartesianLayer import com.patrykandpatrick.vico.compose.chart.line.lineSpec +import com.patrykandpatrick.vico.compose.chart.line.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.fromComponent import com.patrykandpatrick.vico.compose.component.shape.shader.verticalGradient import com.patrykandpatrick.vico.compose.component.shapeComponent @@ -47,13 +49,15 @@ import com.patrykandpatrick.vico.compose.dimensions.dimensionsOf import com.patrykandpatrick.vico.core.axis.horizontal.createHorizontalAxis import com.patrykandpatrick.vico.core.axis.vertical.VerticalAxis import com.patrykandpatrick.vico.core.axis.vertical.createVerticalAxis -import com.patrykandpatrick.vico.core.chart.values.AxisValuesOverrider +import com.patrykandpatrick.vico.core.chart.CartesianChartModel +import com.patrykandpatrick.vico.core.chart.column.ColumnCartesianLayerModel +import com.patrykandpatrick.vico.core.chart.line.LineCartesianLayerModel +import com.patrykandpatrick.vico.core.chart.values.AxisValueOverrider import com.patrykandpatrick.vico.core.component.shape.DashedShape import com.patrykandpatrick.vico.core.component.shape.LineComponent import com.patrykandpatrick.vico.core.component.shape.Shapes.pillShape import com.patrykandpatrick.vico.core.component.shape.Shapes.rectShape import com.patrykandpatrick.vico.core.component.shape.shader.DynamicShaders -import com.patrykandpatrick.vico.core.entry.entryModelOf import com.patrykandpatrick.vico.sample.utils.VicoTheme private val chartModifier = Modifier.height(100.dp) @@ -64,20 +68,21 @@ public fun ColumnChartCard(): Unit = VicoTheme { val colors = MaterialTheme.colors SampleCard { - Chart( + CartesianChartHost( modifier = chartModifier, chart = - columnChart( - columns = + rememberCartesianChart( + rememberColumnCartesianLayer( listOf( lineComponent( - colors.primary, + color = colors.primary, thickness = 8.dp, shape = RoundedCornerShape(4.dp), dynamicShader = verticalGradient(arrayOf(colors.primary, colors.secondary)) ) ) + ) ), startAxis = createVerticalAxis { @@ -103,7 +108,7 @@ public fun ColumnChartCard(): Unit = VicoTheme { guideline = LineComponent(colors.primary.copy(alpha = 0.1f).toArgb(), 1.dp.value) }, - model = entryModelOf(1, 2, 3, 2) + model = CartesianChartModel(ColumnCartesianLayerModel.build { series(1, 2, 3, 2) }) ) } } @@ -114,16 +119,16 @@ public fun LineChartCard(): Unit = VicoTheme { val colors = MaterialTheme.colors SampleCard { - Chart( + CartesianChartHost( modifier = Modifier.height(100.dp), chart = - lineChart( - lines = + rememberCartesianChart( + rememberLineCartesianLayer( listOf( lineSpec( point = null, - lineColor = colors.primary, - lineBackgroundShader = + shader = DynamicShaders.color(colors.primary), + backgroundShader = DynamicShaders.fromComponent( componentSize = 4.dp, component = @@ -135,9 +140,15 @@ public fun LineChartCard(): Unit = VicoTheme { ) ) ), - axisValuesOverrider = AxisValuesOverrider.fixed(minX = 0f, maxY = 3f) + axisValueOverrider = AxisValueOverrider.fixed(minX = 0f, maxY = 3f) + ) + ), + model = + CartesianChartModel( + LineCartesianLayerModel.build { + series(x = listOf(-1, 0, 1, 2, 3, 4, 5), y = listOf(0, 0, 1, 2, 0, 2, 1)) + } ), - model = entryModelOf(-1 to 0, 0 to 0, 1 to 1, 2 to 2, 3 to 0, 4 to 2, 5 to 1), startAxis = createVerticalAxis { label = diff --git a/sample/src/main/java/com/patrykandpatrick/vico/sample/previews/StackedColumnChartsWithNegativeValuesPreviews.kt b/sample/src/main/java/com/patrykandpatrick/vico/sample/previews/StackedColumnChartsWithNegativeValuesPreviews.kt index 37a284f87..14bb9e9c0 100644 --- a/sample/src/main/java/com/patrykandpatrick/vico/sample/previews/StackedColumnChartsWithNegativeValuesPreviews.kt +++ b/sample/src/main/java/com/patrykandpatrick/vico/sample/previews/StackedColumnChartsWithNegativeValuesPreviews.kt @@ -26,23 +26,26 @@ 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.column.rememberColumnCartesianLayer +import com.patrykandpatrick.vico.compose.chart.rememberCartesianChart import com.patrykandpatrick.vico.compose.component.lineComponent import com.patrykandpatrick.vico.core.axis.AxisItemPlacer -import com.patrykandpatrick.vico.core.chart.column.ColumnChart.MergeMode.Stack -import com.patrykandpatrick.vico.core.chart.values.AxisValuesOverrider +import com.patrykandpatrick.vico.core.chart.CartesianChartModel +import com.patrykandpatrick.vico.core.chart.column.ColumnCartesianLayer +import com.patrykandpatrick.vico.core.chart.column.ColumnCartesianLayerModel +import com.patrykandpatrick.vico.core.chart.values.AxisValueOverrider import com.patrykandpatrick.vico.core.component.shape.LineComponent import com.patrykandpatrick.vico.core.component.text.textComponent -import com.patrykandpatrick.vico.core.entry.entriesOf -import com.patrykandpatrick.vico.core.entry.entryModelOf import com.patrykandpatrick.vico.sample.showcase.rememberMarker private val model = - entryModelOf( - entriesOf(2f, -1f, -4f, 2f, 1f, -5f, -2f, -3f), - entriesOf(3f, -2f, 2f, -1f, 2f, -3f, -4f, -1f), - entriesOf(1f, -2f, 2f, 1f, -1f, 4f, 4f, -2f) + CartesianChartModel( + ColumnCartesianLayerModel.build { + series(2, -1, -4, 2, 1, -5, -2, -3) + series(3, -2, 2, -1, 2, -3, -4, -1) + series(1, -2, 2, 1, -1, 4, 4, -2) + } ) private val columns: List @@ -59,13 +62,15 @@ private val columns: List public fun StackedColumnChartWithNegativeValues() { val marker = rememberMarker() Surface { - Chart( + CartesianChartHost( modifier = Modifier.height(250.dp), chart = - columnChart( - columns = columns, - persistentMarkers = mapOf(2f to marker, 3f to marker), - mergeMode = Stack + rememberCartesianChart( + rememberColumnCartesianLayer( + columns = columns, + mergeMode = ColumnCartesianLayer.MergeMode.Stacked + ), + persistentMarkers = mapOf(2f to marker, 3f to marker) ), model = model, startAxis = @@ -81,8 +86,15 @@ public fun StackedColumnChartWithNegativeValues() { @Composable public fun StackedColumnChartWithNegativeValuesAndDataLabels() { Surface { - Chart( - chart = columnChart(columns = columns, dataLabel = textComponent(), mergeMode = Stack), + CartesianChartHost( + chart = + rememberCartesianChart( + rememberColumnCartesianLayer( + columns = columns, + dataLabel = textComponent(), + mergeMode = ColumnCartesianLayer.MergeMode.Stacked + ) + ), model = model, startAxis = rememberStartAxis( @@ -97,12 +109,14 @@ public fun StackedColumnChartWithNegativeValuesAndDataLabels() { @Composable public fun StackedColumnChartWithNegativeValuesAndAxisValuesOverridden() { Surface { - Chart( + CartesianChartHost( chart = - columnChart( - columns = columns, - axisValuesOverrider = AxisValuesOverrider.fixed(minY = 1f, maxY = 4f), - mergeMode = Stack + rememberCartesianChart( + rememberColumnCartesianLayer( + columns = columns, + axisValueOverrider = AxisValueOverrider.fixed(minY = 1f, maxY = 4f), + mergeMode = ColumnCartesianLayer.MergeMode.Stacked + ) ), model = model, startAxis = @@ -118,12 +132,14 @@ public fun StackedColumnChartWithNegativeValuesAndAxisValuesOverridden() { @Composable public fun StackedColumnChartWithNegativeValuesAndAxisValuesOverridden2() { Surface { - Chart( + CartesianChartHost( chart = - columnChart( - columns = columns, - axisValuesOverrider = AxisValuesOverrider.fixed(minY = -2f, maxY = 0f), - mergeMode = Stack + rememberCartesianChart( + rememberColumnCartesianLayer( + columns = columns, + axisValueOverrider = AxisValueOverrider.fixed(minY = -2f, maxY = 0f), + mergeMode = ColumnCartesianLayer.MergeMode.Stacked + ) ), model = model, startAxis = diff --git a/sample/src/main/java/com/patrykandpatrick/vico/sample/previews/ThresholdLinePreviews.kt b/sample/src/main/java/com/patrykandpatrick/vico/sample/previews/ThresholdLinePreviews.kt index a690ade4c..cbd543689 100644 --- a/sample/src/main/java/com/patrykandpatrick/vico/sample/previews/ThresholdLinePreviews.kt +++ b/sample/src/main/java/com/patrykandpatrick/vico/sample/previews/ThresholdLinePreviews.kt @@ -29,8 +29,9 @@ 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.column.rememberColumnCartesianLayer +import com.patrykandpatrick.vico.compose.chart.rememberCartesianChart import com.patrykandpatrick.vico.compose.chart.scroll.rememberChartScrollSpec import com.patrykandpatrick.vico.compose.component.lineComponent import com.patrykandpatrick.vico.compose.component.shape.shader.toDynamicShader @@ -38,12 +39,13 @@ import com.patrykandpatrick.vico.compose.component.shapeComponent import com.patrykandpatrick.vico.compose.component.textComponent import com.patrykandpatrick.vico.compose.dimensions.dimensionsOf import com.patrykandpatrick.vico.compose.style.LocalChartStyle +import com.patrykandpatrick.vico.core.chart.CartesianChartModel +import com.patrykandpatrick.vico.core.chart.column.ColumnCartesianLayerModel import com.patrykandpatrick.vico.core.chart.decoration.ThresholdLine import com.patrykandpatrick.vico.core.component.shape.Shapes import com.patrykandpatrick.vico.core.component.shape.shader.ComponentShader -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) }) public val Color.Companion.DimmedGray: Color get() = Color(0xFFAAAAAA) @@ -59,10 +61,10 @@ private fun ProvidePreviewChartStyle(content: @Composable () -> Unit) { axisTickColor = Color.DimmedGray, axisGuidelineColor = Color.DimmedGray ), - columnChart = - LocalChartStyle.current.columnChart.copy( + columnLayer = + LocalChartStyle.current.columnLayer.copy( columns = - LocalChartStyle.current.columnChart.columns.map { + LocalChartStyle.current.columnLayer.columns.map { lineComponent( color = Color.DimmedGray, thickness = it.thicknessDp.dp, @@ -87,22 +89,24 @@ private fun ProvidePreviewChartStyle(content: @Composable () -> Unit) { @Composable public fun ThresholdLine() { ProvidePreviewChartStyle { - Chart( + CartesianChartHost( modifier = Modifier, chart = - columnChart().apply { - addDecoration( - ThresholdLine( - thresholdValue = 2f, - lineComponent = shapeComponent(color = Color.Black), - labelComponent = - textComponent( - Color.Black, - padding = dimensionsOf(horizontal = 8.dp) - ) + rememberCartesianChart( + rememberColumnCartesianLayer(), + decorations = + listOf( + ThresholdLine( + thresholdValue = 2f, + lineComponent = shapeComponent(color = Color.Black), + labelComponent = + textComponent( + Color.Black, + padding = dimensionsOf(horizontal = 8.dp) + ) + ) ) - ) - }, + ), model = model, startAxis = rememberStartAxis(), bottomAxis = rememberBottomAxis(), @@ -115,70 +119,70 @@ public fun ThresholdLine() { @Composable public fun ThresholdLineWithCustomText() { ProvidePreviewChartStyle { - Chart( + CartesianChartHost( modifier = Modifier, chart = - columnChart().apply { - addDecoration( - ThresholdLine( - thresholdValue = 2f, - thresholdLabel = "Threshold line 1 📐", - lineComponent = shapeComponent(color = Color.Black), - labelComponent = - textComponent( - color = Color.White, - lineCount = 3, - background = - shapeComponent( - shape = - Shapes.roundedCornerShape( - bottomLeftPercent = 25, - bottomRightPercent = 25 - ), - color = Color.Black - ), - padding = - dimensionsOf( - start = 8.dp, - top = 2.dp, - end = 8.dp, - bottom = 4.dp - ), - margins = dimensionsOf(horizontal = 4.dp) - ), - labelVerticalPosition = ThresholdLine.LabelVerticalPosition.Bottom - ) - ) - addDecoration( - ThresholdLine( - thresholdValue = 3f, - thresholdLabel = "Threshold line 2 📐", - lineComponent = shapeComponent(color = Color.DarkGray), - labelComponent = - textComponent( - color = Color.White, - lineCount = 3, - background = - shapeComponent( - shape = - Shapes.cutCornerShape( - topLeftPercent = 25, - topRightPercent = 25 - ), - color = Color.DarkGray - ), - padding = - dimensionsOf( - start = 8.dp, - top = 4.dp, - end = 8.dp, - bottom = 2.dp - ), - margins = dimensionsOf(horizontal = 4.dp) - ) + rememberCartesianChart( + rememberColumnCartesianLayer(), + decorations = + listOf( + ThresholdLine( + thresholdValue = 2f, + thresholdLabel = "Threshold line 1 📐", + lineComponent = shapeComponent(color = Color.Black), + labelComponent = + textComponent( + color = Color.White, + lineCount = 3, + background = + shapeComponent( + shape = + Shapes.roundedCornerShape( + bottomLeftPercent = 25, + bottomRightPercent = 25 + ), + color = Color.Black + ), + padding = + dimensionsOf( + start = 8.dp, + top = 2.dp, + end = 8.dp, + bottom = 4.dp + ), + margins = dimensionsOf(horizontal = 4.dp) + ), + labelVerticalPosition = ThresholdLine.LabelVerticalPosition.Bottom + ), + ThresholdLine( + thresholdValue = 3f, + thresholdLabel = "Threshold line 2 📐", + lineComponent = shapeComponent(color = Color.DarkGray), + labelComponent = + textComponent( + color = Color.White, + lineCount = 3, + background = + shapeComponent( + shape = + Shapes.cutCornerShape( + topLeftPercent = 25, + topRightPercent = 25 + ), + color = Color.DarkGray + ), + padding = + dimensionsOf( + start = 8.dp, + top = 4.dp, + end = 8.dp, + bottom = 2.dp + ), + margins = dimensionsOf(horizontal = 4.dp) + ) + ) ) - ) - }, + ), model = model, startAxis = rememberStartAxis(), bottomAxis = rememberBottomAxis(), @@ -191,22 +195,25 @@ public fun ThresholdLineWithCustomText() { @Composable public fun RangedThresholdLine() { ProvidePreviewChartStyle { - Chart( + CartesianChartHost( modifier = Modifier, chart = - columnChart().apply { - addDecoration( - ThresholdLine( - thresholdRange = 2f..3f, - lineComponent = shapeComponent(color = Color.Black.copy(alpha = 0.5f)), - labelComponent = - textComponent( - color = Color.Black, - padding = dimensionsOf(horizontal = 8.dp) - ) + rememberCartesianChart( + rememberColumnCartesianLayer(), + decorations = + listOf( + ThresholdLine( + thresholdRange = 2f..3f, + lineComponent = + shapeComponent(color = Color.Black.copy(alpha = 0.5f)), + labelComponent = + textComponent( + color = Color.Black, + padding = dimensionsOf(horizontal = 8.dp) + ) + ) ) - ) - }, + ), model = model, startAxis = rememberStartAxis(), bottomAxis = rememberBottomAxis(), @@ -219,34 +226,36 @@ public fun RangedThresholdLine() { @Composable public fun RangedThresholdLineWithBrushShader() { ProvidePreviewChartStyle { - Chart( + CartesianChartHost( modifier = Modifier, chart = - columnChart().apply { - addDecoration( - ThresholdLine( - thresholdRange = 2f..3f, - lineComponent = - shapeComponent( - color = Color.Black, - dynamicShader = - Brush.verticalGradient( - colors = - listOf( - Color.Black.copy(0.75f), - Color.Black.copy(0.25f) - ) - ) - .toDynamicShader() - ), - labelComponent = - textComponent( - color = Color.Black, - padding = dimensionsOf(horizontal = 8.dp) - ) + rememberCartesianChart( + rememberColumnCartesianLayer(), + decorations = + listOf( + ThresholdLine( + thresholdRange = 2f..3f, + lineComponent = + shapeComponent( + color = Color.Black, + dynamicShader = + Brush.verticalGradient( + colors = + listOf( + Color.Black.copy(0.75f), + Color.Black.copy(0.25f) + ) + ) + .toDynamicShader() + ), + labelComponent = + textComponent( + color = Color.Black, + padding = dimensionsOf(horizontal = 8.dp) + ) + ) ) - ) - }, + ), model = model, startAxis = rememberStartAxis(), bottomAxis = rememberBottomAxis(), @@ -259,35 +268,37 @@ public fun RangedThresholdLineWithBrushShader() { @Composable public fun RangedThresholdLineWithComponentShader() { ProvidePreviewChartStyle { - Chart( + CartesianChartHost( modifier = Modifier, chart = - columnChart().apply { - addDecoration( - ThresholdLine( - thresholdRange = 2f..3f, - lineComponent = - shapeComponent( - color = Color.Black, - dynamicShader = - ComponentShader( - shapeComponent( - shape = Shapes.pillShape, - color = Color.Black + rememberCartesianChart( + rememberColumnCartesianLayer(), + decorations = + listOf( + ThresholdLine( + thresholdRange = 2f..3f, + lineComponent = + shapeComponent( + color = Color.Black, + dynamicShader = + ComponentShader( + shapeComponent( + shape = Shapes.pillShape, + color = Color.Black + ), + componentSizeDp = 4f ), - componentSizeDp = 4f - ), - strokeWidth = 2.dp, - strokeColor = Color.Black - ), - labelComponent = - textComponent( - color = Color.Black, - padding = dimensionsOf(horizontal = 8.dp) - ) + strokeWidth = 2.dp, + strokeColor = Color.Black + ), + labelComponent = + textComponent( + color = Color.Black, + padding = dimensionsOf(horizontal = 8.dp) + ) + ) ) - ) - }, + ), model = model, startAxis = rememberStartAxis(), bottomAxis = rememberBottomAxis(), diff --git a/sample/src/main/java/com/patrykandpatrick/vico/sample/previews/composables/column/ColumnCharts.kt b/sample/src/main/java/com/patrykandpatrick/vico/sample/previews/composables/column/ColumnCharts.kt index d8fd0bbf5..e82c7f858 100644 --- a/sample/src/main/java/com/patrykandpatrick/vico/sample/previews/composables/column/ColumnCharts.kt +++ b/sample/src/main/java/com/patrykandpatrick/vico/sample/previews/composables/column/ColumnCharts.kt @@ -19,29 +19,30 @@ package com.patrykandpatrick.vico.sample.previews.composables.column import androidx.compose.runtime.Composable 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.column.rememberColumnCartesianLayer +import com.patrykandpatrick.vico.compose.chart.rememberCartesianChart import com.patrykandpatrick.vico.compose.chart.scroll.rememberChartScrollSpec -import com.patrykandpatrick.vico.core.entry.ChartEntryModel +import com.patrykandpatrick.vico.core.chart.CartesianChartModel import com.patrykandpatrick.vico.core.scroll.AutoScrollCondition import com.patrykandpatrick.vico.core.scroll.InitialScroll import com.patrykandpatrick.vico.sample.previews.annotation.ChartPreview import com.patrykandpatrick.vico.sample.previews.resource.PreviewSurface -import com.patrykandpatrick.vico.sample.previews.resource.mediumEntryModel -import com.patrykandpatrick.vico.sample.previews.resource.shortEntryModel +import com.patrykandpatrick.vico.sample.previews.resource.mediumColumnModel +import com.patrykandpatrick.vico.sample.previews.resource.shortColumnModel @ChartPreview @Composable public fun DefaultColumnChart( - model: ChartEntryModel = shortEntryModel, - oldModel: ChartEntryModel? = null, + model: CartesianChartModel = shortColumnModel, + oldModel: CartesianChartModel? = null, scrollable: Boolean = true, initialScroll: InitialScroll = InitialScroll.Start, - autoScrollCondition: AutoScrollCondition = AutoScrollCondition.Never + autoScrollCondition: AutoScrollCondition = AutoScrollCondition.Never ) { PreviewSurface { - Chart( - chart = columnChart(), + CartesianChartHost( + chart = rememberCartesianChart(rememberColumnCartesianLayer()), model = model, oldModel = oldModel, startAxis = rememberStartAxis(), @@ -59,17 +60,17 @@ public fun DefaultColumnChart( @ChartPreview @Composable public fun DefaultColumnChartLongScrollable() { - DefaultColumnChart(model = mediumEntryModel) + DefaultColumnChart(model = mediumColumnModel) } @ChartPreview @Composable public fun DefaultColumnChartLongScrollableEnd() { - DefaultColumnChart(model = mediumEntryModel, initialScroll = InitialScroll.End) + DefaultColumnChart(model = mediumColumnModel, initialScroll = InitialScroll.End) } @ChartPreview @Composable public fun DefaultColumnChartLongNonScrollable() { - DefaultColumnChart(model = mediumEntryModel, scrollable = false) + DefaultColumnChart(model = mediumColumnModel, scrollable = false) } diff --git a/sample/src/main/java/com/patrykandpatrick/vico/sample/previews/composables/line/LineCharts.kt b/sample/src/main/java/com/patrykandpatrick/vico/sample/previews/composables/line/LineCharts.kt index 63071df1f..9d79dd643 100644 --- a/sample/src/main/java/com/patrykandpatrick/vico/sample/previews/composables/line/LineCharts.kt +++ b/sample/src/main/java/com/patrykandpatrick/vico/sample/previews/composables/line/LineCharts.kt @@ -19,26 +19,27 @@ package com.patrykandpatrick.vico.sample.previews.composables.line import androidx.compose.runtime.Composable 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.line.lineChart +import com.patrykandpatrick.vico.compose.chart.CartesianChartHost +import com.patrykandpatrick.vico.compose.chart.line.rememberLineCartesianLayer +import com.patrykandpatrick.vico.compose.chart.rememberCartesianChart import com.patrykandpatrick.vico.compose.chart.scroll.rememberChartScrollSpec -import com.patrykandpatrick.vico.core.entry.ChartEntryModel +import com.patrykandpatrick.vico.core.chart.CartesianChartModel import com.patrykandpatrick.vico.core.scroll.InitialScroll import com.patrykandpatrick.vico.sample.previews.annotation.ChartPreview import com.patrykandpatrick.vico.sample.previews.resource.PreviewSurface -import com.patrykandpatrick.vico.sample.previews.resource.mediumEntryModel -import com.patrykandpatrick.vico.sample.previews.resource.shortEntryModel +import com.patrykandpatrick.vico.sample.previews.resource.mediumLineModel +import com.patrykandpatrick.vico.sample.previews.resource.shortLineModel @ChartPreview @Composable public fun DefaultLineChart( - model: ChartEntryModel = shortEntryModel, + model: CartesianChartModel = shortLineModel, scrollable: Boolean = true, initialScroll: InitialScroll = InitialScroll.Start ) { PreviewSurface { - Chart( - chart = lineChart(), + CartesianChartHost( + chart = rememberCartesianChart(rememberLineCartesianLayer()), model = model, startAxis = rememberStartAxis(), bottomAxis = rememberBottomAxis(), @@ -51,17 +52,17 @@ public fun DefaultLineChart( @ChartPreview @Composable public fun DefaultLineChartLongScrollable() { - DefaultLineChart(model = mediumEntryModel) + DefaultLineChart(model = mediumLineModel) } @ChartPreview @Composable public fun DefaultLineChartLongScrollableEnd() { - DefaultLineChart(model = mediumEntryModel, initialScroll = InitialScroll.End) + DefaultLineChart(model = mediumLineModel, initialScroll = InitialScroll.End) } @ChartPreview @Composable public fun DefaultLineChartLongNonScrollable() { - DefaultLineChart(model = mediumEntryModel, scrollable = false) + DefaultLineChart(model = mediumLineModel, scrollable = false) } diff --git a/sample/src/main/java/com/patrykandpatrick/vico/sample/previews/resource/SampleModels.kt b/sample/src/main/java/com/patrykandpatrick/vico/sample/previews/resource/SampleModels.kt index 90134f735..e6751262c 100644 --- a/sample/src/main/java/com/patrykandpatrick/vico/sample/previews/resource/SampleModels.kt +++ b/sample/src/main/java/com/patrykandpatrick/vico/sample/previews/resource/SampleModels.kt @@ -16,9 +16,22 @@ package com.patrykandpatrick.vico.sample.previews.resource -import com.patrykandpatrick.vico.core.entry.ChartEntryModel -import com.patrykandpatrick.vico.core.entry.entryModelOf +import com.patrykandpatrick.vico.core.chart.CartesianChartModel +import com.patrykandpatrick.vico.core.chart.column.ColumnCartesianLayerModel +import com.patrykandpatrick.vico.core.chart.line.LineCartesianLayerModel -public val shortEntryModel: ChartEntryModel = entryModelOf(1, 2, 4, 8, 3) +public val shortColumnModel = + CartesianChartModel(ColumnCartesianLayerModel.build { series(1, 2, 4, 8, 3) }) -public val mediumEntryModel: ChartEntryModel = entryModelOf(1, 2, 4, 8, 3, 10, 4, 7, 2, 6, 4, 8) +public val mediumColumnModel = + CartesianChartModel( + ColumnCartesianLayerModel.build { series(1, 2, 4, 8, 3, 10, 4, 7, 2, 6, 4, 8) } + ) + +public val shortLineModel = + CartesianChartModel(LineCartesianLayerModel.build { series(1, 2, 4, 8, 3) }) + +public val mediumLineModel = + CartesianChartModel( + LineCartesianLayerModel.build { series(1, 2, 4, 8, 3, 10, 4, 7, 2, 6, 4, 8) } + ) diff --git a/sample/src/main/java/com/patrykandpatrick/vico/sample/showcase/ChartStyle.kt b/sample/src/main/java/com/patrykandpatrick/vico/sample/showcase/ChartStyle.kt index 0b0a0c1a9..a7b0d9efd 100644 --- a/sample/src/main/java/com/patrykandpatrick/vico/sample/showcase/ChartStyle.kt +++ b/sample/src/main/java/com/patrykandpatrick/vico/sample/showcase/ChartStyle.kt @@ -22,12 +22,13 @@ import androidx.compose.runtime.remember import androidx.compose.ui.graphics.Brush import androidx.compose.ui.graphics.Color import androidx.compose.ui.graphics.toArgb +import com.patrykandpatrick.vico.compose.component.shape.shader.color import com.patrykandpatrick.vico.compose.component.shape.shader.fromBrush import com.patrykandpatrick.vico.compose.style.ChartStyle import com.patrykandpatrick.vico.core.DefaultAlpha import com.patrykandpatrick.vico.core.DefaultColors import com.patrykandpatrick.vico.core.DefaultDimens -import com.patrykandpatrick.vico.core.chart.line.LineChart +import com.patrykandpatrick.vico.core.chart.line.LineCartesianLayer import com.patrykandpatrick.vico.core.component.shape.LineComponent import com.patrykandpatrick.vico.core.component.shape.Shapes import com.patrykandpatrick.vico.core.component.shape.shader.DynamicShaders @@ -46,7 +47,7 @@ internal fun rememberChartStyle( axisGuidelineColor = Color(defaultColors.axisGuidelineColor), axisLineColor = Color(defaultColors.axisLineColor) ), - ChartStyle.ColumnChart( + ChartStyle.ColumnLayer( columnChartColors.map { columnChartColor -> LineComponent( columnChartColor.toArgb(), @@ -55,11 +56,11 @@ internal fun rememberChartStyle( ) } ), - ChartStyle.LineChart( + ChartStyle.LineLayer( lineChartColors.map { lineChartColor -> - LineChart.LineSpec( - lineColor = lineChartColor.toArgb(), - lineBackgroundShader = + LineCartesianLayer.LineSpec( + shader = DynamicShaders.color(lineChartColor), + backgroundShader = DynamicShaders.fromBrush( Brush.verticalGradient( listOf( diff --git a/sample/src/main/java/com/patrykandpatrick/vico/sample/showcase/ShowcaseScreen.kt b/sample/src/main/java/com/patrykandpatrick/vico/sample/showcase/ShowcaseScreen.kt index eda4c7324..923f2fba6 100644 --- a/sample/src/main/java/com/patrykandpatrick/vico/sample/showcase/ShowcaseScreen.kt +++ b/sample/src/main/java/com/patrykandpatrick/vico/sample/showcase/ShowcaseScreen.kt @@ -26,7 +26,6 @@ import androidx.compose.foundation.lazy.LazyListScope import androidx.compose.foundation.lazy.rememberLazyListState import androidx.compose.material3.Card import androidx.compose.material3.CardDefaults -import androidx.compose.material3.ExperimentalMaterial3Api import androidx.compose.material3.Icon import androidx.compose.material3.MaterialTheme import androidx.compose.material3.NavigationBar @@ -51,14 +50,13 @@ import com.patrykandpatrick.vico.sample.showcase.charts.Chart9 import com.patrykandpatrick.vico.sample.utils.plus @Composable -@OptIn(ExperimentalMaterial3Api::class) internal fun ShowcaseScreen(viewModel: ShowcaseViewModel = viewModel()) { val composeShowcaseState = rememberLazyListState() val viewShowcaseState = rememberLazyListState() Scaffold( bottomBar = { NavigationBar { - UISystem.values().forEach { uiSystem -> + UISystem.entries.forEach { uiSystem -> NavigationBarItem( selected = viewModel.uiSystem == uiSystem, onClick = { viewModel.setUISystem(uiSystem) }, @@ -91,15 +89,15 @@ internal fun ShowcaseScreen(viewModel: ShowcaseViewModel = viewModel()) { } private fun LazyListScope.chartItems(uiSystem: UISystem, viewModel: ShowcaseViewModel) { - cardItem { Chart9(uiSystem, viewModel.positiveAndNegativeChartEntryModelProducer) } - cardItem { Chart1(uiSystem, viewModel.customStepChartEntryModelProducer) } - cardItem { Chart2(uiSystem, viewModel.chartEntryModelProducer) } - cardItem { Chart3(uiSystem, viewModel.chartEntryModelProducer) } - cardItem { Chart4(uiSystem, viewModel.composedChartEntryModelProducer) } - cardItem { Chart5(uiSystem, viewModel.multiDataSetChartEntryModelProducer) } - cardItem { Chart6(uiSystem, viewModel.multiDataSetChartEntryModelProducer) } - cardItem { Chart7(uiSystem, viewModel.multiDataSetChartEntryModelProducer) } - cardItem { Chart8(uiSystem, viewModel.composedChartEntryModelProducer) } + cardItem { Chart1(uiSystem, viewModel.modelProducer1) } + cardItem { Chart2(uiSystem, viewModel.modelProducer2) } + cardItem { Chart3(uiSystem, viewModel.modelProducer1) } + cardItem { Chart4(uiSystem, viewModel.modelProducer3) } + cardItem { Chart5(uiSystem, viewModel.modelProducer4) } + cardItem { Chart6(uiSystem, viewModel.modelProducer4) } + cardItem { Chart7(uiSystem, viewModel.modelProducer5) } + cardItem { Chart8(uiSystem, viewModel.modelProducer3) } + cardItem { Chart9(uiSystem, viewModel.modelProducer6) } } private fun LazyListScope.cardItem(content: @Composable () -> Unit) { diff --git a/sample/src/main/java/com/patrykandpatrick/vico/sample/showcase/ShowcaseViewModel.kt b/sample/src/main/java/com/patrykandpatrick/vico/sample/showcase/ShowcaseViewModel.kt index c4ad0b34f..b9434a8e8 100644 --- a/sample/src/main/java/com/patrykandpatrick/vico/sample/showcase/ShowcaseViewModel.kt +++ b/sample/src/main/java/com/patrykandpatrick/vico/sample/showcase/ShowcaseViewModel.kt @@ -21,72 +21,50 @@ import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.setValue import androidx.lifecycle.ViewModel import androidx.lifecycle.viewModelScope -import com.patrykandpatrick.vico.core.entry.ChartEntryModelProducer -import com.patrykandpatrick.vico.core.entry.composed.ComposedChartEntryModelProducer -import com.patrykandpatrick.vico.core.util.RandomEntriesGenerator +import com.patrykandpatrick.vico.core.entry.composed.CartesianChartModelProducer +import com.patrykandpatrick.vico.core.util.RandomCartesianModelGenerator +import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.currentCoroutineContext import kotlinx.coroutines.delay import kotlinx.coroutines.isActive import kotlinx.coroutines.launch internal class ShowcaseViewModel : ViewModel() { - - private val generator = - RandomEntriesGenerator( - xRange = 0..GENERATOR_X_RANGE_TOP, - yRange = GENERATOR_Y_RANGE_BOTTOM..GENERATOR_Y_RANGE_TOP - ) - - private val customStepGenerator = - RandomEntriesGenerator( - xRange = - IntProgression.fromClosedRange( - rangeStart = 0, - rangeEnd = GENERATOR_X_RANGE_TOP, - step = 2 - ), - yRange = GENERATOR_Y_RANGE_BOTTOM..GENERATOR_Y_RANGE_TOP - ) - - private val positiveAndNegativeValuesGenerator = - RandomEntriesGenerator( - xRange = 0..GENERATOR_X_RANGE_TOP, - yRange = -10..GENERATOR_Y_RANGE_TOP - ) - - internal val chartEntryModelProducer: ChartEntryModelProducer = ChartEntryModelProducer() - - internal val positiveAndNegativeChartEntryModelProducer: ChartEntryModelProducer = - ChartEntryModelProducer() - - internal val customStepChartEntryModelProducer: ChartEntryModelProducer = - ChartEntryModelProducer() - - internal val multiDataSetChartEntryModelProducer: ChartEntryModelProducer = - ChartEntryModelProducer() - - internal val composedChartEntryModelProducer = ComposedChartEntryModelProducer.build() + internal val modelProducer1 = CartesianChartModelProducer.build() + internal val modelProducer2 = CartesianChartModelProducer.build() + internal val modelProducer3 = CartesianChartModelProducer.build() + internal val modelProducer4 = CartesianChartModelProducer.build() + internal val modelProducer5 = CartesianChartModelProducer.build() + internal val modelProducer6 = CartesianChartModelProducer.build() var uiSystem by mutableStateOf(UISystem.Compose) private set init { - viewModelScope.launch { + viewModelScope.launch(Dispatchers.Default) { while (currentCoroutineContext().isActive) { - val randomSeries = generator.generateRandomEntries() - val randomDataSet = - List(MULTI_ENTRIES_COMBINED) { generator.generateRandomEntries() } - chartEntryModelProducer.setEntries(randomSeries) - positiveAndNegativeChartEntryModelProducer.setEntries( - positiveAndNegativeValuesGenerator.generateRandomEntries() - ) - multiDataSetChartEntryModelProducer.setEntries(randomDataSet) - customStepChartEntryModelProducer.setEntries( - customStepGenerator.generateRandomEntries() - ) - composedChartEntryModelProducer.runTransaction { - add(randomDataSet) - add(randomSeries) + val singleSeriesLineLayerModelPartial = + RandomCartesianModelGenerator.getRandomLineLayerModelPartial() + val tripleSeriesColumnLayerModelPartial = + RandomCartesianModelGenerator.getRandomColumnLayerModelPartial(seriesCount = 3) + modelProducer1.tryRunTransaction { add(singleSeriesLineLayerModelPartial) } + modelProducer2.tryRunTransaction { + add(RandomCartesianModelGenerator.getRandomColumnLayerModelPartial()) + } + modelProducer3.tryRunTransaction { + add(tripleSeriesColumnLayerModelPartial) + add(singleSeriesLineLayerModelPartial) + } + modelProducer4.tryRunTransaction { add(tripleSeriesColumnLayerModelPartial) } + modelProducer5.tryRunTransaction { + add( + RandomCartesianModelGenerator.getRandomLineLayerModelPartial( + seriesCount = 3 + ) + ) + } + modelProducer6.tryRunTransaction { + add(RandomCartesianModelGenerator.getRandomLineLayerModelPartial(y = -10f..20f)) } delay(UPDATE_FREQUENCY) } @@ -98,10 +76,6 @@ internal class ShowcaseViewModel : ViewModel() { } private companion object { - const val MULTI_ENTRIES_COMBINED = 3 - const val GENERATOR_X_RANGE_TOP = 96 - const val GENERATOR_Y_RANGE_BOTTOM = 2 - const val GENERATOR_Y_RANGE_TOP = 20 const val UPDATE_FREQUENCY = 2000L } } diff --git a/sample/src/main/java/com/patrykandpatrick/vico/sample/showcase/charts/Chart1.kt b/sample/src/main/java/com/patrykandpatrick/vico/sample/showcase/charts/Chart1.kt index e642c951b..9f7561147 100644 --- a/sample/src/main/java/com/patrykandpatrick/vico/sample/showcase/charts/Chart1.kt +++ b/sample/src/main/java/com/patrykandpatrick/vico/sample/showcase/charts/Chart1.kt @@ -22,34 +22,36 @@ import androidx.compose.ui.graphics.Color import androidx.compose.ui.viewinterop.AndroidViewBinding 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.line.lineChart +import com.patrykandpatrick.vico.compose.chart.CartesianChartHost +import com.patrykandpatrick.vico.compose.chart.line.rememberLineCartesianLayer +import com.patrykandpatrick.vico.compose.chart.rememberCartesianChart import com.patrykandpatrick.vico.compose.style.ProvideChartStyle import com.patrykandpatrick.vico.core.axis.Axis -import com.patrykandpatrick.vico.core.entry.ChartEntryModelProducer +import com.patrykandpatrick.vico.core.entry.composed.CartesianChartModelProducer import com.patrykandpatrick.vico.databinding.Chart1Binding import com.patrykandpatrick.vico.sample.showcase.UISystem import com.patrykandpatrick.vico.sample.showcase.rememberChartStyle import com.patrykandpatrick.vico.sample.showcase.rememberMarker @Composable -internal fun Chart1(uiSystem: UISystem, chartEntryModelProducer: ChartEntryModelProducer) { +internal fun Chart1(uiSystem: UISystem, modelProducer: CartesianChartModelProducer) { when (uiSystem) { - UISystem.Compose -> ComposeChart1(chartEntryModelProducer) - UISystem.Views -> ViewChart1(chartEntryModelProducer) + UISystem.Compose -> ComposeChart1(modelProducer) + UISystem.Views -> ViewChart1(modelProducer) } } @Composable -private fun ComposeChart1(chartEntryModelProducer: ChartEntryModelProducer) { +private fun ComposeChart1(modelProducer: CartesianChartModelProducer) { val marker = rememberMarker() ProvideChartStyle(rememberChartStyle(chartColors)) { - Chart( + CartesianChartHost( chart = - lineChart( + rememberCartesianChart( + rememberLineCartesianLayer(), persistentMarkers = remember(marker) { mapOf(PERSISTENT_MARKER_X to marker) } ), - chartModelProducer = chartEntryModelProducer, + modelProducer = modelProducer, startAxis = rememberStartAxis(), bottomAxis = rememberBottomAxis(guideline = null), marker = marker, @@ -59,13 +61,13 @@ private fun ComposeChart1(chartEntryModelProducer: ChartEntryModelProducer) { } @Composable -private fun ViewChart1(chartEntryModelProducer: ChartEntryModelProducer) { +private fun ViewChart1(modelProducer: CartesianChartModelProducer) { val marker = rememberMarker() AndroidViewBinding(Chart1Binding::inflate) { with(chartView) { chart?.addPersistentMarker(PERSISTENT_MARKER_X, marker) runInitialAnimation = false - entryProducer = chartEntryModelProducer + this.modelProducer = modelProducer (bottomAxis as Axis).guideline = null this.marker = marker } @@ -73,7 +75,7 @@ private fun ViewChart1(chartEntryModelProducer: ChartEntryModelProducer) { } private const val COLOR_1_CODE = 0xffa485e0 -private const val PERSISTENT_MARKER_X = 10f +private const val PERSISTENT_MARKER_X = 5f private val color1 = Color(COLOR_1_CODE) private val chartColors = listOf(color1) diff --git a/sample/src/main/java/com/patrykandpatrick/vico/sample/showcase/charts/Chart2.kt b/sample/src/main/java/com/patrykandpatrick/vico/sample/showcase/charts/Chart2.kt index fc5493c1b..816c9b86f 100644 --- a/sample/src/main/java/com/patrykandpatrick/vico/sample/showcase/charts/Chart2.kt +++ b/sample/src/main/java/com/patrykandpatrick/vico/sample/showcase/charts/Chart2.kt @@ -24,8 +24,9 @@ import androidx.compose.ui.unit.dp import androidx.compose.ui.viewinterop.AndroidViewBinding 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.column.rememberColumnCartesianLayer +import com.patrykandpatrick.vico.compose.chart.rememberCartesianChart import com.patrykandpatrick.vico.compose.component.shapeComponent import com.patrykandpatrick.vico.compose.component.textComponent import com.patrykandpatrick.vico.compose.dimensions.dimensionsOf @@ -41,7 +42,7 @@ import com.patrykandpatrick.vico.core.chart.decoration.ThresholdLine import com.patrykandpatrick.vico.core.chart.layout.HorizontalLayout import com.patrykandpatrick.vico.core.component.shape.LineComponent import com.patrykandpatrick.vico.core.component.shape.Shapes -import com.patrykandpatrick.vico.core.entry.ChartEntryModelProducer +import com.patrykandpatrick.vico.core.entry.composed.CartesianChartModelProducer import com.patrykandpatrick.vico.core.extension.half import com.patrykandpatrick.vico.databinding.Chart2Binding import com.patrykandpatrick.vico.sample.showcase.UISystem @@ -49,34 +50,31 @@ import com.patrykandpatrick.vico.sample.showcase.rememberChartStyle import com.patrykandpatrick.vico.sample.showcase.rememberMarker @Composable -internal fun Chart2(uiSystem: UISystem, chartEntryModelProducer: ChartEntryModelProducer) { +internal fun Chart2(uiSystem: UISystem, modelProducer: CartesianChartModelProducer) { when (uiSystem) { - UISystem.Compose -> ComposeChart2(chartEntryModelProducer) - UISystem.Views -> ViewChart2(chartEntryModelProducer) + UISystem.Compose -> ComposeChart2(modelProducer) + UISystem.Views -> ViewChart2(modelProducer) } } @Composable -private fun ComposeChart2(chartEntryModelProducer: ChartEntryModelProducer) { +private fun ComposeChart2(modelProducer: CartesianChartModelProducer) { val thresholdLine = rememberThresholdLine() ProvideChartStyle(rememberChartStyle(chartColors)) { - val defaultColumns = currentChartStyle.columnChart.columns - Chart( + val defaultColumns = currentChartStyle.columnLayer.columns + CartesianChartHost( chart = - columnChart( - columns = + rememberCartesianChart( + rememberColumnCartesianLayer( remember(defaultColumns) { - defaultColumns.map { defaultColumn -> - LineComponent( - defaultColumn.color, - COLUMN_WIDTH_DP, - defaultColumn.shape - ) + defaultColumns.map { + LineComponent(it.color, COLUMN_WIDTH_DP, it.shape) } - }, + } + ), decorations = remember(thresholdLine) { listOf(thresholdLine) } ), - chartModelProducer = chartEntryModelProducer, + modelProducer = modelProducer, startAxis = rememberStartAxis( valueFormatter = startAxisValueFormatter, @@ -91,14 +89,14 @@ private fun ComposeChart2(chartEntryModelProducer: ChartEntryModelProducer) { } @Composable -private fun ViewChart2(chartEntryModelProducer: ChartEntryModelProducer) { +private fun ViewChart2(modelProducer: CartesianChartModelProducer) { val thresholdLine = rememberThresholdLine() val marker = rememberMarker() AndroidViewBinding(Chart2Binding::inflate) { with(chartView) { chart?.addDecoration(thresholdLine) runInitialAnimation = false - entryProducer = chartEntryModelProducer + this.modelProducer = modelProducer with(startAxis as VerticalAxis) { itemPlacer = startAxisItemPlacer valueFormatter = startAxisValueFormatter diff --git a/sample/src/main/java/com/patrykandpatrick/vico/sample/showcase/charts/Chart3.kt b/sample/src/main/java/com/patrykandpatrick/vico/sample/showcase/charts/Chart3.kt index 53e0dca3d..273dbde70 100644 --- a/sample/src/main/java/com/patrykandpatrick/vico/sample/showcase/charts/Chart3.kt +++ b/sample/src/main/java/com/patrykandpatrick/vico/sample/showcase/charts/Chart3.kt @@ -25,39 +25,44 @@ import androidx.compose.ui.viewinterop.AndroidViewBinding import com.patrykandpatrick.vico.R 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.CartesianChartHost import com.patrykandpatrick.vico.compose.chart.edges.rememberFadingEdges import com.patrykandpatrick.vico.compose.chart.layout.fullWidth -import com.patrykandpatrick.vico.compose.chart.line.lineChart +import com.patrykandpatrick.vico.compose.chart.line.rememberLineCartesianLayer +import com.patrykandpatrick.vico.compose.chart.rememberCartesianChart import com.patrykandpatrick.vico.compose.component.shapeComponent import com.patrykandpatrick.vico.compose.component.textComponent import com.patrykandpatrick.vico.compose.dimensions.dimensionsOf import com.patrykandpatrick.vico.compose.style.ProvideChartStyle import com.patrykandpatrick.vico.core.axis.vertical.VerticalAxis import com.patrykandpatrick.vico.core.chart.layout.HorizontalLayout -import com.patrykandpatrick.vico.core.chart.line.LineChart -import com.patrykandpatrick.vico.core.chart.values.AxisValuesOverrider +import com.patrykandpatrick.vico.core.chart.line.LineCartesianLayer +import com.patrykandpatrick.vico.core.chart.line.LineCartesianLayerModel +import com.patrykandpatrick.vico.core.chart.values.AxisValueOverrider import com.patrykandpatrick.vico.core.component.shape.Shapes -import com.patrykandpatrick.vico.core.entry.ChartEntryModelProducer +import com.patrykandpatrick.vico.core.entry.composed.CartesianChartModelProducer import com.patrykandpatrick.vico.databinding.Chart3Binding import com.patrykandpatrick.vico.sample.showcase.UISystem import com.patrykandpatrick.vico.sample.showcase.rememberChartStyle import com.patrykandpatrick.vico.sample.showcase.rememberMarker @Composable -internal fun Chart3(uiSystem: UISystem, chartEntryModelProducer: ChartEntryModelProducer) { +internal fun Chart3(uiSystem: UISystem, modelProducer: CartesianChartModelProducer) { when (uiSystem) { - UISystem.Compose -> ComposeChart3(chartEntryModelProducer) - UISystem.Views -> ViewChart3(chartEntryModelProducer) + UISystem.Compose -> ComposeChart3(modelProducer) + UISystem.Views -> ViewChart3(modelProducer) } } @Composable -private fun ComposeChart3(chartEntryModelProducer: ChartEntryModelProducer) { +private fun ComposeChart3(modelProducer: CartesianChartModelProducer) { ProvideChartStyle(rememberChartStyle(chartColors)) { - Chart( - chart = lineChart(axisValuesOverrider = axisValueOverrider), - chartModelProducer = chartEntryModelProducer, + CartesianChartHost( + chart = + rememberCartesianChart( + rememberLineCartesianLayer(axisValueOverrider = axisValueOverrider) + ), + modelProducer = modelProducer, startAxis = rememberStartAxis( guideline = null, @@ -93,13 +98,13 @@ private fun ComposeChart3(chartEntryModelProducer: ChartEntryModelProducer) { } @Composable -private fun ViewChart3(chartEntryModelProducer: ChartEntryModelProducer) { +private fun ViewChart3(modelProducer: CartesianChartModelProducer) { val marker = rememberMarker() AndroidViewBinding(Chart3Binding::inflate) { with(chartView) { - (chart as LineChart).axisValuesOverrider = axisValueOverrider + (chart?.layers?.get(0) as LineCartesianLayer?)?.axisValueOverrider = axisValueOverrider runInitialAnimation = false - entryProducer = chartEntryModelProducer + this.modelProducer = modelProducer this.marker = marker } } @@ -113,7 +118,10 @@ private val color1 = Color(COLOR_1_CODE) private val color2 = Color(COLOR_2_CODE) private val chartColors = listOf(color1, color2) private val axisValueOverrider = - AxisValuesOverrider.adaptiveYValues(yFraction = AXIS_VALUE_OVERRIDER_Y_FRACTION, round = true) + AxisValueOverrider.adaptiveYValues( + yFraction = AXIS_VALUE_OVERRIDER_Y_FRACTION, + round = true + ) private val axisTitleHorizontalPaddingValue = 8.dp private val axisTitleVerticalPaddingValue = 2.dp private val axisTitlePadding = diff --git a/sample/src/main/java/com/patrykandpatrick/vico/sample/showcase/charts/Chart4.kt b/sample/src/main/java/com/patrykandpatrick/vico/sample/showcase/charts/Chart4.kt index 509cab94a..b72f89d68 100644 --- a/sample/src/main/java/com/patrykandpatrick/vico/sample/showcase/charts/Chart4.kt +++ b/sample/src/main/java/com/patrykandpatrick/vico/sample/showcase/charts/Chart4.kt @@ -23,59 +23,57 @@ import androidx.compose.ui.unit.dp import androidx.compose.ui.viewinterop.AndroidViewBinding import com.patrykandpatrick.vico.compose.axis.horizontal.rememberTopAxis import com.patrykandpatrick.vico.compose.axis.vertical.rememberEndAxis -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.column.rememberColumnCartesianLayer +import com.patrykandpatrick.vico.compose.chart.line.rememberLineCartesianLayer +import com.patrykandpatrick.vico.compose.chart.rememberCartesianChart import com.patrykandpatrick.vico.compose.component.shape.roundedCornerShape import com.patrykandpatrick.vico.compose.style.ProvideChartStyle import com.patrykandpatrick.vico.compose.style.currentChartStyle import com.patrykandpatrick.vico.core.chart.DefaultPointConnector -import com.patrykandpatrick.vico.core.chart.composed.plus import com.patrykandpatrick.vico.core.chart.copy import com.patrykandpatrick.vico.core.component.shape.LineComponent import com.patrykandpatrick.vico.core.component.shape.Shapes -import com.patrykandpatrick.vico.core.entry.composed.ComposedChartEntryModelProducer +import com.patrykandpatrick.vico.core.entry.composed.CartesianChartModelProducer import com.patrykandpatrick.vico.databinding.Chart4Binding import com.patrykandpatrick.vico.sample.showcase.UISystem import com.patrykandpatrick.vico.sample.showcase.rememberChartStyle import com.patrykandpatrick.vico.sample.showcase.rememberMarker @Composable -internal fun Chart4(uiSystem: UISystem, chartEntryModelProducer: ComposedChartEntryModelProducer) { +internal fun Chart4(uiSystem: UISystem, modelProducer: CartesianChartModelProducer) { when (uiSystem) { - UISystem.Compose -> ComposeChart4(chartEntryModelProducer) - UISystem.Views -> ViewChart4(chartEntryModelProducer) + UISystem.Compose -> ComposeChart4(modelProducer) + UISystem.Views -> ViewChart4(modelProducer) } } @Composable -private fun ComposeChart4(chartEntryModelProducer: ComposedChartEntryModelProducer) { +private fun ComposeChart4(modelProducer: CartesianChartModelProducer) { ProvideChartStyle(rememberChartStyle(columnChartColors, lineChartColors)) { - val defaultColumns = currentChartStyle.columnChart.columns - val defaultLines = currentChartStyle.lineChart.lines - val columnChart = - columnChart( - remember(defaultColumns) { - defaultColumns.map { defaultColumn -> - LineComponent( - defaultColumn.color, - defaultColumn.thicknessDp, - Shapes.roundedCornerShape(columnCornerRadius) - ) - } - } - ) - val lineChart = - lineChart( - remember(defaultLines) { - defaultLines.map { defaultLine -> - defaultLine.copy(pointConnector = pointConnector) - } - } - ) - Chart( - chart = remember(columnChart, lineChart) { columnChart + lineChart }, - chartModelProducer = chartEntryModelProducer, + val defaultColumns = currentChartStyle.columnLayer.columns + val defaultLines = currentChartStyle.lineLayer.lines + CartesianChartHost( + chart = + rememberCartesianChart( + rememberColumnCartesianLayer( + remember(defaultColumns) { + defaultColumns.map { + LineComponent( + it.color, + it.thicknessDp, + Shapes.roundedCornerShape(columnCornerRadius) + ) + } + } + ), + rememberLineCartesianLayer( + remember(defaultLines) { + defaultLines.map { it.copy(pointConnector = pointConnector) } + } + ) + ), + modelProducer = modelProducer, topAxis = rememberTopAxis(), endAxis = rememberEndAxis(), marker = rememberMarker(), @@ -85,12 +83,12 @@ private fun ComposeChart4(chartEntryModelProducer: ComposedChartEntryModelProduc } @Composable -private fun ViewChart4(chartEntryModelProducer: ComposedChartEntryModelProducer) { +private fun ViewChart4(modelProducer: CartesianChartModelProducer) { val marker = rememberMarker() AndroidViewBinding(Chart4Binding::inflate) { with(chartView) { runInitialAnimation = false - entryProducer = chartEntryModelProducer + this.modelProducer = modelProducer this.marker = marker } } diff --git a/sample/src/main/java/com/patrykandpatrick/vico/sample/showcase/charts/Chart5.kt b/sample/src/main/java/com/patrykandpatrick/vico/sample/showcase/charts/Chart5.kt index a190ce095..79cf65406 100644 --- a/sample/src/main/java/com/patrykandpatrick/vico/sample/showcase/charts/Chart5.kt +++ b/sample/src/main/java/com/patrykandpatrick/vico/sample/showcase/charts/Chart5.kt @@ -22,63 +22,70 @@ import androidx.compose.ui.graphics.Color import androidx.compose.ui.viewinterop.AndroidViewBinding 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.column.rememberColumnCartesianLayer +import com.patrykandpatrick.vico.compose.chart.rememberCartesianChart import com.patrykandpatrick.vico.compose.style.ProvideChartStyle import com.patrykandpatrick.vico.compose.style.currentChartStyle import com.patrykandpatrick.vico.core.DefaultDimens import com.patrykandpatrick.vico.core.axis.AxisItemPlacer import com.patrykandpatrick.vico.core.axis.vertical.VerticalAxis -import com.patrykandpatrick.vico.core.chart.column.ColumnChart +import com.patrykandpatrick.vico.core.chart.column.ColumnCartesianLayer import com.patrykandpatrick.vico.core.component.shape.LineComponent import com.patrykandpatrick.vico.core.component.shape.Shapes -import com.patrykandpatrick.vico.core.entry.ChartEntryModelProducer +import com.patrykandpatrick.vico.core.entry.composed.CartesianChartModelProducer import com.patrykandpatrick.vico.databinding.Chart5Binding import com.patrykandpatrick.vico.sample.showcase.UISystem import com.patrykandpatrick.vico.sample.showcase.rememberChartStyle import com.patrykandpatrick.vico.sample.showcase.rememberMarker @Composable -internal fun Chart5(uiSystem: UISystem, chartEntryModelProducer: ChartEntryModelProducer) { +internal fun Chart5(uiSystem: UISystem, modelProducer: CartesianChartModelProducer) { when (uiSystem) { - UISystem.Compose -> ComposeChart5(chartEntryModelProducer) - UISystem.Views -> ViewChart5(chartEntryModelProducer) + UISystem.Compose -> ComposeChart5(modelProducer) + UISystem.Views -> ViewChart5(modelProducer) } } @Composable -private fun ComposeChart5(chartEntryModelProducer: ChartEntryModelProducer) { +private fun ComposeChart5(modelProducer: CartesianChartModelProducer) { ProvideChartStyle(rememberChartStyle(chartColors)) { - val defaultColumns = currentChartStyle.columnChart.columns - Chart( + val defaultColumns = currentChartStyle.columnLayer.columns + CartesianChartHost( chart = - columnChart( - columns = - remember(defaultColumns) { - defaultColumns.mapIndexed { index, defaultColumn -> - val topCornerRadiusPercent = - if (index == defaultColumns.lastIndex) { - DefaultDimens.COLUMN_ROUNDNESS_PERCENT - } else { - 0 - } - val bottomCornerRadiusPercent = - if (index == 0) DefaultDimens.COLUMN_ROUNDNESS_PERCENT else 0 - LineComponent( - defaultColumn.color, - defaultColumn.thicknessDp, - Shapes.roundedCornerShape( - topCornerRadiusPercent, - topCornerRadiusPercent, - bottomCornerRadiusPercent, - bottomCornerRadiusPercent + rememberCartesianChart( + rememberColumnCartesianLayer( + columns = + remember(defaultColumns) { + defaultColumns.mapIndexed { index, defaultColumn -> + val topCornerRadiusPercent = + if (index == defaultColumns.lastIndex) { + DefaultDimens.COLUMN_ROUNDNESS_PERCENT + } else { + 0 + } + val bottomCornerRadiusPercent = + if (index == 0) { + DefaultDimens.COLUMN_ROUNDNESS_PERCENT + } else { + 0 + } + LineComponent( + defaultColumn.color, + defaultColumn.thicknessDp, + Shapes.roundedCornerShape( + topCornerRadiusPercent, + topCornerRadiusPercent, + bottomCornerRadiusPercent, + bottomCornerRadiusPercent + ) ) - ) - } - }, - mergeMode = ColumnChart.MergeMode.Stack + } + }, + mergeMode = ColumnCartesianLayer.MergeMode.Stacked + ) ), - chartModelProducer = chartEntryModelProducer, + modelProducer = modelProducer, startAxis = rememberStartAxis( itemPlacer = startAxisItemPlacer, @@ -92,13 +99,12 @@ private fun ComposeChart5(chartEntryModelProducer: ChartEntryModelProducer) { } @Composable -private fun ViewChart5(chartEntryModelProducer: ChartEntryModelProducer) { +private fun ViewChart5(modelProducer: CartesianChartModelProducer) { val marker = rememberMarker() AndroidViewBinding(Chart5Binding::inflate) { with(chartView) { - (chart as ColumnChart).mergeMode = ColumnChart.MergeMode.Stack runInitialAnimation = false - entryProducer = chartEntryModelProducer + this.modelProducer = modelProducer (startAxis as VerticalAxis).itemPlacer = startAxisItemPlacer this.marker = marker } diff --git a/sample/src/main/java/com/patrykandpatrick/vico/sample/showcase/charts/Chart6.kt b/sample/src/main/java/com/patrykandpatrick/vico/sample/showcase/charts/Chart6.kt index 9461c34d7..58c4e9b91 100644 --- a/sample/src/main/java/com/patrykandpatrick/vico/sample/showcase/charts/Chart6.kt +++ b/sample/src/main/java/com/patrykandpatrick/vico/sample/showcase/charts/Chart6.kt @@ -24,8 +24,9 @@ import androidx.compose.ui.unit.dp import androidx.compose.ui.viewinterop.AndroidViewBinding 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.column.rememberColumnCartesianLayer +import com.patrykandpatrick.vico.compose.chart.rememberCartesianChart import com.patrykandpatrick.vico.compose.component.shapeComponent import com.patrykandpatrick.vico.compose.component.textComponent import com.patrykandpatrick.vico.compose.dimensions.dimensionsOf @@ -34,33 +35,33 @@ import com.patrykandpatrick.vico.compose.style.currentChartStyle 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.column.ColumnChart +import com.patrykandpatrick.vico.core.chart.column.ColumnCartesianLayer import com.patrykandpatrick.vico.core.chart.decoration.ThresholdLine import com.patrykandpatrick.vico.core.component.shape.LineComponent import com.patrykandpatrick.vico.core.component.shape.Shapes -import com.patrykandpatrick.vico.core.entry.ChartEntryModelProducer +import com.patrykandpatrick.vico.core.entry.composed.CartesianChartModelProducer import com.patrykandpatrick.vico.databinding.Chart6Binding import com.patrykandpatrick.vico.sample.showcase.UISystem import com.patrykandpatrick.vico.sample.showcase.rememberChartStyle import com.patrykandpatrick.vico.sample.showcase.rememberMarker @Composable -internal fun Chart6(uiSystem: UISystem, chartEntryModelProducer: ChartEntryModelProducer) { +internal fun Chart6(uiSystem: UISystem, modelProducer: CartesianChartModelProducer) { when (uiSystem) { - UISystem.Compose -> ComposeChart6(chartEntryModelProducer) - UISystem.Views -> ViewChart6(chartEntryModelProducer) + UISystem.Compose -> ComposeChart6(modelProducer) + UISystem.Views -> ViewChart6(modelProducer) } } @Composable -private fun ComposeChart6(chartEntryModelProducer: ChartEntryModelProducer) { +private fun ComposeChart6(modelProducer: CartesianChartModelProducer) { val thresholdLine = rememberThresholdLine() ProvideChartStyle(rememberChartStyle(chartColors)) { - val defaultColumns = currentChartStyle.columnChart.columns - Chart( + val defaultColumns = currentChartStyle.columnLayer.columns + CartesianChartHost( chart = - columnChart( - columns = + rememberCartesianChart( + rememberColumnCartesianLayer( remember(defaultColumns) { defaultColumns.map { defaultColumn -> LineComponent( @@ -72,10 +73,11 @@ private fun ComposeChart6(chartEntryModelProducer: ChartEntryModelProducer) { ) } }, - mergeMode = ColumnChart.MergeMode.Grouped, + mergeMode = ColumnCartesianLayer.MergeMode.Grouped + ), decorations = remember(thresholdLine) { listOf(thresholdLine) } ), - chartModelProducer = chartEntryModelProducer, + modelProducer = modelProducer, startAxis = rememberStartAxis(), bottomAxis = rememberBottomAxis(valueFormatter = bottomAxisValueFormatter), marker = rememberMarker(), @@ -85,7 +87,7 @@ private fun ComposeChart6(chartEntryModelProducer: ChartEntryModelProducer) { } @Composable -private fun ViewChart6(chartEntryModelProducer: ChartEntryModelProducer) { +private fun ViewChart6(modelProducer: CartesianChartModelProducer) { val thresholdLine = rememberThresholdLine() val decorations = remember(thresholdLine) { listOf(thresholdLine) } val marker = rememberMarker() @@ -93,7 +95,7 @@ private fun ViewChart6(chartEntryModelProducer: ChartEntryModelProducer) { with(chartView) { chart?.setDecorations(decorations) runInitialAnimation = false - entryProducer = chartEntryModelProducer + this.modelProducer = modelProducer (bottomAxis as? HorizontalAxis)?.valueFormatter = bottomAxisValueFormatter this.marker = marker @@ -146,6 +148,6 @@ private val thresholdLineLabelMargins = dimensionsOf(thresholdLineLabelMarginVal private val thresholdLineColor = color4.copy(THRESHOLD_LINE_ALPHA) private val daysOfWeek = listOf("Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun") private val bottomAxisValueFormatter = - AxisValueFormatter { x, _ -> + AxisValueFormatter { x, _, _ -> daysOfWeek[x.toInt() % daysOfWeek.size] } diff --git a/sample/src/main/java/com/patrykandpatrick/vico/sample/showcase/charts/Chart7.kt b/sample/src/main/java/com/patrykandpatrick/vico/sample/showcase/charts/Chart7.kt index db8cfacb6..64d2643f2 100644 --- a/sample/src/main/java/com/patrykandpatrick/vico/sample/showcase/charts/Chart7.kt +++ b/sample/src/main/java/com/patrykandpatrick/vico/sample/showcase/charts/Chart7.kt @@ -28,8 +28,9 @@ import com.patrykandpatrick.vico.R import com.patrykandpatrick.vico.compose.axis.axisLabelComponent 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.line.lineChart +import com.patrykandpatrick.vico.compose.chart.CartesianChartHost +import com.patrykandpatrick.vico.compose.chart.line.rememberLineCartesianLayer +import com.patrykandpatrick.vico.compose.chart.rememberCartesianChart import com.patrykandpatrick.vico.compose.component.shape.roundedCornerShape import com.patrykandpatrick.vico.compose.component.shapeComponent import com.patrykandpatrick.vico.compose.component.textComponent @@ -41,34 +42,34 @@ import com.patrykandpatrick.vico.compose.style.currentChartStyle import com.patrykandpatrick.vico.core.axis.vertical.VerticalAxis import com.patrykandpatrick.vico.core.chart.copy import com.patrykandpatrick.vico.core.component.shape.Shapes -import com.patrykandpatrick.vico.core.entry.ChartEntryModelProducer +import com.patrykandpatrick.vico.core.entry.composed.CartesianChartModelProducer import com.patrykandpatrick.vico.databinding.Chart7Binding import com.patrykandpatrick.vico.sample.showcase.UISystem import com.patrykandpatrick.vico.sample.showcase.rememberChartStyle import com.patrykandpatrick.vico.sample.showcase.rememberMarker @Composable -internal fun Chart7(uiSystem: UISystem, chartEntryModelProducer: ChartEntryModelProducer) { +internal fun Chart7(uiSystem: UISystem, modelProducer: CartesianChartModelProducer) { when (uiSystem) { - UISystem.Compose -> ComposeChart7(chartEntryModelProducer) - UISystem.Views -> ViewChart7(chartEntryModelProducer) + UISystem.Compose -> ComposeChart7(modelProducer) + UISystem.Views -> ViewChart7(modelProducer) } } @Composable -private fun ComposeChart7(chartEntryModelProducer: ChartEntryModelProducer) { +private fun ComposeChart7(modelProducer: CartesianChartModelProducer) { ProvideChartStyle(rememberChartStyle(chartColors)) { - val defaultLines = currentChartStyle.lineChart.lines - Chart( + val defaultLines = currentChartStyle.lineLayer.lines + CartesianChartHost( chart = - lineChart( - remember(defaultLines) { - defaultLines.map { defaultLine -> - defaultLine.copy(lineBackgroundShader = null) + rememberCartesianChart( + rememberLineCartesianLayer( + remember(defaultLines) { + defaultLines.map { it.copy(backgroundShader = null) } } - } + ) ), - chartModelProducer = chartEntryModelProducer, + modelProducer = modelProducer, startAxis = rememberStartAxis( label = rememberStartAxisLabel(), @@ -83,14 +84,14 @@ private fun ComposeChart7(chartEntryModelProducer: ChartEntryModelProducer) { } @Composable -private fun ViewChart7(chartEntryModelProducer: ChartEntryModelProducer) { +private fun ViewChart7(modelProducer: CartesianChartModelProducer) { val startAxisLabel = rememberStartAxisLabel() val marker = rememberMarker() val legend = rememberLegend() AndroidViewBinding(Chart7Binding::inflate) { with(chartView) { runInitialAnimation = false - entryProducer = chartEntryModelProducer + this.modelProducer = modelProducer (startAxis as VerticalAxis).horizontalLabelPosition = VerticalAxis.HorizontalLabelPosition.Inside (startAxis as VerticalAxis).label = startAxisLabel @@ -125,7 +126,7 @@ private fun rememberLegend() = textSize = legendItemLabelTextSize, typeface = Typeface.MONOSPACE ), - labelText = stringResource(R.string.data_set_x, index + 1) + labelText = stringResource(R.string.series_x, index + 1) ) }, iconSize = legendItemIconSize, diff --git a/sample/src/main/java/com/patrykandpatrick/vico/sample/showcase/charts/Chart8.kt b/sample/src/main/java/com/patrykandpatrick/vico/sample/showcase/charts/Chart8.kt index 14a5f940f..029c91a41 100644 --- a/sample/src/main/java/com/patrykandpatrick/vico/sample/showcase/charts/Chart8.kt +++ b/sample/src/main/java/com/patrykandpatrick/vico/sample/showcase/charts/Chart8.kt @@ -17,47 +17,48 @@ package com.patrykandpatrick.vico.sample.showcase.charts import androidx.compose.runtime.Composable -import androidx.compose.runtime.remember import androidx.compose.ui.graphics.Color import androidx.compose.ui.viewinterop.AndroidViewBinding 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.column.rememberColumnCartesianLayer +import com.patrykandpatrick.vico.compose.chart.line.rememberLineCartesianLayer +import com.patrykandpatrick.vico.compose.chart.rememberCartesianChart import com.patrykandpatrick.vico.compose.style.ProvideChartStyle import com.patrykandpatrick.vico.core.axis.Axis import com.patrykandpatrick.vico.core.axis.AxisPosition -import com.patrykandpatrick.vico.core.chart.column.ColumnChart -import com.patrykandpatrick.vico.core.chart.composed.ComposedChart -import com.patrykandpatrick.vico.core.chart.composed.plus -import com.patrykandpatrick.vico.core.chart.line.LineChart -import com.patrykandpatrick.vico.core.entry.composed.ComposedChartEntryModelProducer +import com.patrykandpatrick.vico.core.chart.column.ColumnCartesianLayer +import com.patrykandpatrick.vico.core.chart.line.LineCartesianLayer +import com.patrykandpatrick.vico.core.entry.composed.CartesianChartModelProducer import com.patrykandpatrick.vico.databinding.Chart8Binding import com.patrykandpatrick.vico.sample.showcase.UISystem import com.patrykandpatrick.vico.sample.showcase.rememberChartStyle import com.patrykandpatrick.vico.sample.showcase.rememberMarker @Composable -internal fun Chart8(uiSystem: UISystem, chartEntryModelProducer: ComposedChartEntryModelProducer) { +internal fun Chart8(uiSystem: UISystem, modelProducer: CartesianChartModelProducer) { when (uiSystem) { - UISystem.Compose -> ComposeChart8(chartEntryModelProducer) - UISystem.Views -> ViewChart8(chartEntryModelProducer) + UISystem.Compose -> ComposeChart8(modelProducer) + UISystem.Views -> ViewChart8(modelProducer) } } @Composable -private fun ComposeChart8(chartEntryModelProducer: ComposedChartEntryModelProducer) { +private fun ComposeChart8(modelProducer: CartesianChartModelProducer) { ProvideChartStyle(rememberChartStyle(columnChartColors, lineChartColors)) { - val columnChart = - columnChart( - mergeMode = ColumnChart.MergeMode.Stack, - targetVerticalAxisPosition = AxisPosition.Vertical.Start - ) - val lineChart = lineChart(targetVerticalAxisPosition = AxisPosition.Vertical.End) - Chart( - chart = remember(columnChart, lineChart) { columnChart + lineChart }, - chartModelProducer = chartEntryModelProducer, + CartesianChartHost( + chart = + rememberCartesianChart( + rememberColumnCartesianLayer( + mergeMode = ColumnCartesianLayer.MergeMode.Stacked, + targetVerticalAxisPosition = AxisPosition.Vertical.Start + ), + rememberLineCartesianLayer( + targetVerticalAxisPosition = AxisPosition.Vertical.End + ) + ), + modelProducer = modelProducer, startAxis = rememberStartAxis(guideline = null), endAxis = rememberEndAxis(), marker = rememberMarker(), @@ -67,18 +68,16 @@ private fun ComposeChart8(chartEntryModelProducer: ComposedChartEntryModelProduc } @Composable -private fun ViewChart8(chartEntryModelProducer: ComposedChartEntryModelProducer) { +private fun ViewChart8(modelProducer: CartesianChartModelProducer) { val marker = rememberMarker() AndroidViewBinding(Chart8Binding::inflate) { with(chartView) { - ((chart as ComposedChart).charts[0] as ColumnChart).mergeMode = - ColumnChart.MergeMode.Stack - ((chart as ComposedChart).charts[0] as ColumnChart).targetVerticalAxisPosition = + (chart?.layers?.get(0) as ColumnCartesianLayer).targetVerticalAxisPosition = AxisPosition.Vertical.Start - ((chart as ComposedChart).charts[1] as LineChart).targetVerticalAxisPosition = + (chart?.layers?.get(1) as LineCartesianLayer).targetVerticalAxisPosition = AxisPosition.Vertical.End runInitialAnimation = false - entryProducer = chartEntryModelProducer + this.modelProducer = modelProducer (startAxis as Axis).guideline = null this.marker = marker } diff --git a/sample/src/main/java/com/patrykandpatrick/vico/sample/showcase/charts/Chart9.kt b/sample/src/main/java/com/patrykandpatrick/vico/sample/showcase/charts/Chart9.kt index ddf458c45..dc7dc4b9a 100644 --- a/sample/src/main/java/com/patrykandpatrick/vico/sample/showcase/charts/Chart9.kt +++ b/sample/src/main/java/com/patrykandpatrick/vico/sample/showcase/charts/Chart9.kt @@ -30,14 +30,15 @@ import com.patrykandpatrick.vico.R import com.patrykandpatrick.vico.compose.axis.axisLabelComponent 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.CartesianChartHost import com.patrykandpatrick.vico.compose.chart.layout.fullWidth -import com.patrykandpatrick.vico.compose.chart.line.lineChart import com.patrykandpatrick.vico.compose.chart.line.lineSpec +import com.patrykandpatrick.vico.compose.chart.line.rememberLineCartesianLayer +import com.patrykandpatrick.vico.compose.chart.rememberCartesianChart import com.patrykandpatrick.vico.compose.component.lineComponent import com.patrykandpatrick.vico.compose.component.shape.dashedShape +import com.patrykandpatrick.vico.compose.component.shape.shader.color import com.patrykandpatrick.vico.compose.component.shape.shader.fromComponent -import com.patrykandpatrick.vico.compose.component.shape.shader.split import com.patrykandpatrick.vico.compose.component.shape.shader.verticalGradient import com.patrykandpatrick.vico.compose.component.shapeComponent import com.patrykandpatrick.vico.compose.dimensions.dimensionsOf @@ -45,77 +46,84 @@ import com.patrykandpatrick.vico.compose.style.ProvideChartStyle import com.patrykandpatrick.vico.core.axis.Axis import com.patrykandpatrick.vico.core.axis.AxisItemPlacer import com.patrykandpatrick.vico.core.chart.layout.HorizontalLayout -import com.patrykandpatrick.vico.core.chart.line.LineChart +import com.patrykandpatrick.vico.core.chart.line.LineCartesianLayer import com.patrykandpatrick.vico.core.component.shape.ShapeComponent import com.patrykandpatrick.vico.core.component.shape.Shapes import com.patrykandpatrick.vico.core.component.shape.shader.DynamicShaders -import com.patrykandpatrick.vico.core.component.shape.shader.splitShader -import com.patrykandpatrick.vico.core.entry.ChartEntryModelProducer +import com.patrykandpatrick.vico.core.component.shape.shader.TopBottomShader +import com.patrykandpatrick.vico.core.entry.composed.CartesianChartModelProducer import com.patrykandpatrick.vico.databinding.Chart9Binding import com.patrykandpatrick.vico.sample.showcase.UISystem import com.patrykandpatrick.vico.sample.showcase.rememberChartStyle import com.patrykandpatrick.vico.sample.showcase.rememberMarker @Composable -internal fun Chart9(uiSystem: UISystem, chartEntryModelProducer: ChartEntryModelProducer) { +internal fun Chart9(uiSystem: UISystem, modelProducer: CartesianChartModelProducer) { when (uiSystem) { - UISystem.Compose -> ComposeChart9(chartEntryModelProducer) - UISystem.Views -> ViewChart9(chartEntryModelProducer) + UISystem.Compose -> ComposeChart9(modelProducer) + UISystem.Views -> ViewChart9(modelProducer) } } @Composable -private fun ComposeChart9(chartEntryModelProducer: ChartEntryModelProducer) { +private fun ComposeChart9(modelProducer: CartesianChartModelProducer) { val marker = rememberMarker() ProvideChartStyle(rememberChartStyle(chartColors)) { - Chart( + CartesianChartHost( chart = - lineChart( - lines = - listOf( - lineSpec( - lineShader = DynamicShaders.split(chartColors[0], chartColors[1]), - lineBackgroundShader = - DynamicShaders.splitShader( - DynamicShaders.composeShader( - DynamicShaders.fromComponent( - componentSize = 6.dp, - component = - shapeComponent( - shape = Shapes.pillShape, - color = chartColors[0], - margins = remember { dimensionsOf(1.dp) } - ) - ), - verticalGradient( - arrayOf(Color.Black, Color.Transparent) - ), - PorterDuff.Mode.DST_IN + rememberCartesianChart( + rememberLineCartesianLayer( + lines = + listOf( + lineSpec( + shader = + TopBottomShader( + DynamicShaders.color(chartColors[0]), + DynamicShaders.color(chartColors[1]) ), - DynamicShaders.composeShader( - DynamicShaders.fromComponent( - componentSize = 5.dp, - component = - shapeComponent( - shape = Shapes.rectShape, - color = chartColors[1], - margins = - remember { - dimensionsOf(horizontal = 2.dp) - } - ), - checkeredArrangement = false - ), - verticalGradient( - arrayOf(Color.Transparent, Color.Black) + backgroundShader = + TopBottomShader( + DynamicShaders.composeShader( + DynamicShaders.fromComponent( + componentSize = 6.dp, + component = + shapeComponent( + shape = Shapes.pillShape, + color = chartColors[0], + margins = + remember { dimensionsOf(1.dp) } + ) + ), + verticalGradient( + arrayOf(Color.Black, Color.Transparent) + ), + PorterDuff.Mode.DST_IN ), - PorterDuff.Mode.DST_IN + DynamicShaders.composeShader( + DynamicShaders.fromComponent( + componentSize = 5.dp, + component = + shapeComponent( + shape = Shapes.rectShape, + color = chartColors[1], + margins = + remember { + dimensionsOf(horizontal = 2.dp) + } + ), + checkeredArrangement = false + ), + verticalGradient( + arrayOf(Color.Transparent, Color.Black) + ), + PorterDuff.Mode.DST_IN + ) ) - ) + ) ) - ) + ) ), - chartModelProducer = chartEntryModelProducer, + modelProducer = modelProducer, startAxis = rememberStartAxis( label = @@ -151,7 +159,13 @@ private fun ComposeChart9(chartEntryModelProducer: ChartEntryModelProducer) { bottomAxis = rememberBottomAxis( guideline = null, - itemPlacer = AxisItemPlacer.Horizontal.default(spacing = 3) + itemPlacer = + remember { + AxisItemPlacer.Horizontal.default( + spacing = 3, + addExtremeLabelPadding = true + ) + } ), marker = marker, runInitialAnimation = false, @@ -161,22 +175,26 @@ private fun ComposeChart9(chartEntryModelProducer: ChartEntryModelProducer) { } @Composable -private fun ViewChart9(chartEntryModelProducer: ChartEntryModelProducer) { +private fun ViewChart9(modelProducer: CartesianChartModelProducer) { val marker = rememberMarker() val colors = chartColors AndroidViewBinding(Chart9Binding::inflate) { with(chartView) { runInitialAnimation = false - entryProducer = chartEntryModelProducer + this.modelProducer = modelProducer (bottomAxis as Axis).guideline = null this.marker = marker - with(chart as LineChart) { + with(chart?.layers?.get(0) as LineCartesianLayer) { lines = listOf( - LineChart.LineSpec( - lineShader = DynamicShaders.split(colors[0], colors[1]), - lineBackgroundShader = - DynamicShaders.splitShader( + LineCartesianLayer.LineSpec( + shader = + TopBottomShader( + DynamicShaders.color(colors[0]), + DynamicShaders.color(colors[1]) + ), + backgroundShader = + TopBottomShader( DynamicShaders.composeShader( DynamicShaders.fromComponent( componentSize = 6.dp, diff --git a/sample/src/main/res/layout/chart_1.xml b/sample/src/main/res/layout/chart_1.xml index d725c7366..d19263639 100644 --- a/sample/src/main/res/layout/chart_1.xml +++ b/sample/src/main/res/layout/chart_1.xml @@ -19,13 +19,12 @@ android:layout_width="wrap_content" android:layout_height="wrap_content"> - diff --git a/sample/src/main/res/layout/chart_2.xml b/sample/src/main/res/layout/chart_2.xml index 27dab5533..02cccd055 100644 --- a/sample/src/main/res/layout/chart_2.xml +++ b/sample/src/main/res/layout/chart_2.xml @@ -19,13 +19,13 @@ android:layout_width="wrap_content" android:layout_height="wrap_content"> - - diff --git a/sample/src/main/res/layout/chart_4.xml b/sample/src/main/res/layout/chart_4.xml index ff8308348..021fe85d1 100644 --- a/sample/src/main/res/layout/chart_4.xml +++ b/sample/src/main/res/layout/chart_4.xml @@ -19,13 +19,13 @@ android:layout_width="wrap_content" android:layout_height="wrap_content"> - diff --git a/sample/src/main/res/layout/chart_5.xml b/sample/src/main/res/layout/chart_5.xml index 1aaf69d00..9f899f989 100644 --- a/sample/src/main/res/layout/chart_5.xml +++ b/sample/src/main/res/layout/chart_5.xml @@ -19,13 +19,13 @@ android:layout_width="wrap_content" android:layout_height="wrap_content"> - diff --git a/sample/src/main/res/layout/chart_6.xml b/sample/src/main/res/layout/chart_6.xml index 62426d299..925e53091 100644 --- a/sample/src/main/res/layout/chart_6.xml +++ b/sample/src/main/res/layout/chart_6.xml @@ -19,12 +19,12 @@ android:layout_width="wrap_content" android:layout_height="wrap_content"> - diff --git a/sample/src/main/res/layout/chart_7.xml b/sample/src/main/res/layout/chart_7.xml index f29e9408b..8961b7eba 100644 --- a/sample/src/main/res/layout/chart_7.xml +++ b/sample/src/main/res/layout/chart_7.xml @@ -19,12 +19,12 @@ android:layout_width="wrap_content" android:layout_height="wrap_content"> - diff --git a/sample/src/main/res/layout/chart_8.xml b/sample/src/main/res/layout/chart_8.xml index 79618a81d..7afc46bba 100644 --- a/sample/src/main/res/layout/chart_8.xml +++ b/sample/src/main/res/layout/chart_8.xml @@ -19,13 +19,13 @@ android:layout_width="wrap_content" android:layout_height="wrap_content"> - diff --git a/sample/src/main/res/layout/chart_9.xml b/sample/src/main/res/layout/chart_9.xml index 81a618baa..3356c4811 100644 --- a/sample/src/main/res/layout/chart_9.xml +++ b/sample/src/main/res/layout/chart_9.xml @@ -19,20 +19,18 @@ android:layout_width="wrap_content" android:layout_height="wrap_content"> - diff --git a/sample/src/main/res/values-night/chart_9_styles.xml b/sample/src/main/res/values-night/chart_9_styles.xml index 5097869de..240d9d53f 100644 --- a/sample/src/main/res/values-night/chart_9_styles.xml +++ b/sample/src/main/res/values-night/chart_9_styles.xml @@ -15,6 +15,6 @@ --> - #C7FF6C - #A46CFF + #735cff + #ff337d diff --git a/sample/src/main/res/values/chart_5_styles.xml b/sample/src/main/res/values/chart_5_styles.xml index 35150e750..2fd5ff54a 100644 --- a/sample/src/main/res/values/chart_5_styles.xml +++ b/sample/src/main/res/values/chart_5_styles.xml @@ -60,5 +60,6 @@ @style/Chart5Column1Style @style/Chart5Column2Style @style/Chart5Column3Style + stacked diff --git a/sample/src/main/res/values/chart_8_styles.xml b/sample/src/main/res/values/chart_8_styles.xml index d93f61daf..32466e71a 100644 --- a/sample/src/main/res/values/chart_8_styles.xml +++ b/sample/src/main/res/values/chart_8_styles.xml @@ -40,6 +40,7 @@ @style/Chart8Column1Style @style/Chart8Column2Style @style/Chart8Column3Style + stacked