MPAndroidChart库绘制图表入门

MPAndroidChart截图

MPAndroidChart ,一个强大的 Android 图表视图/图形视图库,包括折线图/条形图/圆饼图/雷达图/气泡图/烛台图等样式,支持缩放,拖动和动画等操作。

Grddle 添加依赖

在项目的 build.gradle 文件中添加

allprojects {
    repositories {
        maven { url 'https://jitpack.io' }
    }
}
...
dependencies {
    implementation 'com.github.PhilJay:MPAndroidChart:v3.0.3'
}

创建图表

.xml 布局文件中定义自己所需的视图控件,包括 LineChartBarChartScatterChartCandleStickChartPieChartBubbleChartRadarChart。本文主要介绍 LineChart 相关使用,如下:

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

在布局所对应的 Activity,Fragment 中或其他地方进行实例化:

// in this example, a LineChart is initialized from xml
LineChart chart = (LineChart) findViewById(R.id.chart);

或者动态添加该图表控件到代码中:

// programmatically create a LineChart
LineChart chart = new LineChart(Context);

// get a layout defined in xml
RelativeLayout rl = (RelativeLayout) findViewById(R.id.relativeLayout);
rl.add(chart); // add the programmatically created chart

添加数据

获取到图表实例后,可以创建数据并将其添加到图表上。下面例子使用的 LineChart,对于这个 Entry 类,图表中有一个单独的条目,其中有x和y坐标

要将数据添加到图表,需要把每个数据对象包装到 Entry 对象中。如下:

YourData[] dataObjects = ...;

List<Entry> entries = new ArrayList<Entry>();

for (YourData data : dataObjects) {
    // 把数据转变为一系列 Entry 对象
    entries.add(new Entry(data.getValueX(), data.getValueY())); 
}

接下来,我们需要将创建的 Li

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值