Android 第三方框架MPAndroidChart-v3.1.0的简单使用
前言
文章内容主要是如何去用条形图、折线图、饼状图、水平条形图。
一些方法的的使用意思都是本人自己理解的仅供大家学习参考!
引入库
GitHub地址:https://github.com/PhilJay/MPAndroidChart
'com.github.PhilJay:MPAndroidChart:v3.1.0'
布局
<com.github.mikephil.charting.charts.BarChart
android:id="@+id/barchart"
android:layout_width="match_parent"
android:layout_height="300dp" />
<com.github.mikephil.charting.charts.LineChart
android:id="@+id/linechart"
android:layout_width="match_parent"
android:layout_height="300dp" />
<com.github.mikephil.charting.charts.PieChart
android:id="@+id/pieChart"
android:layout_width="match_parent"
android:layout_height="300dp" />
<com.github.mikephil.charting.charts.HorizontalBarChart
android:id="@+id/Hbarchart"
android:layout_width="match_parent"
android:layout_height="300dp" />
初始化动画
Hbarchart.animateX(500);
linechart.animateXY(500,500);
pieChart.animateXY(500,500);
barchart.animateXY(500,500);
条形图
代码
barchart.getXAxis().setPosition(XAxis.XAxisPosition.BOTTOM);
barchart.getAxisRight().setEnabled(false);
barchart.getDescription().setEnabled(false);
List<BarEntry> barEntries1 = new ArrayList<>();
List<BarEntry> barEntries2 = new ArrayList<>();
List<String> stringList = new ArrayList<>();
for (int i = 0; i < 5; i++) {
barEntries1.add(new BarEntry(i, new Random().nextInt(100)));
barEntries2.add(new BarEntry((float) (i+0.3), new Random().nextInt(100)));
stringList.add("测试" + i);
}
BarDataSet barDataSet1 = new BarDataSet(barEntries1, "测试1");
BarDataSet barDataSet2 = new BarDataSet(barEntries2, "测试2");
barDataSet1.setColor(getResources().getColor(R.color.red));
barDataSet2.setColor(getResources().getColor(R.color.blue));
BarData barData = new BarData(barDataSet1, barDataSet2);
barData.setBarWidth(0.3f);
barchart.getXAxis().setValueFormatter(new IndexAxisValueFormatter(stringList));
barchart.getXAxis().setGranularity(1);
barchart.setData(barData);
效果
折线图
代码
linechart.getXAxis().setPosition(XAxis.XAxisPosition.BOTTOM);
linechart.getAxisRight().setEnabled(false);
linechart.getDescription().setEnabled(false);
List<Entry> list = new ArrayList<>();
List<String> stringList = new ArrayList<>();
for (int i = 0; i < 5; i++) {
list.add(new Entry(i, new Random().nextInt(100)));
stringList.add("测试" + i);
}
linechart.getXAxis().setGranularity(1);
linechart.getXAxis().setValueFormatter(new IndexAxisValueFormatter(stringList));
LineDataSet lineDataSet = new LineDataSet(list, "测试");
LineData lineData = new LineData(lineDataSet);
linechart.setData(lineData);
效果
饼状图
代码
pieChart.getDescription().setEnabled(false);
List<PieEntry> list = new ArrayList<>();
for (int i = 0; i < 5; i++) {
list.add(new PieEntry(new Random().nextInt(100), "测试" + i));
}
PieDataSet pieDataSet = new PieDataSet(list, "图例1");
List<Integer> colors = new ArrayList<>();
colors.add(getResources().getColor(R.color.red));
colors.add(getResources().getColor(R.color.blue));
colors.add(getResources().getColor(R.color.orang));
pieDataSet.setColors(colors);
pieDataSet.setXValuePosition(PieDataSet.ValuePosition.OUTSIDE_SLICE);
pieDataSet.setYValuePosition(PieDataSet.ValuePosition.OUTSIDE_SLICE);
PieData pieData = new PieData(pieDataSet);
pieData.setValueFormatter(new PercentFormatter(pieChart));
pieChart.setUsePercentValues(true);
pieData.setValueTextColor(Color.BLACK);
pieData.setValueTextSize(17);
pieChart.setEntryLabelColor(Color.BLACK);
pieChart.setData(pieData);
效果
水平条形图(和条形图的用法差不多)
水平条形图可以想成就是将一个条形图进行了顺时针90°的旋转形成的
代码
Hbarchart.getXAxis().setPosition(XAxis.XAxisPosition.BOTTOM);
Hbarchart.getAxisRight().setEnabled(false);
Hbarchart.getDescription().setEnabled(false);
List<BarEntry> list = new ArrayList<>();
List<String> stringList = new ArrayList<>();
for (int i = 0; i < 5; i++) {
list.add(new BarEntry(i, new Random().nextInt(100)));
stringList.add("测试" + i);
}
BarDataSet barDataSet = new BarDataSet(list, "测试");
barDataSet.setColor(getResources().getColor(R.color.red));
BarData barData = new BarData(barDataSet);
Hbarchart.getXAxis().setValueFormatter(new IndexAxisValueFormatter(stringList));
Hbarchart.getXAxis().setGranularity(1);
Hbarchart.setData(barData);
效果
最后希望这篇文章能帮助到各位!!!
|