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)