——热爱开源,乐于分享

MpAndroidChart图表框架的使用方法

两种方法:

1.Eclipse平台:建议到csdn下载一个MpAndroidChart框架的jar包,然后拷贝到libs文件夹中,然后 Add as Libary。

2.AndroidStudio平台:建议直接到GitHub上,然后导入其lib依赖,并添加依赖关系。

导入的过程中可能会报错:xxx…maven…not found,直接点击报错信息的open file,将apply plugin: ‘android-maven’直接注释掉就行了:

android图表 组织架构图 安卓图表框架使用_android图表 组织架构图


android图表 组织架构图 安卓图表框架使用_MpChart_02


本人使用的是AndroidStudio,导入成功后就可以正常使用了。
直接上代码,因为在源码中已经把注释和思路写得很清楚了):

1.布局文件:activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

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

</LinearLayout>

2.MainActivity.java

package com.example.mp;

import java.util.ArrayList;
import java.util.Random;

import com.github.mikephil.charting.charts.BarChart;
import com.github.mikephil.charting.data.BarData;
import com.github.mikephil.charting.data.BarDataSet;
import com.github.mikephil.charting.data.BarEntry;
import com.github.mikephil.charting.utils.ColorTemplate;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
/**
 * 主要思路结构:采用逆向分析!!!很重要
 * 1.barChart.setData(BarData)
 * =>2.BarData = new BarData(List<String>xVals, BarDataSet)
 * =>3.BarDataSet = new BarDataSet(List<Entry> yVals,String label);//对应纵坐标条目和对数据集的描述
 * =>4.现在只需要创建ArrayList<Entry> yVals即可;
 * @author Administrator
 *
 */

public class MainActivity extends Activity {

    private Random random;//用于产生模拟数据,真是数据可以从数据库或者Json、XML等文件中获取

    BarChart barChart;
    BarData barData;
    BarDataSet barDataSet;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        barChart=(BarChart) findViewById(R.id.barchart);

        ArrayList<BarEntry> entries = new ArrayList<BarEntry>();//显示条目
        ArrayList<String> xVals = new ArrayList<String>();//横坐标
        random = new Random();
        for(int i=0;i<12;i++){
            float profit = random.nextFloat()*1000;//随机返回一个0-1之间的浮点数
            entries.add(new BarEntry(profit, i));//在x=i的横坐标位置添加一个纵坐标值,并呈柱状图显示
            xVals.add((i+1)+"月");
        }

        barDataSet =new BarDataSet(entries, "年度营业额报表");//中文为图例
        barDataSet.setColors(ColorTemplate.COLORFUL_COLORS);
        barData = new BarData(xVals, barDataSet);
        barChart.setData(barData);
        barChart.animateY(3000);
        barChart.setDescription("营业额(单位:万元)");//表头名称
    }

}

3.效果图

android图表 组织架构图 安卓图表框架使用_android_03

很简单吧,其他的折线图、饼图等原理相似,大家在看之前一定要先看代码顶部的思路,有助于理解和掌握。这个框架还有很多强大的功能,主要是要熟练掌握其包含的方法和属性。
4.补充说明相关属性

setDescription(String desc) : 设置表格的描述
setDescriptionTypeface(Typeface t) :自定义表格中显示的字体
setDrawYValues(boolean enabled) : 设置是否显示y轴的值的数据
setValuePaintColor(int color) :设置表格中y轴的值的颜色,但是必须设置setDrawYValues(true)
setValueTypeface(Typeface t):设置字体
setValueFormatter(DecimalFormat format) : 设置显示的格式
setPaint(Paint p, int which) : 自定义笔刷
public ChartData getDataCurrent() :返回ChartData对象当前显示的图表。它包含了所有信息的显示值最小和最大值等
public float getYChartMin() : 返回当前最小值
public float getYChartMax() : 返回当前最大值
public float getAverage() : 返回所有值的平均值。
public float getAverage(int type) : 返回平均值
public PointF getCenter() : 返回中间点
public Paint getPaint(int which) : 得到笔刷
setTouchEnabled(boolean enabled) : 设置是否可以触摸,如为false,则不能拖动,缩放等
setDragScaleEnabled(boolean enabled) : 设置是否可以拖拽,缩放
setOnChartValueSelectedListener(OnChartValueSelectedListener l): 设置表格上的点,被点击的时候,的回调函数
setHighlightEnabled(boolean enabled) : 设置点击value的时候,是否高亮显示
public void highlightValues(Highlight[] highs) : 设置高亮显示
saveToGallery(String title) : 保存图表到图库中
saveToPath(String title, String pathOnSD) : 保存.
setScaleMinima(float x, float y) : 设置最小的缩放
centerViewPort(int xIndex, float val) : 设置视口
fitScreen() : 适应屏幕
动画:

三种动画:分别是x方向,y方向,xy方向。

animateX(int durationMillis) : x轴方向
animateY(int durationMillis) : y轴方向
animateXY(int xDuration, int yDuration) : xy轴方向
mChart.animateX(3000f); // 沿X轴3秒钟展示完毕
mChart.animateY(3000f); // 同理
mChart.animateXY(3000f, 3000f); //同理
注意:如果调用动画方法后,就没有必要调用 invalidate()方法,来刷新界面了,所以一般都调用一个动画,既方便又好看。