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

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

具体的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. Java反射之数组的反射应用

    上一篇我们说了Java反射之成员方法的反射 这一篇我们说一说数组的反射应用,数组的有长度等属性,所以也会有相应的方法获得这些属性,这里我们不一一列举哪些方法.我们来了解反射包中的一个类----Arra ...

  2. 仿flash实现图片轮换播放

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  3. 使用form 表单 弹出登录框,只传递数据,不刷新界面

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  4. 题解 UVA12186 【工人的请愿书 Another Crisis】

    俺太难了 记录一下我调了一个小时的错误 多测不清空 多测清空只清空了\(vector\) 多测全清空了,但是忘了清空\(vector[0]\) \(priority\)_ \(queue\)把\(gr ...

  5. 组件/ 外层数据初始化时候,不应该触发 on-change 事件

    组件/ 外层数据初始化时候,不应该触发 on-change 事件 watch: { value (value) { this.noOnChange = true // 外层传值 不触发on-chang ...

  6. 大数据软件安装之HBase(NoSQL数据库)

    一.安装部署 1.Zookeeper正常部署 (见前篇博文大数据软件安装之ZooKeeper监控 ) [test@hadoop102 zookeeper-3.4.10]$ bin/zkServer.s ...

  7. 《2018面向对象程序设计(java)课程学习进度条》

     学习收获最大的程序阅读或编程任务    课堂/课余学习时间(小时)    发布博客/评论他人博客数量   (阅读/编写)代码行数        周次                  九九乘法表   ...

  8. 北京大学公开课《数据结构与算法Python版》

    之前我分享过一个数据结构与算法的课程,很多小伙伴私信我问有没有Python版. 看了一些公开课后,今天特向大家推荐北京大学的这门课程:<数据结构与算法Python版>. 课程概述 很多同学 ...

  9. PAT-B 1040. 有几个PAT(25)

    1040. 有几个PAT(25) 时间限制 120 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CAO, Peng 字符串APPAPT中包含了两个单 ...

  10. iOS 设备尺寸与系统信息

    参考 http://blog.csdn.net/newbieprogrammer/article/details/50569384 http://blog.csdn.net/developer_zha ...