当前位置:网站首页 > 更多 > 编程开发 > 正文

[安卓] 安卓MPAndroidChart绘制多层级的堆叠条形图

作者:CC下载站 日期:2020-10-09 00:00:00 浏览:52 分类:编程开发

这次是在上一篇的基础上增加的,所以导包这些啥的就跳过了研究了一下代码,发现主要的区别就在于增加data的时候,第二个参数传递的是一个数组,然后就变成了堆叠条形图。

最后的代码:

XML布局文件:

<?xmlversion="1.0"encoding="utf-8"?>
<LinearLayoutxmlns: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,这里只把堆叠图的代码放出来了,之前的看上一篇文章

publicclassMainActivityextendsAppCompatActivity{

@Override
protectedvoidonCreate(BundlesavedInstanceState){
BarChartduiDieChart=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标签的位置
YAxisleftAxis=duiDieChart.getAxisLeft();
leftAxis.setDrawGridLines(false);
leftAxis.setAxisMinimum(0f);
duiDieChart.getAxisRight().setEnabled(false);

XAxisxLabels=duiDieChart.getXAxis();
xLabels.setDrawGridLines(true);
xLabels.setPosition(XAxis.XAxisPosition.TOP);

Legendl=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=newArrayList<>();
for(inti=0;i<=5;i++){
floata=newRandom().nextInt(400);
floatb=newRandom().nextInt(400);
weiZhangZhanBi.add(newBarEntry(i,newfloat[]{a,b}));
}
BarDataSetset1;
if(duiDieChart.getData()!=null&&
duiDieChart.getData().getDataSetCount()>0){
set1=(BarDataSet)duiDieChart.getData().getDataSetByIndex(0);
set1.setValues(weiZhangZhanBi);
duiDieChart.getData().notifyDataChanged();
duiDieChart.notifyDataSetChanged();
}else{
set1=newBarDataSet(weiZhangZhanBi,"年龄群体车辆违章的占比统计
");
set1.setColors(getColors());
set1.setStackLabels(newString[]{"有违章","无违章"});
ArrayList<IBarDataSet>dataSets=newArrayList<IBarDataSet>();
dataSets.add(set1);
BarDatadata=newBarData(dataSets);
data.setValueTextColor(Color.WHITE);
duiDieChart.setData(data);
}
duiDieChart.setFitBars(true);
duiDieChart.invalidate();
}
}

看着这篇文章来的:https://blog.csdn.net/qq_26787115/article/details/53323046


您需要 登录账户 后才能发表评论

取消回复欢迎 发表评论:

关灯