Android Studio 中用MPAndroidChart 绘制柱状图

本文介绍了如何在Android Studio中利用MPAndroidChart库创建柱状图。首先,添加MPAndroidChart的依赖,然后创建一个新的Empty Activity,并提供XML布局文件和对应的页面代码,最后在主页面调用相关代码实现柱状图展示。
摘要由CSDN通过智能技术生成

一、首先添加远程依赖或者本地依赖MPAndroidChart的jar包),没有包的可联系我发你。

二、new一个 Empty Activity,名称取你喜欢的,我的示例中是叫“f_ym4”, 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">

    <com.github.mikephil.charting.charts.BarChart
        android:id="@+id/bc"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

</LinearLayout>

三、编辑对应页面class的代码,源码来源于CSDN,但不全,我把完整能用的代码附上

package com.example.daohang;

import android.content.Context;
import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Bundle;
import android.util.DisplayMetrics;
import 
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是在Android Studio中使用MPAndroidChart绘制柱状图的步骤: 1.在项目的build.gradle文件中添加以下依赖项: ```gradle dependencies { implementation 'com.github.PhilJay:MPAndroidChart:v3.0.3' } ``` 2.在布局文件中添加一个BarChart视图: ```xml <com.github.mikephil.charting.charts.BarChart android:id="@+id/bar_chart" android:layout_width="match_parent" android:layout_height="match_parent" /> ``` 3.在Java代码中获取BarChart对象并设置数据: ```java // 获取BarChart对象 BarChart barChart = findViewById(R.id.bar_chart); // 创建一个BarDataSet对象,用于存储柱状图的数据 BarDataSet dataSet = new BarDataSet(entries, "Label"); // 创建一个BarData对象,用于存储BarDataSet对象 BarData barData = new BarData(dataSet); // 设置BarChart的数据 barChart.setData(barData); ``` 4.设置柱状图的样式和属性: ```java // 设置柱状图的颜色 dataSet.setColor(Color.BLUE); // 设置柱状图的宽度 dataSet.setBarWidth(0.9f); // 设置柱状图的值是否显示在柱子上方 dataSet.setValueTextSize(10f); dataSet.setValueTextColor(Color.BLACK); // 设置X轴和Y轴的属性 XAxis xAxis = barChart.getXAxis(); xAxis.setPosition(XAxis.XAxisPosition.BOTTOM); xAxis.setDrawGridLines(false); YAxis yAxis = barChart.getAxisLeft(); yAxis.setDrawGridLines(false); yAxis.setAxisMinimum(0f); // 设置柱状图的动画效果 barChart.animateY(1000); ``` 5.最后,记得在AndroidManifest.xml文件中添加以下权限: ```xml <uses-permission android:name="android.permission.INTERNET" /> ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值