• 最牛逼android上的图表库MpChart(二) 折线图


     

    最牛逼android上的图表库MpChart(二) 折线图

    最近工作中,用到了mpchart图表库,现在分享受下mpchart图表库的各个图表在实际工作应用场景:

    • 最牛逼android上的图表库MpChart(一) 介绍篇
    • 最牛逼android上的图表库MpChart(二) 折线图
    • 最牛逼android上的图表库MpChart(三) 条形图
    • 最牛逼android上的图表库MpChart(四) 饼图
    • 最牛逼android上的图表库MpChart(五) 泡泡图

    使用mpchart jar包:mpandroidchartlibrary-2-1-6.jar 
    如果是在studio下,进行如下引用: 
    repositories { 
    maven { url “https://jitpack.io” } 
    }

    dependencies { 
    compile ‘com.github.PhilJay:MPAndroidChart:v2.1.6’ 
    }

    MpChart折线图介绍

    • LineChart类
    • 使用哪些API

      • setBackgroundColor(int color): Sets the background color that will cover the whole chart-view. In addition, a background-color can be set via .xml in the layout file.
      • setDescription(String desc): Set a description text that appears in the bottom right corner of the chart.
      • setDescriptionColor(int color): Sets the color of the description text.
      • setDescriptionPosition(float x, float y): Sets a custom position for the description text in pixels on the screen.
      • setDescriptionTypeface(Typeface t): Sets the Typeface used for drawing the description text.
      • setDescriptionTextSize(float size): Sets the size of the description text in pixels, min 6f, max 16f.
      • setNoDataTextDescription(String desc): Sets the text that should appear if the chart is empty.
      • setDrawGridBackground(boolean enabled): If enabled, the background rectangle behind the chart drawing-area will be drawn.
      • setGridBackgroundColor(int color): Sets the color the grid-background should be drawn with.
      • setDrawBorders(boolean enabled): Enables / disables drawing the chart borders (lines surrounding the chart).
      • setBorderColor(int color): Sets the color of the chart border lines.
      • setBorderWidth(float width): Sets the width of the chart border lines in dp.
      • setMaxVisibleValueCount(int count): Sets the number of maximum visible drawn value-labels - on the chart. This only takes affect when setDrawValues() is enabled.

    MpChart折线图实例

    • 布局文件
    • Java代码   
     1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
     2     xmlns:tools="http://schemas.android.com/tools"
     3     android:layout_width="match_parent"
     4     android:layout_height="match_parent" >
     5 
     6     <com.github.mikephil.charting.charts.LineChart
     7         android:id="@+id/chart1"
     8         android:layout_width="match_parent"
     9         android:layout_height="match_parent"/>
    10 
    11 </RelativeLayout>
      1 package com.example.mpchart;
      2 
      3 import java.util.ArrayList;
      4 
      5 import android.app.Activity;
      6 import android.graphics.Color;
      7 import android.graphics.Typeface;
      8 import android.os.Bundle;
      9 import android.os.Handler;
     10 import android.os.Looper;
     11 import android.os.Message;
     12 import android.view.WindowManager;
     13 
     14 import com.example.mpchart.data.IDataSource;
     15 import com.example.mpchart.data.IDataSource.onDataChangedListener;
     16 import com.example.mpchart.data.SucRateDataSource;
     17 import com.example.mpchart.utils.DBHelper;
     18 import com.example.mpchart.utils.DateUtils;
     19 import com.example.mpchart.utils.LogUtils;
     20 import com.github.mikephil.charting.charts.LineChart;
     21 import com.github.mikephil.charting.components.Legend;
     22 import com.github.mikephil.charting.components.Legend.LegendDirection;
     23 import com.github.mikephil.charting.components.Legend.LegendForm;
     24 import com.github.mikephil.charting.components.LimitLine;
     25 import com.github.mikephil.charting.components.LimitLine.LimitLabelPosition;
     26 import com.github.mikephil.charting.components.XAxis;
     27 import com.github.mikephil.charting.components.XAxis.XAxisPosition;
     28 import com.github.mikephil.charting.components.YAxis;
     29 import com.github.mikephil.charting.components.YAxis.YAxisLabelPosition;
     30 import com.github.mikephil.charting.data.Entry;
     31 import com.github.mikephil.charting.data.LineData;
     32 import com.github.mikephil.charting.data.LineDataSet;
     33 
     34 public class MainActivity extends Activity {
     35 
     36     private static final String TAG = "MainActivity";
     37 
     38     private LineChart mChart;
     39     private IDataSource mDataSource = new SucRateDataSource();
     40 
     41     private Handler mHandler = new Handler(Looper.getMainLooper()) {
     42         @Override
     43         public void handleMessage(Message msg) {
     44             super.handleMessage(msg);
     45             getData();
     46         }
     47     };
     48 
     49     @Override
     50     protected void onCreate(Bundle savedInstanceState) {
     51         super.onCreate(savedInstanceState);
     52         getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
     53                 WindowManager.LayoutParams.FLAG_FULLSCREEN);
     54         setContentView(R.layout.activity_main);
     55 
     56         mChart = (LineChart) findViewById(R.id.chart1);
     57 
     58 //      //在chart上的右下角加描述
     59         mChart.setDescription(mDataSource.getDescription());
     60         mChart.setDescriptionTextSize(30);
     61 //      //设置Y轴上的单位
     62 //      mChart.setUnit("%"); 
     63         //设置透明度
     64 //      mChart.setAlpha(0.8f);
     65         //设置网格底下的那条线的颜色
     66 //      mChart.setBorderColor(Color.rgb(213, 216, 214));
     67 //      mChart.setBorderColor(Color.rgb(0, 0, 0));
     68 //      mChart.setBackgroundColor(Color.rgb(255, 255, 255));
     69         mChart.setGridBackgroundColor(Color.rgb(255, 255, 255));
     70 
     71         //设置Y轴前后倒置
     72 //        mChart.setInvertYAxisEnabled(false);
     73 //        //设置高亮显示
     74 //        mChart.setHighlightEnabled(true);
     75         //设置是否可以触摸,如为false,则不能拖动,缩放等
     76         mChart.setTouchEnabled(true);
     77         //设置是否可以拖拽,缩放
     78         mChart.setDragEnabled(true);
     79         mChart.setScaleEnabled(true);
     80         //设置是否能扩大扩小
     81         mChart.setPinchZoom(true);
     82         // 设置背景颜色
     83 //         mChart.setBackgroundColor(Color.GRAY);
     84         //设置点击chart图对应的数据弹出标注
     85         MyMarkerView mv = new MyMarkerView(this, R.layout.custom_marker_view);
     86         // define an offset to change the original position of the marker
     87         // (optional)
     88 //        mv.setOffsets(-mv.getMeasuredWidth() / 2, -mv.getMeasuredHeight());
     89 //        mv.setMinimumHeight(80);
     90 //        // set the marker to the chart
     91 //        mChart.setMarkerView(mv);
     92 //        // enable/disable highlight indicators (the lines that indicate the
     93 //        // highlighted Entry)
     94 //        mChart.setHighlightIndicatorEnabled(false);
     95         //设置字体格式,如正楷
     96         Typeface tf = Typeface.createFromAsset(getAssets(), "OpenSans-Regular.ttf");
     97         mChart.setDescriptionTypeface(tf);
     98 
     99         LimitLine ll1 = new LimitLine(95f, "警戒值 95%");
    100         ll1.setLineWidth(2f);
    101 //        ll1.setLineColor(Color.rgb(0,0,0));
    102 //        ll1.enableDashedLine(10f, 10f, 0f);
    103         ll1.setLabelPosition(LimitLabelPosition.LEFT_TOP);
    104         ll1.setTextSize(15f);
    105         ll1.setTypeface(tf);
    106 
    107         XAxis xl = mChart.getXAxis();
    108 //      xl.setAvoidFirstLastClipping(true);
    109 //      xl.setAdjustXLabels(true);
    110         xl.setPosition(XAxisPosition.BOTTOM); // 设置X轴的数据在底部显示
    111         xl.setTypeface(tf); // 设置字体
    112         xl.setTextSize(10f); // 设置字体大小
    113         xl.setSpaceBetweenLabels(0); // 设置数据之间的间距'
    114 
    115         YAxis yl = mChart.getAxisLeft();
    116         yl.setPosition(YAxisLabelPosition.OUTSIDE_CHART);
    117 //      yl.setAxisMaxValue(220f);
    118         yl.addLimitLine(ll1);
    119         yl.setTypeface(tf); // 设置字体
    120         yl.setTextSize(10f); // s设置字体大小
    121         yl.setTypeface(tf); 
    122         yl.setAxisMinValue(90f);
    123         yl.setStartAtZero(false);
    124 //      yl.setLabelCount(5); // 设置Y轴最多显示的数据个数
    125 
    126         YAxis y2 = mChart.getAxisRight();
    127         y2.setPosition(YAxisLabelPosition.OUTSIDE_CHART);
    128         y2.setTypeface(tf); // 设置字体
    129         y2.setTextSize(10f); // s设置字体大小
    130         y2.setTypeface(tf); 
    131         y2.setAxisMinValue(90f);
    132         y2.setStartAtZero(false);
    133         getData();
    134         new Thread(mRunnable).start();
    135     }
    136 
    137     private Runnable mRunnable = new Runnable() {
    138         @Override
    139         public void run() {
    140             while(true) {
    141                   try {
    142                        Thread.sleep(15*1000);//每隔15s刷新一次,可以看到动态图
    143                        mHandler.sendMessage(mHandler.obtainMessage());
    144                    } catch (InterruptedException e) {
    145                        e.printStackTrace();
    146                   }
    147                   }
    148         }
    149     };
    150 
    151     private onDataChangedListener listener = new onDataChangedListener() {
    152         @Override
    153         public void onChanged(String[] xx, String[] yy) {
    154             notifyDataChanged(xx, yy);
    155         }
    156 
    157     };
    158 
    159     private void getData() {
    160         LogUtils.d(TAG, "getData() " + DateUtils.getCurrentDate()); 
    161         new Thread(new Runnable() {
    162             @Override
    163             public void run() {
    164                 DBHelper.getInstance().init();
    165                 String sql = "select *from suc_rate_chart_0614";
    166                 final String[] xx = DBHelper.getInstance().query(sql,2); 
    167                 final String[] yy = DBHelper.getInstance().query(sql,3); 
    168                 mHandler.post(new Runnable() {
    169                     @Override
    170                     public void run() {
    171                         listener.onChanged(xx, yy);
    172                     }
    173                 });
    174             }
    175         }).start();
    176     }
    177 
    178     private void notifyDataChanged(String[] xx, String[] yy) {
    179         Typeface tf = Typeface.createFromAsset(getAssets(),"OpenSans-Regular.ttf");
    180        // 加载数据
    181         setData(xx,yy );
    182         //从X轴进入的动画
    183         mChart.animateX(2000);
    184 //        mChart.animateY(2000);   //从Y轴进入的动画
    185 //        mChart.animateXY(2000, 2000);    //从XY轴一起进入的动画
    186 
    187         //设置最小的缩放
    188          mChart.setScaleMinima(0.5f, 1f);
    189         //设置视口
    190         // mChart.centerViewPort(10, 50);
    191 
    192         // get the legend (only possible after setting data)
    193         Legend l = mChart.getLegend();
    194         l.setForm(LegendForm.CIRCLE);  //设置图最下面显示的类型
    195         l.setTypeface(tf);  
    196         l.setTextSize(30);
    197         l.setTextColor(Color.rgb(244, 117, 117));
    198         l.setDirection(LegendDirection.LEFT_TO_RIGHT);
    199         l.setYOffset(100);
    200         l.setFormSize(20f); // set the size of the legend forms/shapes
    201 
    202         // 刷新图表
    203         mChart.invalidate();
    204     }
    205 
    206     private void setData(String[] xx, String[] yy) {
    207 
    208         ArrayList<String> xVals = new ArrayList<String>();
    209         for (int i = 0; i < xx.length; i++) {
    210             xVals.add(xx[i]);
    211         }
    212 
    213         ArrayList<Entry> yVals = new ArrayList<Entry>();
    214 
    215         for (int i = 0; i < yy.length; i++) {
    216             yVals.add(new Entry(Float.parseFloat(yy[i]), i));
    217             LogUtils.d(TAG, "yVals() " + Float.parseFloat(yy[i])); 
    218         }
    219 
    220         // create a dataset and give it a type
    221         LineDataSet set1 = new LineDataSet(yVals, "成功率监控");
    222 
    223         set1.setDrawCubic(false);  //设置曲线为圆滑的线
    224         set1.setCubicIntensity(0.2f);
    225         set1.setDrawFilled(false);  //设置包括的范围区域填充颜色
    226         set1.setDrawCircles(true);  //设置有圆点
    227         set1.setLineWidth(2f);    //设置线的宽度
    228         set1.setCircleSize(5f);   //设置小圆的大小
    229         set1.setHighLightColor(Color.rgb(244, 117, 117));
    230         set1.setColor(Color.rgb(244, 117, 117));    //设置曲线的颜色
    231 
    232         // create a data object with the datasets
    233         LineData data = new LineData(xVals, set1);
    234 
    235         // set data
    236         mChart.setData(data);
    237     }
    238 }

    MpChart效果

    这里写图片描述

  • 相关阅读:
    树莓派4B对接苹果Homebrigde
    分享一款好看的PyCharm风格(转)
    Centos7安装opencv-python缺少共享库(libSM.so.6, libXrender.so.1, libXext.so.6)的解决办法
    win10 python3.7安装 opencv 和 face_recognition
    Python出现Could not find a version that satisfies the requirement openpyxl (from versions: )
    从零开始开发标准的s57电子海图第十三篇 电子海图中特征记录各字段结构和内容(共一百篇)
    从零开始开发标准的s57电子海图第十二篇 编码原则与记录结构(共一百篇)
    从零开始开发标准的s57电子海图第十一篇--海图文件中的数据类型(共一百篇)
    从零开始开发标准的s57电子海图第十篇--海图文件示例(共一百篇)
    从零开始开发标准的s57电子海图第九篇--数据记录字段DR区的结构(共一百篇)
  • 原文地址:https://www.cnblogs.com/huolongluo/p/6094829.html
Copyright © 2020-2023  润新知