MPAndroidChart 的基本使用和配置

MPAndroidChart 是一個Android非常強大的圖標庫,包括線形圖,柱狀圖,餅圖,雷達圖,散列圖,等等只要你見過的圖都有。

效果圖

為了演示各個屬性,好多對默認屬性進行了相反操作:

靜態效果圖

常用屬性的基本配置

package me.febsky.test;

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.AxisBase;
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.formatter.IAxisValueFormatter;

import java.util.ArrayList;

import static com.github.mikephil.charting.components.YAxis.YAxisLabelPosition.INSIDE_CHART;

public class MainActivity extends AppCompatActivity {

    private LineChart mChart;

    private int[] mColors = new int[]{
            Color.parseColor("#5abdfc"),    //藍色
            Color.parseColor("#eb73f6")    //紫色
    };

    protected String[] mMonths = new String[]{
            "1月", "2月", "3月", "4月", "5月", "6月", "7月", "8月", "9月", "10月", "11月", "12月"
    };

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

        mChart = (LineChart) findViewById(R.id.chart);


        initChartView();

        /**-------------這里的數據不重要,主要用隨機數的方式生成點坐標-------------**/
        //設置模擬數據
        ArrayList<Entry> yVals = new ArrayList<Entry>();
        for (int i = 0; i < 12; i++) {
            yVals.add(new Entry(i, (float) (Math.random() * 10000f)));
        }

        ArrayList<Entry> yVals2 = new ArrayList<Entry>();
        for (int i = 0; i < 12; i++) {
            yVals2.add(new Entry(i, (float) (Math.random() * 10000f)));
        }

        addDataSet(yVals, "一居");
        addDataSet(yVals2, "兩居");
        /**--------------------------**/

        //圖標的下邊的指示塊  圖例
        Legend l = mChart.getLegend();
        l.setForm(Legend.LegendForm.LINE);
        l.setXEntrySpace(40);
    }


    private void initChartView() {
        mChart.setDrawGridBackground(false);
        mChart.setDescription(null);    //右下角說明文字
        mChart.setDrawBorders(true);    //四周是不是有邊框
        mChart.setBorderWidth(0.5f);
        mChart.setBorderColor(Color.parseColor("#b3b3b3"));    //邊框顏色,默認黑色?
//        mChart.setVisibleXRangeMaximum(4);

        // enable touch gestures
        mChart.setTouchEnabled(true);
        // if disabled, scaling can be done on x- and y-axis separately
        //禁止x軸y軸同時進行縮放
        mChart.setPinchZoom(false);
        // enable scaling and dragging
        mChart.setDragEnabled(true);
        mChart.setScaleEnabled(true);

        //控制軸上的坐標繪制在什么地方 上邊下邊左邊右邊
        XAxis xAxis = mChart.getXAxis();
        xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);    //x軸是在上邊顯示還是顯示在下邊
        xAxis.enableGridDashedLine(10f, 10f, 0f);    //背景用虛線表格來繪制  給整成虛線
        xAxis.setAxisMinimum(0f);//設置軸的最小值。這樣設置將不會根據提供的數據自動計算。
        xAxis.setGranularityEnabled(true);    //粒度
        xAxis.setGranularity(1f);    //縮放的時候有用,比如放大的時候,我不想把橫軸的月份再細分

        xAxis.setValueFormatter(new IAxisValueFormatter() {
            @Override
            public String getFormattedValue(float value, AxisBase axis) {
                return mMonths[(int) value % mMonths.length];
            }

            @Override
            public int getDecimalDigits() {
                return 0;
            }
        });
//        xAxis.setAxisLineWidth(0f);    //設置坐標軸那條線的寬度
        xAxis.setDrawAxisLine(false);    //是否顯示坐標軸那條軸
        xAxis.setDrawLabels(true);    //是不是顯示軸上的刻度
        xAxis.setLabelCount(mMonths.length);    //強制有多少個刻度
        xAxis.setTextColor(Color.parseColor("#b3b3b3"));


        //隱藏左側坐標軸顯示右側坐標軸,并對右側的軸進行配置
        mChart.getAxisLeft().setEnabled(false);
        YAxis leftAxis = mChart.getAxisRight();
        leftAxis.setEnabled(true);
        leftAxis.enableGridDashedLine(10f, 10f, 0f);
        leftAxis.setAxisMinimum(0);
        leftAxis.setDrawAxisLine(false);
        //坐標軸繪制在圖表的內側
        leftAxis.setPosition(INSIDE_CHART);
        leftAxis.setTextColor(Color.parseColor("#b3b3b3"));
        //確實沒看懂這個是干嘛用的,默認是10f
        //這個玩意好像有坐標軸enable的時候是不可用的
        leftAxis.setSpaceBottom(10f);

        //一個chart中包含一個Data對象,一個Data對象包含多個DataSet對象,
        // 每個DataSet是對應一條線上的所有點(相對于折線圖來說)
        mChart.setData(new LineData());

    }


    /**
     * Author: liuqiang
     * Time: 2016-12-26 15:58
     * Description: 根據數據集合,動態構建DataSet,來繪制界面
     */
    private void addDataSet(ArrayList<Entry> entryList, String dataSetName) {

        LineData data = mChart.getData();

        if (data != null) {
            int count = data.getDataSetCount();

            LineDataSet set = new LineDataSet(entryList, dataSetName);
            set.setLineWidth(1.5f);
            set.setCircleRadius(3.5f);

            int color = mColors[count % mColors.length];

            set.setColor(color);
            set.setCircleColor(color);
            set.setHighLightColor(color);
            set.setValueTextSize(10f);
            set.setDrawValues(false);    //節點不顯示具體數值
            set.setValueTextColor(color);
            set.enableDashedHighlightLine(10f, 5f, 0f);    //選中某個點的時候高亮顯示只是線
            set.setDrawFilled(true);     //填充折線圖折線和坐標軸之間
            set.setFillColor(color);

//            set.setDrawVerticalHighlightIndicator(false);//取消縱向輔助線
            set.setDrawHorizontalHighlightIndicator(false);//取消橫向輔助線

            data.addDataSet(set);
            data.notifyDataChanged();
            mChart.notifyDataSetChanged();
            //這行代碼必須放到這里,這里設置的是圖表這個視窗能顯示,x坐標軸,從最大值到最小值之間
            //多少段,好像這個庫沒有辦法設置x軸中的間隔的大小
            mChart.setVisibleXRangeMaximum(6);
            mChart.invalidate();
        }
    }
}

 

 

 

 本文由用戶 LaverneJett 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!