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.components.XAxis;
import
com.github.mikephil.charting.components.YAxis;
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;
import
java.util.ArrayList;
public
class
MainActivity
extends
AppCompatActivity {
private
ScatterChart chart;
@Override
protected
void
onCreate(Bundle savedInstanceState) {
super
.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
chart = findViewById(R.id.chart1);
chart.getDescription().setEnabled(
false
);
chart.setDrawGridBackground(
false
);
chart.setTouchEnabled(
true
);
chart.setMaxHighlightDistance(50f);
chart.setDragEnabled(
true
);
chart.setScaleEnabled(
true
);
chart.setMaxVisibleValueCount(
200
);
chart.setPinchZoom(
true
);
Legend l = chart.getLegend();
l.setVerticalAlignment(Legend.LegendVerticalAlignment.TOP);
l.setHorizontalAlignment(Legend.LegendHorizontalAlignment.RIGHT);
l.setOrientation(Legend.LegendOrientation.VERTICAL);
l.setDrawInside(
false
);
l.setXOffset(5f);
YAxis yl = chart.getAxisLeft();
yl.setAxisMinimum(0f);
chart.getAxisRight().setEnabled(
false
);
XAxis xl = chart.getXAxis();
xl.setDrawGridLines(
false
);
ArrayList<Entry> values1 =
new
ArrayList<>();
ArrayList<Entry> values2 =
new
ArrayList<>();
ArrayList<Entry> values3 =
new
ArrayList<>();
for
(
int
i =
0
; i <
11
; i++) {
values1.add(
new
Entry(i, (i *
2
)));
}
for
(
int
i =
11
; i <
21
; i++) {
values2.add(
new
Entry(i, (i *
3
)));
}
for
(
int
i =
21
; i <
31
; i++) {
values3.add(
new
Entry(i, (i *
4
)));
}
ScatterDataSet set1 =
new
ScatterDataSet(values1,
"Point 1"
);
set1.setScatterShape(ScatterChart.ScatterShape.SQUARE);
set1.setColor(ColorTemplate.COLORFUL_COLORS[
0
]);
ScatterDataSet set2 =
new
ScatterDataSet(values2,
"Point 2"
);
set2.setScatterShape(ScatterChart.ScatterShape.CIRCLE);
set2.setScatterShapeHoleColor(ColorTemplate.COLORFUL_COLORS[
3
]);
set2.setScatterShapeHoleRadius(3f);
set2.setColor(ColorTemplate.COLORFUL_COLORS[
1
]);
ScatterDataSet set3 =
new
ScatterDataSet(values3,
"Point 3"
);
set3.setColor(ColorTemplate.COLORFUL_COLORS[
2
]);
set1.setScatterShapeSize(8f);
set2.setScatterShapeSize(8f);
set3.setScatterShapeSize(8f);
ArrayList<IScatterDataSet> dataSets =
new
ArrayList<>();
dataSets.add(set1);
dataSets.add(set2);
dataSets.add(set3);
ScatterData data =
new
ScatterData(dataSets);
chart.setData(data);
chart.invalidate();
}
}