当前位置: 代码迷 >> 综合 >> Android开源图表---MPAndroidChart
  详细解决方案

Android开源图表---MPAndroidChart

热度:15   发布时间:2024-01-14 21:14:08.0

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();
    }

  相关解决方案