Skip to content

Commit

Permalink
Make HorizontalAxis and VerticalAxis class properties read-only
Browse files Browse the repository at this point in the history
  • Loading branch information
patrickmichalik committed Aug 12, 2024
1 parent ff04243 commit f5bc4e1
Show file tree
Hide file tree
Showing 13 changed files with 336 additions and 181 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import com.patrykandpatrick.vico.compose.cartesian.rememberCartesianChart
import com.patrykandpatrick.vico.compose.cartesian.rememberVicoZoomState
import com.patrykandpatrick.vico.compose.common.data.rememberExtraLambda
import com.patrykandpatrick.vico.compose.common.fill
import com.patrykandpatrick.vico.core.cartesian.axis.BaseAxis
import com.patrykandpatrick.vico.core.cartesian.axis.HorizontalAxis
import com.patrykandpatrick.vico.core.cartesian.data.CartesianChartModelProducer
import com.patrykandpatrick.vico.core.cartesian.data.lineSeries
import com.patrykandpatrick.vico.core.cartesian.layer.LineCartesianLayer
Expand Down Expand Up @@ -91,7 +91,7 @@ private fun ViewChart1(modelProducer: CartesianChartModelProducer, modifier: Mod
with(chartView) {
chart?.persistentMarkers = { marker at PERSISTENT_MARKER_X }
this.modelProducer = modelProducer
(chart?.bottomAxis as BaseAxis).guideline = null
chart?.bottomAxis = (chart?.bottomAxis as HorizontalAxis).copy(guideline = null)
chart?.marker = marker
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ import com.patrykandpatrick.vico.compose.common.component.rememberShapeComponent
import com.patrykandpatrick.vico.compose.common.component.rememberTextComponent
import com.patrykandpatrick.vico.compose.common.of
import com.patrykandpatrick.vico.core.cartesian.HorizontalLayout
import com.patrykandpatrick.vico.core.cartesian.axis.BaseAxis
import com.patrykandpatrick.vico.core.cartesian.axis.HorizontalAxis
import com.patrykandpatrick.vico.core.cartesian.data.CartesianChartModelProducer
import com.patrykandpatrick.vico.core.cartesian.data.CartesianValueFormatter
Expand Down Expand Up @@ -121,7 +120,8 @@ private fun ViewChart2(modelProducer: CartesianChartModelProducer, modifier: Mod
with(chartView) {
chart?.decorations = listOf(getViewHorizontalLine())
this.modelProducer = modelProducer
(chart?.bottomAxis as BaseAxis).valueFormatter = bottomAxisValueFormatter
chart?.bottomAxis =
(chart?.bottomAxis as HorizontalAxis).copy(valueFormatter = bottomAxisValueFormatter)
chart?.marker = marker
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ private fun ViewChart5(modelProducer: CartesianChartModelProducer, modifier: Mod
AndroidViewBinding(Chart5Binding::inflate, modifier) {
with(chartView) {
this.modelProducer = modelProducer
(chart?.startAxis as VerticalAxis).itemPlacer = startAxisItemPlacer
chart?.startAxis = (chart?.startAxis as VerticalAxis).copy(itemPlacer = startAxisItemPlacer)
chart?.marker = marker
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ 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.of
import com.patrykandpatrick.vico.core.cartesian.axis.BaseAxis
import com.patrykandpatrick.vico.core.cartesian.axis.HorizontalAxis
import com.patrykandpatrick.vico.core.cartesian.data.CartesianChartModelProducer
import com.patrykandpatrick.vico.core.cartesian.data.CartesianValueFormatter
import com.patrykandpatrick.vico.core.cartesian.data.columnSeries
Expand Down Expand Up @@ -117,7 +117,8 @@ private fun ViewChart6(modelProducer: CartesianChartModelProducer, modifier: Mod
with(chartView) {
chart?.decorations = listOf(getViewHorizontalBox())
this.modelProducer = modelProducer
(chart?.bottomAxis as BaseAxis<*>).valueFormatter = bottomAxisValueFormatter
chart?.bottomAxis =
(chart?.bottomAxis as HorizontalAxis).copy(valueFormatter = bottomAxisValueFormatter)
chart?.marker = marker
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,9 +131,11 @@ private fun ViewChart7(modelProducer: CartesianChartModelProducer, modifier: Mod
AndroidViewBinding(Chart7Binding::inflate, modifier) {
with(chartView) {
this.modelProducer = modelProducer
(chart?.startAxis as VerticalAxis).horizontalLabelPosition =
VerticalAxis.HorizontalLabelPosition.Inside
(chart?.startAxis as VerticalAxis).label = startAxisLabel
chart?.startAxis =
(chart?.startAxis as VerticalAxis).copy(
label = startAxisLabel,
horizontalLabelPosition = VerticalAxis.HorizontalLabelPosition.Inside,
)
chart?.marker = marker
chart?.legend = legend
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ import com.patrykandpatrick.vico.compose.cartesian.rememberVicoZoomState
import com.patrykandpatrick.vico.compose.common.component.rememberLineComponent
import com.patrykandpatrick.vico.compose.common.fill
import com.patrykandpatrick.vico.core.cartesian.axis.Axis
import com.patrykandpatrick.vico.core.cartesian.axis.BaseAxis
import com.patrykandpatrick.vico.core.cartesian.axis.VerticalAxis
import com.patrykandpatrick.vico.core.cartesian.data.CartesianChartModelProducer
import com.patrykandpatrick.vico.core.cartesian.data.columnSeries
import com.patrykandpatrick.vico.core.cartesian.data.lineSeries
Expand Down Expand Up @@ -129,7 +129,7 @@ private fun ViewChart8(modelProducer: CartesianChartModelProducer, modifier: Mod
(chart?.layers?.get(1) as LineCartesianLayer).verticalAxisPosition =
Axis.Position.Vertical.End
this.modelProducer = modelProducer
(chart?.startAxis as BaseAxis).guideline = null
chart?.startAxis = (chart?.startAxis as VerticalAxis).copy(guideline = null)
chart?.marker = marker
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ import com.patrykandpatrick.vico.compose.common.shader.component
import com.patrykandpatrick.vico.compose.common.shader.verticalGradient
import com.patrykandpatrick.vico.compose.common.shape.dashed
import com.patrykandpatrick.vico.core.cartesian.HorizontalLayout
import com.patrykandpatrick.vico.core.cartesian.axis.BaseAxis
import com.patrykandpatrick.vico.core.cartesian.axis.HorizontalAxis
import com.patrykandpatrick.vico.core.cartesian.axis.VerticalAxis
import com.patrykandpatrick.vico.core.cartesian.data.CartesianChartModelProducer
Expand Down Expand Up @@ -196,7 +195,7 @@ private fun ViewChart9(modelProducer: CartesianChartModelProducer, modifier: Mod
Chart9Binding.inflate(inflater, parent, attachToParent).apply {
with(chartView) {
this.modelProducer = modelProducer
(chart?.bottomAxis as BaseAxis).guideline = null
chart?.bottomAxis = (chart?.bottomAxis as HorizontalAxis).copy(guideline = null)
chart?.marker = marker
with(chart?.layers?.get(0) as LineCartesianLayer) {
lineProvider =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,20 +43,33 @@ public fun rememberTopAxis(
titleComponent: TextComponent? = null,
title: CharSequence? = null,
): HorizontalAxis<Axis.Position.Horizontal.Top> =
remember { HorizontalAxis.top() }
.apply {
this.line = line
this.label = label
this.labelRotationDegrees = labelRotationDegrees
this.valueFormatter = valueFormatter
this.tick = tick
tickLengthDp = tickLength.value
this.guideline = guideline
this.itemPlacer = itemPlacer
this.sizeConstraint = sizeConstraint
this.titleComponent = titleComponent
this.title = title
}
remember(
line,
label,
labelRotationDegrees,
valueFormatter,
tick,
tickLength.value,
guideline,
itemPlacer,
sizeConstraint,
titleComponent,
title,
) {
HorizontalAxis.top(
line,
label,
labelRotationDegrees,
valueFormatter,
tick,
tickLength.value,
guideline,
itemPlacer,
sizeConstraint,
titleComponent,
title,
)
}

/** Creates and remembers a bottom [HorizontalAxis]. */
@Composable
Expand All @@ -73,17 +86,30 @@ public fun rememberBottomAxis(
titleComponent: TextComponent? = null,
title: CharSequence? = null,
): HorizontalAxis<Axis.Position.Horizontal.Bottom> =
remember { HorizontalAxis.bottom() }
.apply {
this.line = line
this.label = label
this.labelRotationDegrees = labelRotationDegrees
this.valueFormatter = valueFormatter
this.tick = tick
tickLengthDp = tickLength.value
this.guideline = guideline
this.itemPlacer = itemPlacer
this.sizeConstraint = sizeConstraint
this.titleComponent = titleComponent
this.title = title
}
remember(
line,
label,
labelRotationDegrees,
valueFormatter,
tick,
tickLength.value,
guideline,
itemPlacer,
sizeConstraint,
titleComponent,
title,
) {
HorizontalAxis.bottom(
line,
label,
labelRotationDegrees,
valueFormatter,
tick,
tickLength.value,
guideline,
itemPlacer,
sizeConstraint,
titleComponent,
title,
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,22 +47,37 @@ public fun rememberStartAxis(
titleComponent: TextComponent? = null,
title: CharSequence? = null,
): VerticalAxis<Axis.Position.Vertical.Start> =
remember { VerticalAxis.start() }
.apply {
this.line = line
this.label = label
this.labelRotationDegrees = labelRotationDegrees
this.horizontalLabelPosition = horizontalLabelPosition
this.verticalLabelPosition = verticalLabelPosition
this.valueFormatter = valueFormatter
this.tick = tick
tickLengthDp = tickLength.value
this.guideline = guideline
this.itemPlacer = itemPlacer
this.sizeConstraint = sizeConstraint
this.titleComponent = titleComponent
this.title = title
}
remember(
line,
label,
labelRotationDegrees,
horizontalLabelPosition,
verticalLabelPosition,
valueFormatter,
tick,
tickLength.value,
guideline,
itemPlacer,
sizeConstraint,
titleComponent,
title,
) {
VerticalAxis.start(
line,
label,
labelRotationDegrees,
horizontalLabelPosition,
verticalLabelPosition,
valueFormatter,
tick,
tickLength.value,
guideline,
itemPlacer,
sizeConstraint,
titleComponent,
title,
)
}

/** Creates and remembers an end [VerticalAxis]. */
@Composable
Expand All @@ -83,19 +98,34 @@ public fun rememberEndAxis(
titleComponent: TextComponent? = null,
title: CharSequence? = null,
): VerticalAxis<Axis.Position.Vertical.End> =
remember { VerticalAxis.end() }
.apply {
this.line = line
this.label = label
this.labelRotationDegrees = labelRotationDegrees
this.horizontalLabelPosition = horizontalLabelPosition
this.verticalLabelPosition = verticalLabelPosition
this.valueFormatter = valueFormatter
this.tick = tick
tickLengthDp = tickLength.value
this.guideline = guideline
this.itemPlacer = itemPlacer
this.sizeConstraint = sizeConstraint
this.titleComponent = titleComponent
this.title = title
}
remember(
line,
label,
labelRotationDegrees,
horizontalLabelPosition,
verticalLabelPosition,
valueFormatter,
tick,
tickLength.value,
guideline,
itemPlacer,
sizeConstraint,
titleComponent,
title,
) {
VerticalAxis.end(
line,
label,
labelRotationDegrees,
horizontalLabelPosition,
verticalLabelPosition,
valueFormatter,
tick,
tickLength.value,
guideline,
itemPlacer,
sizeConstraint,
titleComponent,
title,
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,16 @@ import com.patrykandpatrick.vico.core.common.setAll
* @property title the title text.
*/
public abstract class BaseAxis<P : Axis.Position>(
public var line: LineComponent?,
public var label: TextComponent?,
public var labelRotationDegrees: Float,
public var valueFormatter: CartesianValueFormatter,
public var tick: LineComponent?,
public var tickLengthDp: Float,
public var guideline: LineComponent?,
public var sizeConstraint: SizeConstraint,
public var titleComponent: TextComponent?,
public var title: CharSequence?,
protected val line: LineComponent?,
protected val label: TextComponent?,
protected val labelRotationDegrees: Float,
protected val valueFormatter: CartesianValueFormatter,
protected val tick: LineComponent?,
protected val tickLengthDp: Float,
protected val guideline: LineComponent?,
protected val sizeConstraint: SizeConstraint,
protected val titleComponent: TextComponent?,
protected val title: CharSequence?,
) : Axis<P> {
private val restrictedBounds: MutableList<RectF> = mutableListOf()

Expand Down
Loading

0 comments on commit f5bc4e1

Please sign in to comment.