Skip to content

Commit

Permalink
AND-7346 [Markets chart] Add dynamic width guideline support to the l…
Browse files Browse the repository at this point in the history
…ibrary
  • Loading branch information
MasterBin committed Jun 10, 2024
1 parent 3019aaf commit 592b627
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 133 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@

package com.tangem.vico.demo.chart

import android.os.Build
import android.util.Log
import android.text.Layout
import androidx.compose.animation.core.LinearEasing
import androidx.compose.animation.core.animate
import androidx.compose.animation.core.tween
Expand Down Expand Up @@ -51,7 +50,7 @@ import com.patrykandpatrick.vico.compose.cartesian.CartesianChartHost
import com.patrykandpatrick.vico.compose.cartesian.axis.rememberAxisGuidelineComponent
import com.patrykandpatrick.vico.compose.cartesian.axis.rememberAxisLabelComponent
import com.patrykandpatrick.vico.compose.cartesian.axis.rememberBottomAxis
import com.patrykandpatrick.vico.compose.cartesian.axis.rememberUnrestrictedStartAxis
import com.patrykandpatrick.vico.compose.cartesian.axis.rememberCustomStartAxis
import com.patrykandpatrick.vico.compose.cartesian.fullWidth
import com.patrykandpatrick.vico.compose.cartesian.layer.rememberLineCartesianLayer
import com.patrykandpatrick.vico.compose.cartesian.layer.rememberLineSpec
Expand All @@ -77,7 +76,6 @@ import com.patrykandpatrick.vico.core.common.component.LineComponent
import com.patrykandpatrick.vico.core.common.shader.ColorShader
import com.patrykandpatrick.vico.core.common.shader.LinearGradientShader
import com.patrykandpatrick.vico.core.common.shape.Shape
import com.tangem.vico.demo.utils.MyVerticalAxisItemPlacer
import java.time.LocalDateTime
import java.time.OffsetDateTime
import java.time.ZoneOffset
Expand Down Expand Up @@ -398,7 +396,7 @@ fun MarketDetailsChart(modifier: Modifier = Modifier) {

@Composable
fun rememberMyStartAxis(modifier: Modifier = Modifier): VerticalAxis<AxisPosition.Vertical.Start> {
return rememberUnrestrictedStartAxis(
return rememberCustomStartAxis(
axis = null,
tick = null,
guideline = rememberMyAxisGuidelineComponent(),
Expand All @@ -408,7 +406,7 @@ fun rememberMyStartAxis(modifier: Modifier = Modifier): VerticalAxis<AxisPositio
start = 4.dp,
),
textSize = 14.sp,
textAlignment = android.text.Layout.Alignment.ALIGN_CENTER,
textAlignment = Layout.Alignment.ALIGN_CENTER,
),
horizontalLabelPosition = VerticalAxis.HorizontalLabelPosition.Inside,
verticalLabelPosition = VerticalAxis.VerticalLabelPosition.Center,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,6 @@ fun rememberTangemChartMarker(
label = TextComponent.build { textSizeSp = 0f },
indicator = indicator,
indicatorSizeDp = 16f,
// setIndicatorColor = { bColor ->
// indicatorRearComponent.color = color.toArgb().copyColor(alpha = .24f)
// indicatorCenterComponent.color = color.toArgb()
// },
guideline = guideline,
) {
override fun getInsets(
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import androidx.compose.ui.unit.dp
import com.patrykandpatrick.vico.core.cartesian.axis.AxisItemPlacer
import com.patrykandpatrick.vico.core.cartesian.axis.AxisPosition
import com.patrykandpatrick.vico.core.cartesian.axis.BaseAxis
import com.patrykandpatrick.vico.core.cartesian.axis.UnrestrictedVerticalAxis
import com.patrykandpatrick.vico.core.cartesian.axis.CustomVerticalAxis
import com.patrykandpatrick.vico.core.cartesian.axis.VerticalAxis
import com.patrykandpatrick.vico.core.cartesian.data.CartesianValueFormatter
import com.patrykandpatrick.vico.core.common.Defaults
Expand All @@ -38,6 +38,7 @@ import com.patrykandpatrick.vico.core.common.component.TextComponent
* @param tick the [LineComponent] to use for the ticks.
* @param tickLength the length of the ticks.
* @param guideline the [LineComponent] to use for the guidelines.
* @param labelGuideline the [LineComponent] to use for the guidelines which starts from label text.
* @param valueFormatter formats the labels.
* @param sizeConstraint defines how the [VerticalAxis] is to size itself.
* @param horizontalLabelPosition the horizontal position of the labels.
Expand All @@ -49,12 +50,13 @@ import com.patrykandpatrick.vico.core.common.component.TextComponent
* @param title the axis title.
*/
@Composable
public fun rememberUnrestrictedStartAxis(
public fun rememberCustomStartAxis(
label: TextComponent? = rememberAxisLabelComponent(),
axis: LineComponent? = rememberAxisLineComponent(),
tick: LineComponent? = rememberAxisTickComponent(),
tickLength: Dp = Defaults.AXIS_TICK_LENGTH.dp,
guideline: LineComponent? = rememberAxisGuidelineComponent(),
labelGuideline: LineComponent? = null,
valueFormatter: CartesianValueFormatter = remember { CartesianValueFormatter.decimal() },
sizeConstraint: BaseAxis.SizeConstraint = BaseAxis.SizeConstraint.Auto(),
horizontalLabelPosition: VerticalAxis.HorizontalLabelPosition =
Expand All @@ -65,9 +67,10 @@ public fun rememberUnrestrictedStartAxis(
labelRotationDegrees: Float = Defaults.AXIS_LABEL_ROTATION_DEGREES,
titleComponent: TextComponent? = null,
title: CharSequence? = null,
): UnrestrictedVerticalAxis<AxisPosition.Vertical.Start> =
remember { UnrestrictedVerticalAxis.build<AxisPosition.Vertical.Start>() }
): CustomVerticalAxis<AxisPosition.Vertical.Start> =
remember { CustomVerticalAxis.build<AxisPosition.Vertical.Start>() }
.apply {
this.labelGuideline = labelGuideline
this.label = label
axisLine = axis
this.tick = tick
Expand All @@ -91,6 +94,7 @@ public fun rememberUnrestrictedStartAxis(
* @param tick the [LineComponent] to use for the ticks.
* @param tickLength the length of the ticks.
* @param guideline the [LineComponent] to use for the guidelines.
* @param labelGuideline the [LineComponent] to use for the guidelines which starts from label text.
* @param valueFormatter formats the labels.
* @param sizeConstraint defines how the [VerticalAxis] is to size itself.
* @param horizontalLabelPosition the horizontal position of the labels.
Expand All @@ -102,12 +106,13 @@ public fun rememberUnrestrictedStartAxis(
* @param title the axis title.
*/
@Composable
public fun rememberUnrestrictedEndAxis(
public fun rememberCustomEndAxis(
label: TextComponent? = rememberAxisLabelComponent(),
axis: LineComponent? = rememberAxisLineComponent(),
tick: LineComponent? = rememberAxisTickComponent(),
tickLength: Dp = Defaults.AXIS_TICK_LENGTH.dp,
guideline: LineComponent? = rememberAxisGuidelineComponent(),
labelGuideline: LineComponent? = null,
valueFormatter: CartesianValueFormatter = remember { CartesianValueFormatter.decimal() },
sizeConstraint: BaseAxis.SizeConstraint = BaseAxis.SizeConstraint.Auto(),
horizontalLabelPosition: VerticalAxis.HorizontalLabelPosition =
Expand All @@ -118,9 +123,10 @@ public fun rememberUnrestrictedEndAxis(
labelRotationDegrees: Float = Defaults.AXIS_LABEL_ROTATION_DEGREES,
titleComponent: TextComponent? = null,
title: CharSequence? = null,
): UnrestrictedVerticalAxis<AxisPosition.Vertical.End> =
remember { UnrestrictedVerticalAxis.build<AxisPosition.Vertical.End>() }
): CustomVerticalAxis<AxisPosition.Vertical.End> =
remember { CustomVerticalAxis.build<AxisPosition.Vertical.End>() }
.apply {
this.labelGuideline = labelGuideline
this.label = label
axisLine = axis
this.tick = tick
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,27 @@

package com.patrykandpatrick.vico.core.cartesian.axis

import android.graphics.Color
import com.patrykandpatrick.vico.core.cartesian.CartesianDrawContext
import com.patrykandpatrick.vico.core.common.component.LineComponent
import com.patrykandpatrick.vico.core.common.component.TextComponent


/**
* This class represents an unrestricted vertical axis. It extends the VerticalAxis class and
* provides additional functionality for handling vertical axis without any restrictions.
* Custom implementation of vertical axis. It extends the VerticalAxis class and
* provides additional functionality for handling vertical axis labels and guidelines.
*
* @param position The position of the axis
*
* @see VerticalAxis
* @see AxisPosition.Vertical
*/
public class UnrestrictedVerticalAxis<Position : AxisPosition.Vertical>(position: Position) :
public class CustomVerticalAxis<Position : AxisPosition.Vertical>(position: Position) :
VerticalAxis<Position>(position = position) {

/** The [LineComponent] to use for guidelines that aware of label width. */
public var labelGuideline: LineComponent? = null

/**
* This function checks if the axis is not in restricted bounds.
* As this is an unrestricted vertical axis, it always returns true.
Expand All @@ -43,11 +51,42 @@ public class UnrestrictedVerticalAxis<Position : AxisPosition.Vertical>(position
left: Float,
top: Float,
right: Float,
bottom: Float
bottom: Float,
): Boolean = true

override fun drawLabel(
context: CartesianDrawContext,
label: TextComponent,
labelText: CharSequence,
labelX: Float,
tickCenterY: Float,
) {
with(context) {
val textBounds =
label.getTextBounds(this, labelText, rotationDegrees = labelRotationDegrees)

if (position.isStart) {
labelGuideline?.drawHorizontal(
context = context,
left = chartBounds.left + textBounds.width(),
right = chartBounds.right,
centerY = tickCenterY,
)
} else {
labelGuideline?.drawHorizontal(
context = context,
left = chartBounds.left,
right = chartBounds.right - textBounds.width(),
centerY = tickCenterY,
)
}
}

super.drawLabel(context, label, labelText, labelX, tickCenterY)
}

public class Builder<Position : AxisPosition.Vertical>(
builder: BaseAxis.Builder<Position>? = null
builder: BaseAxis.Builder<Position>? = null,
) : BaseAxis.Builder<Position>(builder) {
/**
* Determines for what _y_ values this [VerticalAxis] is to display labels, ticks, and
Expand All @@ -63,7 +102,7 @@ public class UnrestrictedVerticalAxis<Position : AxisPosition.Vertical>(position

/** Creates a [VerticalAxis] instance with the properties from this [Builder]. */
@Suppress("UNCHECKED_CAST")
public inline fun <reified T : Position> build(): UnrestrictedVerticalAxis<T> {
public inline fun <reified T : Position> build(): CustomVerticalAxis<T> {
val position =
when (T::class.java) {
AxisPosition.Vertical.Start::class.java -> AxisPosition.Vertical.Start
Expand All @@ -72,19 +111,19 @@ public class UnrestrictedVerticalAxis<Position : AxisPosition.Vertical>(position
throw IllegalStateException("Got unknown AxisPosition class ${T::class.java.name}")
}
as Position
return setTo(UnrestrictedVerticalAxis(position)).also { axis ->
return setTo(CustomVerticalAxis(position)).also { axis ->
axis.itemPlacer = itemPlacer
axis.horizontalLabelPosition = horizontalLabelPosition
axis.verticalLabelPosition = verticalLabelPosition
} as UnrestrictedVerticalAxis<T>
} as CustomVerticalAxis<T>
}
}

/** Houses a [VerticalAxis] factory function. */
public companion object {
/** Creates a [VerticalAxis] via [Builder]. */
public inline fun <reified P : AxisPosition.Vertical> build(
block: Builder<P>.() -> Unit = {}
): UnrestrictedVerticalAxis<P> = Builder<P>().apply(block).build()
block: Builder<P>.() -> Unit = {},
): CustomVerticalAxis<P> = Builder<P>().apply(block).build()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,6 @@ public fun interface CartesianValueFormatter {
verticalAxisPosition: AxisPosition.Vertical?,
): CharSequence

// public fun format(
// value: Number,
// chartValues: ChartValues,
// verticalAxisPosition: AxisPosition.Vertical?,
// ): CharSequence = format(value.toFloat(), chartValues, verticalAxisPosition)

/** Houses [CartesianValueFormatter] factory functions. */
public companion object {
private class Decimal(private val decimalFormat: DecimalFormat) : CartesianValueFormatter {
Expand Down

0 comments on commit 592b627

Please sign in to comment.