From 91a8e16855681600b87cb6c2cfe23e831e4bc864 Mon Sep 17 00:00:00 2001 From: Patrick Michalik <120058021+patrickmichalik@users.noreply.github.com> Date: Tue, 28 Nov 2023 12:30:04 +0100 Subject: [PATCH] Rework selected core APIs (WIP) Co-authored-by: Patryk Goworowski --- .../vico/sample/previews/AxisLinePreviews.kt | 30 +- .../ColumnChartsWithNegativeValuesPreviews.kt | 46 +-- .../sample/previews/ContainedChartsPreview.kt | 72 ++-- .../vico/sample/previews/LineChartPreviews.kt | 122 ++++--- .../LineChartsWithNegativeValuesPreviews.kt | 65 ++-- .../vico/sample/previews/Previews.kt | 68 ++-- ...dColumnChartsWithNegativeValuesPreviews.kt | 73 ++-- .../sample/previews/ThresholdLinePreviews.kt | 71 ++-- .../composables/column/ColumnCharts.kt | 27 +- .../previews/composables/line/LineCharts.kt | 23 +- .../sample/previews/resource/SampleModels.kt | 15 +- .../vico/sample/showcase/ChartStyle.kt | 13 +- .../vico/sample/showcase/ShowcaseScreen.kt | 22 +- .../vico/sample/showcase/ShowcaseViewModel.kt | 68 ++-- .../vico/sample/showcase/charts/Chart1.kt | 30 +- .../vico/sample/showcase/charts/Chart2.kt | 37 +- .../vico/sample/showcase/charts/Chart3.kt | 38 +- .../vico/sample/showcase/charts/Chart4.kt | 59 ++- .../vico/sample/showcase/charts/Chart5.kt | 69 ++-- .../vico/sample/showcase/charts/Chart6.kt | 53 +-- .../vico/sample/showcase/charts/Chart7.kt | 35 +- .../vico/sample/showcase/charts/Chart8.kt | 50 ++- .../vico/sample/showcase/charts/Chart9.kt | 99 ++--- 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 +- .../{Charts.kt => CartesianChartHost.kt} | 116 +++--- .../compose/chart/cartesian/CartesianChart.kt | 37 ++ ...ColumnChart.kt => ColumnCartesianLayer.kt} | 67 ++-- .../chart/entry/ChartEntryModelExtensions.kt | 51 ++- .../compose/chart/line/LineCartesianLayer.kt | 180 +++++++++ .../vico/compose/chart/line/LineChart.kt | 238 ------------ .../compose/chart/scroll/ChartScrollSpec.kt | 18 +- .../compose/chart/scroll/ChartScrollState.kt | 4 +- .../component/shape/shader/DynamicShaders.kt | 15 +- .../layout/MeasureContextExtensions.kt | 9 +- .../state/CartesianChartModelWrapper.kt | 64 ++++ .../compose/state/ChartEntryModelWrapper.kt | 65 ---- .../vico/compose/style/ChartStyle.kt | 32 +- .../vico/core/axis/AxisItemPlacer.kt | 15 +- .../vico/core/axis/AxisManager.kt | 12 +- .../vico/core/axis/AxisPosition.kt | 30 +- .../vico/core/axis/AxisRenderer.kt | 11 +- .../DefaultHorizontalAxisItemPlacer.kt | 48 ++- .../core/axis/horizontal/HorizontalAxis.kt | 15 +- .../vertical/DefaultVerticalAxisItemPlacer.kt | 34 +- .../vico/core/axis/vertical/VerticalAxis.kt | 26 +- .../vico/core/chart/BaseCartesianLayer.kt | 53 +++ .../vico/core/chart/BaseChart.kt | 139 ------- .../vico/core/chart/CartesianLayer.kt | 74 ++++ .../patrykandpatrick/vico/core/chart/Chart.kt | 213 ----------- .../vico/core/chart/ChartExtensions.kt | 4 +- .../vico/core/chart/DefaultPointConnector.kt | 8 +- .../vico/core/chart/EntryModelExtensions.kt | 56 +-- .../vico/core/chart/LineSpecExtensions.kt | 26 +- ...ColumnChart.kt => ColumnCartesianLayer.kt} | 183 +++++----- ...kt => ColumnCartesianLayerDrawingModel.kt} | 14 +- .../chart/column/ColumnCartesianLayerModel.kt | 180 +++++++++ .../core/chart/composed/CartesianChart.kt | 241 ++++++++++++ .../chart/composed/CartesianChartModel.kt | 105 ++++++ .../vico/core/chart/composed/ComposedChart.kt | 169 --------- .../chart/composed/ComposedChartEntryModel.kt | 30 -- .../chart/composed/ComposedChartExtensions.kt | 44 --- .../vico/core/chart/decoration/Decoration.kt | 18 +- .../core/chart/decoration/ThresholdLine.kt | 19 +- .../chart/dimensions/HorizontalDimensions.kt | 11 +- .../vico/core/chart/draw/ChartDrawContext.kt | 12 +- .../chart/draw/ChartDrawContextExtensions.kt | 15 +- .../vico/core/chart/insets/ChartInsetter.kt | 14 +- .../core/chart/insets/HorizontalInsets.kt | 6 +- .../core/chart/layout/HorizontalLayout.kt | 18 +- .../{LineChart.kt => LineCartesianLayer.kt} | 322 +++++++--------- ...l.kt => LineCartesianLayerDrawingModel.kt} | 21 +- ...ons.kt => LineCartesianLayerExtensions.kt} | 4 +- .../chart/line/LineCartesianLayerModel.kt | 151 ++++++++ ...luesOverrider.kt => AxisValueOverrider.kt} | 39 +- .../vico/core/chart/values/ChartValues.kt | 78 ++-- .../core/chart/values/ChartValuesManager.kt | 107 ------ .../core/chart/values/ChartValuesProvider.kt | 39 -- .../core/chart/values/MutableChartValues.kt | 92 +++-- .../core/component/marker/MarkerComponent.kt | 5 +- .../component/shape/shader/ColorShader.kt | 40 ++ .../component/shape/shader/DynamicShader.kt | 44 +-- .../component/shape/shader/DynamicShaders.kt | 13 +- .../shape/shader/HorizontalSplitShader.kt | 227 ------------ .../component/shape/shader/SolidShader.kt | 79 ---- .../component/shape/shader/TopBottomShader.kt | 75 ++++ .../vico/core/context/MeasureContext.kt | 7 +- .../core/context/MutableMeasureContext.kt | 4 +- .../vico/core/draw/DrawContextExtensions.kt | 4 +- .../vico/core/entry/CartesianLayerModel.kt | 86 +++++ .../vico/core/entry/ChartEntry.kt | 51 --- .../vico/core/entry/ChartEntryExtensions.kt | 82 +---- .../vico/core/entry/ChartEntryModel.kt | 104 ------ .../core/entry/ChartEntryModelProducer.kt | 233 ------------ .../vico/core/entry/ChartModelProducer.kt | 77 ---- .../vico/core/entry/EntryListExtensions.kt | 48 --- .../vico/core/entry/FloatEntry.kt | 31 -- .../composed/CartesianChartModelProducer.kt | 229 ++++++++++++ .../ComposedChartEntryModelProducer.kt | 343 ------------------ .../composed/ComposedEntryListExtensions.kt | 35 -- .../vico/core/entry/diff/DrawingModel.kt | 6 +- .../vico/core/extension/MapExtensions.kt | 7 +- .../vico/core/extension/NumberExtensions.kt | 5 + .../formatter/DecimalFormatValueFormatter.kt | 4 +- .../core/formatter/DefaultValueFormatter.kt | 4 +- .../PercentageFormatValueFormatter.kt | 6 +- .../vico/core/formatter/ValueFormatter.kt | 4 +- .../vico/core/layout/VirtualLayout.kt | 7 +- .../marker/DefaultMarkerLabelFormatter.kt | 10 + .../vico/core/marker/Marker.kt | 18 +- .../vico/core/scroll/AutoScrollCondition.kt | 25 +- .../util/RandomCartesianModelGenerator.kt | 71 ++++ .../vico/core/util/RandomEntriesGenerator.kt | 80 ---- .../vico/core/ChartEntryModelProducerTest.kt | 44 --- ...BaseChartView.kt => CartesianChartView.kt} | 132 ++++--- .../vico/views/chart/ChartView.kt | 63 ---- .../vico/views/chart/ComposedChartView.kt | 68 ---- .../chart/column/ColumnChartExtensions.kt | 21 +- .../views/chart/line/LineChartExtensions.kt | 23 +- .../component/shape/shader/DynamicShaders.kt | 17 - .../vico/views/scroll/ChartScrollSpec.kt | 18 +- .../vico/views/theme/ChartStyleExtensions.kt | 62 ++-- .../views/theme/ComponentStyleExtensions.kt | 20 +- .../vico/views/theme/ThemeHandler.kt | 136 +++---- 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, 3181 insertions(+), 4390 deletions(-) rename vico/compose/src/main/java/com/patrykandpatrick/vico/compose/chart/{Charts.kt => CartesianChartHost.kt} (84%) create mode 100644 vico/compose/src/main/java/com/patrykandpatrick/vico/compose/chart/cartesian/CartesianChart.kt rename vico/compose/src/main/java/com/patrykandpatrick/vico/compose/chart/column/{ColumnChart.kt => ColumnCartesianLayer.kt} (54%) 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/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} (67%) create mode 100644 vico/core/src/main/java/com/patrykandpatrick/vico/core/chart/column/ColumnCartesianLayerModel.kt create mode 100644 vico/core/src/main/java/com/patrykandpatrick/vico/core/chart/composed/CartesianChart.kt create mode 100644 vico/core/src/main/java/com/patrykandpatrick/vico/core/chart/composed/CartesianChartModel.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} (63%) rename vico/core/src/main/java/com/patrykandpatrick/vico/core/chart/line/{LineChartDrawingModel.kt => LineCartesianLayerDrawingModel.kt} (63%) rename vico/core/src/main/java/com/patrykandpatrick/vico/core/chart/line/{LineChartExtensions.kt => LineCartesianLayerExtensions.kt} (89%) 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} (66%) 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} (82%) 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 2751f9203..5dc6bd7f7 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.cartesian.rememberCartesianChart +import com.patrykandpatrick.vico.compose.chart.column.rememberColumnCartesianLayer import com.patrykandpatrick.vico.compose.component.lineComponent import com.patrykandpatrick.vico.compose.component.shapeComponent import com.patrykandpatrick.vico.compose.style.LocalChartStyle import com.patrykandpatrick.vico.core.axis.vertical.VerticalAxis +import com.patrykandpatrick.vico.core.chart.column.ColumnCartesianLayerModel +import com.patrykandpatrick.vico.core.chart.composed.CartesianChartModel import com.patrykandpatrick.vico.core.component.shape.Shapes import com.patrykandpatrick.vico.core.component.shape.cornered.Corner import com.patrykandpatrick.vico.core.component.shape.cornered.CorneredShape import com.patrykandpatrick.vico.core.component.shape.cornered.CutCornerTreatment import com.patrykandpatrick.vico.core.component.shape.cornered.RoundedCornerTreatment -import com.patrykandpatrick.vico.core.entry.entryModelOf -private val model = entryModelOf(1, 2, 3, 4) +private val model = CartesianChartModel(ColumnCartesianLayerModel.build { series(1, 2, 3, 4) }) @Composable private fun ProvidePreviewChartStyle(content: @Composable () -> Unit) { @@ -48,8 +50,8 @@ private fun ProvidePreviewChartStyle(content: @Composable () -> Unit) { axisLineColor = Color.Black.copy(alpha = 0.5f), axisGuidelineColor = Color.Black.copy(alpha = 0.2f), ), - columnChart = LocalChartStyle.current.columnChart.copy( - columns = LocalChartStyle.current.columnChart.columns.map { + columnLayer = LocalChartStyle.current.columnLayer.copy( + columns = LocalChartStyle.current.columnLayer.columns.map { lineComponent( color = Color.Gray, thickness = it.thicknessDp.dp, @@ -88,8 +90,8 @@ public fun HorizontalAxisTextInside() { verticalMargin = 4.dp, horizontalMargin = 4.dp, ) - Chart( - chart = columnChart(), + CartesianChartHost( + chart = rememberCartesianChart(rememberColumnCartesianLayer()), model = model, startAxis = rememberStartAxis( horizontalLabelPosition = VerticalAxis.HorizontalLabelPosition.Inside, @@ -118,8 +120,8 @@ public fun HorizontalAxisTextInsideAndBottomAxis() { verticalMargin = 4.dp, horizontalMargin = 4.dp, ) - Chart( - chart = columnChart(), + CartesianChartHost( + chart = rememberCartesianChart(rememberColumnCartesianLayer()), model = model, startAxis = rememberStartAxis( horizontalLabelPosition = VerticalAxis.HorizontalLabelPosition.Inside, @@ -139,8 +141,8 @@ public fun HorizontalAxisTextInsideAndBottomAxis() { @Preview(showBackground = true, widthDp = 250) public fun HorizontalAxisTextOutside() { ProvidePreviewChartStyle { - Chart( - chart = columnChart(), + CartesianChartHost( + chart = rememberCartesianChart(rememberColumnCartesianLayer()), model = model, startAxis = rememberStartAxis( horizontalLabelPosition = VerticalAxis.HorizontalLabelPosition.Outside, @@ -157,8 +159,8 @@ public fun HorizontalAxisTextOutside() { @Preview(showBackground = true, widthDp = 250) public fun HorizontalAxisGuidelineDoesNotOverlayBottomAxisLine() { ProvidePreviewChartStyle { - Chart( - chart = columnChart(), + CartesianChartHost( + chart = rememberCartesianChart(rememberColumnCartesianLayer()), model = model, startAxis = rememberStartAxis( horizontalLabelPosition = VerticalAxis.HorizontalLabelPosition.Outside, 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 6c92f521d..0702615ce 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,28 +25,28 @@ import androidx.compose.ui.tooling.preview.Preview import androidx.compose.ui.unit.dp import com.patrykandpatrick.vico.compose.axis.horizontal.rememberBottomAxis import com.patrykandpatrick.vico.compose.axis.vertical.rememberStartAxis -import com.patrykandpatrick.vico.compose.chart.Chart -import com.patrykandpatrick.vico.compose.chart.column.columnChart +import com.patrykandpatrick.vico.compose.chart.CartesianChartHost +import com.patrykandpatrick.vico.compose.chart.cartesian.rememberCartesianChart +import com.patrykandpatrick.vico.compose.chart.column.rememberColumnCartesianLayer import com.patrykandpatrick.vico.core.axis.AxisItemPlacer -import com.patrykandpatrick.vico.core.chart.values.AxisValuesOverrider +import com.patrykandpatrick.vico.core.chart.composed.CartesianChartModel +import com.patrykandpatrick.vico.core.chart.line.LineCartesianLayerModel +import com.patrykandpatrick.vico.core.chart.values.AxisValueOverrider import com.patrykandpatrick.vico.core.component.text.textComponent -import com.patrykandpatrick.vico.core.entry.entryModelOf import com.patrykandpatrick.vico.sample.showcase.rememberMarker -private val model = entryModelOf(2f, -1f, 4f, -2f, 1f, 5f, -3f) +private val model = CartesianChartModel(LineCartesianLayerModel.build { series(2f, -1f, 4f, -2f, 1f, 5f, -3f) }) @Preview @Composable public fun SingleColumnChartWithNegativeValues() { val marker = rememberMarker() Surface { - Chart( + CartesianChartHost( modifier = Modifier.height(250.dp), - chart = columnChart( - persistentMarkers = mapOf( - 2f to marker, - 3f to marker, - ), + chart = rememberCartesianChart( + rememberColumnCartesianLayer(), + persistentMarkers = mapOf(2f to marker, 3f to marker), ), model = model, startAxis = rememberStartAxis(itemPlacer = remember { AxisItemPlacer.Vertical.default(maxItemCount = 9) }), @@ -59,10 +59,8 @@ public fun SingleColumnChartWithNegativeValues() { @Composable public fun SingleColumnChartWithNegativeValuesAndDataLabels() { Surface { - Chart( - chart = columnChart( - dataLabel = textComponent(), - ), + CartesianChartHost( + chart = rememberCartesianChart(rememberColumnCartesianLayer(dataLabel = textComponent())), model = model, startAxis = rememberStartAxis(), bottomAxis = rememberBottomAxis(), @@ -74,12 +72,9 @@ public fun SingleColumnChartWithNegativeValuesAndDataLabels() { @Composable public fun SingleColumnChartWithNegativeValuesAndAxisValuesOverridden() { Surface { - Chart( - chart = columnChart( - axisValuesOverrider = AxisValuesOverrider.fixed( - minY = 1f, - maxY = 4f, - ), + CartesianChartHost( + chart = rememberCartesianChart( + rememberColumnCartesianLayer(axisValueOverrider = AxisValueOverrider.fixed(minY = 1f, maxY = 4f)), ), model = model, startAxis = rememberStartAxis(itemPlacer = remember { AxisItemPlacer.Vertical.default(maxItemCount = 4) }), @@ -92,12 +87,9 @@ public fun SingleColumnChartWithNegativeValuesAndAxisValuesOverridden() { @Composable public fun SingleColumnChartWithNegativeValuesAndAxisValuesOverridden2() { Surface { - Chart( - chart = columnChart( - axisValuesOverrider = AxisValuesOverrider.fixed( - minY = -2f, - maxY = 0f, - ), + CartesianChartHost( + chart = rememberCartesianChart( + rememberColumnCartesianLayer(axisValueOverrider = AxisValueOverrider.fixed(minY = -2f, maxY = 0f)), ), model = model, startAxis = rememberStartAxis(itemPlacer = remember { AxisItemPlacer.Vertical.default(maxItemCount = 3) }), 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 7f603b641..131a0d3e8 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.cartesian.rememberCartesianChart +import com.patrykandpatrick.vico.compose.chart.column.rememberColumnCartesianLayer import com.patrykandpatrick.vico.compose.chart.line.lineSpec +import com.patrykandpatrick.vico.compose.chart.line.rememberLineCartesianLayer import com.patrykandpatrick.vico.compose.component.lineComponent +import com.patrykandpatrick.vico.compose.component.shape.shader.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,26 +39,24 @@ import com.patrykandpatrick.vico.core.axis.AxisItemPlacer import com.patrykandpatrick.vico.core.axis.AxisPosition.Vertical import com.patrykandpatrick.vico.core.axis.AxisPosition.Vertical.End import com.patrykandpatrick.vico.core.axis.AxisPosition.Vertical.Start -import com.patrykandpatrick.vico.core.chart.column.ColumnChart -import com.patrykandpatrick.vico.core.chart.composed.plus -import com.patrykandpatrick.vico.core.chart.line.LineChart +import com.patrykandpatrick.vico.core.chart.column.ColumnCartesianLayerModel +import com.patrykandpatrick.vico.core.chart.composed.CartesianChartModel +import com.patrykandpatrick.vico.core.chart.line.LineCartesianLayerModel import com.patrykandpatrick.vico.core.component.shape.Shapes -import com.patrykandpatrick.vico.core.entry.composed.plus -import com.patrykandpatrick.vico.core.entry.entryModelOf +import com.patrykandpatrick.vico.core.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, @@ -64,24 +64,19 @@ private fun getColumnChart( 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 = verticalGradient( + shader = DynamicShaders.color(Color.DarkGray), + backgroundShader = verticalGradient( arrayOf(Color.DarkGray, Color.DarkGray.copy(alpha = 0f)), ), ), ), - persistentMarkers = markerMap, targetVerticalAxisPosition = targetVerticalAxisPosition, ) @@ -100,14 +95,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, @@ -118,13 +108,9 @@ public fun ChartWithIndependentAxes(modifier: Modifier = Modifier) { @Composable @Preview("Chart with dependent axes", widthDp = 350) public fun ChartWithDependentAxes(modifier: Modifier = Modifier) { - val composedChart = getColumnChart() + getLineChart() - - composedChart.setPersistentMarkers(markerMap) - - Chart( - chart = composedChart, - model = model1 + model2, + CartesianChartHost( + chart = rememberCartesianChart(getColumnLayer(), getLineLayer(), persistentMarkers = markerMap), + model = model, startAxis = startAxis, bottomAxis = rememberBottomAxis(), endAxis = endAxis, @@ -135,9 +121,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, @@ -147,9 +133,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 4dc2d74b7..56dbd11f4 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,28 @@ 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.cartesian.rememberCartesianChart import com.patrykandpatrick.vico.compose.chart.line.lineSpec +import com.patrykandpatrick.vico.compose.chart.line.rememberLineCartesianLayer +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.composed.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 @@ -49,24 +58,26 @@ public fun LineChartDark() { val yellow = Color(0xFFFFAA4A) val pink = Color(0xFFFF4AAA) - Chart( + CartesianChartHost( modifier = Modifier.padding(8.dp), - chart = lineChart( - lines = listOf( - lineSpec( - lineColor = yellow, - lineBackgroundShader = verticalGradient( - arrayOf(yellow.copy(0.5f), yellow.copy(alpha = 0f)), + chart = rememberCartesianChart( + rememberLineCartesianLayer( + listOf( + lineSpec( + shader = DynamicShaders.color(yellow), + backgroundShader = verticalGradient( + arrayOf(yellow.copy(alpha = 0.5f), yellow.copy(alpha = 0f)), + ), ), - ), - lineSpec( - lineColor = pink, - lineBackgroundShader = verticalGradient( - arrayOf(pink.copy(0.5f), pink.copy(alpha = 0f)), + lineSpec( + shader = DynamicShaders.color(pink), + backgroundShader = verticalGradient( + arrayOf(pink.copy(alpha = 0.5f), pink.copy(alpha = 0f)), + ), ), ), + axisValueOverrider = AxisValueOverrider.fixed(maxY = 4f), ), - axisValuesOverrider = AxisValuesOverrider.fixed(maxY = 4f), ), model = model3, ) @@ -76,8 +87,8 @@ public fun LineChartDark() { @Preview("Line Chart", widthDp = 200) @Composable public fun RegularLineChart() { - Chart( - chart = lineChart(), + CartesianChartHost( + chart = rememberCartesianChart(rememberLineCartesianLayer()), model = model1, startAxis = rememberStartAxis(), ) @@ -86,12 +97,9 @@ public fun RegularLineChart() { @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(), @@ -101,12 +109,9 @@ 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(), @@ -116,21 +121,21 @@ public fun RegularLineChartCollapsed() { @Preview("Composed Chart", widthDp = 200) @Composable public fun ComposedLineChart() { - Chart( - chart = lineChart() + lineChart( - lines = listOf( - lineSpec( - lineColor = Color.Blue, - lineBackgroundShader = verticalGradient( - colors = arrayOf( - Color.Blue.copy(alpha = 0.4f), - Color.Blue.copy(alpha = 0f), + CartesianChartHost( + chart = 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(), ) } @@ -138,19 +143,22 @@ public fun ComposedLineChart() { @Preview("Composed Chart Collapsed", widthDp = 200) @Composable public fun ComposedLineChartCollapsed() { - Chart( - chart = lineChart( - axisValuesOverrider = AxisValuesOverrider.fixed( - minY = 1f, - maxY = 3f, + CartesianChartHost( + chart = rememberCartesianChart( + rememberLineCartesianLayer( + axisValueOverrider = AxisValueOverrider.fixed( + minY = 1f, + maxY = 3f, + ), ), - ) + lineChart( - axisValuesOverrider = AxisValuesOverrider.fixed( - minY = 1f, - maxY = 3f, + rememberLineCartesianLayer( + axisValueOverrider = AxisValueOverrider.fixed( + minY = 1f, + maxY = 3f, + ), ), ), - model = model1 + model2, + 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 96e7e17ef..8a2c92530 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,42 +27,43 @@ 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.cartesian.rememberCartesianChart 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.component.shape.shader.color import com.patrykandpatrick.vico.compose.component.textComponent import com.patrykandpatrick.vico.core.axis.AxisItemPlacer +import com.patrykandpatrick.vico.core.chart.composed.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), + chart = rememberCartesianChart( + rememberLineCartesianLayer( + lines = listOf( + lineSpec( + shader = TopBottomShader( + DynamicShaders.color(Color(0xFF25BE53)), + DynamicShaders.color(Color(0xFFE73B3B)), + ), ), ), ), - persistentMarkers = mapOf( - 2f to marker, - 3f to marker, - ), + persistentMarkers = mapOf(2f to marker, 3f to marker), ), model = model, startAxis = rememberStartAxis( @@ -84,10 +85,12 @@ public fun SingleLineChartWithNegativeValues() { @Composable public fun SingleLineChartWithNegativeValuesAndDataLabels() { Surface { - Chart( - chart = lineChart( - lines = listOf( - lineSpec(lineShader = DynamicShaders.solid(Color.DarkGray), dataLabel = textComponent()), + CartesianChartHost( + chart = rememberCartesianChart( + rememberLineCartesianLayer( + lines = listOf( + lineSpec(shader = DynamicShaders.color(Color.DarkGray), dataLabel = textComponent()), + ), ), ), model = model, @@ -101,12 +104,9 @@ public fun SingleLineChartWithNegativeValuesAndDataLabels() { @Composable public fun SingleLineChartWithNegativeValuesAndAxisValuesOverridden() { Surface { - Chart( - chart = lineChart( - axisValuesOverrider = AxisValuesOverrider.fixed( - minY = 1f, - maxY = 4f, - ), + CartesianChartHost( + chart = rememberCartesianChart( + rememberLineCartesianLayer(axisValueOverrider = AxisValueOverrider.fixed(minY = 1f, maxY = 4f)), ), model = model, startAxis = rememberStartAxis(itemPlacer = remember { AxisItemPlacer.Vertical.default(maxItemCount = 4) }), @@ -119,12 +119,9 @@ public fun SingleLineChartWithNegativeValuesAndAxisValuesOverridden() { @Composable public fun SingleLineChartWithNegativeValuesAndAxisValuesOverridden2() { Surface { - Chart( - chart = lineChart( - axisValuesOverrider = AxisValuesOverrider.fixed( - minY = -2f, - maxY = 0f, - ), + CartesianChartHost( + chart = rememberCartesianChart( + rememberLineCartesianLayer(axisValueOverrider = AxisValueOverrider.fixed(minY = -2f, maxY = 0f)), ), model = model, startAxis = rememberStartAxis(itemPlacer = remember { AxisItemPlacer.Vertical.default(maxItemCount = 3) }), 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 68ee890e0..bef0d2f76 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.cartesian.rememberCartesianChart +import com.patrykandpatrick.vico.compose.chart.column.rememberColumnCartesianLayer import com.patrykandpatrick.vico.compose.chart.line.lineSpec +import com.patrykandpatrick.vico.compose.chart.line.rememberLineCartesianLayer import com.patrykandpatrick.vico.compose.component.lineComponent +import com.patrykandpatrick.vico.compose.component.shape.shader.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.column.ColumnCartesianLayerModel +import com.patrykandpatrick.vico.core.chart.composed.CartesianChartModel +import com.patrykandpatrick.vico.core.chart.line.LineCartesianLayerModel +import com.patrykandpatrick.vico.core.chart.values.AxisValueOverrider import com.patrykandpatrick.vico.core.component.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,15 +68,17 @@ public fun ColumnChartCard(): Unit = VicoTheme { val colors = MaterialTheme.colors SampleCard { - Chart( + CartesianChartHost( modifier = chartModifier, - chart = columnChart( - columns = listOf( - lineComponent( - colors.primary, - thickness = 8.dp, - shape = RoundedCornerShape(4.dp), - dynamicShader = verticalGradient(arrayOf(colors.primary, colors.secondary)), + chart = rememberCartesianChart( + rememberColumnCartesianLayer( + listOf( + lineComponent( + color = colors.primary, + thickness = 8.dp, + shape = RoundedCornerShape(4.dp), + dynamicShader = verticalGradient(arrayOf(colors.primary, colors.secondary)), + ), ), ), ), @@ -98,7 +104,7 @@ public fun ColumnChartCard(): Unit = VicoTheme { 1.dp.value, ) }, - model = @Suppress("MagicNumber") (entryModelOf(1, 2, 3, 2)), + model = CartesianChartModel(ColumnCartesianLayerModel.build { series(1, 2, 3, 2) }), ) } } @@ -109,27 +115,29 @@ public fun LineChartCard(): Unit = VicoTheme { val colors = MaterialTheme.colors SampleCard { - Chart( + CartesianChartHost( modifier = Modifier.height(100.dp), - chart = lineChart( - lines = listOf( - lineSpec( - point = null, - lineColor = colors.primary, - lineBackgroundShader = DynamicShaders.fromComponent( - componentSize = 4.dp, - component = shapeComponent(shape = pillShape, color = colors.primary).apply { - setMargins(0.5.dp.value) - }, + chart = rememberCartesianChart( + rememberLineCartesianLayer( + listOf( + lineSpec( + point = null, + shader = DynamicShaders.color(colors.primary), + backgroundShader = DynamicShaders.fromComponent( + componentSize = 4.dp, + component = shapeComponent(shape = pillShape, color = colors.primary) + .apply { setMargins(0.5.dp.value) }, + ), ), ), + axisValueOverrider = AxisValueOverrider.fixed(minX = 0f, maxY = 3f), ), - axisValuesOverrider = AxisValuesOverrider.fixed( - minX = 0f, - maxY = 3f, - ), ), - model = entryModelOf(-1 to 0, 0 to 0, 1 to 1, 2 to 2, 3 to 0, 4 to 2, 5 to 1), + model = CartesianChartModel( + LineCartesianLayerModel.build { + series(x = listOf(-1, 0, 1, 2, 3, 4, 5), y = listOf(0, 0, 1, 2, 0, 2, 1)) + }, + ), startAxis = createVerticalAxis { label = textComponent( color = colors.onSurface, 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 e893f7acd..84a3e6c67 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,22 +26,25 @@ import androidx.compose.ui.tooling.preview.Preview import androidx.compose.ui.unit.dp import com.patrykandpatrick.vico.compose.axis.horizontal.rememberBottomAxis import com.patrykandpatrick.vico.compose.axis.vertical.rememberStartAxis -import com.patrykandpatrick.vico.compose.chart.Chart -import com.patrykandpatrick.vico.compose.chart.column.columnChart +import com.patrykandpatrick.vico.compose.chart.CartesianChartHost +import com.patrykandpatrick.vico.compose.chart.cartesian.rememberCartesianChart +import com.patrykandpatrick.vico.compose.chart.column.rememberColumnCartesianLayer import com.patrykandpatrick.vico.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.column.ColumnCartesianLayer +import com.patrykandpatrick.vico.core.chart.column.ColumnCartesianLayerModel +import com.patrykandpatrick.vico.core.chart.composed.CartesianChartModel +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), +private val model = 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 @@ -57,15 +60,11 @@ 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, + chart = rememberCartesianChart( + rememberColumnCartesianLayer(columns = columns, mergeMode = ColumnCartesianLayer.MergeMode.Stacked), + persistentMarkers = mapOf(2f to marker, 3f to marker), ), model = model, startAxis = rememberStartAxis(itemPlacer = remember { AxisItemPlacer.Vertical.default(maxItemCount = 8) }), @@ -78,11 +77,13 @@ 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(itemPlacer = remember { AxisItemPlacer.Vertical.default(maxItemCount = 8) }), @@ -95,14 +96,13 @@ public fun StackedColumnChartWithNegativeValuesAndDataLabels() { @Composable public fun StackedColumnChartWithNegativeValuesAndAxisValuesOverridden() { Surface { - Chart( - chart = columnChart( - columns = columns, - axisValuesOverrider = AxisValuesOverrider.fixed( - minY = 1f, - maxY = 4f, + CartesianChartHost( + chart = rememberCartesianChart( + rememberColumnCartesianLayer( + columns = columns, + axisValueOverrider = AxisValueOverrider.fixed(minY = 1f, maxY = 4f), + mergeMode = ColumnCartesianLayer.MergeMode.Stacked, ), - mergeMode = Stack, ), model = model, startAxis = rememberStartAxis(itemPlacer = remember { AxisItemPlacer.Vertical.default(maxItemCount = 4) }), @@ -115,14 +115,13 @@ public fun StackedColumnChartWithNegativeValuesAndAxisValuesOverridden() { @Composable public fun StackedColumnChartWithNegativeValuesAndAxisValuesOverridden2() { Surface { - Chart( - chart = columnChart( - columns = columns, - axisValuesOverrider = AxisValuesOverrider.fixed( - minY = -2f, - maxY = 0f, + CartesianChartHost( + chart = rememberCartesianChart( + rememberColumnCartesianLayer( + columns = columns, + axisValueOverrider = AxisValueOverrider.fixed(minY = -2f, maxY = 0f), + mergeMode = ColumnCartesianLayer.MergeMode.Stacked, ), - mergeMode = Stack, ), model = model, startAxis = rememberStartAxis(itemPlacer = remember { AxisItemPlacer.Vertical.default(maxItemCount = 3) }), 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 f4c25ac82..065be0afe 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.cartesian.rememberCartesianChart +import com.patrykandpatrick.vico.compose.chart.column.rememberColumnCartesianLayer 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.column.ColumnCartesianLayerModel +import com.patrykandpatrick.vico.core.chart.composed.CartesianChartModel 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) @@ -57,8 +59,8 @@ private fun ProvidePreviewChartStyle(content: @Composable () -> Unit) { axisTickColor = Color.DimmedGray, axisGuidelineColor = Color.DimmedGray, ), - columnChart = LocalChartStyle.current.columnChart.copy( - columns = LocalChartStyle.current.columnChart.columns.map { + columnLayer = LocalChartStyle.current.columnLayer.copy( + columns = LocalChartStyle.current.columnLayer.columns.map { lineComponent( color = Color.DimmedGray, thickness = it.thicknessDp.dp, @@ -83,17 +85,18 @@ private fun ProvidePreviewChartStyle(content: @Composable () -> Unit) { @Composable public fun ThresholdLine() { ProvidePreviewChartStyle { - Chart( + CartesianChartHost( modifier = Modifier, - chart = columnChart().apply { - addDecoration( + chart = 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(), @@ -106,10 +109,11 @@ public fun ThresholdLine() { @Composable public fun ThresholdLineWithCustomText() { ProvidePreviewChartStyle { - Chart( + CartesianChartHost( modifier = Modifier, - chart = columnChart().apply { - addDecoration( + chart = rememberCartesianChart( + rememberColumnCartesianLayer(), + decorations = listOf( ThresholdLine( thresholdValue = 2f, thresholdLabel = "Threshold line 1 📐", @@ -131,8 +135,6 @@ public fun ThresholdLineWithCustomText() { ), labelVerticalPosition = ThresholdLine.LabelVerticalPosition.Bottom, ), - ) - addDecoration( ThresholdLine( thresholdValue = 3f, thresholdLabel = "Threshold line 2 📐", @@ -153,8 +155,8 @@ public fun ThresholdLineWithCustomText() { margins = dimensionsOf(horizontal = 4.dp), ), ), - ) - }, + ), + ), model = model, startAxis = rememberStartAxis(), bottomAxis = rememberBottomAxis(), @@ -167,17 +169,18 @@ public fun ThresholdLineWithCustomText() { @Composable public fun RangedThresholdLine() { ProvidePreviewChartStyle { - Chart( + CartesianChartHost( modifier = Modifier, - chart = columnChart().apply { - addDecoration( + chart = 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(), @@ -190,10 +193,11 @@ public fun RangedThresholdLine() { @Composable public fun RangedThresholdLineWithBrushShader() { ProvidePreviewChartStyle { - Chart( + CartesianChartHost( modifier = Modifier, - chart = columnChart().apply { - addDecoration( + chart = rememberCartesianChart( + rememberColumnCartesianLayer(), + decorations = listOf( ThresholdLine( thresholdRange = 2f..3f, lineComponent = shapeComponent( @@ -207,8 +211,8 @@ public fun RangedThresholdLineWithBrushShader() { ), labelComponent = textComponent(color = Color.Black, padding = dimensionsOf(horizontal = 8.dp)), ), - ) - }, + ), + ), model = model, startAxis = rememberStartAxis(), bottomAxis = rememberBottomAxis(), @@ -221,10 +225,11 @@ public fun RangedThresholdLineWithBrushShader() { @Composable public fun RangedThresholdLineWithComponentShader() { ProvidePreviewChartStyle { - Chart( + CartesianChartHost( modifier = Modifier, - chart = columnChart().apply { - addDecoration( + chart = rememberCartesianChart( + rememberColumnCartesianLayer(), + decorations = listOf( ThresholdLine( thresholdRange = 2f..3f, lineComponent = shapeComponent( @@ -238,8 +243,8 @@ public fun RangedThresholdLineWithComponentShader() { ), 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 6e3015af3..e396ec9bd 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.cartesian.rememberCartesianChart +import com.patrykandpatrick.vico.compose.chart.column.rememberColumnCartesianLayer import com.patrykandpatrick.vico.compose.chart.scroll.rememberChartScrollSpec -import com.patrykandpatrick.vico.core.entry.ChartEntryModel +import com.patrykandpatrick.vico.core.chart.composed.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(), @@ -58,17 +59,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 77eae7c41..e300d9516 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.cartesian.rememberCartesianChart +import com.patrykandpatrick.vico.compose.chart.line.rememberLineCartesianLayer import com.patrykandpatrick.vico.compose.chart.scroll.rememberChartScrollSpec -import com.patrykandpatrick.vico.core.entry.ChartEntryModel +import com.patrykandpatrick.vico.core.chart.composed.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(), @@ -50,17 +51,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..b5a79e686 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,16 @@ 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.column.ColumnCartesianLayerModel +import com.patrykandpatrick.vico.core.chart.composed.CartesianChartModel +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 ef22255b3..0df8c1784 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 @@ -43,7 +44,7 @@ internal fun rememberChartStyle(columnChartColors: List, lineChartColors: axisGuidelineColor = Color(defaultColors.axisGuidelineColor), axisLineColor = Color(defaultColors.axisLineColor), ), - ChartStyle.ColumnChart( + ChartStyle.ColumnLayer( columnChartColors.map { columnChartColor -> LineComponent( columnChartColor.toArgb(), @@ -52,11 +53,11 @@ internal fun rememberChartStyle(columnChartColors: List, lineChartColors: ) }, ), - ChartStyle.LineChart( + ChartStyle.LineLayer( lineChartColors.map { lineChartColor -> - LineChart.LineSpec( - lineColor = lineChartColor.toArgb(), - lineBackgroundShader = DynamicShaders.fromBrush( + LineCartesianLayer.LineSpec( + shader = DynamicShaders.color(lineChartColor), + backgroundShader = DynamicShaders.fromBrush( Brush.verticalGradient( listOf( lineChartColor.copy(DefaultAlpha.LINE_BACKGROUND_SHADER_START), 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 db24809f5..f78172d17 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) }, @@ -90,15 +88,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 21eae0e0f..ff45a6611 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,40 +21,20 @@ 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.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 @@ -62,17 +42,25 @@ internal class ShowcaseViewModel : ViewModel() { init { viewModelScope.launch { 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 singleSeriesColumnLayerModelPartial = + RandomCartesianModelGenerator.getRandomColumnLayerModelPartial() + val singleSeriesLineLayerModelPartial = + RandomCartesianModelGenerator.getRandomLineLayerModelPartial() + val tripleSeriesColumnLayerModelPartial = + RandomCartesianModelGenerator.getRandomColumnLayerModelPartial(seriesCount = 3) + val tripleSeriesLineLayerModelPartial = + RandomCartesianModelGenerator.getRandomLineLayerModelPartial(seriesCount = 3) + val positiveNegativeLineLayerModelPartial = + RandomCartesianModelGenerator.getRandomLineLayerModelPartial(y = -10f..20f) + modelProducer1.tryRunTransaction { add(singleSeriesLineLayerModelPartial) } + modelProducer2.tryRunTransaction { add(singleSeriesColumnLayerModelPartial) } + modelProducer3.tryRunTransaction { + add(tripleSeriesColumnLayerModelPartial) + add(singleSeriesLineLayerModelPartial) } + modelProducer4.tryRunTransaction { add(tripleSeriesColumnLayerModelPartial) } + modelProducer5.tryRunTransaction { add(tripleSeriesLineLayerModelPartial) } + modelProducer6.tryRunTransaction { add(positiveNegativeLineLayerModelPartial) } delay(UPDATE_FREQUENCY) } } @@ -83,10 +71,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 34e2d997b..30d36f947 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,31 +22,35 @@ 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.cartesian.rememberCartesianChart +import com.patrykandpatrick.vico.compose.chart.line.rememberLineCartesianLayer 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( - chart = lineChart(persistentMarkers = remember(marker) { mapOf(PERSISTENT_MARKER_X to marker) }), - chartModelProducer = chartEntryModelProducer, + CartesianChartHost( + chart = rememberCartesianChart( + rememberLineCartesianLayer(), + persistentMarkers = remember(marker) { mapOf(PERSISTENT_MARKER_X to marker) }, + ), + modelProducer = modelProducer, startAxis = rememberStartAxis(), bottomAxis = rememberBottomAxis(guideline = null), marker = marker, @@ -56,13 +60,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 } @@ -70,7 +74,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 77f63b773..ef653fe34 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.cartesian.rememberCartesianChart +import com.patrykandpatrick.vico.compose.chart.column.rememberColumnCartesianLayer 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,28 +50,28 @@ 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( - chart = columnChart( - columns = remember(defaultColumns) { - defaultColumns.map { defaultColumn -> - LineComponent(defaultColumn.color, COLUMN_WIDTH_DP, defaultColumn.shape) - } - }, + val defaultColumns = currentChartStyle.columnLayer.columns + CartesianChartHost( + chart = rememberCartesianChart( + rememberColumnCartesianLayer( + remember(defaultColumns) { + defaultColumns.map { LineComponent(it.color, COLUMN_WIDTH_DP, it.shape) } + }, + ), decorations = remember(thresholdLine) { listOf(thresholdLine) }, ), - chartModelProducer = chartEntryModelProducer, + modelProducer = modelProducer, startAxis = rememberStartAxis(valueFormatter = startAxisValueFormatter, itemPlacer = startAxisItemPlacer), bottomAxis = rememberBottomAxis(itemPlacer = bottomAxisItemPlacer), marker = rememberMarker(), @@ -81,14 +82,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 9f7b22257..8b06f6a05 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,41 @@ 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.cartesian.rememberCartesianChart 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.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, horizontalLabelPosition = VerticalAxis.HorizontalLabelPosition.Inside, @@ -89,13 +91,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 } } @@ -108,8 +110,10 @@ private const val AXIS_VALUE_OVERRIDER_Y_FRACTION = 1.2f 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) +private val axisValueOverrider = AxisValueOverrider.adaptiveYValues( + yFraction = AXIS_VALUE_OVERRIDER_Y_FRACTION, + round = true, +) private val axisTitleHorizontalPaddingValue = 8.dp private val axisTitleVerticalPaddingValue = 2.dp private val axisTitlePadding = dimensionsOf(axisTitleHorizontalPaddingValue, axisTitleVerticalPaddingValue) 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 0265dc2d8..27eab5fc3 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,55 +23,50 @@ 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.cartesian.rememberCartesianChart +import com.patrykandpatrick.vico.compose.chart.column.rememberColumnCartesianLayer +import com.patrykandpatrick.vico.compose.chart.line.rememberLineCartesianLayer 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(), @@ -81,12 +76,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 26d0e04f4..b9261a410 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,56 +22,60 @@ 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.cartesian.rememberCartesianChart +import com.patrykandpatrick.vico.compose.chart.column.rememberColumnCartesianLayer 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( - 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, - ), - ) - } - }, - mergeMode = ColumnChart.MergeMode.Stack, + val defaultColumns = currentChartStyle.columnLayer.columns + CartesianChartHost( + chart = 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 = ColumnCartesianLayer.MergeMode.Stacked, + ), ), - chartModelProducer = chartEntryModelProducer, + modelProducer = modelProducer, startAxis = rememberStartAxis( itemPlacer = startAxisItemPlacer, labelRotationDegrees = AXIS_LABEL_ROTATION_DEGREES, @@ -84,13 +88,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 4d8add880..acee57868 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.cartesian.rememberCartesianChart +import com.patrykandpatrick.vico.compose.chart.column.rememberColumnCartesianLayer import com.patrykandpatrick.vico.compose.component.shapeComponent import com.patrykandpatrick.vico.compose.component.textComponent import com.patrykandpatrick.vico.compose.dimensions.dimensionsOf @@ -34,44 +35,46 @@ 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( - chart = columnChart( - columns = remember(defaultColumns) { - defaultColumns.map { defaultColumn -> - LineComponent( - defaultColumn.color, - defaultColumn.thicknessDp, - Shapes.cutCornerShape(topLeftPercent = COLUMN_CORNER_CUT_SIZE_PERCENT), - ) - } - }, - mergeMode = ColumnChart.MergeMode.Grouped, + val defaultColumns = currentChartStyle.columnLayer.columns + CartesianChartHost( + chart = rememberCartesianChart( + rememberColumnCartesianLayer( + remember(defaultColumns) { + defaultColumns.map { defaultColumn -> + LineComponent( + defaultColumn.color, + defaultColumn.thicknessDp, + Shapes.cutCornerShape(topLeftPercent = COLUMN_CORNER_CUT_SIZE_PERCENT), + ) + } + }, + mergeMode = ColumnCartesianLayer.MergeMode.Grouped, + ), decorations = remember(thresholdLine) { listOf(thresholdLine) }, ), - chartModelProducer = chartEntryModelProducer, + modelProducer = modelProducer, startAxis = rememberStartAxis(), bottomAxis = rememberBottomAxis(valueFormatter = bottomAxisValueFormatter), marker = rememberMarker(), @@ -81,7 +84,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() @@ -89,7 +92,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 } @@ -135,4 +138,4 @@ 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, _ -> daysOfWeek[x.toInt() % daysOfWeek.size] } + 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 824021971..ed8427148 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.cartesian.rememberCartesianChart +import com.patrykandpatrick.vico.compose.chart.line.rememberLineCartesianLayer import com.patrykandpatrick.vico.compose.component.shape.roundedCornerShape import com.patrykandpatrick.vico.compose.component.shapeComponent import com.patrykandpatrick.vico.compose.component.textComponent @@ -41,31 +42,31 @@ 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( - chart = lineChart( - remember(defaultLines) { - defaultLines.map { defaultLine -> defaultLine.copy(lineBackgroundShader = null) } - }, + val defaultLines = currentChartStyle.lineLayer.lines + CartesianChartHost( + chart = rememberCartesianChart( + rememberLineCartesianLayer( + remember(defaultLines) { defaultLines.map { it.copy(backgroundShader = null) } }, + ), ), - chartModelProducer = chartEntryModelProducer, + modelProducer = modelProducer, startAxis = rememberStartAxis( label = rememberStartAxisLabel(), horizontalLabelPosition = VerticalAxis.HorizontalLabelPosition.Inside, @@ -79,14 +80,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 this.marker = marker @@ -115,7 +116,7 @@ private fun rememberLegend() = verticalLegend( 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 062e631dc..c460c3091 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,46 +17,45 @@ 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.cartesian.rememberCartesianChart +import com.patrykandpatrick.vico.compose.chart.column.rememberColumnCartesianLayer +import com.patrykandpatrick.vico.compose.chart.line.rememberLineCartesianLayer 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(), @@ -66,15 +65,14 @@ 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 = AxisPosition.Vertical.Start - ((chart as ComposedChart).charts[1] as LineChart).targetVerticalAxisPosition = AxisPosition.Vertical.End + (chart?.layers?.get(0) as ColumnCartesianLayer).targetVerticalAxisPosition = AxisPosition.Vertical.Start + (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 e61e652b9..b0c5fe9ef 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.cartesian.rememberCartesianChart 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.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,65 +46,70 @@ 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( - 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) }, + CartesianChartHost( + chart = rememberCartesianChart( + rememberLineCartesianLayer( + lines = listOf( + lineSpec( + shader = TopBottomShader( + DynamicShaders.color(chartColors[0]), + DynamicShaders.color(chartColors[1]), + ), + 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, ), - verticalGradient(arrayOf(Color.Black, Color.Transparent)), - 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) }, + DynamicShaders.composeShader( + DynamicShaders.fromComponent( + componentSize = 5.dp, + component = shapeComponent( + shape = Shapes.rectShape, + color = chartColors[1], + margins = remember { dimensionsOf(horizontal = 2.dp) }, + ), + checkeredArrangement = false, ), - checkeredArrangement = false, + verticalGradient(arrayOf(Color.Transparent, Color.Black)), + PorterDuff.Mode.DST_IN, ), - verticalGradient(arrayOf(Color.Transparent, Color.Black)), - PorterDuff.Mode.DST_IN, ), ), ), ), ), - chartModelProducer = chartEntryModelProducer, + modelProducer = modelProducer, startAxis = rememberStartAxis( label = axisLabelComponent( color = MaterialTheme.colorScheme.onBackground, @@ -133,9 +139,7 @@ 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, @@ -145,20 +149,23 @@ 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..f6154151a 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 + #593dff + #ff166a 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