这次完成的主要的怎样从数据库中调出数据。之后通过相关的数据,生成想要的圆饼图。以方便用户更加直观的看见关于账本的基本情况。

在圆饼图生成中用到了一些外部资源

具体的import如下:

import com.github.mikephil.charting.animation.Easing;
import com.github.mikephil.charting.charts.PieChart;
import com.github.mikephil.charting.components.Legend;
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 java.util.ArrayList;
这些是用到的外部的相关资源:
通过这些就可以很简单的是用圆饼图的生成:
具体的代码分析如下:
一、饼状图上字体的设置
// entry label styling
pieChart.setDrawEntryLabels(true);//设置是否绘制Label
pieChart.setEntryLabelColor(Color.RED);//设置绘制Label的颜色
pieChart.setEntryLabelTextSize(20f);//设置绘制Label的字体大小
二、饼图颜色
colors.add(Color.rgb(205, 205, 205));
colors.add(Color.rgb(114, 188, 223));
pieDataSet.setColors(colors); pieDataSet.setSliceSpace(0f);//设置选中的Tab离两边的距离
pieDataSet.setSelectionShift(5f);//设置选中的tab的多出来的
PieData pieData = new PieData();
pieData.setDataSet(pieDataSet); 三、各个饼状图所占比例数字的设置
pieData.setValueFormatter(new PercentFormatter());//设置%
pieData.setValueTextSize(20f);
pieData.setValueTextColor(Color.YELLOW);
四、
pieChart.setUsePercentValues(true);//设置value是否用显示百分数,默认为false
。这些是用到的主要知识点:
具体的代码只有一个.java文件还有一个.xml文件
java文件如下:
public class  ThePieChare3 extends AppCompatActivity {
private PieChart pieChart;
private Context context;
private Intent intent2;
private String username;
private int zhichujine,shourujine;
private DBOpenMessage dbOpenMessage;
private TextView shourutxt,zhichutxt; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.piechare3);
pieChart = (PieChart) findViewById(R.id.wenpie3); context=this;
intent2=getIntent();
username=intent2.getStringExtra("username");
dbOpenMessage=new DBOpenMessage(ThePieChare3.this,"db_wen2",null,1);
getMessage1(username); shourutxt=(TextView)findViewById(R.id.wentext32);
zhichutxt=(TextView)findViewById(R.id.wentext31); shourutxt.setText(Integer.toString(shourujine));
zhichutxt.setText(Integer.toString(zhichujine)); pieChart.setUsePercentValues(true);
pieChart.setDescription("所有金额支出收入总情况");
pieChart.setDescriptionTextSize(20); pieChart.setExtraOffsets(5, 5, 5, 5); pieChart.setDrawCenterText(true);
pieChart.setCenterTextColor(Color.RED);
pieChart.setCenterTextSize(15); pieChart.setDrawHoleEnabled(true);
pieChart.setHoleColor(Color.WHITE);
pieChart.setHoleRadius(40f); pieChart.setTransparentCircleColor(Color.BLACK);
pieChart.setTransparentCircleAlpha(100);
pieChart.setTransparentCircleRadius(40f); // enable rotation of the chart by touch
pieChart.setRotationEnabled(true);
pieChart.setRotationAngle(10); pieChart.setHighlightPerTapEnabled(true); Legend l = pieChart.getLegend();
l.setPosition(Legend.LegendPosition.RIGHT_OF_CHART_CENTER); l.setForm(Legend.LegendForm.LINE);
l.setYEntrySpace(0f);
l.setYOffset(0f); // entry label styling
pieChart.setDrawEntryLabels(true);
pieChart.setEntryLabelColor(Color.RED);
pieChart.setEntryLabelTextSize(20f); // pieChart.setOnChartValueSelectedListener(this);
pieChart.animateY(3400, Easing.EasingOption.EaseInQuad);
ArrayList<PieEntry> pieEntries = new ArrayList<PieEntry>(); pieEntries.add(new PieEntry(zhichujine, "总支出金额"));
pieEntries.add(new PieEntry(shourujine, "总收入金额"));
String centerText ;
if(shourujine>zhichujine)
{
centerText = "整体为正资产:\n+¥" + (shourujine-zhichujine);
}
else if(shourujine<zhichujine)
{
centerText = "整体为负资产:\n-¥" + (zhichujine-shourujine);
}
else
{
centerText = "整体资产为零";
} pieChart.setCenterText(centerText);
PieDataSet pieDataSet = new PieDataSet(pieEntries, "");
ArrayList<Integer> colors = new ArrayList<Integer>(); colors.add(Color.rgb(205, 205, 205));
colors.add(Color.rgb(114, 188, 223));
pieDataSet.setColors(colors); pieDataSet.setSliceSpace(0f);
pieDataSet.setSelectionShift(5f);
PieData pieData = new PieData();
pieData.setDataSet(pieDataSet); pieData.setValueFormatter(new PercentFormatter());
pieData.setValueTextSize(20f);
pieData.setValueTextColor(Color.YELLOW); pieChart.setData(pieData);
// undo all highlights
pieChart.highlightValues(null);
pieChart.invalidate();
}
private void getMessage1(String username) {
Cursor cursor=dbOpenMessage.getAllCostData(username);
if(cursor!=null){
while(cursor.moveToNext()){
Message message2=new Message();
message2.userkind=cursor.getString(cursor.getColumnIndex("userkind"));
message2.usermoney=cursor.getString(cursor.getColumnIndex("usermoney"));
message2.userchoice=cursor.getString(cursor.getColumnIndex("userchoice"));
if(message2.userchoice.equals("支出"))
{
zhichujine+=Integer.parseInt(message2.usermoney);
}
else if(message2.userchoice.equals("收入"))
{
shourujine+=Integer.parseInt(message2.usermoney);
} }
}
}
}

