Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import info.appdev.charting.highlight.Highlight
import info.appdev.charting.utils.FSize
import info.appdev.charting.utils.PointF
import java.lang.ref.WeakReference
import androidx.core.graphics.withTranslation

/**
* View that can be displayed when selecting values in the chart. Extend this class to provide custom layouts for your markers.
Expand Down Expand Up @@ -119,11 +120,10 @@ class MarkerImage(private var mContext: Context, drawableResourceId: Int) : IMar
mDrawableBoundsCache.top + height.toInt()
)

val saveId = canvas.save()
// translate to the correct position and draw
canvas.translate(posX + offset.x, posY + offset.y)
drawable!!.draw(canvas)
canvas.restoreToCount(saveId)
canvas.withTranslation(posX + offset.x, posY + offset.y) {
// translate to the correct position and draw
drawable!!.draw(canvas)
}

drawable!!.bounds = mDrawableBoundsCache
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import info.appdev.charting.data.Entry
import info.appdev.charting.highlight.Highlight
import info.appdev.charting.utils.PointF
import java.lang.ref.WeakReference
import androidx.core.graphics.withTranslation

/**
* View that can be displayed when selecting values in the chart. Extend this class to provide custom layouts for your markers.
Expand Down Expand Up @@ -91,10 +92,9 @@ open class MarkerView(context: Context?, layoutResource: Int) : RelativeLayout(c
override fun draw(canvas: Canvas, posX: Float, posY: Float) {
val offset: PointF = getOffsetForDrawingAtPoint(posX, posY)

val saveId = canvas.save()
// translate to the correct position and draw
canvas.translate(posX + offset.x, posY + offset.y)
draw(canvas)
canvas.restoreToCount(saveId)
canvas.withTranslation(posX + offset.x, posY + offset.y) {
// translate to the correct position and draw
draw(canvas)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -687,16 +687,16 @@ open class PieChartRenderer(

val layoutHeight = centerTextLayout!!.height.toFloat()

canvas.save()
val path = mDrawCenterTextPathBuffer
path.reset()
path.addOval(holeRect, Path.Direction.CW)
canvas.clipPath(path)
canvas.withSave {
val path = mDrawCenterTextPathBuffer
path.reset()
path.addOval(holeRect, Path.Direction.CW)
clipPath(path)

canvas.translate(boundingRect.left, boundingRect.top + (boundingRect.height() - layoutHeight) / 2f)
centerTextLayout!!.draw(canvas)
translate(boundingRect.left, boundingRect.top + (boundingRect.height() - layoutHeight) / 2f)
centerTextLayout!!.draw(this)

canvas.restore()
}

PointF.recycleInstance(center)
PointF.recycleInstance(offset)
Expand Down
218 changes: 109 additions & 109 deletions chartLib/src/main/kotlin/info/appdev/charting/renderer/YAxisRenderer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -228,28 +228,28 @@ open class YAxisRenderer(
* Draws the zero line.
*/
protected open fun drawZeroLine(canvas: Canvas) {
val clipRestoreCount = canvas.save()
zeroLineClippingRect.set(viewPortHandler.contentRect)
zeroLineClippingRect.inset(0f, -yAxis.zeroLineWidth)
canvas.clipRect(zeroLineClippingRect)
canvas.withSave {
zeroLineClippingRect.set(viewPortHandler.contentRect)
zeroLineClippingRect.inset(0f, -yAxis.zeroLineWidth)
canvas.clipRect(zeroLineClippingRect)

// draw zero line
val pos = transformer?.getPixelForValues(0f, 0f)
pos?.let {
zeroLinePaint.color = yAxis.zeroLineColor
zeroLinePaint.strokeWidth = yAxis.zeroLineWidth
// draw zero line
val pos = transformer?.getPixelForValues(0f, 0f)
pos?.let {
zeroLinePaint.color = yAxis.zeroLineColor
zeroLinePaint.strokeWidth = yAxis.zeroLineWidth

val zeroLinePath = drawZeroLinePath
zeroLinePath.reset()
val zeroLinePath = drawZeroLinePath
zeroLinePath.reset()

zeroLinePath.moveTo(viewPortHandler.contentLeft(), it.y.toFloat())
zeroLinePath.lineTo(viewPortHandler.contentRight(), it.y.toFloat())
zeroLinePath.moveTo(viewPortHandler.contentLeft(), it.y.toFloat())
zeroLinePath.lineTo(viewPortHandler.contentRight(), it.y.toFloat())

// draw a path because lines don't support dashing on lower android versions
canvas.drawPath(zeroLinePath, zeroLinePaint)
}
// draw a path because lines don't support dashing on lower android versions
canvas.drawPath(zeroLinePath, zeroLinePaint)
}

canvas.restoreToCount(clipRestoreCount)
}
}

