Skip to content

Commit

Permalink
Merge pull request #721
Browse files Browse the repository at this point in the history
  • Loading branch information
patrickmichalik committed May 27, 2024
2 parents d52b6c7 + f7a95bc commit 9f21a5a
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -101,14 +101,18 @@ internal fun rememberMarker(
horizontalDimensions: HorizontalDimensions,
) {
with(context) {
outInsets.top =
super.getInsets(context, outInsets, horizontalDimensions)
val shadowInset =
(
CLIPPING_FREE_SHADOW_RADIUS_MULTIPLIER * LABEL_BACKGROUND_SHADOW_RADIUS_DP -
LABEL_BACKGROUND_SHADOW_DY_DP
)
.pixels
if (labelPosition == LabelPosition.AroundPoint) return
outInsets.top += label.getHeight(context) + labelBackgroundShape.tickSizeDp.pixels
when (labelPosition) {
LabelPosition.Top, LabelPosition.AroundPoint, LabelPosition.AbovePoint ->
outInsets.top += shadowInset
LabelPosition.Bottom -> outInsets.bottom += shadowInset
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,29 +122,39 @@ public open class DefaultCartesianMarker(
val tickPosition: MarkerCorneredShape.TickPosition
val y: Float
val verticalPosition: VerticalPosition
if (labelPosition == LabelPosition.Top) {
tickPosition = MarkerCorneredShape.TickPosition.Bottom
y = context.chartBounds.top - label.tickSizeDp.pixels
verticalPosition = VerticalPosition.Top
} else {
val topPointY =
targets.minOf { target ->
when (target) {
is CandlestickCartesianLayerMarkerTarget -> target.highCanvasY
is ColumnCartesianLayerMarkerTarget ->
target.columns.minOf(ColumnCartesianLayerMarkerTarget.Column::canvasY)
is LineCartesianLayerMarkerTarget ->
target.points.minOf(LineCartesianLayerMarkerTarget.Point::canvasY)
else -> error("Unexpected `CartesianMarker.Target` implementation.")
when (labelPosition) {
LabelPosition.Top -> {
tickPosition = MarkerCorneredShape.TickPosition.Bottom
y = context.chartBounds.top - label.tickSizeDp.pixels
verticalPosition = VerticalPosition.Top
}

LabelPosition.Bottom -> {
tickPosition = MarkerCorneredShape.TickPosition.Top
y = context.chartBounds.bottom + label.tickSizeDp.pixels
verticalPosition = VerticalPosition.Bottom
}

LabelPosition.AroundPoint, LabelPosition.AbovePoint -> {
val topPointY =
targets.minOf { target ->
when (target) {
is CandlestickCartesianLayerMarkerTarget -> target.highCanvasY
is ColumnCartesianLayerMarkerTarget ->
target.columns.minOf(ColumnCartesianLayerMarkerTarget.Column::canvasY)
is LineCartesianLayerMarkerTarget ->
target.points.minOf(LineCartesianLayerMarkerTarget.Point::canvasY)
else -> error("Unexpected `CartesianMarker.Target` implementation.")
}
}
}
val flip =
labelPosition == LabelPosition.AroundPoint &&
topPointY - labelBounds.height() - label.tickSizeDp.pixels < context.chartBounds.top
tickPosition =
if (flip) MarkerCorneredShape.TickPosition.Top else MarkerCorneredShape.TickPosition.Bottom
y = topPointY + (if (flip) 1 else -1) * label.tickSizeDp.pixels
verticalPosition = if (flip) VerticalPosition.Bottom else VerticalPosition.Top
val flip =
labelPosition == LabelPosition.AroundPoint &&
topPointY - labelBounds.height() - label.tickSizeDp.pixels < context.chartBounds.top
tickPosition =
if (flip) MarkerCorneredShape.TickPosition.Top else MarkerCorneredShape.TickPosition.Bottom
y = topPointY + (if (flip) 1 else -1) * label.tickSizeDp.pixels
verticalPosition = if (flip) VerticalPosition.Bottom else VerticalPosition.Top
}
}
extraStore[MarkerCorneredShape.tickPositionKey] = tickPosition

Expand Down Expand Up @@ -181,8 +191,17 @@ public open class DefaultCartesianMarker(
outInsets: Insets,
horizontalDimensions: HorizontalDimensions,
) {
if (labelPosition == LabelPosition.AroundPoint) return
with(context) { outInsets.top = label.getHeight(context) + label.tickSizeDp.pixels }
with(context) {
when (labelPosition) {
LabelPosition.Top, LabelPosition.AbovePoint ->
outInsets.top = label.getHeight(context) + label.tickSizeDp.pixels

LabelPosition.Bottom ->
outInsets.bottom = label.getHeight(context) + label.tickSizeDp.pixels

LabelPosition.AroundPoint -> Unit // Will be inside the chart
}
}
}

/** Specifies the position of a [DefaultCartesianMarker]’s label. */
Expand All @@ -192,6 +211,11 @@ public open class DefaultCartesianMarker(
*/
Top,

/**
* Positions the label at the bottom of the [CartesianChart]. Sufficient room is made.
*/
Bottom,

/**
* Positions the label above the topmost marked point or, if there isn’t enough room, below it.
*/
Expand Down

0 comments on commit 9f21a5a

Please sign in to comment.