如何使用开源的图表库MPAndroidChart

来源:互联网 发布:蒸汽巴士八戒有淘宝店 编辑:程序博客网 时间:2024/06/04 18:48

如何使用开源的图表库MPAndroidChart(LineChart为例)

1.新建自己的工程:YuMpChart,并把之前准备好的包,添加到自己工程里面。

2.在xml添加一个

<com.github.mikephil.charting.charts.LineChart

        android:id="@+id/yuchart1"

        android:layout_width="match_parent"

        android:layout_height="match_parent"

/>

3.在LineChartActivity中定义一个LineChart类型的yuChart,并实例化

yuChart = (LineChart) findViewById(R.id.yuchart1);

4.设置yuChart 的相关属性

       yuChart.setDescription("");//设置表格的描述

yuChart.setNoDataTextDescription("这个图表没有数据你需要提供数据");

yuChart.setHighlightEnabled(true);//: 设置点击value的时候,是否高亮显示

yuChart.setTouchEnabled(true); //设置是否可以触摸,如为false,则不能拖动,缩放等

yuChart.setDragEnabled(true);//设置是否可以拖拽,缩放

        yuChart.setScaleEnabled(true);

        

        yuChart.setPinchZoom(true);

        

        yuChart.setHighlightIndicatorEnabled(false);

 

        

        YAxis leftAxis = yuChart.getAxisLeft();//沿y轴的动画

        leftAxis.removeAllLimitLines();

        leftAxis.setStartAtZero(false);

        leftAxis.enableGridDashedLine(10f, 10f, 0f);

        leftAxis.setDrawLimitLinesBehindData(true);

 

        yuChart.getAxisRight().setEnabled(false);

        setData(Name.length);//添加图表的数据

             yuChart.animateX(2500,Easing.EasingOption.EaseInOutQuart);

      Legend l = yuChart.getLegend();

      l.setForm(LegendForm.LINE);

5.实现setData()方法

private void setData(int count) {

        ArrayList<String> xVals = new ArrayList<String>();//x轴的显示标签

        for (int i = 0; i < count; i++) {

            xVals.add(Name[i] + "");

        }

 

        ArrayList<Entry> xyVals = new ArrayList<Entry>();//

 

        for (int i = 0; i < count; i++) {

 

            xyVals.add(new Entry(Time[i], i));

        }

 

        LineDataSet set1 = new LineDataSet(xyVals, "DataSet 1");

       

        set1.enableDashedLine(10f, 5f, 0f);

        set1.setColor(Color.RED);//线的颜色

        set1.setCircleColor(Color.RED);//点的颜色

        set1.setLineWidth(1f);

        set1.setCircleSize(3f);

        set1.setDrawCircleHole(false);

        set1.setValueTextSize(9f);

        set1.setFillAlpha(65);

        set1.setFillColor(Color.RED);//

 

        ArrayList<LineDataSet> dataSets = new ArrayList<LineDataSet>();

        dataSets.add(set1); 

        LineData data = new LineData(xVals, dataSets);

        yuChart.setData(data);

        

    }

6.测试结果:

 

1 0
原创粉丝点击