与之对应的.xml文件如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".ThePieChare1">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="支出各项占比圆饼图"
android:layout_marginTop="10dp"
android:textSize="30dp"
android:textColor="@color/lanse"
android:layout_marginBottom="20dp"
android:gravity="center_horizontal"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="20dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/colorAccent"
android:layout_marginLeft="70dp"
android:textSize="20dp"
android:text="食品总支出:"/>
<TextView
android:id="@+id/wentext11"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/colorPrimaryDark"
android:layout_marginLeft="10dp"
android:textSize="20dp"
android:text=""/>
</LinearLayout> <LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="20dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/colorAccent"
android:layout_marginLeft="70dp"
android:textSize="20dp"
android:text="衣物总支出:"/>
<TextView
android:id="@+id/wentext12"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/colorPrimaryDark"
android:layout_marginLeft="10dp"
android:textSize="20dp"
android:text=""/>
</LinearLayout> <LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="20dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/colorAccent"
android:layout_marginLeft="70dp"
android:textSize="20dp"
android:text="出行总支出:"/>
<TextView
android:id="@+id/wentext13"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/colorPrimaryDark"
android:layout_marginLeft="10dp"
android:textSize="20dp"
android:text=""/>
</LinearLayout> <LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="20dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/colorAccent"
android:layout_marginLeft="70dp"
android:textSize="20dp"
android:text="其他总支出:"/>
<TextView
android:id="@+id/wentext14"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/colorPrimaryDark"
android:layout_marginLeft="10dp"
android:textSize="20dp"
android:text=""/>
</LinearLayout> <com.github.mikephil.charting.charts.PieChart
android:id="@+id/wenpie1"
android:layout_width="match_parent"
android:layout_height="320dp"
android:layout_gravity="center_vertical">
</com.github.mikephil.charting.charts.PieChart>
</LinearLayout>

这样就完成了关于图表生成的步骤;

具体对应的实验结果如下:

点击第一个生成第一个图表:

图表是动态生成的:(可以满足旋转生成)

