android 畫圖表的第三方工具
(四)scatterChart(散佈圖)
implementation 'com.github.PhilJay:MPAndroidChart:v3.0.3'
XML中建立view
<com.github.mikephil.charting.charts.ScatterChart
android:id="@+id/bar_ScatterChart"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"></com.github.mikephil.charting.charts.ScatterChart>
Activity中設定
val scatterChart = bar_ScatterChart
建立資料
val entries = ArrayList()
entries.add(Entry(1f,10f))
entries.add(Entry(2f,20f))
entries.add(Entry(3f,15f))
entries.add(Entry(4f,25f))
entries.add(Entry(5f,11f))
entries.add(Entry(6f,21f))
entries.add(Entry(7f,12f))
entries.add(Entry(8f,22f))
entries.add(Entry(9f,13f))
Entry格式為<X軸值,Y軸值>
建立dataset
val dataSet = ScatterDataSet(entries,"label")
建立data 加入scatterchart
val data = ScatterData(dataSet)
scatterChart.data = data
scatterChart.invalidate()
scatterChart.xAxis.position =XAxis.XAxisPosition.BOTTOM
scatterChart.axisRight.isEnabled = false
scatterChart.axisLeft.axisMinimum=0f
完工