Skip to content

Commit

Permalink
Make CartesianMarker-related updates
Browse files Browse the repository at this point in the history
Co-authored-by: Patryk Goworowski <[email protected]>
  • Loading branch information
patrickmichalik and Gowsky committed Apr 4, 2024
1 parent 706a55a commit d46d8e9
Show file tree
Hide file tree
Showing 28 changed files with 602 additions and 624 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,27 +22,27 @@ import androidx.compose.material3.MaterialTheme
import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.ui.unit.dp
import com.patrykandpatrick.vico.compose.cartesian.axis.rememberAxisGuidelineComponent
import com.patrykandpatrick.vico.compose.common.component.fixed
import com.patrykandpatrick.vico.compose.common.component.rememberLayeredComponent
import com.patrykandpatrick.vico.compose.common.component.rememberLineComponent
import com.patrykandpatrick.vico.compose.common.component.rememberShapeComponent
import com.patrykandpatrick.vico.compose.common.component.rememberTextComponent
import com.patrykandpatrick.vico.compose.common.dimension.dimensionsOf
import com.patrykandpatrick.vico.compose.common.shape.dashedShape
import com.patrykandpatrick.vico.compose.common.shape.markerCorneredShape
import com.patrykandpatrick.vico.core.cartesian.CartesianMeasureContext
import com.patrykandpatrick.vico.core.cartesian.dimensions.HorizontalDimensions
import com.patrykandpatrick.vico.core.cartesian.insets.Insets
import com.patrykandpatrick.vico.core.cartesian.marker.CartesianMarker
import com.patrykandpatrick.vico.core.common.component.CartesianMarkerComponent
import com.patrykandpatrick.vico.core.cartesian.marker.DefaultCartesianMarker
import com.patrykandpatrick.vico.core.common.component.TextComponent
import com.patrykandpatrick.vico.core.common.extension.copyColor
import com.patrykandpatrick.vico.core.common.shape.Corner
import com.patrykandpatrick.vico.core.common.shape.Shapes