protected var renderLimitRanges: Path = Path()
Expand Down Expand Up @@ -384,105 +384,105 @@ open class YAxisRenderer(
if (!limitRange.isEnabled)
continue

val clipRestoreCount = canvas.save()
limitLineClippingRect.set(viewPortHandler.contentRect)
limitLineClippingRect.inset(0f, -limitRange.lineWidth)
canvas.clipRect(limitLineClippingRect)

limitRangePaint.style = Paint.Style.STROKE
limitRangePaint.color = limitRange.lineColor
limitRangePaint.strokeWidth = limitRange.lineWidth
limitRangePaint.pathEffect = limitRange.dashPathEffect

limitRangePaintFill.style = Paint.Style.FILL
limitRangePaintFill.color = limitRange.rangeColor

ptsr[1] = limitRange.limit.high
ptsr2[1] = limitRange.limit.low

transformer?.pointValuesToPixel(ptsr)
transformer?.pointValuesToPixel(ptsr2)

limitRangePathFill.moveTo(viewPortHandler.contentLeft(), ptsr[1])
limitRangePathFill.addRect(
viewPortHandler.contentLeft(),
ptsr[1],
viewPortHandler.contentRight(),
ptsr2[1],
Path.Direction.CW
)
canvas.drawPath(limitRangePathFill, limitRangePaintFill)
limitRangePathFill.reset()

if (limitRange.lineWidth > 0) {
limitRangePath.moveTo(viewPortHandler.contentLeft(), ptsr[1])
limitRangePath.lineTo(viewPortHandler.contentRight(), ptsr[1])
canvas.drawPath(limitRangePath, limitRangePaint)

limitRangePath.moveTo(viewPortHandler.contentLeft(), ptsr2[1])
limitRangePath.lineTo(viewPortHandler.contentRight(), ptsr2[1])
canvas.drawPath(limitRangePath, limitRangePaint)
}
canvas.withSave {
limitLineClippingRect.set(viewPortHandler.contentRect)
limitLineClippingRect.inset(0f, -limitRange.lineWidth)
canvas.clipRect(limitLineClippingRect)

limitRangePath.reset()

val label = limitRange.label

// if drawing the limit-value label is enabled
if (label != null && label != "") {
limitRangePaint.style = limitRange.textStyle
limitRangePaint.pathEffect = null
limitRangePaint.color = limitRange.textColor
limitRangePaint.typeface = limitRange.typeface
limitRangePaint.strokeWidth = 0.5f
limitRangePaint.textSize = limitRange.textSize

val labelLineHeight = limitRangePaint.calcTextHeight(label).toFloat()
val xOffset = 4f.convertDpToPixel() + limitRange.xOffset
val yOffset = limitRange.lineWidth + labelLineHeight + limitRange.yOffset

val position = limitRange.labelPosition

when (position) {
LimitLabelPosition.RIGHT_TOP -> {
limitRangePaint.textAlign = Align.RIGHT
canvas.drawText(
label,
viewPortHandler.contentRight() - xOffset,
ptsr[1] - yOffset + labelLineHeight, limitRangePaint
)
}
limitRangePaint.style = Paint.Style.STROKE
limitRangePaint.color = limitRange.lineColor
limitRangePaint.strokeWidth = limitRange.lineWidth
limitRangePaint.pathEffect = limitRange.dashPathEffect

limitRangePaintFill.style = Paint.Style.FILL
limitRangePaintFill.color = limitRange.rangeColor

ptsr[1] = limitRange.limit.high
ptsr2[1] = limitRange.limit.low

transformer?.pointValuesToPixel(ptsr)
transformer?.pointValuesToPixel(ptsr2)

limitRangePathFill.moveTo(viewPortHandler.contentLeft(), ptsr[1])
limitRangePathFill.addRect(
viewPortHandler.contentLeft(),
ptsr[1],
viewPortHandler.contentRight(),
ptsr2[1],
Path.Direction.CW
)
canvas.drawPath(limitRangePathFill, limitRangePaintFill)
limitRangePathFill.reset()

if (limitRange.lineWidth > 0) {
limitRangePath.moveTo(viewPortHandler.contentLeft(), ptsr[1])
limitRangePath.lineTo(viewPortHandler.contentRight(), ptsr[1])
canvas.drawPath(limitRangePath, limitRangePaint)

limitRangePath.moveTo(viewPortHandler.contentLeft(), ptsr2[1])
limitRangePath.lineTo(viewPortHandler.contentRight(), ptsr2[1])
canvas.drawPath(limitRangePath, limitRangePaint)
}

LimitLabelPosition.RIGHT_BOTTOM -> {
limitRangePaint.textAlign = Align.RIGHT
canvas.drawText(
label,
viewPortHandler.contentRight() - xOffset,
ptsr[1] + yOffset, limitRangePaint
)
}
limitRangePath.reset()

LimitLabelPosition.LEFT_TOP -> {
limitRangePaint.textAlign = Align.LEFT
canvas.drawText(
label,
viewPortHandler.contentLeft() + xOffset,
ptsr[1] - yOffset + labelLineHeight, limitRangePaint
)
}
val label = limitRange.label

// if drawing the limit-value label is enabled
if (label != null && label != "") {
limitRangePaint.style = limitRange.textStyle
limitRangePaint.pathEffect = null
limitRangePaint.color = limitRange.textColor
limitRangePaint.typeface = limitRange.typeface
limitRangePaint.strokeWidth = 0.5f
limitRangePaint.textSize = limitRange.textSize

val labelLineHeight = limitRangePaint.calcTextHeight(label).toFloat()
val xOffset = 4f.convertDpToPixel() + limitRange.xOffset
val yOffset = limitRange.lineWidth + labelLineHeight + limitRange.yOffset

val position = limitRange.labelPosition

when (position) {
LimitLabelPosition.RIGHT_TOP -> {
limitRangePaint.textAlign = Align.RIGHT
canvas.drawText(
label,
viewPortHandler.contentRight() - xOffset,
ptsr[1] - yOffset + labelLineHeight, limitRangePaint
)
}

LimitLabelPosition.RIGHT_BOTTOM -> {
limitRangePaint.textAlign = Align.RIGHT
canvas.drawText(
label,
viewPortHandler.contentRight() - xOffset,
ptsr[1] + yOffset, limitRangePaint
)
}

else -> {
limitRangePaint.textAlign = Align.LEFT
canvas.drawText(
label,
viewPortHandler.offsetLeft() + xOffset,
ptsr[1] + yOffset, limitRangePaint
)
LimitLabelPosition.LEFT_TOP -> {
limitRangePaint.textAlign = Align.LEFT
canvas.drawText(
label,
viewPortHandler.contentLeft() + xOffset,
ptsr[1] - yOffset + labelLineHeight, limitRangePaint
)
}

else -> {
limitRangePaint.textAlign = Align.LEFT
canvas.drawText(
label,
viewPortHandler.offsetLeft() + xOffset,
ptsr[1] + yOffset, limitRangePaint
)
}
}
}
}

canvas.restoreToCount(clipRestoreCount)
}
}
}
}
Expand Down
15 changes: 7 additions & 8 deletions chartLib/src/main/kotlin/info/appdev/charting/utils/Fill.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import android.graphics.RectF
import android.graphics.Shader
import android.graphics.drawable.Drawable
import kotlin.math.floor
import androidx.core.graphics.withClip

open class Fill {
enum class Type {
Expand Down Expand Up @@ -90,12 +91,11 @@ open class Fill {
}

if (this.isClipPathSupported) {
val save = canvas.save()
canvas.withClip(left, top, right, bottom) {

canvas.clipRect(left, top, right, bottom)
canvas.drawColor(mFinalColor!!)
canvas.drawColor(mFinalColor!!)

canvas.restoreToCount(save)
}
} else {
// save
val previous = paint.style
Expand Down Expand Up @@ -171,12 +171,11 @@ open class Fill {
}

if (clipRect != null && this.isClipPathSupported) {
val save = canvas.save()
canvas.withClip(path) {

canvas.clipPath(path)
canvas.drawColor(mFinalColor!!)
canvas.drawColor(mFinalColor!!)

canvas.restoreToCount(save)
}
} else {
// save
val previous = paint.style
Expand Down
Loading