package
com.gtappdevelopers.kotlingfgproject
import
android.os.Bundle
import
androidx.appcompat.app.AppCompatActivity
import
com.github.mikephil.charting.charts.ScatterChart
import
com.github.mikephil.charting.components.Legend
import
com.github.mikephil.charting.data.Entry
import
com.github.mikephil.charting.data.ScatterData
import
com.github.mikephil.charting.data.ScatterDataSet
import
com.github.mikephil.charting.interfaces.datasets.IScatterDataSet
import
com.github.mikephil.charting.utils.ColorTemplate
class
MainActivity : AppCompatActivity() {
lateinit var scatteredChart: ScatterChart
override fun onCreate(savedInstanceState: Bundle?) {
super
.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
scatteredChart = findViewById(R.id.idChart)
scatteredChart.description.isEnabled =
false
scatteredChart.setDrawGridBackground(
false
)
scatteredChart.setTouchEnabled(
true
)
scatteredChart.maxHighlightDistance = 50f
scatteredChart.isDragEnabled =
true
scatteredChart.setScaleEnabled(
true
)
scatteredChart.setMaxVisibleValueCount(
200
)
scatteredChart.setPinchZoom(
true
)
val legend = scatteredChart.legend
legend.verticalAlignment = Legend.LegendVerticalAlignment.TOP
legend.horizontalAlignment = Legend.LegendHorizontalAlignment.RIGHT
legend.orientation = Legend.LegendOrientation.VERTICAL
legend.setDrawInside(
false
)
legend.xOffset = 5f
val yl = scatteredChart.axisLeft
yl.axisMinimum = 0f
scatteredChart.axisRight.isEnabled =
false
val xl = scatteredChart.xAxis
xl.setDrawGridLines(
false
)
val values1: ArrayList<Entry> = ArrayList()
val values2: ArrayList<Entry> = ArrayList()
val values3: ArrayList<Entry> = ArrayList()
for
(i in
0
..
10
) {
values1.add(Entry(i.toFloat(), ((i *
2
).toFloat())))
}
for
(i in
11
..
21
) {
values2.add(Entry(i.toFloat(), ((i *
3
).toFloat())))
}
for
(i in
21
..
31
) {
values3.add(Entry(i.toFloat(), ((i *
4
).toFloat())))
}
val set1 = ScatterDataSet(values1,
"Point 1"
)
set1.setColor(ColorTemplate.COLORFUL_COLORS[
0
])
val set2 = ScatterDataSet(values2,
"Point 2"
)
set2.setScatterShape(ScatterChart.ScatterShape.CIRCLE)
set2.scatterShapeHoleColor = ColorTemplate.COLORFUL_COLORS[
3
]
set2.scatterShapeHoleRadius = 3f
set2.setColor(ColorTemplate.COLORFUL_COLORS[
1
])
val set3 = ScatterDataSet(values3,
"Point 3"
)
set3.setColor(ColorTemplate.COLORFUL_COLORS[
2
])
set1.scatterShapeSize = 8f
set2.scatterShapeSize = 8f
set3.scatterShapeSize = 8f
val dataSet: ArrayList<IScatterDataSet> = ArrayList()
dataSet.add(set1)
dataSet.add(set2)
dataSet.add(set3)
val data: ScatterData = ScatterData(dataSet)
scatteredChart.data = data
scatteredChart.invalidate()
}
}