patrykandpatrick / vico

A light and extensible chart library for Android.
https://patrykandpatrick.com/vico/guide
Apache License 2.0
2.19k stars 131 forks source link

Centered text in Marker #159

Closed joharei closed 2 years ago

joharei commented 2 years ago

Hi,

I used the sample to create a Marker with two lines of text, and now I would like to center the text. When I specify textAlign = Paint.Align.CENTER the text is aligned properly, but the background is offset incorrectly. Is this a bug, or is there anything I can do in my code?

bilde
fun getMarker(
    @ColorInt labelColor: Int,
    @ColorInt bubbleColor: Int,
    @ColorInt shadowColor: Int,
    typeface: Typeface,
    formatDate: (LocalDate) -> String,
    formatValue: (Float) -> String,
): Marker {
    val labelBackgroundShape = MarkerCorneredShape(
        all = Corner.Absolute(sizeDp = 4f, cornerTreatment = RoundedCornerTreatment),
        tickSizeDp = 6f,
    )
    val label = textComponent {
        color = labelColor
        ellipsize = TextUtils.TruncateAt.END
        lineCount = 2
        padding = MutableDimensions(horizontalDp = 8f, verticalDp = 8f)
        textSizeSp = 12f
        this.typeface = typeface
        textAlign = Paint.Align.CENTER
        background = ShapeComponent(shape = labelBackgroundShape, color = bubbleColor)
            .setShadow(
                radius = SHADOW_RADIUS,
                dy = SHADOW_DY,
                color = shadowColor,
                applyElevationOverlay = true,
            )
    }

    return object : MarkerComponent(
        label = label,
        indicator = null,
        guideline = null,
    ) {
        init {
            labelFormatter = MarkerLabelFormatter { markedEntries ->
                markedEntries.transformToSpannable { model ->
                    val entry = model.entry as DateEntry
                    bold {
                        appendCompat(
                            formatDate(entry.localDate),
                            ForegroundColorSpan(labelColor),
                            Spannable.SPAN_EXCLUSIVE_EXCLUSIVE,
                        )
                    }
                    appendLine()
                    appendCompat(
                        formatValue(entry.y),
                        ForegroundColorSpan(labelColor),
                        Spannable.SPAN_EXCLUSIVE_EXCLUSIVE,
                    )
                }
            }
        }

        override fun getInsets(
            context: MeasureContext,
            outInsets: Insets,
            segmentProperties: SegmentProperties,
        ) = with(context) {
            outInsets.top = label.getHeight(context) + labelBackgroundShape.tickSizeDp.pixels +
                    SHADOW_RADIUS.pixels * SHADOW_RADIUS_TO_PX_MULTIPLIER - SHADOW_DY.pixels
        }
    }
}
ghost commented 2 years ago

This would appear to be a bug. Thanks for the report!

ghost commented 2 years ago

Hello! Vico 1.6.0, which is now rolling out, resolves the bug in question. Thank you for the report!

joharei commented 2 years ago

Amazing, thanks for the quick response!