|
|
|
@ -9,7 +9,10 @@ import android.util.AttributeSet
|
|
|
|
import android.view.GestureDetector
|
|
|
|
import android.view.GestureDetector
|
|
|
|
import android.view.MotionEvent
|
|
|
|
import android.view.MotionEvent
|
|
|
|
import android.view.View
|
|
|
|
import android.view.View
|
|
|
|
|
|
|
|
import androidx.appcompat.widget.TooltipCompat
|
|
|
|
import androidx.core.graphics.ColorUtils
|
|
|
|
import androidx.core.graphics.ColorUtils
|
|
|
|
|
|
|
|
import androidx.core.view.PointerIconCompat
|
|
|
|
|
|
|
|
import androidx.core.view.ViewCompat
|
|
|
|
import org.koitharu.kotatsu.core.util.ext.getThemeColor
|
|
|
|
import org.koitharu.kotatsu.core.util.ext.getThemeColor
|
|
|
|
import org.koitharu.kotatsu.core.util.ext.resolveDp
|
|
|
|
import org.koitharu.kotatsu.core.util.ext.resolveDp
|
|
|
|
import org.koitharu.kotatsu.parsers.util.replaceWith
|
|
|
|
import org.koitharu.kotatsu.parsers.util.replaceWith
|
|
|
|
@ -21,10 +24,12 @@ class PieChartView @JvmOverloads constructor(
|
|
|
|
) : View(context, attrs, defStyleAttr), GestureDetector.OnGestureListener {
|
|
|
|
) : View(context, attrs, defStyleAttr), GestureDetector.OnGestureListener {
|
|
|
|
|
|
|
|
|
|
|
|
private val paint = Paint(Paint.ANTI_ALIAS_FLAG)
|
|
|
|
private val paint = Paint(Paint.ANTI_ALIAS_FLAG)
|
|
|
|
|
|
|
|
private val activePointerIcon = PointerIconCompat.getSystemIcon(context, PointerIconCompat.TYPE_HAND)
|
|
|
|
private val segments = ArrayList<Segment>()
|
|
|
|
private val segments = ArrayList<Segment>()
|
|
|
|
private val chartBounds = RectF()
|
|
|
|
private val chartBounds = RectF()
|
|
|
|
private val clearColor = context.getThemeColor(android.R.attr.colorBackground)
|
|
|
|
private val clearColor = context.getThemeColor(android.R.attr.colorBackground)
|
|
|
|
private val touchDetector = GestureDetector(context, this)
|
|
|
|
private val touchDetector = GestureDetector(context, this)
|
|
|
|
|
|
|
|
private var hoverSegment = -1
|
|
|
|
private var highlightedSegment = -1
|
|
|
|
private var highlightedSegment = -1
|
|
|
|
|
|
|
|
|
|
|
|
var onSegmentClickListener: OnSegmentClickListener? = null
|
|
|
|
var onSegmentClickListener: OnSegmentClickListener? = null
|
|
|
|
@ -41,6 +46,8 @@ class PieChartView @JvmOverloads constructor(
|
|
|
|
paint.color = segment.color
|
|
|
|
paint.color = segment.color
|
|
|
|
if (i == highlightedSegment) {
|
|
|
|
if (i == highlightedSegment) {
|
|
|
|
paint.color = ColorUtils.setAlphaComponent(paint.color, 180)
|
|
|
|
paint.color = ColorUtils.setAlphaComponent(paint.color, 180)
|
|
|
|
|
|
|
|
} else if (i == hoverSegment) {
|
|
|
|
|
|
|
|
paint.color = ColorUtils.setAlphaComponent(paint.color, 200)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
paint.style = Paint.Style.FILL
|
|
|
|
paint.style = Paint.Style.FILL
|
|
|
|
val sweepAngle = segment.percent * 360f
|
|
|
|
val sweepAngle = segment.percent * 360f
|
|
|
|
@ -119,6 +126,23 @@ class PieChartView @JvmOverloads constructor(
|
|
|
|
|
|
|
|
|
|
|
|
override fun onFling(e1: MotionEvent?, e2: MotionEvent, velocityX: Float, velocityY: Float): Boolean = false
|
|
|
|
override fun onFling(e1: MotionEvent?, e2: MotionEvent, velocityX: Float, velocityY: Float): Boolean = false
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
override fun onHoverEvent(event: MotionEvent): Boolean {
|
|
|
|
|
|
|
|
val segment = when (event.actionMasked) {
|
|
|
|
|
|
|
|
MotionEvent.ACTION_HOVER_ENTER,
|
|
|
|
|
|
|
|
MotionEvent.ACTION_HOVER_MOVE -> findSegmentIndex(event.x, event.y)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
MotionEvent.ACTION_HOVER_EXIT -> -1
|
|
|
|
|
|
|
|
else -> hoverSegment
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if (hoverSegment != segment) {
|
|
|
|
|
|
|
|
hoverSegment = segment
|
|
|
|
|
|
|
|
TooltipCompat.setTooltipText(this, segments.getOrNull(segment)?.label)
|
|
|
|
|
|
|
|
ViewCompat.setPointerIcon(this, if (segment == -1) null else activePointerIcon)
|
|
|
|
|
|
|
|
invalidate()
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return super.onHoverEvent(event) || segment != -1
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
fun setData(value: List<Segment>) {
|
|
|
|
fun setData(value: List<Segment>) {
|
|
|
|
segments.replaceWith(value)
|
|
|
|
segments.replaceWith(value)
|
|
|
|
invalidate()
|
|
|
|
invalidate()
|
|
|
|
|