家庭版记账本app开发之关于(数据库查找出数据)圆饼图的生成
这次完成的主要的怎样从数据库中调出数据。之后通过相关的数据,生成想要的圆饼图。以方便用户更加直观的看见关于账本的基本情况。
在圆饼图生成中用到了一些外部资源
具体的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开发之关于(数据库查找出数据)圆饼图的生成的更多相关文章
- 家庭版记账本app开发完成
经过这几天关于android的相关学习,对于家庭版记账本app以及开发结束. 实现的功能为:用户的注册.登录.添加支出账单.添加收入账单.显示所有的该用户的账单情况(收入和支出).生产图表(直观的显示 ...
- 家庭版记账本app开发进度相关界面的规划
总的app界面包括四个页面,页面可以来回滑动.设计的时候就和微信的四个页面类似. 由于没有找到合适的图标进行替换,在此仍应用微信对应的四个图标. 总的四个页面是: 1.增加收入或者支出的小账单.当点击 ...
- 家庭版记账本app开发进度。开发到现在整个app只剩下关于图表的设计了,具体功能如下
首先说一下自己的功能: 实现了用户的登录和注册.添加收入记账和添加支出记账.粗略显示每条账单基本情况.通过点击每条账单来显示具体的情况, 之后就是退出当前用户的操作. 具体的页面情况如下: 这就是整个 ...
- 简单记账本APP开发一
在对Android的一些基础的知识有了一定了解,以及对于AndroidStudio的如何使用有了 一定的熟悉后,决定做一个简单的记账本APP 开发流程 1.记账本的页面 2.可以添加新的账目 (一)页 ...
- 家庭版记账本app进度之关于listview显示账单,并为其添加点击事件
这个主要学习是关于listview的学习. 怎样去自定义adapter,以及使用.自己创建文件,还有就是为listview的每一个子控件添加点击事件. 在整个过程中收获到的知识点如下: 一.对于数据库 ...
- 简单记账本APP开发二
今天主要是进行了适配器的编写,数据库的创建以及对完善了业务逻辑,简单的APP到此已经通过测试可以使用.
- 家庭版记账本app进度之关于android界面布局的相关学习
1.线性布局(linearlayout)是一种让视图水平或垂直线性排列的布局线性布局使用<LinearLayout>标签进行配置对应代码中的类是android.widget.LinearL ...
- 家庭版记账本app进度之对于按钮的点击事件以及线性布局以及(alertdialog)等相关内容的应用测试
通过线性布局,制作出连个按钮还有文本输入框以及嘴上放的标题文本进行信息的相关显示,完后最后的信息的输入,之后在屏幕的的下方进行显示 当点击第一个按钮的时候,在下方就会简单的出现你自己刚刚输入的相关信息 ...
- 家庭版记账本app之常用控件的使用方法
现在先介绍在android开发的时候会用的相关的控件,做一个基本的了解方便我们之后对其进行相关具体的操作.下面是相应额详细情况: TextView android:layout_width 和 and ...
随机推荐
- 通过实现简单聊天室了解websocket的基础使用
websocket基础使用 用到的依赖包 websocket的依赖 <dependency> <groupId>javax.websocket</groupId> ...
- [CSP初赛] 组合数学的三个技巧以及从另一方面思考组合类问题
也不知道老师讲不讲 话说好久没有水博客了,看了一点\(python\)然后就去搞文化课了 正好网课讲到组合数学,然后觉得还蛮难的(其实是我变菜了),就想到了以前的\(csp\)的组合数学基础 果然被我 ...
- 谈谈一些逻辑相同,性能差异却很大的sql
总结写在前面: 1. 本篇讲述了三个例子,其本质都是揭示了若对索引字段做函数操作,可能会破坏索引值的有序性,由此优化器就决定放弃走树搜索功能. 2. 由第1点提供了一个优化思路,即我们能否避免或转化s ...
- Natas26 Writeup(PHP反序列化漏洞)
Natas26: 打开页面是一个输入坐标点进行绘图的页面. <html> <head> <!-- This stuff in the header has nothing ...
- [剑指offer]25.合并两个排序的链表(迭代+递归)
25.合并两个排序的链表 题目 输入两个递增排序的链表,合并这两个链表并使新链表中的节点仍然是递增排序的. 示例1: 输入:1->2->4, 1->3->4 输出:1-> ...
- shell编程之字符串处理
# .#号截取,删除左边字符,保留右边字符,*// 表示从左边开始删除第一个 // 号及左边的所有字符 echo ${var#*//} # . ## 号截取,删除左边字符,保留右边字符,##*/ 表示 ...
- hdu2066多源最短路
题目链接:http://icpc.njust.edu.cn/Problem/Hdu/2066/ SPFA可以高效过,代码如下: #include<bits/stdc++.h> using ...
- 题解 P1002 【过河卒】
正文 简单描述一下题意: 士兵想要过河,他每一次可以往下走一格,也可以往右走一格,但马一步走到的地方是不能走的,问走到\(n\)行,\(m\)列有多少种走法 我们显然应该先根据马的位置将不能走的格子做 ...
- 干货系列之java注解
干货系列之java注解 前言 java反射和注解在java里面很重要,但是很多人对这方面的知识理解不是很好,我来说说我自己对java反射和注解的理解,这两块内容本来应该出在一个博客文章里面讲解,但是由 ...
- 特征选择与稀疏学习(Feature Selection and Sparse Learning)
本博客是针对周志华教授所著<机器学习>的"第11章 特征选择与稀疏学习"部分内容的学习笔记. 在实际使用机器学习算法的过程中,往往在特征选择这一块是一个比较让人模棱两可 ...