Android饼图中间带有图标的柱状图
创始人
2024-10-06 12:04:02
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应用中实现一个带有图标的饼图和柱状图了。你可以根据自己的需求调整图表的样式和数据。

相关内容

热门资讯

安卓换鸿蒙系统会卡吗,体验流畅... 最近手机圈可是热闹非凡呢!不少安卓用户都在议论纷纷,说鸿蒙系统要来啦!那么,安卓手机换上鸿蒙系统后,...
app安卓系统登录不了,解锁登... 最近是不是你也遇到了这样的烦恼:手机里那个心爱的APP,突然就登录不上了?别急,让我来帮你一步步排查...
安卓系统拦截短信在哪,安卓系统... 你是不是也遇到了这种情况:手机里突然冒出了很多垃圾短信,烦不胜烦?别急,今天就来教你怎么在安卓系统里...
windows官网系统多少钱 Windows官网系统价格一览:了解正版Windows的购买成本Windows 11官方价格解析微软...
iwatch怎么连接安卓系统,... 你有没有想过,那款时尚又实用的iWatch,竟然只能和iPhone好上好?别急,今天就来给你揭秘,怎...
安卓系统要维护多久,安卓系统维... 你有没有想过,你的安卓手机里那个陪伴你度过了无数日夜的安卓系统,它究竟要陪伴你多久呢?这个问题,估计...
iphone系统与安卓系统更新... 最近是不是你也遇到了这样的烦恼?手机更新系统总是失败,急得你团团转。别急,今天就来给你揭秘为什么iP...
安卓系统如何卸载app,轻松掌... 手机里的App越来越多,是不是感觉内存不够用了?别急,今天就来教你怎么轻松卸载安卓系统里的App,让...
安卓系统怎么连不上carlif... 安卓系统无法连接CarLife的原因及解决方法随着智能手机的普及,CarLife这一车载互联功能为驾...
安卓系统应用怎么重装,安卓应用... 手机里的安卓应用突然罢工了,是不是让你头疼不已?别急,今天就来手把手教你如何重装安卓系统应用,让你的...