Skip to content

Commit

Permalink
AlignedHorizontalAxisItemPlacer: Convert spacing and offset to …
Browse files Browse the repository at this point in the history
…`ExtraStore` lambdas
  • Loading branch information
patrickmichalik committed Dec 22, 2024
1 parent e0b588c commit 5fb6d07
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ private fun ComposeChart10(modelProducer: CartesianChartModelProducer, modifier:
guideline = null,
itemPlacer =
remember {
HorizontalAxis.ItemPlacer.aligned(spacing = 3, addExtremeLabelPadding = true)
HorizontalAxis.ItemPlacer.aligned(spacing = { 3 }, addExtremeLabelPadding = true)
},
),
marker = marker,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ private fun ComposeChart2(modelProducer: CartesianChartModelProducer, modifier:
valueFormatter = bottomAxisValueFormatter,
itemPlacer =
remember {
HorizontalAxis.ItemPlacer.aligned(spacing = 3, addExtremeLabelPadding = true)
HorizontalAxis.ItemPlacer.aligned(spacing = { 3 }, addExtremeLabelPadding = true)
},
),
marker = rememberMarker(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ private fun ComposeChart9(modelProducer: CartesianChartModelProducer, modifier:
guideline = null,
itemPlacer =
remember {
HorizontalAxis.ItemPlacer.aligned(spacing = 3, addExtremeLabelPadding = true)
HorizontalAxis.ItemPlacer.aligned(spacing = { 3 }, addExtremeLabelPadding = true)
},
),
marker = marker,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

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

import androidx.annotation.IntRange
import androidx.annotation.RestrictTo
import com.patrykandpatrick.vico.core.cartesian.CartesianDrawingContext
import com.patrykandpatrick.vico.core.cartesian.CartesianMeasuringContext
Expand All @@ -31,6 +30,7 @@ import com.patrykandpatrick.vico.core.common.Insets
import com.patrykandpatrick.vico.core.common.VerticalPosition
import com.patrykandpatrick.vico.core.common.component.LineComponent
import com.patrykandpatrick.vico.core.common.component.TextComponent
import com.patrykandpatrick.vico.core.common.data.ExtraStore
import com.patrykandpatrick.vico.core.common.doubled
import com.patrykandpatrick.vico.core.common.getStart
import com.patrykandpatrick.vico.core.common.half
Expand Down Expand Up @@ -597,15 +597,15 @@ protected constructor(
public companion object {
/**
* Adds a label, tick, and guideline for each _x_ value given by [CartesianChartRanges.minX] +
* (k × [spacing] + [offset]) × [CartesianChartRanges.xStep], where _k_ ∈ ℕ, with these
* (_k_ × spacing + offset) × [CartesianChartRanges.xStep], where _k_ ∈ ℕ, with these
* components being horizontally centered relative to one another. [shiftExtremeLines] is used
* as the return value of [ItemPlacer.getShiftExtremeLines]. [addExtremeLabelPadding]
* specifies whether [CartesianLayer] padding should be added for the first and last labels,
* ensuring their visibility.
*/
public fun aligned(
@IntRange(from = 1) spacing: Int = 1,
@IntRange(from = 0) offset: Int = 0,
spacing: (ExtraStore) -> Int = { 1 },
offset: (ExtraStore) -> Int = { 0 },
shiftExtremeLines: Boolean = true,
addExtremeLabelPadding: Boolean = true,
): ItemPlacer =
Expand All @@ -615,7 +615,7 @@ protected constructor(
* Adds a label for each major _x_ value, and adds ticks between the labels and for
* [CartesianChartRanges.minX] − [CartesianChartRanges.xStep] ÷ 2 and
* [CartesianChartRanges.maxX] + [CartesianChartRanges.xStep] ÷ 2. (Major _x_ values are given
* by [CartesianChartRanges.minX] + k × [CartesianChartRanges.xStep], where _k_ ∈ ℕ.)
* by [CartesianChartRanges.minX] + _k_ × [CartesianChartRanges.xStep], where _k_ ∈ ℕ.)
* [shiftExtremeLines] is used as the return value of [ItemPlacer.getShiftExtremeLines].
*/
public fun segmented(shiftExtremeLines: Boolean = true): ItemPlacer =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import com.patrykandpatrick.vico.core.cartesian.CartesianDrawingContext
import com.patrykandpatrick.vico.core.cartesian.CartesianMeasuringContext
import com.patrykandpatrick.vico.core.cartesian.HorizontalDimensions
import com.patrykandpatrick.vico.core.cartesian.data.CartesianChartRanges
import com.patrykandpatrick.vico.core.common.data.ExtraStore
import com.patrykandpatrick.vico.core.common.half
import com.patrykandpatrick.vico.core.common.roundedToNearest
import kotlin.math.ceil
Expand Down Expand Up @@ -77,22 +78,31 @@ internal abstract class BaseHorizontalAxisItemPlacer(private val shiftExtremeLin
}

internal class AlignedHorizontalAxisItemPlacer(
private val spacing: Int,
private val offset: Int,
private val spacing: (ExtraStore) -> Int,
private val offset: (ExtraStore) -> Int,
private val shiftExtremeLines: Boolean,
private val addExtremeLabelPadding: Boolean,
) : BaseHorizontalAxisItemPlacer(shiftExtremeLines) {
init {
require(spacing > 0) { "`spacing` must be positive." }
require(offset >= 0) { "`offset` must be nonnegative." }
}
private fun CartesianMeasuringContext.getSpacingOrThrow() =
spacing(model.extraStore).also { require(it > 0) { "`spacing` must return a positive value." } }

private fun CartesianMeasuringContext.getOffsetOrThrow() =
offset(model.extraStore).also {
require(it >= 0) { "`offset` must return a nonnegative value." }
}

override fun getFirstLabelValue(context: CartesianMeasuringContext, maxLabelWidth: Float) =
if (addExtremeLabelPadding) context.ranges.minX + offset * context.ranges.xStep else null
context.run {
if (addExtremeLabelPadding) ranges.minX + getOffsetOrThrow() * ranges.xStep else null
}

override fun getLastLabelValue(context: CartesianMeasuringContext, maxLabelWidth: Float) =
if (addExtremeLabelPadding) {
with(context.ranges) { maxX - (xLength - xStep * offset) % (xStep * spacing) }
context.run {
ranges.maxX -
(ranges.xLength - ranges.xStep * getOffsetOrThrow()) %
(ranges.xStep * getSpacingOrThrow())
}
} else {
null
}
Expand All @@ -103,18 +113,21 @@ internal class AlignedHorizontalAxisItemPlacer(
fullXRange: ClosedFloatingPointRange<Double>,
maxLabelWidth: Float,
) =
context.getLabelValues(
visibleXRange = visibleXRange,
fullXRange = fullXRange,
offset = offset,
spacing =
spacing *
if (addExtremeLabelPadding && maxLabelWidth != 0f) {
ceil(maxLabelWidth / (context.horizontalDimensions.xSpacing * spacing)).toInt()
} else {
1
},
)
context.run {
val spacing = getSpacingOrThrow()
getLabelValues(
visibleXRange = visibleXRange,
fullXRange = fullXRange,
offset = getOffsetOrThrow(),
spacing =
spacing *
if (addExtremeLabelPadding && maxLabelWidth != 0f) {
ceil(maxLabelWidth / (context.horizontalDimensions.xSpacing * spacing)).toInt()
} else {
1
},
)
}

override fun getWidthMeasurementLabelValues(
context: CartesianMeasuringContext,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -279,11 +279,13 @@ internal class ThemeHandler(private val context: Context, attrs: AttributeSet?)

private fun TypedArray.getHorizontalAxisItemPlacer(): HorizontalAxis.ItemPlacer {
val shiftExtremeLines = getBoolean(R.styleable.AxisStyle_shiftExtremeHorizontalAxisLines, true)
val spacing = getInteger(R.styleable.AxisStyle_horizontalAxisLabelSpacing, 1)
val offset = getInteger(R.styleable.AxisStyle_horizontalAxisLabelOffset, 0)
return when (getInteger(R.styleable.AxisStyle_horizontalAxisItemPlacer, 0)) {
0 ->
HorizontalAxis.ItemPlacer.aligned(
getInteger(R.styleable.AxisStyle_horizontalAxisLabelSpacing, 1),
getInteger(R.styleable.AxisStyle_horizontalAxisLabelOffset, 0),
{ spacing },
{ offset },
shiftExtremeLines,
getBoolean(R.styleable.AxisStyle_addExtremeHorizontalAxisLabelPadding, true),
)
Expand Down

0 comments on commit 5fb6d07

Please sign in to comment.