@Composable
internal fun rememberMarker(
labelPosition: CartesianMarkerComponent.LabelPosition = CartesianMarkerComponent.LabelPosition.Top,
labelPosition: DefaultCartesianMarker.LabelPosition = DefaultCartesianMarker.LabelPosition.Top,
showIndicator: Boolean = true,
): CartesianMarker {
val labelBackgroundShape = Shapes.markerCorneredShape(Corner.FullyRounded)
val labelBackground =
Expand Down Expand Up @@ -75,25 +75,25 @@ internal fun rememberMarker(
),
padding = dimensionsOf(10.dp),
)
val guideline =
rememberLineComponent(
color = MaterialTheme.colorScheme.onSurface.copy(.2f),
thickness = 2.dp,
shape = Shapes.dashedShape(shape = Shapes.pillShape, dashLength = 8.dp, gapLength = 4.dp),
)
return remember(label, labelPosition, indicator, guideline) {
object : CartesianMarkerComponent(label, labelPosition, indicator, guideline) {
init {
indicatorSizeDp = 36f
onApplyEntryColor = { entryColor ->
indicatorRearComponent.color = entryColor.copyColor(alpha = .15f)
with(indicatorCenterComponent) {
color = entryColor
setShadow(radius = 12f, color = entryColor)
val guideline = rememberAxisGuidelineComponent()
return remember(label, labelPosition, indicator, showIndicator, guideline) {
object : DefaultCartesianMarker(
label = label,
labelPosition = labelPosition,
indicator = if (showIndicator) indicator else null,
indicatorSizeDp = 36f,
setIndicatorColor =
if (showIndicator) {
{ color ->
indicatorRearComponent.color = color.copyColor(alpha = .15f)
indicatorCenterComponent.color = color
indicatorCenterComponent.setShadow(radius = 12f, color = color)
}
}
}

} else {
null
},
guideline = guideline,
) {
override fun getInsets(
context: CartesianMeasureContext,
outInsets: Insets,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ private fun ComposeChart10(
modelProducer: CartesianChartModelProducer,
modifier: Modifier,
) {
val marker = rememberMarker()
val marker = rememberMarker(showIndicator = false)
CartesianChartHost(
chart =
rememberCartesianChart(
Expand All @@ -85,7 +85,7 @@ private fun ViewChart10(
modelProducer: CartesianChartModelProducer,
modifier: Modifier,
) {
val marker = rememberMarker()
val marker = rememberMarker(showIndicator = false)
AndroidViewBinding(Chart10Binding::inflate, modifier = modifier) {
with(chartView) {
runInitialAnimation = false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,10 @@ import com.patrykandpatrick.vico.compose.common.shader.color
import com.patrykandpatrick.vico.core.cartesian.HorizontalLayout
import com.patrykandpatrick.vico.core.cartesian.axis.VerticalAxis
import com.patrykandpatrick.vico.core.cartesian.layer.LineCartesianLayer
import com.patrykandpatrick.vico.core.cartesian.marker.DefaultCartesianMarker
import com.patrykandpatrick.vico.core.cartesian.model.CartesianChartModelProducer
import com.patrykandpatrick.vico.core.cartesian.model.lineSeries
import com.patrykandpatrick.vico.core.cartesian.values.AxisValueOverrider
import com.patrykandpatrick.vico.core.common.component.CartesianMarkerComponent
import com.patrykandpatrick.vico.core.common.shader.DynamicShaders
import com.patrykandpatrick.vico.core.common.shape.Shapes
import com.patrykandpatrick.vico.databinding.Chart3Binding
Expand Down Expand Up @@ -122,7 +122,7 @@ private fun ComposeChart3(
),
modelProducer = modelProducer,
modifier = modifier,
marker = rememberMarker(CartesianMarkerComponent.LabelPosition.AroundPoint),
marker = rememberMarker(DefaultCartesianMarker.LabelPosition.AroundPoint),
runInitialAnimation = false,
horizontalLayout = HorizontalLayout.fullWidth(),
zoomState = rememberVicoZoomState(zoomEnabled = false),
Expand All @@ -134,7 +134,7 @@ private fun ViewChart3(
modelProducer: CartesianChartModelProducer,
modifier: Modifier,
) {
val marker = rememberMarker(CartesianMarkerComponent.LabelPosition.AroundPoint)
val marker = rememberMarker(DefaultCartesianMarker.LabelPosition.AroundPoint)

AndroidViewBinding(Chart3Binding::inflate, modifier) {
with(chartView) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ import com.patrykandpatrick.vico.core.cartesian.dimensions.MutableHorizontalDime
import com.patrykandpatrick.vico.core.cartesian.draw.cartesianChartDrawContext
import com.patrykandpatrick.vico.core.cartesian.draw.drawMarker
import com.patrykandpatrick.vico.core.cartesian.marker.CartesianMarker
import com.patrykandpatrick.vico.core.cartesian.marker.CartesianMarkerVisibilityChangeListener
import com.patrykandpatrick.vico.core.cartesian.marker.CartesianMarkerVisibilityListener
import com.patrykandpatrick.vico.core.cartesian.model.CartesianChartModel
import com.patrykandpatrick.vico.core.cartesian.model.CartesianChartModelProducer
import com.patrykandpatrick.vico.core.cartesian.values.ChartValues
Expand All @@ -74,7 +74,7 @@ import kotlinx.coroutines.launch
* @param modelProducer creates and updates the [CartesianChartModel].
* @param modifier the modifier to be applied to the chart.
* @param marker appears when the chart is touched, highlighting the entry or entries nearest to the touch point.
* @param markerVisibilityChangeListener allows for listening to [marker] visibility changes.
* @param markerVisibilityListener allows for listening to [marker] visibility changes.
* @param scrollState houses information on the [CartesianChart]’s scroll value. Allows for scroll customization and
* programmatic scrolling.
* @param zoomState houses information on the [CartesianChart]’s zoom factor. Allows for zoom customization.
Expand All @@ -92,7 +92,7 @@ public fun CartesianChartHost(
modelProducer: CartesianChartModelProducer,
modifier: Modifier = Modifier,
marker: CartesianMarker? = null,
markerVisibilityChangeListener: CartesianMarkerVisibilityChangeListener? = null,
markerVisibilityListener: CartesianMarkerVisibilityListener? = null,
scrollState: VicoScrollState = rememberVicoScrollState(),
zoomState: VicoZoomState = rememberDefaultVicoZoomState(scrollState.scrollEnabled),
diffAnimationSpec: AnimationSpec<Float>? = defaultCartesianDiffAnimationSpec,
Expand All @@ -118,7 +118,7 @@ public fun CartesianChartHost(
model = model,
oldModel = previousModel,
marker = marker,
markerVisibilityChangeListener = markerVisibilityChangeListener,
markerVisibilityListener = markerVisibilityListener,
scrollState = scrollState,
zoomState = zoomState,
horizontalLayout = horizontalLayout,
Expand All @@ -138,7 +138,7 @@ public fun CartesianChartHost(
* @param model the [CartesianChartModel].
* @param modifier the modifier to be applied to the chart.
* @param marker appears when the chart is touched, highlighting the entry or entries nearest to the touch point.
* @param markerVisibilityChangeListener allows for listening to [marker] visibility changes.
* @param markerVisibilityListener allows for listening to [marker] visibility changes.
* @param scrollState houses information on the [CartesianChart]’s scroll value. Allows for scroll customization and
* programmatic scrolling.
* @param zoomState houses information on the [CartesianChart]’s zoom factor. Allows for zoom customization.
Expand All @@ -155,7 +155,7 @@ public fun CartesianChartHost(
model: CartesianChartModel,
modifier: Modifier = Modifier,
marker: CartesianMarker? = null,
markerVisibilityChangeListener: CartesianMarkerVisibilityChangeListener? = null,
markerVisibilityListener: CartesianMarkerVisibilityListener? = null,
scrollState: VicoScrollState = rememberVicoScrollState(),
zoomState: VicoZoomState = rememberDefaultVicoZoomState(scrollState.scrollEnabled),
oldModel: CartesianChartModel? = null,
Expand All @@ -177,7 +177,7 @@ public fun CartesianChartHost(
chart = chart,
model = model,
marker = marker,
markerVisibilityChangeListener = markerVisibilityChangeListener,
markerVisibilityListener = markerVisibilityListener,
scrollState = scrollState,
zoomState = zoomState,
oldModel = oldModel,
Expand All @@ -192,7 +192,7 @@ internal fun CartesianChartHostImpl(
chart: CartesianChart,
model: CartesianChartModel,
marker: CartesianMarker?,
markerVisibilityChangeListener: CartesianMarkerVisibilityChangeListener?,
markerVisibilityListener: CartesianMarkerVisibilityListener?,
scrollState: VicoScrollState,
zoomState: VicoZoomState,
oldModel: CartesianChartModel?,
Expand All @@ -209,10 +209,9 @@ internal fun CartesianChartHostImpl(
with(LocalContext.current) { ::spToPx },
chartValues,
)
val lastMarkerEntryModels = remember { mutableStateOf(emptyList<CartesianMarker.EntryModel>()) }
val previousMarkerX = remember { ValueWrapper<Float?>(null) }

val elevationOverlayColor = vicoTheme.elevationOverlayColor.toArgb()
val (wasMarkerVisible, setWasMarkerVisible) = remember { mutableStateOf(false) }
val coroutineScope = rememberCoroutineScope()
var previousModelID by remember { ValueWrapper(model.id) }
val horizontalDimensions = remember { MutableHorizontalDimensions() }
Expand Down Expand Up @@ -279,16 +278,14 @@ internal fun CartesianChartHostImpl(
chart.draw(chartDrawContext, model)

if (marker != null) {
chartDrawContext.drawMarker(
marker = marker,
markerTouchPoint = markerTouchPoint.value,
chart = chart,
markerVisibilityChangeListener = markerVisibilityChangeListener,
wasMarkerVisible = wasMarkerVisible,
setWasMarkerVisible = setWasMarkerVisible,
lastMarkerEntryModels = lastMarkerEntryModels.value,
onMarkerEntryModelsChange = lastMarkerEntryModels.component2(),
)
previousMarkerX.value =
chartDrawContext.drawMarker(
marker,
markerTouchPoint.value,
chart,
markerVisibilityListener,
previousMarkerX.value,
)
}

measureContext.reset()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* Copyright 2024 by Patryk Goworowski and Patrick Michalik.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.patrykandpatrick.vico.compose.cartesian.marker

import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp
import com.patrykandpatrick.vico.core.cartesian.marker.CartesianMarkerValueFormatter
import com.patrykandpatrick.vico.core.cartesian.marker.DefaultCartesianMarker
import com.patrykandpatrick.vico.core.cartesian.marker.DefaultCartesianMarkerValueFormatter
import com.patrykandpatrick.vico.core.common.Defaults
import com.patrykandpatrick.vico.core.common.component.Component
import com.patrykandpatrick.vico.core.common.component.LineComponent
import com.patrykandpatrick.vico.core.common.component.TextComponent

/** Creates and remembers a [DefaultCartesianMarker]. */
@Composable
public fun rememberDefaultCartesianMarker(
label: TextComponent,
valueFormatter: CartesianMarkerValueFormatter = remember { DefaultCartesianMarkerValueFormatter() },
labelPosition: DefaultCartesianMarker.LabelPosition = DefaultCartesianMarker.LabelPosition.Top,
indicator: Component? = null,
indicatorSize: Dp = Defaults.MARKER_INDICATOR_SIZE.dp,
setIndicatorColor: ((Int) -> Unit)? = null,
guideline: LineComponent? = null,
): DefaultCartesianMarker =
remember(label, valueFormatter, labelPosition, indicator, indicatorSize, setIndicatorColor, guideline) {
DefaultCartesianMarker(
label,
valueFormatter,
labelPosition,
indicator,
indicatorSize.value,
setIndicatorColor,
guideline,
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,8 @@ import androidx.compose.ui.graphics.toArgb
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp
import com.patrykandpatrick.vico.core.common.Defaults.SHADOW_COLOR
import com.patrykandpatrick.vico.core.common.component.CartesianMarkerComponent
import com.patrykandpatrick.vico.core.common.component.ShapeComponent

/**
* The indicator size.
*/
public var CartesianMarkerComponent.indicatorSize: Dp
get() = indicatorSizeDp.dp
set(value) {
indicatorSizeDp = value.value
}

/**
* Applies a drop shadow to this [ShapeComponent].
*
Expand Down

This file was deleted.

Loading

0 comments on commit d46d8e9

Please sign in to comment.