Android饼图中间带有图标的柱状图
创始人
2024-10-06 12:03:45
0

要实现一个Android饼图中间带有图标的柱状图,可以使用第三方库MPAndroidChart。下面是一个使用MPAndroidChart库的示例代码:

  1. 首先,添加依赖项到你的项目的build.gradle文件中:
implementation 'com.github.PhilJay:MPAndroidChart:v3.1.0'
  1. 在你的布局文件中,添加一个饼图和一个柱状图的组合视图:


    

    


  1. 在你的Activity或Fragment中,创建饼图和柱状图的实例,并设置数据和样式:
import android.graphics.Typeface;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import com.github.mikephil.charting.charts.HorizontalBarChart;
import com.github.mikephil.charting.charts.PieChart;
import com.github.mikephil.charting.components.Description;
import com.github.mikephil.charting.components.Legend;
import com.github.mikephil.charting.data.BarData;
import com.github.mikephil.charting.data.BarDataSet;
import com.github.mikephil.charting.data.BarEntry;
import com.github.mikephil.charting.data.PieData;
import com.github.mikephil.charting.data.PieDataSet;
import com.github.mikephil.charting.data.PieEntry;
import com.github.mikephil.charting.formatter.PercentFormatter;
import com.github.mikephil.charting.utils.ColorTemplate;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    private PieChart pieChart;
    private HorizontalBarChart barChart;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        pieChart = findViewById(R.id.pieChart);
        barChart = findViewById(R.id.barChart);

        // 设置饼图数据
        List pieEntries = new ArrayList<>();
        pieEntries.add(new PieEntry(40f, "Category 1"));
        pieEntries.add(new PieEntry(30f, "Category 2"));
        pieEntries.add(new PieEntry(20f, "Category 3"));
        pieEntries.add(new PieEntry(10f, "Category 4"));

        PieDataSet pieDataSet = new PieDataSet(pieEntries, "Pie Chart");
        pieDataSet.setColors(ColorTemplate.COLORFUL_COLORS);
        pieDataSet.setValueTextSize(12f);
        pieDataSet.setValueFormatter(new PercentFormatter());

        PieData pieData = new PieData(pieDataSet);
        pieChart.setData(pieData);
        pieChart.setUsePercentValues(true);
        pieChart.setDrawHoleEnabled(false);
        pieChart.setCenterText("Pie Chart");
        pieChart.setCenterTextSize(18f);
        pieChart.animateY(1000);
        pieChart.getDescription().setEnabled(false);

        // 设置柱状图数据
        List barEntries = new ArrayList<>();
        barEntries.add(new BarEntry(40f, 0));
        barEntries.add(new BarEntry(30f, 1));
        barEntries.add(new BarEntry(20f, 2));
        barEntries.add(new BarEntry(10f, 3));

        BarDataSet barDataSet = new BarDataSet(barEntries, "Bar Chart");
        barDataSet.setColors(ColorTemplate.COLORFUL_COLORS);
        barDataSet.setValueTextSize(12f);

        BarData barData = new BarData(barDataSet);
        barChart.setData(barData);
        barChart.getXAxis().setEnabled(false);
        barChart.getAxisRight().setEnabled(false);
        barChart.getAxisLeft().setEnabled(false);
        barChart.getLegend().setEnabled(false);
        barChart.getDescription().setEnabled(false);
        barChart.animateY(1000);
    }
}

这样,你就可以在Android应用中实现一个带有图标的饼图和柱状图了。你可以根据自己的需求调整图表的样式和数据。

相关内容

热门资讯

三分钟外挂!德普之星私人局辅助... 三分钟外挂!德普之星私人局辅助,蛮王辅助,第三方教程(有挂规律)-哔哩哔哩1、德普之星私人局辅助系统...
今天上午!欢乐情怀开挂(辅助挂... 今天上午!欢乐情怀开挂(辅助挂)果然确实有挂(有挂手段)-哔哩哔哩;无需打开直接搜索打开薇:1367...
透视辅助!wepoker辅助挂... 无需打开直接搜索加微信客服(136704302)咨询了解wepoker辅助挂有没有辅助器目前(微信:...
六分钟外挂!四川熊猫辅助软件下... 六分钟外挂!四川熊猫辅助软件下载,约逗东乡辅助器,解密教程(有挂头条)-哔哩哔哩1、全新机制【四川熊...
围绕透视问题!河洛杠次脚本开发... 围绕透视问题!河洛杠次脚本开发(辅助挂)其实真的有挂(有挂手段)-哔哩哔哩;亲,河洛杠次脚本开发这款...
透视辅助!WPK透视挂怎么用,... 无需打开直接搜索;操作使用教程:透视辅助!WPK透视挂怎么用,wpk有挂吗,(总算了解外挂辅助神器)...
十分钟外挂!休闲九九破解版,闲... 十分钟外挂!休闲九九破解版,闲来山水广西辅助,介绍教程(有挂实锤)-哔哩哔哩闲来山水广西辅助辅助器中...
现场直击!鲨鱼大厅可不可以破解... 大家好,今天小编来为大家解答鲨鱼大厅可不可以破解这个问题咨询软件客服可以免费测试直接加微信(1367...
辅助工具!wepoker私人局... 辅助工具!wepoker私人局开挂透视,wepoker透视挂是真的吗,(交流学习经验外挂辅助软件)辅...
八分钟外挂!顺欣茶坊辅助器试用... 八分钟外挂!顺欣茶坊辅助器试用版,微信小程序微乐辅助免费,高科技教程(有挂细节)-哔哩哔哩进入游戏-...