MPAndroidChart图表绘制库的安装与…

    xiaoxiao2021-03-25  218

     

    简介:MPAndroidChart是一个成熟的库,用于在安卓软件上绘制图表,目前2016年的最新MPAC为3.0.0版本,很多网络教程是2.0.5或者2.0.8或者更久远的版本,一些函数已经无法使用,库的安置方式也已经有所不同。

     

    Github:https://github.com/PhilJay/MPAndroidChart

     

    一.安装

    从以上地址下载后,先在我们需要添加这一库的工程中

    打开build.gradle(project)

    添加

    allprojects {

        repositories {

            maven { url "https://jitpack.io" }

        }

    }

     

    打开build.gradle(app)

    添加

    dependencies {

        compile 'com.github.PhilJay:MPAndroidChart:v3.0.0-beta1'

    }

     

    然后在AndroidStudio中导入库

    File->New->Import Module

    选择刚才下载的文件中的MPChartLib文件夹

    FIle -> Project Structure

    选择库,完成导入步骤

    二. 建立一个最简单的LineChart

    先在xml文件中加入一个linechart控件

    <<span style="color:#000080;font-weight:bold;">com.github.mikephil.charting.charts.LineChart android:id="@+id/chart" android:layout_width="match_parent" android:layout_height="match_parent" />

    图表的实例化配置过程:

    1.向数据列中添加xy数据(Entry),新版本已经不再支持单个x或者y的添加

    2.配置图表相应功能

    3.后续可以再添加数据并刷新图表

    mLineChart  图表实例化  mLineData  图表数据

    MainActivity的整体代码:

    package com.sinzo.mpacharttest; import android.content.Context; import android.graphics.BitmapFactory; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.View; import com.github.mikephil.charting.charts.LineChart; import com.github.mikephil.charting.components.Legend; import com.github.mikephil.charting.data.Entry; import com.github.mikephil.charting.data.LineData; import com.github.mikephil.charting.data.LineDataSet; import java.util.ArrayList; public class MainActivity extends AppCompatActivity { private LineChart mLineChart; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mLineChart = (LineChart)findViewById(R.id.chart); LineData mLineData =getLineData(36,100); showChart(mLineChart, mLineData, Color.rgb(114, 188, 223)); } public void showChart(LineChart lineChart, LineData lineData, int color) { lineChart.setDrawBorders(false); //是否在折线图上添加边框 // no description text lineChart.setDescription("");// 数据描述 // 如果没有数据的时候,会显示这个,类似listviewemtpyview lineChart.setNoDataTextDescription("You need to provide data for the chart."); // enable / disable grid background lineChart.setDrawGridBackground(false); // 是否显示表格颜色 lineChart.setGridBackgroundColor(0x70FFFFFF); // 表格的的颜色,在这里是是给颜色设置一个透明度 // enable touch gestures lineChart.setTouchEnabled(true); // 设置是否可以触摸 // enable scaling and dragging lineChart.setDragEnabled(true);// 是否可以拖拽 lineChart.setScaleEnabled(true);// 是否可以缩放 // if disabled, scaling can be done on x- and y-axis separately lineChart.setPinchZoom(false);// lineChart.setBackgroundColor(color);// 设置背景 // add data lineChart.setData(lineData); // 设置数据 // get the legend (only possible after setting data) Legend mLegend = lineChart.getLegend(); // 设置比例图标示,就是那个一组yvalue // modify the legend ... // mLegend.setPosition(LegendPosition.LEFT_OF_CHART); mLegend.setForm(Legend.LegendForm.CIRCLE);// 样式 mLegend.setFormSize(6f);// 字体 mLegend.setTextColor(Color.WHITE);// 颜色// mLegend.setTypeface(mTf);// 字体 lineChart.animateX(2500); // 立即执行的动画,x } public LineData getLineData(int count, float range) { ArrayList entries = new ArrayList<>(); entries.add(new Entry(0, 4f)); entries.add(new Entry(1, 8f)); entries.add(new Entry(2, 6f)); entries.add(new Entry(3, 2f)); entries.add(new Entry(4, 18f)); entries.add(new Entry(5, 9f)); LineDataSet dataSet = new LineDataSet(entries, "# of Calls"); LineData lineData = new LineData(dataSet); return lineData; } } 效果: 使用这一库制作的噪音计: https://github.com/arenascats/SoundPrint 部分代码参考自:http://blog.csdn.net/shineflowers/article/details/44704723
    转载请注明原文地址: https://ju.6miu.com/read-392.html

    最新回复(0)