MPAndroidChart——LineChart,配置 MarkerView。

MPAndroidChart

想要运用好本篇博客,读者至少需要掌握 Android 开发有关 Activity 的部分,并且对 MPAndroidChart 有简单的了解。本文是傻瓜式教程,更多需求请查看官方文档。

下面是图表画出来的样子~如果你觉得可能会对你有帮助,那么就接着往下看吧,冲!
在这里插入图片描述

基本使用步骤

【注意】chart 使用分为两大部分,第一部分可以实现基本绘图,第二部分便是 MarkerView——点击出现具体数据。

1.添加依赖

project 的 build.gradle 中添加依赖

allprojects {
   
    repositories {
   
        ...
        maven {
    url 'https://jitpack.io' }
    }
}

app 的 build.gradle 中添加依赖:

dependencies {
   
    implementation 'com.github.PhilJay:MPAndroidChart:v3.1.0'
}

2.配置 xml

在 Activity 对应的 Layout.xml 中配置所要使用的图表——可以把它看做一个控件,除了 android:id 属性外,其他需要自行配置

<com.github.mikephil.charting.charts.LineChart
        android:id="@+id/mChart"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="100dp"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="5dp"
        android:layout_marginBottom="10dp"/>

3. java 配置图表“骨架”

新建一个包 util,新建一个类 chart_util,这个类完成图表的基本配置和添加删除数据功能。代码如下:

package “你滴包名🤩”;

import android.graphics.Color;

import com.github.mikephil.charting.charts.LineChart;
import com.github.mikephil.charting.components.Description;
import com.github.mikephil.charting.components.Legend;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.LineData;
import com.github.mikephil.charting.data.LineDataSet;
import com.github.mikephil.charting.interfaces.datasets.ILineDataSet;
import com.github.mikephil.charting.utils.ColorTemplate;

public class chart_util {
   
    public static void chart_init(LineChart chart) {
   
        //chart.setOnChartValueSelectedListener(this);
        // enable description text
        chart.getDescription().setEnabled(true);
        // enable touch gestures
        chart.setTouchEnabled(true);

        /****************设置描述信息*************/
        Description description = new Description();
        description.setText("description here");
        description.setPosition(700, 50);
        description.setTextColor(Color.CYAN);                   //青色
        description.setTextSize(15);
        chart.setDescription(description);                      //设置图表描述信息
        chart.setNoDataText("No chart data available! Try to figure it.");                        //没有数据时显示的文字
        chart.setNoDataTextColor(Color.CYAN);                    //没有数据时显示文字的颜色
        chart.setDrawGridBackground(false);                     //chart 绘图区后面的背景矩形将绘制
        chart.setDrawBorders(true);                            //绘制图表边框的线
        // enable scaling and dragging
        chart.setDragEnabled(true);
        chart.setScaleEnabled(true);
        chart.setDrawGridBackground(false);

        //set chart 动画~
        //chart.animateX(8000);

        // if disabled, scaling can be done on x- and y-axis separately
        chart.setPinchZoom(false);

        // set an alternative background color
        //chart.setBackgroundColor(0x4169E1FF);

        LineData data = new LineData();
        data.setValueTextColor(Color.BLACK);

        // add empty data
        chart.setData(data);

        // get the legend (only possible after setting data)
        Legend l = chart.getLegend();

        // modify the legend ...
        l.setForm(Legend.LegendForm.LINE);
        l.setTextColor(Color.BLACK);
        //x轴配置
        XAxis xl = chart.getXAxis();
        //xl.setTypeface(tfLight);
        xl.setTextColor(Color.BLACK);
        xl.setDrawGridLines(false);
        xl.setAvoidFirstLastClipping(true);
        xl.setEnabled(true)
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值