Android之使用MPAndroidChart绘画饼图

一、简单使用

 pic_chart=findViewById(R.id.pic_chart);
        list.add(new PieEntry(2f,"米饭"));
        list.add(new PieEntry(3f,"鸡肉"));
        list.add(new PieEntry(5f,"鱼肉"));
        pieDataSet=new PieDataSet(list,"吃的饭菜");
        //为每个分组设置颜色,如果不为每个都设置颜色最后饼图只会有一个颜色,这样无法有效看出占比
        pieDataSet.setColors(Color.GREEN,Color.BLUE,Color.RED);
        pieData=new PieData(pieDataSet);
        pic_chart.setData(pieData);

效果展示

注:一定要为每个都设置颜色不然就只会出现一个颜色,没有设置颜色产生的效果

二、对标题的设置

private void title() {
        Description description=new Description();
        //设置标题
        description.setText("吃的什么饭菜");
        //设置标题颜色
        description.setTextColor(getResources().getColor(R.color.colorAccent));
        //设置标题字体大小
        description.setTextSize(23f);
        //设置标题位置
        WindowManager windowManager= (WindowManager) this.getSystemService(WINDOW_SERVICE);
        DisplayMetrics displayMetrics=new DisplayMetrics();
        windowManager.getDefaultDisplay().getMetrics(displayMetrics);
        description.setPosition(displayMetrics.widthPixels/2+120,60);
        pic_chart.setDescription(description);
    }

效果展示

三、设置饼图的位置

 //设置饼图位置(偏移量)
        pic_chart.setExtraOffsets(20,20,20,20);

效果展示

四、对图例的设置

 private void legend() {
        //获取图例的实例
        Legend legend=pic_chart.getLegend();
        //图例文字的大小
        legend.setTextSize(16f);
        //图例文字的颜色
        legend.setTextColor(getResources().getColor(R.color.colorAccent));
        //图例的大小
        legend.setFormSize(16f);
        //图例的模式
        legend.setForm(Legend.LegendForm.CIRCLE);
        //图例的位置
        //图例水平方向为靠右
        legend.setHorizontalAlignment(Legend.LegendHorizontalAlignment.RIGHT);
        //图例垂直方向居中
        legend.setVerticalAlignment(Legend.LegendVerticalAlignment.CENTER);
        //图例分布模式(这里是垂直分布,默认的为水平分布)
        legend.setOrientation(Legend.LegendOrientation.VERTICAL);
    }

效果展示

五、对图形的设置

private void init() {
        //设置图形上文字的大小
        pieDataSet.setValueTextSize(16f);
        //设置图形上数值的颜色
        pieDataSet.setValueTextColor(Color.YELLOW);
        //是否绘制饼图上的文字
       // pieDataSet.setDrawValues(true);
        //文字绘制在图形内还是图形外(这里绘制在外部。默认在内部)
        pieDataSet.setXValuePosition(PieDataSet.ValuePosition.OUTSIDE_SLICE);
        //图形上的数值绘制在图形内还是图形外(这里绘制在外部。默认在内部)
        pieDataSet.setYValuePosition(PieDataSet.ValuePosition.OUTSIDE_SLICE);
        //连线的颜色
        pieDataSet.setValueLineColor(getResources().getColor(R.color.colorAccent));
        // 设置图形上文字的颜色
        pic_chart.setEntryLabelColor(getResources().getColor(R.color.colorAccent));
        //设置连线长度
        pieDataSet.setValueLinePart1Length(1f);
        //设置连线长度是否可变
        pieDataSet.setValueLineVariableLength(false);
        //设置连接线距离饼图的距离
        pieDataSet.setValueLinePart1OffsetPercentage(100f);
    }

效果展示

//设置图形是否绘制空洞
        pic_chart.setDrawHoleEnabled(true);
        //设置空心圆半径的大小
        pic_chart.setHoleRadius(5f);
        //设置空心圆颜色
        pic_chart.setHoleColor(Color.TRANSPARENT);
        //设置透明圈的透明度
        pic_chart.setTransparentCircleAlpha(0);
        //设置饼块与饼块之间的距离
        pieDataSet.setSliceSpace(6f);

效果展示

结语:有很多属性没有实现,如果我有任何错误或不准确的地方,请各位大佬批评指正,我将非常感激您的帮助。

  • 11
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用 MPAndroidChart 绘制饼图,你需要进行以下步骤: 1.在你的项目中添加 MPAndroidChart 依赖。你可以在 gradle 文件中添加以下代码: ``` implementation 'com.github.PhilJay:MPAndroidChart:v3.1.0' ``` 2.在你的布局文件中添加一个 PieChart 视图: ``` <com.github.mikephil.charting.charts.PieChart android:id="@+id/pie_chart" android:layout_width="match_parent" android:layout_height="match_parent"/> ``` 3.在你的 Activity 或 Fragment 中找到这个 PieChart 视图,并为它设置一些属性: ``` PieChart pieChart = findViewById(R.id.pie_chart); pieChart.setUsePercentValues(true); // 设置使用百分比值 pieChart.getDescription().setEnabled(false); // 隐藏描述 pieChart.setExtraOffsets(5, 10, 5, 5); // 设置间距 pieChart.setDrawHoleEnabled(true); // 绘制中间的空心圆 pieChart.setHoleColor(Color.WHITE); // 设置空心圆的颜色 pieChart.setTransparentCircleRadius(61f); // 设置透明圆的半径 pieChart.setHoleRadius(58f); // 设置空心圆的半径 pieChart.setRotationAngle(0); // 设置起始角度 pieChart.setRotationEnabled(true); // 可以旋转 pieChart.setHighlightPerTapEnabled(true); // 可以高亮显示 ``` 4.为饼图添加数据。首先创建一个 ArrayList<PieEntry>,然后将每个数据添加到其中: ``` ArrayList<PieEntry> entries = new ArrayList<>(); entries.add(new PieEntry(18.5f, "Green")); entries.add(new PieEntry(26.7f, "Yellow")); entries.add(new PieEntry(24.0f, "Red")); entries.add(new PieEntry(30.8f, "Blue")); ``` 5.将数据添加到 PieDataSet 中,并为数据集设置一些属性: ``` PieDataSet dataSet = new PieDataSet(entries, "Election Results"); dataSet.setSliceSpace(3f); // 设置每个扇形之间的间距 dataSet.setSelectionShift(5f); // 设置点击时的扇形半径 dataSet.setColors(ColorTemplate.JOYFUL_COLORS); // 设置颜色 ``` 6.创建一个 PieData 对象,将数据集传递给它: ``` PieData data = new PieData(dataSet); data.setValueTextSize(10f); // 设置文字大小 data.setValueTextColor(Color.YELLOW); // 设置文字颜色 ``` 7.将 PieData 对象设置到 PieChart 中: ``` pieChart.setData(data); pieChart.invalidate(); // 刷新图表 ``` 现在,你就可以显示你的饼图了。上面的代码只是一个简单的示例,你可以根据自己的需求调整图表的属性和样式。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值