Skip to content

Commit

Permalink
LineCartesianLayer: Make line-drawing updates
Browse files Browse the repository at this point in the history
  • Loading branch information
patrickmichalik committed Dec 17, 2024
1 parent 31d52ad commit 1f35617
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import android.graphics.Canvas
import android.graphics.Paint
import android.graphics.Path
import android.graphics.PorterDuff
import android.graphics.PorterDuffXfermode
import androidx.compose.runtime.Immutable
import androidx.compose.runtime.Stable
import com.patrykandpatrick.vico.core.cartesian.CartesianDrawingContext
Expand All @@ -36,6 +37,7 @@ import com.patrykandpatrick.vico.core.cartesian.data.LineCartesianLayerDrawingMo
import com.patrykandpatrick.vico.core.cartesian.data.LineCartesianLayerModel
import com.patrykandpatrick.vico.core.cartesian.data.MutableCartesianChartRanges
import com.patrykandpatrick.vico.core.cartesian.data.forEachIn
import com.patrykandpatrick.vico.core.cartesian.layer.LineCartesianLayer.Line
import com.patrykandpatrick.vico.core.cartesian.marker.CartesianMarker
import com.patrykandpatrick.vico.core.cartesian.marker.LineCartesianLayerMarkerTarget
import com.patrykandpatrick.vico.core.cartesian.marker.MutableLineCartesianLayerMarkerTarget
Expand Down Expand Up @@ -125,6 +127,7 @@ protected constructor(
public fun draw(
context: CartesianDrawingContext,
path: Path,
lineCanvas: Canvas,
fillCanvas: Canvas,
verticalAxisPosition: Axis.Position.Vertical?,
) {
Expand All @@ -133,15 +136,15 @@ protected constructor(
linePaint.strokeWidth = thickness
val halfThickness = thickness.half
areaFill?.draw(context, path, halfThickness, verticalAxisPosition)
fillCanvas.drawPath(path, linePaint)
lineCanvas.drawPath(path, linePaint)
withOtherCanvas(fillCanvas) { fill.draw(context, halfThickness, verticalAxisPosition) }
}
}
}

/** Draws a [LineCartesianLayer] line’s fill. */
public interface LineFill {
/** Draws the line fill. [PorterDuff.Mode.SRC_IN] should be used. */
/** Draws the line fill. */
public fun draw(
context: CartesianDrawingContext,
halfLineThickness: Float,
Expand Down Expand Up @@ -306,8 +309,12 @@ protected constructor(

protected val linePath: Path = Path()

protected val lineCanvas: Canvas = Canvas()

protected val lineFillCanvas: Canvas = Canvas()

private val srcInPaint = Paint().apply { xfermode = PorterDuffXfermode(PorterDuff.Mode.SRC_IN) }

protected val cacheKeyNamespace: CacheStore.KeyNamespace = CacheStore.KeyNamespace()

override val markerTargets: Map<Double, List<CartesianMarker.Target>> = _markerTargets
Expand Down Expand Up @@ -366,10 +373,13 @@ protected constructor(

canvas.saveLayer(opacity = drawingModel?.opacity ?: 1f)

val lineFillBitmap = getBitmap(cacheKeyNamespace, seriesIndex)
val lineBitmap = getBitmap(cacheKeyNamespace, seriesIndex, "line")
lineCanvas.setBitmap(lineBitmap)
val lineFillBitmap = getBitmap(cacheKeyNamespace, seriesIndex, "lineFill")
lineFillCanvas.setBitmap(lineFillBitmap)
line.draw(context, linePath, lineFillCanvas, verticalAxisPosition)
canvas.drawBitmap(lineFillBitmap, 0f, 0f, null)
line.draw(context, linePath, lineCanvas, lineFillCanvas, verticalAxisPosition)
lineCanvas.drawBitmap(lineFillBitmap, 0f, 0f, srcInPaint)
canvas.drawBitmap(lineBitmap, 0f, 0f, null)

forEachPointInBounds(series, drawingStart, pointInfoMap) { entry, x, y, _, _ ->
updateMarkerTargets(entry, x, y, lineFillBitmap)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,13 @@
package com.patrykandpatrick.vico.core.cartesian.layer

import android.graphics.Paint
import android.graphics.PorterDuff
import android.graphics.PorterDuffXfermode
import com.patrykandpatrick.vico.core.cartesian.CartesianDrawingContext
import com.patrykandpatrick.vico.core.cartesian.axis.Axis
import com.patrykandpatrick.vico.core.common.Fill
import com.patrykandpatrick.vico.core.common.data.ExtraStore

internal data class SingleLineFill(val fill: Fill) : LineCartesianLayer.LineFill {
private val paint =
Paint(Paint.ANTI_ALIAS_FLAG).apply {
color = fill.color
xfermode = PorterDuffXfermode(PorterDuff.Mode.SRC_IN)
}
private val paint = Paint(Paint.ANTI_ALIAS_FLAG).apply { color = fill.color }

override fun draw(
context: CartesianDrawingContext,
Expand All @@ -55,8 +49,7 @@ internal data class DoubleLineFill(
val bottomFill: Fill,
val splitY: (ExtraStore) -> Number,
) : LineCartesianLayer.LineFill {
private val paint =
Paint(Paint.ANTI_ALIAS_FLAG).apply { xfermode = PorterDuffXfermode(PorterDuff.Mode.SRC_IN) }
private val paint = Paint(Paint.ANTI_ALIAS_FLAG)

override fun draw(
context: CartesianDrawingContext,
Expand Down

0 comments on commit 1f35617

Please sign in to comment.