2种Android图表的简单介绍+折线图、饼图的例子,高级程序员面试题

本文介绍了Android中使用HelloCharts库创建折线图和饼图的方法,包括添加依赖、设置数据和样式。HelloCharts支持Android 2.2以上版本,提供多种图表类型并支持动画效果。
摘要由CSDN通过智能技术生成

④实现结果:

在这里插入图片描述

3.饼图

①同样的先建一个布局文件

<com.github.mikephil.charting.charts.PieChart

android:id="@+id/pie"

android:layout_width=“match_parent”

android:layout_height=“match_parent”>

</com.github.mikephil.charting.charts.PieChart>

②建一个pieChartActivity.java ,需要完成创建一个list用来保存数据,对饼图的颜色、相隔等进行设计,导入数据以及最后实例化Dataset 。

import android.graphics.Color;

import android.os.Bundle;

import androidx.appcompat.app.AppCompatActivity;

import android.view.WindowManager;

import com.github.mikephil.charting.charts.PieChart;;

import com.github.mikephil.charting.data.PieData;

import com.github.mikephil.charting.data.PieDataSet;

import com.github.mikephil.charting.data.PieEntry;

import java.util.ArrayList;

import java.util.List;

public class pieChartActivity extends AppCompatActivity {

private PieChart pie;

//①实例化一个 List 用来保存你的数据,这里是pieEntry

List list=new ArrayList<>();

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,

WindowManager.LayoutParams.FLAG_FULLSCREEN);

setContentView(R.layout.piechart);

setTitle(“PieChartActivity”);

pie = (PieChart) findViewById(R.id.pie);

//②输入数据

//其中两个数字对应的分别是 X轴Y轴

list.add(new PieEntry(25,“Cola”));

list.add(new PieEntry(10, “Sprite”));

list.add(new PieEntry(13, “Fanta”));

list.add(new PieEntry(20, “Suntory”));

list.add(new PieEntry(5, “Rio”));

list.add(new PieEntry(20, “Beer”));

list.add(new PieEntry(17, “Other”));

//③定义一下饼图的颜色

ArrayList colors = new ArrayList();

colors.add(Color.RED);

colors.add(Color.GREEN);

colors.add(Color.CYAN);

colors.add(Color.MAGENTA);

colors.add(getResources().getColor(R.color.blue));

colors.add(Color.LTGRAY);

colors.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值