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

推荐订阅源

WordPress大学
WordPress大学
B
Blog RSS Feed
The GitHub Blog
The GitHub Blog
爱范儿
爱范儿
博客园 - 司徒正美
J
Java Code Geeks
酷 壳 – CoolShell
酷 壳 – CoolShell
Engineering at Meta
Engineering at Meta
大猫的无限游戏
大猫的无限游戏
D
Docker
Blog — PlanetScale
Blog — PlanetScale
Recent Announcements
Recent Announcements
罗磊的独立博客
Microsoft Azure Blog
Microsoft Azure Blog
博客园 - 聂微东
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
人人都是产品经理
人人都是产品经理
Stack Overflow Blog
Stack Overflow Blog
M
MIT News - Artificial intelligence
腾讯CDC
T
The Blog of Author Tim Ferriss
小众软件
小众软件
U
Unit 42
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-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