菜鸡学习之路之------MPAndroidChart 一个开源图表库

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_30519365/article/details/52719151

一、MPAndroidChart 是什么?

MPAndroidChart 是 Android 一个强大且容易使用的图表库

  • 支持线状图、柱状图、散点图、烛状图、气泡图、饼状图和蜘蛛网状图
  • 支持缩放、拖动(平移)、选择和动画
  • 适用于 Android 2.2 ( API 8 ) 和以上

该图标库支持 跨平台 使用:Android 和 iOS

二、MPAndroidChart 有哪些特性?

  • 8种不同的图表类型  
  • 两轴缩放(支持触摸手势,两轴单独或同时的放缩) 
  • 拖 / 平移 / 抛(触摸手势)  
  • Combined-Charts 组合图表(线状、柱状、散点图等)   
  • 双轴(比如说有两个独立的Y轴数据)  
  • 画值 (draw values into the chart with touch-gesture)
  • 高亮显示值(我们可以自定义Popup-views来高亮显示我们选中的值) 
  • 多个 / 单独的轴  
  • 图保存到sd卡(图像或txt文件)  
  • 预定义的颜色模板  
  • Legends (自动生成,自定义)   
  • 自定义轴(x轴和y轴)   
  • 动画(建立x和y轴动画)
  • 限制线(比如提供附加信息、最大值 …)   
  • 完全自定义(paints、字体、legends、颜色、背景、手势、虚线 …)   

  • 平滑缩放和滚动 30.000 数据点(线状,柱状图表)
  • 代码学习
  • <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout 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:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
    
        <com.github.mikephil.charting.charts.LineChart
            android:id="@+id/chart"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    </RelativeLayout>

  • package org.gsc.zz.Demo;
    
    import android.graphics.Color;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    
    import com.github.mikephil.charting.charts.LineChart;
    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 java.util.ArrayList;
    import java.util.List;
    
    public class MainActivity extends AppCompatActivity {
        private LineChart lc;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
           initchart();
        }
    
        private void  initchart() {
            lc= (LineChart) findViewById(R.id.chart);
            lc.animateX(5000);//设置动画
            lc.animateY(5000);
            XAxis xa=lc.getXAxis();//控制X轴位置,默认顶部XAxis.XAxisPosition.TOP
            xa.setPosition(XAxis.XAxisPosition.BOTTOM);
            xa.setDrawGridLines(false);//默认绘制网格
            xa.setDrawAxisLine(true);//默认绘制X        xa.setDrawLabels(false);//绘制x轴标签
            xa.setDrawLimitLinesBehindData(true);//设置基准线
            YAxis ya=lc.getAxisRight();
            ya.setDrawAxisLine(false);//去掉右边y宙线
            ya.setDrawLabels(false);
            List<String> x=new ArrayList<>();//x轴的名称
            x.add("第一次月考");
            x.add("第二次月考");
            x.add("第三次月考");
            x.add("第四次月考");
            x.add("第五次月考");
            List l=new ArrayList();
            LineDataSet l1=new LineDataSet(l,"数学成绩");//y轴数据
            //设置线的颜色如不设置默认绿色
            l1.setColor(Color.YELLOW);
             Entry e=new Entry(100.0f,0);//数据实体类
           l.add(e);
            e=new Entry(55.0f,2);
           l.add(e);
            e=new Entry(70.0f,3);
           l.add(e);
            e=new Entry(60.0f,1);
            l.add(e);
            e=new Entry(150.0f,4);
            l.add(e);
            List<ILineDataSet> sumy= new ArrayList<>();//装载所有y轴的数据集合,集合的长度决定显示几条直线
            sumy.add(l1);
            lc.setData(new LineData(x, sumy));
            lc.invalidate();
            lc.setLogEnabled(true);
            //-keep class com.github.mikephil.charting.** { *; } 混淆脚本
        }
    }
    

  • 许多图用法类似雷同,举一反三

猜你喜欢

转载自blog.csdn.net/qq_30519365/article/details/52719151