引入MPAndroidChart
1 2 3 4 5
| allprojects { repositories { maven { url "https://jitpack.io" } } }
|
1 2 3
| dependencies { implementation 'com.github.PhilJay:MPAndroidChart:v3.0.3' }
|
线型图
最简单的线型图
在布局文件中添加<com.github.mikephil.charting.charts.LineChart/>标签
1 2 3 4 5 6 7 8 9 10
| <?xml version="1.0" encoding="utf-8"?> <LinearLayout android:layout_height="match_parent" android:layout_width="match_parent" android:orientation="vertical" xmlns:android="http://schemas.android.com/apk/res/android"> <com.github.mikephil.charting.charts.LineChart android:id="@+id/lineChart" android:layout_width="match_parent" android:layout_height="match_parent"/> </LinearLayout>
|
获取View,设置数据,一个简单的线型图就成了
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
| public class LineActivity extends AppCompatActivity { private LineChart lineChart;
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_line); init(); }
private void init() { lineChart = findViewById(R.id.lineChart);
lineChart.setData(setData()); }
private LineData setData() {
List<Entry> entries = new ArrayList<>(); for (int i = 0; i < 10; i++) {
entries.add(new Entry(i,new Random().nextInt(100))); }
LineDataSet lineDataSet = new LineDataSet(entries,"线型图测试");
LineData lineData = new LineData(lineDataSet); return lineData; } }
|
但是现在不好看
设置属性
主要用于演示
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
| public class LineActivity extends AppCompatActivity { private LineChart lineChart; private final String[] strings = new String[]{"测试1","测试2","测试3","测试4","测试5","测试6","测试7","测试8","测试9","测试10"};
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_line); init(); }
private void init() { lineChart = findViewById(R.id.lineChart);
lineChart.setData(setData()); lineChart.invalidate(); lineChart.setDescription(null);
lineChart.setDrawBorders(true); lineChart.setBorderWidth(1); lineChart.setBorderColor(Color.RED);
lineChart.setTouchEnabled(true); lineChart.setScaleEnabled(true); lineChart.setPinchZoom(true); lineChart.setDragEnabled(true);
XAxis xAxis = lineChart.getXAxis(); xAxis.setPosition(XAxis.XAxisPosition.BOTTOM); xAxis.setDrawGridLines(false); xAxis.setDrawAxisLine(true); xAxis.setAxisLineWidth(1); xAxis.setAxisMinimum(0); xAxis.setDrawLabels(true); xAxis.setLabelCount(strings.length); xAxis.setGranularity(1); xAxis.setEnabled(true);
xAxis.setValueFormatter(new ValueFormatter() { @Override public String getFormattedValue(float value) { return strings[(int) (value % strings.length)]; } });
YAxis yAxisRight = lineChart.getAxisRight(); yAxisRight.setAxisMinimum(0); yAxisRight.setDrawGridLines(false); yAxisRight.setDrawAxisLine(true); yAxisRight.setAxisLineWidth(1); yAxisRight.setEnabled(false);
YAxis yAxisLeft = lineChart.getAxisLeft(); yAxisLeft.setAxisMinimum(0); yAxisLeft.setDrawGridLines(false); yAxisLeft.setDrawAxisLine(true); yAxisLeft.setAxisLineWidth(1); yAxisLeft.setEnabled(true); }
private LineData setData() {
List<Entry> entries = new ArrayList<>(); for (int i = 0; i < 10; i++) {
entries.add(new Entry(i,new Random().nextInt(100))); }
LineDataSet lineDataSet = new LineDataSet(entries,"线型图测试"); lineDataSet.setCircleColor(Color.RED); lineDataSet.setColor(Color.BLUE);
LineData lineData = new LineData(lineDataSet);
return lineData; } }
|
现在的效果
常规配置
再来看一下常规配置的效果和代码(布局不变)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
| public class LineActivity extends AppCompatActivity { private LineChart lineChart; private final String[] strings = new String[]{"测试1","测试2","测试3","测试4","测试5","测试6","测试7","测试8","测试9","测试10"};
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_line);
lineChart = findViewById(R.id.lineChart); lineChart.setTouchEnabled(false); lineChart.setDescription(null); lineChart.setData(new LineData()); addData("数据1",Color.BLACK); addData("数据2",Color.BLUE); setAxis(); }
private void setAxis() { XAxis xAxis = lineChart.getXAxis(); xAxis.setDrawGridLines(false); xAxis.setPosition(XAxis.XAxisPosition.BOTTOM); xAxis.setDrawAxisLine(true); xAxis.setAxisMinimum(0); xAxis.setLabelCount(strings.length); xAxis.setGranularity(1); xAxis.setAxisLineWidth(1); xAxis.setValueFormatter(new ValueFormatter() { @Override public String getFormattedValue(float value) { return strings[(int) (value % strings.length)]; } }); xAxis.setEnabled(true);
YAxis yAxisRight = lineChart.getAxisRight(); yAxisRight.setAxisMinimum(0); yAxisRight.setDrawGridLines(false); yAxisRight.setAxisLineWidth(1); yAxisRight.setDrawAxisLine(true); yAxisRight.setEnabled(false);
YAxis yAxisLeft = lineChart.getAxisLeft(); yAxisLeft.setDrawGridLines(false); yAxisLeft.setAxisMinimum(0); yAxisLeft.setAxisLineWidth(1); yAxisLeft.setDrawAxisLine(true); yAxisLeft.setEnabled(true); }
private void addData(String dataName,int color) { LineData lineData = lineChart.getData(); List<Entry> entries = new ArrayList<>(); for (int i = 0; i < strings.length; i++) { entries.add(new Entry(i,new Random().nextInt(100))); } LineDataSet lineDataSet = new LineDataSet(entries,dataName); lineDataSet.setColor(color); lineDataSet.setCircleColor(Color.RED); lineDataSet.setValueTextColor(Color.RED); lineData.addDataSet(lineDataSet); lineChart.notifyDataSetChanged(); lineChart.invalidate(); } }
|
柱状图
最简单的柱状图
在布局文件中添加<com.github.mikephil.charting.charts.BarChart/>标签
1 2 3 4 5 6 7 8 9 10
| <?xml version="1.0" encoding="utf-8"?> <LinearLayout android:layout_height="match_parent" android:layout_width="match_parent" android:orientation="vertical" xmlns:android="http://schemas.android.com/apk/res/android"> <com.github.mikephil.charting.charts.BarChart android:id="@+id/barChart" android:layout_width="match_parent" android:layout_height="match_parent"/> </LinearLayout>
|
获取View,设置数据
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| public class BarActivity extends AppCompatActivity { private BarChart barChart;
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_bar); barChart = findViewById(R.id.barChart); barChart.setData(setData()); }
private BarData setData() { List<BarEntry> barEntries = new ArrayList<>(); for (int i = 0; i < strings.length; i++) { barEntries.add(new BarEntry(i,new Random().nextInt(100))); }
BarDataSet barDataSet = new BarDataSet(barEntries,"测试数据"); BarData barData = new BarData(barDataSet); return barData; } }
|
基础效果
设置属性
其实属性都差不多,只要英语学的好,记得很快
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
| public class BarActivity extends AppCompatActivity { private BarChart barChart; private final String[] strings = new String[]{"测试1","测试2","测试3","测试4","测试5","测试6","测试7","测试8","测试9","测试10"};
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_bar); barChart = findViewById(R.id.barChart); barChart.setData(new BarData()); setData("数据1",Color.BLACK); }
private void setData(String dataName,int color) { BarData barData = barChart.getData(); barChart.setDescription(null); barChart.setScaleEnabled(false); barChart.setDrawBarShadow(false);
XAxis xAxis = barChart.getXAxis(); xAxis.setPosition(XAxis.XAxisPosition.BOTTOM); xAxis.setDrawGridLines(false); xAxis.setAxisLineWidth(1); xAxis.setAxisLineColor(Color.BLACK); xAxis.setDrawAxisLine(true); xAxis.setAxisMinimum(0); xAxis.setGranularity(1); xAxis.setLabelCount(strings.length); xAxis.setValueFormatter(new ValueFormatter() { @Override public String getFormattedValue(float value) { return strings[(int) (value % strings.length)]; } }); xAxis.setEnabled(true);
YAxis yAxisRight = barChart.getAxisRight(); yAxisRight.setAxisLineWidth(1); yAxisRight.setDrawGridLines(false); yAxisRight.setAxisMinimum(0); yAxisRight.setDrawAxisLine(true); yAxisRight.setLabelCount(strings.length); yAxisRight.setEnabled(false);
YAxis yAxisLeft = barChart.getAxisLeft(); yAxisLeft.setDrawAxisLine(true); yAxisLeft.setAxisLineWidth(1); yAxisLeft.setAxisMinimum(0); yAxisLeft.setDrawGridLines(false); yAxisLeft.setLabelCount(strings.length); yAxisLeft.setEnabled(true);
List<BarEntry> barEntries = new ArrayList<>(); for (int i = 0; i < strings.length; i++) { barEntries.add(new BarEntry(i,new Random().nextInt(100))); }
BarDataSet barDataSet = new BarDataSet(barEntries,dataName); barDataSet.setValueTextColor(Color.RED); barDataSet.setColor(color); barData.addDataSet(barDataSet); barChart.notifyDataSetChanged(); barChart.invalidate(); } }
|
分组显示柱子,设置多柱图
设置多柱图一定要给X轴轴线设置最小值并根据x轴的个数比例设置最大值,否则柱子将会失去宽度比例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
| public class BarActivity extends AppCompatActivity { private BarChart barChart; private final String[] strings = new String[]{"测试1","测试2","测试3","测试4","测试5","测试6","测试7","测试8","测试9","测试10"}; private float groupSpace = 0.04f,barSpace = 0.03f,barWidth = 0.45f;
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_bar); barChart = findViewById(R.id.barChart); barChart.setData(new BarData()); setData("数据1",Color.BLACK); setData("数据2",Color.BLUE);
barChart.getBarData().setBarWidth(barWidth); barChart.groupBars(0, groupSpace, barSpace); }
private void setData(String dataName,int color) { BarData barData = barChart.getData(); barChart.setDescription(null); barChart.setScaleEnabled(false); barChart.setDrawBarShadow(false);
XAxis xAxis = barChart.getXAxis(); xAxis.setPosition(XAxis.XAxisPosition.BOTTOM); xAxis.setDrawGridLines(false); xAxis.setAxisLineWidth(1); xAxis.setAxisLineColor(Color.BLACK); xAxis.setDrawAxisLine(true); xAxis.setAxisMinimum(0); xAxis.setAxisMaximum(10); xAxis.setGranularity(1); xAxis.setLabelCount(strings.length); xAxis.setValueFormatter(new ValueFormatter() { @Override public String getFormattedValue(float value) { if ((int)value == -1){
value = 1; } return strings[(int) value % strings.length]; } }); xAxis.setCenterAxisLabels(true); xAxis.setEnabled(true);
YAxis yAxisRight = barChart.getAxisRight(); yAxisRight.setAxisLineWidth(1); yAxisRight.setDrawGridLines(false); yAxisRight.setAxisMinimum(0); yAxisRight.setDrawAxisLine(true); yAxisRight.setEnabled(false);
YAxis yAxisLeft = barChart.getAxisLeft(); yAxisLeft.setDrawAxisLine(true); yAxisLeft.setAxisLineWidth(1); yAxisLeft.setAxisMinimum(0); yAxisLeft.setDrawGridLines(false); yAxisLeft.setEnabled(true);
List<BarEntry> barEntries = new ArrayList<>(); for (int i = 0; i < strings.length; i++) { barEntries.add(new BarEntry(i,new Random().nextInt(100))); }
BarDataSet barDataSet = new BarDataSet(barEntries,dataName); barDataSet.setValueTextColor(Color.RED); barDataSet.setColor(color); barData.addDataSet(barDataSet); barChart.notifyDataSetChanged(); barChart.invalidate(); } }
|
效果
饼状图
基础饼状图
在布局文件中添加<com.github.mikephil.charting.charts.PieChart/>标签
随随便便加个数据
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| public class PieActivity extends AppCompatActivity { private PieChart pieChart; private final String[] strings = new String[]{"测试1","测试2","测试3","测试4","测试5","测试6","测试7","测试8","测试9","测试10"};
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_pie); pieChart = findViewById(R.id.pieChart); pieChart.setData(setData()); pieChart.notifyDataSetChanged(); pieChart.invalidate(); }
private PieData setData() { List<PieEntry> pieEntries = new ArrayList<>(); for (int i = 0; i < strings.length; i++) { pieEntries.add(new PieEntry(i,new Random().nextInt(100))); } PieDataSet pieDataSet = new PieDataSet(pieEntries,"测试"); PieData pieData = new PieData(pieDataSet); return pieData; } }
|
效果不好看
常规属性配置
没研究的配置还有很多,参考https://blog.csdn.net/shineflowers/article/details/44701645
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
| public class PieActivity extends AppCompatActivity { private PieChart pieChart;
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_pie); pieChart = findViewById(R.id.pieChart); pieChart.setDescription(null);
pieChart.setHoleRadius(0); pieChart.setTransparentCircleRadius(0); pieChart.setTransparentCircleAlpha(0);
pieChart.setUsePercentValues(true); pieChart.setRotationEnabled(true);
pieChart.setExtraOffsets(20, 15, 20, 15); pieChart.setCenterText("年度支出"); pieChart.setRotationAngle(120); pieChart.animateX(1000, Easing.EaseInOutQuad); addData(); pieChart.invalidate(); }
private void addData() {
List<PieEntry> pieEntries = new ArrayList<>();
pieEntries.add(new PieEntry(new Random().nextInt(100),"第一季度")); pieEntries.add(new PieEntry(new Random().nextInt(100),"第二季度")); pieEntries.add(new PieEntry(new Random().nextInt(100),"第三季度")); pieEntries.add(new PieEntry(new Random().nextInt(100),"第四季度"));
PieDataSet pieDataSet = new PieDataSet(pieEntries,"年支出比"); pieDataSet.setValueTextColor(Color.BLACK); pieDataSet.setValueTextSize(15); pieDataSet.setSliceSpace(1); pieDataSet.setSelectionShift(5);
pieDataSet.setValueLinePart1OffsetPercentage(50f); pieDataSet.setValueLinePart1Length(0.5f); pieDataSet.setValueLinePart2Length(1.3f); pieDataSet.setValueLineColor(Color.BLACK); pieDataSet.setYValuePosition(PieDataSet.ValuePosition.OUTSIDE_SLICE);
setColor(pieDataSet);
PieData pieData = new PieData(pieDataSet); pieData.setValueFormatter(new PercentFormatter(pieChart)); pieChart.setData(pieData); }
private void setColor(PieDataSet pieDataSet) {
List<Integer> colors = new ArrayList<>(); colors.add(Color.BLUE); colors.add(Color.DKGRAY); colors.add(Color.GRAY); colors.add(Color.RED); pieDataSet.setColors(colors); } }
|
效果展示
雷达图
雷达图以后再说,妈的累死了
参考地址: https://blog.csdn.net/wjk343977868/article/details/53316981