Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide a way to let tooltips draw values #298

Open
joel-apollo opened this issue Feb 26, 2021 · 1 comment
Open

Provide a way to let tooltips draw values #298

joel-apollo opened this issue Feb 26, 2021 · 1 comment

Comments

@joel-apollo
Copy link

Using the code below, I almost had nice tooltips per bar, something like this:
almost tooltips

That is, till I realized the x and y aren't the actual values, just scaled values etc. Can we pass tooltips the x,y values or pair of the point being drawn?

@ExperimentalFeature
class TextToolTip(
    val textSize: Float = 16f,
    @ColorRes val textColor: Int? = null,
    val typeface: Int = Typeface.NORMAL,
    val pixelsAboveBar: Float = 8f,
    val labelsFormatter: ((Float) -> String)?) : Tooltip {

    private lateinit var tooltipView: TextView

    override fun onCreateTooltip(parentView: ViewGroup) {
        tooltipView = TextView(parentView.context)
        tooltipView.layoutParams = ViewGroup.LayoutParams(wrapContent, wrapContent)
        tooltipView.visibility = View.INVISIBLE
        tooltipView.textSize = textSize
        textColor?.let { tooltipView.textColor = ContextCompat.getColor(parentView.context, it) }
        tooltipView.setTypeface(tooltipView.typeface, typeface)
        parentView.addView(tooltipView)
    }

    override fun onDataPointTouch(x: Float, y: Float) {
        tooltipView.visibility = View.VISIBLE
        tooltipView.x = x - tooltipView.width / 2
        tooltipView.y = y - tooltipView.context.dips(pixelsAboveBar)
        tooltipView.text = labelsFormatter?.let { it(y) } ?: y.toString()
    }

    override fun onDataPointClick(x: Float, y: Float) {}

}

fun Context.dips(dipValue: Float) =
    TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dipValue, resources.displayMetrics)

@diogobernardino
Copy link
Owner

That is, till I realized the x and y aren't the actual values, just scaled values etc.

x and y are the chart view coordinates of a given point.

Can we pass tooltips the x,y values or pair of the point being drawn?

Passing the index of the pair might be enough to fulfill this use-case. With that I was able to reach something close to what you need I believe. I will consider it for the next release.

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants