Android开源图表---MPAndroidChart

MPAndroidChart是一款基于Android的开源图表库,MPAndroidChart不仅可以在Android设备上绘制各种统计图表,还可以对图表进行拖动和缩放操作,应用起来非常方便。和AChartEngine相比,MPAndroidChart显得更轻巧和简单,MPAndroidChart同样拥有常用的图表类型:线型图、饼图、柱状图和散点图。


LineChart (with legend, simple design)



PieChart (with selection, ...)





下面把上面的饼状图的使用方法写一下:


 <com.github.mikephil.charting.charts.PieChart
            android:id="@+id/chart1"
            android:layout_width="@dimen/chart_width"
            android:layout_height="@dimen/chart_height"
            android:layout_gravity="center_horizontal"
             />


    private void showchart(){
        mChart.setHoleColorTransparent(true);

        mChart.setHoleRadius(60f);
        mChart.setTransparentCircleRadius(64f);  //半透明圈
        

        mChart.setDescription("");

        mChart.setDrawYValues(true);
        mChart.setDrawCenterText(true);

        mChart.setDrawHoleEnabled(true);

        mChart.setRotationAngle(90);  //初始旋转角度

        // draws the corresponding description value into the slice
        mChart.setDrawXValues(true);

        // enable rotation of the chart by touch
        mChart.setRotationEnabled(true);  // 可以手动旋转

        // display percentage values
        mChart.setUsePercentValues(true);
        // mChart.setUnit(" €");
        // mChart.setDrawUnitsInChart(true);

        // add a selection listener
        mChart.setOnChartValueSelectedListener(this);
        // mChart.setTouchEnabled(false);
        
        mChart.setOnAnimationListener(this);
       

        mChart.setCenterText("MPAndroidChart\nLibrary");

        setData(2, 100);

        mChart.animateXY(1000, 1000);
        // mChart.spin(2000, 0, 360);


      Legend l = mChart.getLegend();
     l.setPosition(LegendPosition.RIGHT_OF_CHART);
      l.setXEntrySpace(7f);
      l.setYEntrySpace(5f);
    }



   private void setData(int count, float range) {


        ArrayList<Entry> yVals1 = new ArrayList<Entry>();

        //饼图数据
        finish_num = 10;
        doing_num = 10;
        yuqi_num = 15;
 
            yVals1.add(new Entry(finish_num, 0));
            yVals1.add(new Entry(doing_num, 1));
            yVals1.add(new Entry(yuqi_num, 2));

        
        ArrayList<String> xVals = new ArrayList<String>();

        for (int i = 0; i < count + 1; i++)
            xVals.add(String.valueOf(i));

        PieDataSet set1 = new PieDataSet(yVals1, "Election Results");
        set1.setSliceSpace(0f); //3f

        ArrayList<Integer> colors = new ArrayList<Integer>();

        //饼图颜色
           colors.add(Color.rgb(205, 205, 205));
           colors.add(Color.rgb(114, 188, 223));
           colors.add(Color.rgb(255, 123, 124));

        set1.setColors(colors);
        
        DisplayMetrics metrics = getActivity().getResources().getDisplayMetrics();
        float px = 5 * (metrics.densityDpi / 160f);
        set1.setSelectionShift(px);  //选中态多出的长度

        PieData data = new PieData(xVals, set1);
        mChart.setData(data);

        // undo all highlights
        mChart.highlightValues(null);

        mChart.invalidate();
    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值