这次是在上一篇的基础上增加的,所以导包这些啥的就跳过了研究了一下代码,发现主要的区别就在于增加data的时候,第二个参数传递的是一个数组,然后就变成了堆叠条形图。
最后的代码:
XML布局文件:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <LinearLayout android:layout_width="match_parent" android:layout_height="50dp"> <TextView android:layout_width="match_parent" android:layout_height="match_parent" android:text="这是一个柱状图" android:gravity="center"/> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="150dp" android:orientation="vertical"> <com.github.mikephil.charting.charts.BarChart android:id="@+id/barChart" android:layout_width="match_parent" android:layout_height="150dp" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="50dp"> <TextView android:layout_width="match_parent" android:layout_height="match_parent" android:text="这是一个堆叠条形图" android:gravity="center"/> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="150dp" android:orientation="vertical"> <com.github.mikephil.charting.charts.BarChart android:id="@+id/duiDieChart" android:layout_width="match_parent" android:layout_height="150dp" /> </LinearLayout> </LinearLayout>
MainActivity,这里只把堆叠图的代码放出来了,之前的看上一篇文章
public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { BarChart duiDieChart = findViewById(R.id.duiDieChart); duiDieChart.getDescription().setEnabled(false); duiDieChart.setMaxVisibleValueCount(40); // 扩展现在只能分别在x轴和y轴 duiDieChart.setPinchZoom(false); duiDieChart.setDrawGridBackground(false); duiDieChart.setDrawBarShadow(false); duiDieChart.setDrawValueAboveBar(false); duiDieChart.setHighlightFullBarEnabled(false); // 改变y标签的位置 YAxis leftAxis = duiDieChart.getAxisLeft(); leftAxis.setDrawGridLines(false); leftAxis.setAxisMinimum(0f); duiDieChart.getAxisRight().setEnabled(false); XAxis xLabels = duiDieChart.getXAxis(); xLabels.setDrawGridLines(true); xLabels.setPosition(XAxis.XAxisPosition.TOP); Legend l = duiDieChart.getLegend(); l.setVerticalAlignment(Legend.LegendVerticalAlignment.BOTTOM); l.setHorizontalAlignment(Legend.LegendHorizontalAlignment.RIGHT); l.setOrientation(Legend.LegendOrientation.HORIZONTAL); l.setDrawInside(false); l.setFormSize(8f); l.setFormToTextSpace(4f); l.setXEntrySpace(6f); ArrayList<BarEntry> weiZhangZhanBi = new ArrayList<>(); for (int i = 0; i <= 5; i++) { float a = new Random().nextInt(400); float b = new Random().nextInt(400); weiZhangZhanBi.add(new BarEntry(i, new float[]{a, b})); } BarDataSet set1; if (duiDieChart.getData() != null && duiDieChart.getData().getDataSetCount() > 0) { set1 = (BarDataSet) duiDieChart.getData().getDataSetByIndex(0); set1.setValues(weiZhangZhanBi); duiDieChart.getData().notifyDataChanged(); duiDieChart.notifyDataSetChanged(); } else { set1 = new BarDataSet(weiZhangZhanBi, "年龄群体车辆违章的占比统计 "); set1.setColors(getColors()); set1.setStackLabels(new String[]{"有违章", "无违章"}); ArrayList<IBarDataSet> dataSets = new ArrayList<IBarDataSet>(); dataSets.add(set1); BarData data = new BarData(dataSets); data.setValueTextColor(Color.WHITE); duiDieChart.setData(data); } duiDieChart.setFitBars(true); duiDieChart.invalidate(); } }
看着这篇文章来的:https://blog.csdn.net/qq_26787115/article/details/53323046
评论 (0)