Skip to content

BubbleChart Icons and values draw order bug #4746

@Turbozanik

Description

@Turbozanik

Problem: I was unable to make values draw on top of icons i have specified, it happend due to wrong z-index for icons and labels.
bug2
I have fixed this just by switching values and icons draw order. Also, my solution breaks the inheritance principle. Now icons are drawn first, and the on top of them we are drawing values. As far as I see this solution, may affect "wrap_content" item behavior if this library supports such feature. So please take a look and tell me the better solution if there is, if not, i think it will be good to add this to the library.
Hot Fix:

class IconAndValueBubbleChartRenderer(
	chart: BubbleDataProvider, animator: ChartAnimator,
	viewPortHandler: ViewPortHandler
) : BubbleChartRenderer(chart, animator, viewPortHandler){

	override fun drawValues(c: Canvas?) {
		val bubbleData = mChart.bubbleData ?: return
		// if values are drawn
		if (isDrawingValuesAllowed(mChart)) {
			val dataSets = bubbleData.dataSets
			val lineHeight =
				Utils.calcTextHeight(mValuePaint, "1").toFloat()
			for (i in dataSets.indices) {
				val dataSet = dataSets[i]
				if (!shouldDrawValues(dataSet) || dataSet.entryCount < 1) continue
				// apply the text-styling defined by the DataSet
				applyValueTextStyle(dataSet)
				val phaseX =
					Math.max(0f, Math.min(1f, mAnimator.phaseX))
				val phaseY = mAnimator.phaseY
				mXBounds[mChart] = dataSet
				val positions =
					mChart.getTransformer(dataSet.axisDependency)
						.generateTransformedValuesBubble(
							dataSet,
							phaseY,
							mXBounds.min,
							mXBounds.max
						)
				val alpha = if (phaseX == 1f) phaseY else phaseX
				val formatter =
					dataSet.valueFormatter
				val iconsOffset = MPPointF.getInstance(dataSet.iconsOffset)
				iconsOffset.x =
					Utils.convertDpToPixel(iconsOffset.x)
				iconsOffset.y =
					Utils.convertDpToPixel(iconsOffset.y)
				var j = 0
				while (j < positions.size) {
					var valueTextColor = dataSet.getValueTextColor(j / 2 + mXBounds.min)
					valueTextColor = Color.argb(
						Math.round(255f * alpha),
						Color.red(valueTextColor),
						Color.green(valueTextColor),
						Color.blue(valueTextColor)
					)
					val x = positions[j]
					val y = positions[j + 1]
					if (!mViewPortHandler.isInBoundsRight(x)) break
					if (!mViewPortHandler.isInBoundsLeft(x) || !mViewPortHandler.isInBoundsY(y)) {
						j += 2
						continue
					}
					val entry = dataSet.getEntryForIndex(j / 2 + mXBounds.min)
					if (entry.icon != null && dataSet.isDrawIconsEnabled) {
						val icon = entry.icon
						Utils.drawImage(
							c,
							icon,
							(x + iconsOffset.x).toInt(),
							(y + iconsOffset.y).toInt(),
							icon.intrinsicWidth,
							icon.intrinsicHeight
						)
					}
					if (dataSet.isDrawValuesEnabled) {
						drawValue(
							c,
							formatter.getBubbleLabel(entry),
							x,
							y + 0.5f * lineHeight,
							valueTextColor
						)
					}
					j += 2
				}
				MPPointF.recycleInstance(iconsOffset)
			}
		}
	}

}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions