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

推荐订阅源

Simon Willison's Weblog
Simon Willison's Weblog
P
Privacy International News Feed
www.infosecurity-magazine.com
www.infosecurity-magazine.com
T
Troy Hunt's Blog
Hacker News - Newest:
Hacker News - Newest: "LLM"
Attack and Defense Labs
Attack and Defense Labs
S
Secure Thoughts
V2EX - 技术
V2EX - 技术
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
O
OpenAI News
Cloudbric
Cloudbric
Google Online Security Blog
Google Online Security Blog
Schneier on Security
Schneier on Security
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Help Net Security
Help Net Security
Cyberwarzone
Cyberwarzone
G
GRAHAM CLULEY
L
Lohrmann on Cybersecurity
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Spread Privacy
Spread Privacy
NISL@THU
NISL@THU
N
News and Events Feed by Topic
T
Tenable Blog
S
Security @ Cisco Blogs
N
News and Events Feed by Topic
The Hacker News
The Hacker News
C
CXSECURITY Database RSS Feed - CXSecurity.com
宝玉的分享
宝玉的分享
月光博客
月光博客
酷 壳 – CoolShell
酷 壳 – CoolShell
美团技术团队
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Google DeepMind News
Google DeepMind News
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
T
Tailwind CSS Blog
V
Visual Studio Blog
P
Proofpoint News Feed
Webroot Blog
Webroot Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
博客园 - 三生石上(FineUI控件)
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Jina AI
Jina AI
雷峰网
雷峰网
T
The Blog of Author Tim Ferriss
Hugging Face - Blog
Hugging Face - Blog
腾讯CDC
L
LangChain Blog
The Register - Security
The Register - Security
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
博客园 - 聂微东

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