前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >安卓MPAndroidChart绘制水平柱状图

安卓MPAndroidChart绘制水平柱状图

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

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

XML文件

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

代码语言:javascript
复制
//初始化水平柱状图
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

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

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

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

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

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
容器服务
腾讯云容器服务(Tencent Kubernetes Engine, TKE)基于原生 kubernetes 提供以容器为核心的、高度可扩展的高性能容器管理服务,覆盖 Serverless、边缘计算、分布式云等多种业务部署场景,业内首创单个集群兼容多种计算节点的容器资源管理模式。同时产品作为云原生 Finops 领先布道者,主导开源项目Crane,全面助力客户实现资源优化、成本控制。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档