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

推荐订阅源

The GitHub Blog
The GitHub Blog
雷峰网
雷峰网
小众软件
小众软件
博客园 - 【当耐特】
J
Java Code Geeks
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Hugging Face - Blog
Hugging Face - Blog
腾讯CDC
Engineering at Meta
Engineering at Meta
aimingoo的专栏
aimingoo的专栏
A
About on SuperTechFans
H
Help Net Security
博客园_首页
P
Proofpoint News Feed
GbyAI
GbyAI
大猫的无限游戏
大猫的无限游戏
N
Netflix TechBlog - Medium
爱范儿
爱范儿
MyScale Blog
MyScale Blog
Blog — PlanetScale
Blog — PlanetScale
The Cloudflare Blog
MongoDB | Blog
MongoDB | Blog
酷 壳 – CoolShell
酷 壳 – CoolShell
Google DeepMind News
Google DeepMind News

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-10 · via Lan小站-嗯,不错! - 安卓

image.png

这个和垂直柱状图一模一样,只不过把控件名换了一下从barchart换成了HorizontalBarChart

XML文件

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="50dp">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:text="水平柱状图" />
</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="150dp"
    android:orientation="vertical">

    <com.github.mikephil.charting.charts.HorizontalBarChart
        android:id="@+id/horizontalBarChart"
        android:layout_width="match_parent"
        android:layout_height="150dp" />
</LinearLayout>

MainActivity

//初始化水平柱状图
HorizontalBarChart horizontalBarChart = findViewById(R.id.horizontalBarChart);
initBarChart(horizontalBarChart);
horizontalBarChart.setData(setBarData());
barChart.invalidate();

public BarChart initBarChart(BarChart barChart) {
    barChart.setDrawBarShadow(false); // 设置每条柱子的阴影不显示
    barChart.setDrawValueAboveBar(true); // 设置每条柱子的数值显示
    barChart.setPinchZoom(false);
    XAxis xAxis = barChart.getXAxis(); // 获取柱状图的x轴
    YAxis yAxisLeft = barChart.getAxisLeft(); // 获取柱状图左侧的y轴
    YAxis yAxisRight = barChart.getAxisRight(); // 获取柱状图右侧的y轴
    setAxis(xAxis, yAxisLeft, yAxisRight); //调用方法设置柱状图的轴线
    return barChart;
}

public BarData setBarData() {
    List<BarEntry> entries = new ArrayList<>(); //定义一个数据容器
    //生成随机数数据
    for (int i = 0; i <= 12; i++) {
        entries.add(new BarEntry(i, new Random().nextInt(300)));
    }
    BarDataSet barDataSet = new BarDataSet(entries, "测试数据");
    BarData barData = new BarData(barDataSet);
    return barData; //返回可用于柱状图的数据
}

文章参考:https://blog.csdn.net/weixin_43344890/article/details/103008320