昨天主要是对饼状图进行学习,找到了一个相关的模板,可以实现从界面的输入框获取数据,并经过相关的计算,算出相应的角度,并调用方法绘制出饼状图
遇到的问题:由于获取数据时是都需要调用hashmap中的数据,所以必须初始化,并且只能实现首先初始化,然后点击生成统计表实现饼状图的绘制
今天准备做,对饼状图处理数据的方法进行研究,增加跟多的消费类型,并且对数据进行赋值,使其能够成功的绘制饼状图。
private void initView() {
mLny = findViewById(R.id.lnyOut);
etCanyin = findViewById(R.id.etCanyin);
etYule = findViewById(R.id.etWenjiao);
etCHuxing = findViewById(R.id.etChuxing);
etFushi = findViewById(R.id.etFushi);
etShenghuo = findViewById(R.id.etShenghuo);
btnUpdate = findViewById(R.id.btnUpdate);
btnUpdate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
uodatePieChart();
}
});
}
@RequiresApi(api = Build.VERSION_CODES.O)
@Override
protected void onResume() {
super.onResume();
myDataView = new MyDataView(this, DataDegree(), DataColor());
params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT);
mLny.addView(myDataView, params);
}
private HashMap<String , Float> DataDegree(){
dataDegee.put("餐饮消费", 32f);
dataDegee.put("文教娱乐", 42f);
dataDegee.put("服饰美容", 52f);
dataDegee.put("出行交通", 92f);
dataDegee.put("其它",142f);
dataDegee.put("1212",32f);
return dataDegee;
}
@RequiresApi(api = Build.VERSION_CODES.O)
private HashMap<String , String> DataColor(){
dataColor.put("餐饮消费", "#6600ff");
dataColor.put("文教娱乐", "#cc00ff");
dataColor.put("服饰美容","#9933ff");
dataColor.put("出行交通", "#ff9900");
dataColor.put("其它","#9999ff");
dataColor.put("1212","#ffff00");
dataColor.put("","#0000ff");
dataColor.put("","#00ff00");
dataColor.put("","#800000");
// dataColor.put("","#800080");
// dataColor.put("","");
return dataColor;
}
private void uodatePieChart(){
month = (TextView)findViewById(R.id.month);
String n = month.getText().toString();
String[] a = n.split("[^0-9]");
String year2 = a[0];
String month2 = a[1];
float canyin = Query(year2,month2,"餐饮");
float yule = Query(year2,month2,"娱乐");
float gouwu = Query(year2,month2,"购物");
float chuxing = 50;
float fushi = 86;
float s = 58;
float total = canyin +yule +fushi +gouwu +chuxing + 58;
if(total>0){
dataDegee.put("餐饮", getDegree(canyin,total));
dataDegee.put("服饰美容", getDegree(fushi,total));
dataDegee.put("其它", getDegree(gouwu,total));
dataDegee.put("出行交通", getDegree(chuxing,total));
dataDegee.put("娱乐", getDegree(yule,total));
dataDegee.put("1212",total);
mLny.removeView(myDataView);
mLny.addView(myDataView, params);
Log.i("test draw", "remove view ; add view");
Toast.makeText(this,"哇,你居然在"+getMaxinumType()+"上花了这么多",Toast.LENGTH_SHORT).show();
}else {
Toast.makeText(this,"输入您的消费额度",Toast.LENGTH_SHORT).show();
}
}
private float getDegree(float number, float total){
return number/total * 360;
}
private String getMaxinumType(){
String type="";
float money = Float.MIN_VALUE;
for(String key : dataDegee.keySet()){
if(dataDegee.get(key) > money){
type = key;
money = dataDegee.get(key);
}
}
return type;
}