惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

爱范儿
爱范儿
Security Latest
Security Latest
NISL@THU
NISL@THU
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
C
Cybersecurity and Infrastructure Security Agency CISA
Cloudbric
Cloudbric
T
Threat Research - Cisco Blogs
大猫的无限游戏
大猫的无限游戏
C
CXSECURITY Database RSS Feed - CXSecurity.com
阮一峰的网络日志
阮一峰的网络日志
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
雷峰网
雷峰网
C
Cisco Blogs
V
Vulnerabilities – Threatpost
S
Security Archives - TechRepublic
V
Visual Studio Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
J
Java Code Geeks
D
Darknet – Hacking Tools, Hacker News & Cyber Security
Know Your Adversary
Know Your Adversary
博客园 - 叶小钗
腾讯CDC
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
P
Privacy International News Feed
P
Palo Alto Networks Blog
博客园_首页
V
V2EX
WordPress大学
WordPress大学
Schneier on Security
Schneier on Security
月光博客
月光博客
博客园 - 司徒正美
Google DeepMind News
Google DeepMind News
TaoSecurity Blog
TaoSecurity Blog
博客园 - 聂微东
酷 壳 – CoolShell
酷 壳 – CoolShell
人人都是产品经理
人人都是产品经理
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
博客园 - 【当耐特】
The Cloudflare Blog
罗磊的独立博客
美团技术团队
N
News | PayPal Newsroom
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
Last Week in AI
Last Week in AI
K
Kaspersky official blog
Google Online Security Blog
Google Online Security Blog
S
SegmentFault 最新的问题
Application and Cybersecurity Blog
Application and Cybersecurity Blog
T
Tailwind CSS Blog

Lan小站-嗯,不错! - 安卓

android stadio 一直在下载download fastutil-7.2.0.jar - Lan小站-嗯,不错! Android为属性组件动画无效 - Lan小站-嗯,不错! JavaScript调用Android方法,显示Echarts - Lan小站-嗯,不错! 安卓实时获取数据并刷新图表 - Lan小站-嗯,不错! Gson解析百度实时热榜Json数据 - Lan小站-嗯,不错! 按照第一行代码使用ToolBar报错。 - Lan小站-嗯,不错! Android webView打开网页 - Lan小站-嗯,不错! Android VideoView播放视频 - Lan小站-嗯,不错! 安卓MPAndroidChart绘制水平柱状图 - Lan小站-嗯,不错!
安卓MPAndroidChart绘制多层级的堆叠条形图 - Lan小站-嗯,不错!
Lan · 2020-10-09 · via Lan小站-嗯,不错! - 安卓

Lan

本文最后更新于2020年10月09日,已超过2073天没有更新,若内容或图片失效,请留言反馈。

image.png

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

image.png

最后的代码:

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