1. 程式人生 > >基於MPAndroidChart的折線圖繪製

基於MPAndroidChart的折線圖繪製

    
public class MainActivity extends AppCompatActivity implements OnChartGestureListener,OnChartValueSelectedListener{

    private LineChart mChart;
    private LineData data;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mChart = (LineChart) findViewById(R.id.chart1);
        mChart.setOnChartGestureListener(this);
        mChart.setOnChartValueSelectedListener(this);
        mChart.setDrawGridBackground(false);
        // 無描述文字
        mChart.getDescription().setEnabled(false);

        // 使能點選
        mChart.setTouchEnabled(true);

        // 使能拖動和縮放
        mChart.setDragEnabled(true);
        mChart.setScaleEnabled(true);

        // 如果為false,則x,y兩個方向可分別縮放
        mChart.setPinchZoom(true);

        /**
         1.新增assets檔案   project->src->main位置,右鍵new->Directory->assets
         2.在assets檔案中   new->Directory->Fonts檔案
         3.在Fonts檔案中複製下載好的ttf檔案,例如OpenSans-Regular.ttf
         */
        //設定上下限
        Typeface tf = Typeface.createFromAsset(getBaseContext().getAssets(), "Fonts/OpenSans-Regular.ttf");

        LimitLine ll1 = new LimitLine(90f, "Upper Limit");
        ll1.setLineWidth(4f);
        ll1.enableDashedLine(10f, 10f, 0f);
        ll1.setLabelPosition(LimitLine.LimitLabelPosition.RIGHT_TOP);
        ll1.setTextSize(10f);
        ll1.setTypeface(tf);

        LimitLine ll2 = new LimitLine(80f, "Lower Limit");
        ll2.setLineWidth(4f);
        ll2.enableDashedLine(10f, 10f, 0f);
        ll2.setLabelPosition(LimitLine.LimitLabelPosition.RIGHT_BOTTOM);
        ll2.setTextSize(10f);
        ll2.setTypeface(tf);

        //設定x軸位置
        XAxis xAxis = mChart.getXAxis();
        xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);

        //去除右邊的y軸
        YAxis yAxisRight = mChart.getAxisRight();
        yAxisRight.setEnabled(false);
        YAxis yAxisLeft = mChart.getAxisLeft();
        yAxisLeft.addLimitLine(ll1);
        yAxisLeft.addLimitLine(ll2);
        init();
    }

    private void init(){
        //初始化資料
        String xl[] ={"1","2","3","4","5","6","7","8","9","10"}; //橫軸資料
        String yl[] ={"80","85","80","90","95","88","90","91","92","93"}; //豎軸資料
        data = getData(xl,yl);
        mChart.setData(data);
        mChart.animateX(2000);//動畫時間
    }

    private LineData getData(String[] xx, String[] yy){
        ArrayList<Entry> yVals = new ArrayList<Entry>();
        for (int i = 0; i < yy.length; i++) {
            // 要顯示的資料
            yVals.add(new Entry(Float.parseFloat(xx[i]),Float.parseFloat(yy[i])));
        }
        //一條曲線對應一個LineDataSet
        LineDataSet set1 = new LineDataSet(yVals, "前五次的評分");
        set1.setMode(LineDataSet.Mode.CUBIC_BEZIER);//設定曲線為圓滑的線
        set1.setCubicIntensity(0.2f);
        set1.setDrawCircles(true);  //設定有圓點
        set1.setLineWidth(2f);    //設定線的寬度
        set1.setCircleSize(5f);   //設定小圓的大小
        set1.setDrawFilled(true);//設定包括的範圍區域填充顏色
        set1.setCircleColor(Color.rgb(244, 117, 117));
        set1.setColor(Color.rgb(244, 117, 117));
        ArrayList<ILineDataSet> dataSets = new ArrayList<ILineDataSet>();
        dataSets.add(set1); // add the datasets

        // create a data object with the datasets
        LineData data = new LineData(dataSets);
        return data;
    }

    @Override
    public void onChartGestureStart(MotionEvent me, ChartTouchListener.ChartGesture lastPerformedGesture) {

    }

    @Override
    public void onChartGestureEnd(MotionEvent me, ChartTouchListener.ChartGesture lastPerformedGesture) {
        if(lastPerformedGesture != ChartTouchListener.ChartGesture.SINGLE_TAP)
            mChart.highlightValues(null); // or highlightTouch(null) for callback to onNothingSelected(...)
    }

    @Override
    public void onChartLongPressed(MotionEvent me) {

    }

    @Override
    public void onChartDoubleTapped(MotionEvent me) {

    }

    @Override
    public void onChartSingleTapped(MotionEvent me) {

    }

    @Override
    public void onChartFling(MotionEvent me1, MotionEvent me2, float velocityX, float velocityY) {

    }

    @Override
    public void onChartScale(MotionEvent me, float scaleX, float scaleY) {

    }

    @Override
    public void onChartTranslate(MotionEvent me, float dX, float dY) {

    }

    @Override
    public void onValueSelected(Entry e, Highlight h) {

    }

    @Override
    public void onNothingSelected() {

    }
}