前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >安卓MPAndroidChart绘制多层级的堆叠条形图

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

作者头像
SingYi
发布2022-07-14 13:54:43
1.1K0
发布2022-07-14 13:54:43
举报
文章被收录于专栏:Lan小站Lan小站
image.png
image.png

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

image.png
image.png

最后的代码:

XML布局文件:

代码语言:javascript
复制
<?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,这里只把堆叠图的代码放出来了,之前的看上一篇文章

代码语言:javascript
复制
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

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档