概要
チャートタップ時に、ツールチップ(下画像で3.0,7.0
と描画されているビュー)を表示させる。
グラフの描画については、この記事を参照。
方法
ツールチップのレイアウト作成。
marker_view.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/marker_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/gray"
android:padding="8dp"
tools:text="1.0, 2.0"/>
処理の部分。MarkerView
を使用。
MainActivity.kt
val mv = object : MarkerView(this, R.layout.marker_view) {
override fun refreshContent(e: Entry?, highlight: Highlight?) {
val textView: TextView = findViewById(R.id.marker_text)
textView.text = "${e?.x}, ${e?.y}"
super.refreshContent(e, highlight)
}
override fun getOffset(): MPPointF {
return MPPointF(-width / 2f, -height - 20f)
}
}
mv.chartView = binding.chart
binding.chart.marker = mv