家庭版记账本app开发之关于(数据库查找出数据)圆饼图的生成的更多相关文章

  1. 家庭版记账本app开发完成

    经过这几天关于android的相关学习,对于家庭版记账本app以及开发结束. 实现的功能为:用户的注册.登录.添加支出账单.添加收入账单.显示所有的该用户的账单情况(收入和支出).生产图表(直观的显示 ...

  2. 家庭版记账本app开发进度相关界面的规划

    总的app界面包括四个页面,页面可以来回滑动.设计的时候就和微信的四个页面类似. 由于没有找到合适的图标进行替换,在此仍应用微信对应的四个图标. 总的四个页面是: 1.增加收入或者支出的小账单.当点击 ...

  3. 家庭版记账本app开发进度。开发到现在整个app只剩下关于图表的设计了,具体功能如下

    首先说一下自己的功能: 实现了用户的登录和注册.添加收入记账和添加支出记账.粗略显示每条账单基本情况.通过点击每条账单来显示具体的情况, 之后就是退出当前用户的操作. 具体的页面情况如下: 这就是整个 ...

  4. 简单记账本APP开发一

    在对Android的一些基础的知识有了一定了解,以及对于AndroidStudio的如何使用有了 一定的熟悉后,决定做一个简单的记账本APP 开发流程 1.记账本的页面 2.可以添加新的账目 (一)页 ...

  5. 家庭版记账本app进度之关于listview显示账单,并为其添加点击事件

    这个主要学习是关于listview的学习. 怎样去自定义adapter,以及使用.自己创建文件,还有就是为listview的每一个子控件添加点击事件. 在整个过程中收获到的知识点如下: 一.对于数据库 ...

  6. 简单记账本APP开发二

    今天主要是进行了适配器的编写,数据库的创建以及对完善了业务逻辑,简单的APP到此已经通过测试可以使用.

  7. 家庭版记账本app进度之关于android界面布局的相关学习

    1.线性布局(linearlayout)是一种让视图水平或垂直线性排列的布局线性布局使用<LinearLayout>标签进行配置对应代码中的类是android.widget.LinearL ...

  8. 家庭版记账本app进度之对于按钮的点击事件以及线性布局以及(alertdialog)等相关内容的应用测试

    通过线性布局,制作出连个按钮还有文本输入框以及嘴上放的标题文本进行信息的相关显示,完后最后的信息的输入,之后在屏幕的的下方进行显示 当点击第一个按钮的时候,在下方就会简单的出现你自己刚刚输入的相关信息 ...

  9. 家庭版记账本app之常用控件的使用方法

    现在先介绍在android开发的时候会用的相关的控件,做一个基本的了解方便我们之后对其进行相关具体的操作.下面是相应额详细情况: TextView android:layout_width 和 and ...

随机推荐

  1. jupyter 安装问题 building 'zmq.libzmq' extension error: Microsoft Visual C++ 14.0 is required. Get it with "Microsoft Visual C++ Build Tools": http://landinghub.visualstudio.com/visual-cpp-build-tools

    1.用pip install jupyter 安装到一半就报错 错误提示: building 'zmq.libzmq' extension error: Microsoft Visual C++ 14 ...

  2. C++ 顺序表练习

    #include <stdio.h> #include <stdlib.h> #include <string.h> struct Array //定义一个数据存储 ...

  3. Java并发编程学习前期知识下篇

    Java并发编程学习前期知识下篇 通过上一篇<Java并发编程学习前期知识上篇>我们知道了在Java并发中的可见性是什么?volatile的定义以及JMM的定义.我们先来看看几个大厂真实的 ...

  4. 小程序中内容审核功能的使用(后台使用thinkPHP5.1)

    本文包含文本和图片的检测 //接收要检测的文本内容并调用检测方法 public function textCheck(Request $request){ //内容安全识别 $data['conten ...

  5. pytorch的自动求导机制 - 计算图的建立

    一.计算图简介 在pytorch的官网上,可以看到一个简单的计算图示意图, 如下. import torchfrom torch.autograd import Variable x = Variab ...

  6. 创建Sphinx + GitHub + ReadtheDocs托管文档

    最新博客链接 "Tsanfer's Blog" 创建Sphinx + GitHub + ReadtheDocs托管文档 Readthedocs在线电子书链接

  7. SQL常见错误总结

    目录 语法错误 标点错漏 重命名 数据拼接 null值 逻辑顺序 函数错误 参数的数量 参数的格式 逻辑错误 数据重复 无效筛选 标签重叠 时间错位 SQL是数据分析中最高频的操作之一,本文梳理常见的 ...

  8. Natas29 Writeup(Perl命令注入、00截断、绕过过滤)

    Natas29: 本关打开后,可以看到一个下拉列表,选择不同的内容,会得到不同的大量文本的页面. 观察url部分:http://natas29.natas.labs.overthewire.org/i ...

  9. Django之CBV装饰器,跨站请求伪造,auth认证

    CBV加装饰器 基于session实现登录 def login(request): if request.method == 'POST': username = request.POST.get(' ...

  10. 初探elasticsearch

    目录 安装elasticsearch elasticsearch中的层级结构与关系型数据库的对比 elasticsearch的分布式特性 集群和节点 为java用户提供的两种内置客户端 节点客户端(n ...