美文网首页Android
MPAndroidChart(一)——PieChart

MPAndroidChart(一)——PieChart

作者: 吃泥巴的猫 | 来源:发表于2018-12-14 20:49 被阅读0次

    MPAndroidChart依赖:implementation 'com.github.PhilJay:MPAndroidChart:v3.0.3'
    PieChart扇形图


    图示.jpg

    界面布局

    <com.github.mikephil.charting.charts.PieChart
            android:id="@+id/chart"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_above="@+id/seekBar1"
            android:layout_margin="20dp"/>
    

    首先在activity中对扇形图的属性进行设置
    其中mChart是对扇形图的样式及效果进行设置,Legend是对右上角图例的样式及效果进行设置

            mChart.getDescription().setEnabled(false);    //设置pieChart图表的描述
            mChart.setBackgroundColor(Color.WHITE);      //设置pieChart图表背景色
            mChart.setRotationEnabled(true);//可以手动旋转
            mChart.setDragDecelerationFrictionCoef(0.95f);//设置pieChart图表转动阻力摩擦系数[0,1]
            mChart.setHighlightPerTapEnabled(true);       //设置piecahrt图表点击Item高亮是否可用
            mChart.animateY(1400, Easing.EasingOption.EaseInOutQuad);// 设置pieChart图表展示动画效果
            Legend l = mChart.getLegend();
            l.setEnabled(true);                    //是否启用图列(true:下面属性才有意义
            l.setVerticalAlignment(Legend.LegendVerticalAlignment.TOP);
            l.setHorizontalAlignment(Legend.LegendHorizontalAlignment.RIGHT);
            l.setOrientation(Legend.LegendOrientation.VERTICAL);
            l.setForm(Legend.LegendForm.DEFAULT); //设置图例的形状
            l.setFormSize(10);                      //设置图例的大小
            l.setFormToTextSpace(10f);              //设置每个图例实体中标签和形状之间的间距
            l.setDrawInside(false);
            l.setWordWrapEnabled(true);              //设置图列换行(注意使用影响性能,仅适用legend位于图表下面)
            l.setXEntrySpace(10f);                  //设置图例实体之间延X轴的间距(setOrientation = HORIZONTAL有效)
            l.setYEntrySpace(8f);                  //设置图例实体之间延Y轴的间距(setOrientation = VERTICAL 有效)
            l.setYOffset(0f);                      //设置比例块Y轴偏移量
            l.setTextSize(14f);                      //设置图例标签文本的大小
            l.setTextColor(Color.parseColor("#333333"));//设置图例标签文本的颜色
    

    在设置好扇形图的样式及效果之后,对其中的数据进行设置(一般数据及颜色为后台获取)

            ArrayList<PieEntry> pieEntryList = new ArrayList();//数据列表
            ArrayList<Integer> colors = new ArrayList();//颜色列表
    

    数据列表以及颜色列表按顺序对应,即数据列表的第一个数据颜色为颜色列表的第一个数据,我做了颜色随机处理及每次加载扇形图时颜色均不一样

    for (int i = 0; i < list.size(); i++) {//list为数据列表
                Random random = new Random();
                int r = random.nextInt(256);
                int g = random.nextInt(256);
                int b = random.nextInt(256);
                colors.add(Color.rgb(r,g,b));
                pieEntryList.add(new PieEntry(Float.parseFloat(townsChartEntity.getRecordSet0().getR().get(i).getC().get(2)) * 100 / all, townsChartEntity.getRecordSet0().getR().get(i).getC().get(1))); 
    }
    

    设置扇形图数据:

            //饼状图数据集 PieDataSet
            PieDataSet pieDataSet = new PieDataSet(pieEntryList, "图表名称");
            pieDataSet.setSliceSpace(3f);           //设置饼状Item之间的间隙
            pieDataSet.setSelectionShift(30f);      //设置饼状Item被选中时变化的距离
            pieDataSet.setColors(colors);           //为DataSet中的数据匹配上颜色集(饼图Item颜色)
            //最终数据 PieData
            PieData pieData = new PieData(pieDataSet);
            pieData.setDrawValues(true);            //设置是否显示数据实体(百分比,true:以下属性才有意义)
            pieData.setValueTextColor(Color.BLUE);  //设置所有DataSet内数据实体(百分比)的文本颜色
            pieData.setValueTextSize(12f);          //设置所有DataSet内数据实体(百分比)的文本字体大小
            pieData.setValueFormatter(new PercentFormatter());//设置所有DataSet内数据实体(百分比)的文本字体格式
    

    将数据设置在mChart中,图表完成

            mChart.setData(pieData);
            mChart.highlightValues(null);
            mChart.invalidate();                    //将图表重绘以显示设置的属性和数据
    

    其中highlightValues未设置

    1. highlightValue(float x, int dataSetIndex, boolean callListener):
      高亮显示在给定的DataSet中x坐标所对应的值。
      设置dataSetIndex为-1会撤销所有高亮显示。
      callListener决定了是否调用选择监听器。
    2. highlightValue(Highlight high, boolean callListener):
      高亮显示的值代表了给定的Highlight对象 。
      提供null撤销所有高亮显示。
      callListener决定了是否调用选择监听器。
    3. highlightValues(Highlight[] highs):
      高亮显示的值代表了给定的Highlight对象数组。
      提供null或空数组撤消所有高亮显示。

    相关文章

      网友评论

        本文标题:MPAndroidChart(一)——PieChart

        本文链接:https://www.haomeiwen.com/subject/gvlnhqtx.html