Android MPAndroidChart柱状图结合Bmob后端云数据库实现统计本周收支详情

        Dialog dialog = new Dialog(this);
        @SuppressLint("InflateParams") View inflate = LayoutInflater.from(this).inflate(R.layout.loading1, null);
        avi = inflate.findViewById(R.id.avi);
        dialog.setContentView(inflate);
        dialog.show();
        AVLoadingIndicatorView avi = new AVLoadingIndicatorView(this);
        avi.smoothToShow();
        //柱状图的期间查询
        @SuppressLint("SimpleDateFormat") SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        // 获取本周的第一天(周日)
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        Date startDate = calendar.getTime();
        String createdAtStart = sdf.format(startDate);
        Date createdAtDateStart = sdf.parse(createdAtStart);
        BmobDate bmobCreatedAtDateStart = new BmobDate(createdAtDateStart);
        // 获取本周的最后一天(周六)
        calendar.set(Calendar.DAY_OF_WEEK, Calendar.SATURDAY);
        Date endDate = calendar.getTime();
        String createdAtEnd = sdf.format(endDate);
        Date createdAtDateEnd = sdf.parse(createdAtEnd);
        BmobDate bmobCreatedAtDateEnd = new BmobDate(createdAtDateEnd);
        BmobQuery<Spend> Spend_Start = new BmobQuery<>();
        BmobQuery<Income> Income_Start = new BmobQuery<>();
        Spend_Start.addWhereGreaterThanOrEqualTo("time", bmobCreatedAtDateStart);
        BmobQuery<Spend> Spend_End = new BmobQuery<>();
        BmobQuery<Income> Income_End = new BmobQuery<>();
        Spend_End.addWhereLessThanOrEqualTo("time", bmobCreatedAtDateEnd);
        List<BmobQuery<Spend>> Spend_queries = new ArrayList<>();
        List<BmobQuery<Income>> Income_queries = new ArrayList<>();
        Spend_queries.add(Spend_Start);
        Spend_queries.add(Spend_End);
        Income_queries.add(Income_Start);
        Income_queries.add(Income_End);
        BmobQuery<Spend> categoryBmobQuery_Spend = new BmobQuery<>();
        BmobQuery<Income> categoryBmobQuery_Income = new BmobQuery<>();
        categoryBmobQuery_Income.and(Income_queries);
        categoryBmobQuery_Income.order("time");
        categoryBmobQuery_Spend.and(Spend_queries);
        categoryBmobQuery_Spend.order("time");
        categoryBmobQuery_Spend.findObjects(new FindListener<Spend>() {
            @Override
            public void done(List<Spend> spendList, BmobException e) {
                List<BarEntry> list1 = new ArrayList<>();
                List<BarEntry> list2 = new ArrayList<>();
                YAxis yAxis_left = bar.getAxisLeft();
                YAxis yAxis_right = bar.getAxisRight();
                yAxis_left.setAxisMinimum(0);
                yAxis_right.setAxisMinimum(0);
                XAxis xAxis = bar.getXAxis();
                xAxis.setGranularity(1f);
                xAxis.setAxisMinimum(0.5f);
                xAxis.setAxisMaximum(7.5f);
                xAxis.setLabelCount(8, true);
                xAxis.setCenterAxisLabels(true);
                bar.setExtraBottomOffset(5f);
                String[] labels = {"日", "一", "二", "三", "四", "五", "六"};
                xAxis.setValueFormatter(new ValueFormatter() {
                    @Override
                    public String getFormattedValue(float value) {
                        int index = (int) value;
                        if (index >= 0 && index < labels.length) {
                            return labels[index];
                        }
                        return "";
                    }
                });
                xAxis.setTextSize(16f);
                if (e == null) {
                    Map<String, Double> timeMapSpend = new HashMap<>();
                    Map<String, Double> timeMapIncome = new HashMap<>();
                    calendar.setTime(startDate);
                    for (int i = 0; i < 7; i++) {
                        String dateString = sdf.format(calendar.getTime());
                        timeMapSpend.put(dateString, 0.0);
                        timeMapIncome.put(dateString, 0.0);
                        calendar.add(Calendar.DATE, 1);
                    }
                    categoryBmobQuery_Income.findObjects(new FindListener<Income>() {
                        @Override
                        public void done(List<Income> list, BmobException e) {
                            if (e == null) {
                                for (Income income : list) {
                                    boolean flag = false;
                                    System.out.println(income.getTime().getDate());
                                    String time = income.getTime().getDate();
                                    double money = income.getMoney();
                                    if (timeMapIncome.containsKey(time)) {
                                        money += timeMapIncome.get(time);
                                    }
                                    for (Income income1 : list) {
                                        if (timeMapIncome.containsKey(time)) {
                                            flag = true;
                                        }
                                    }
                                    if (flag) {
                                        timeMapIncome.put(time, money);
                                    }
                                }
                                for (Spend spend : spendList) {
                                    boolean flag = false;
                                    String time = spend.getTime().getDate();
                                    double money = spend.getMoney();
                                    if (timeMapSpend.containsKey(time)) {
                                        money += timeMapSpend.get(time);
                                    }
                                    for (Spend spend1 : spendList) {
                                        if (timeMapIncome.containsKey(time)) {
                                            flag = true;
                                        }
                                    }
                                    if (flag) {
                                        timeMapSpend.put(time, money);
                                    }
                                }
                                // 对键进行排序
                                List<String> sortedKeys1 = timeMapIncome.keySet().stream().sorted().collect(Collectors.toList());
                                List<String> sortedKeys = timeMapSpend.keySet().stream().sorted().collect(Collectors.toList());
                                int j = 0;
                                // 循环取出排序后键所对应的值
                                for (String key : sortedKeys) {
                                    j++;
                                    double value = timeMapSpend.get(key);
                                    list1.add(new BarEntry(j, (float) value));
                                }
                                int p = 0;
                                for (String key : sortedKeys1) {
                                    p++;
                                    double value = timeMapIncome.get(key);
                                    list2.add(new BarEntry(p, (float) value));
                                }
                                BarDataSet barDataSet1 = new BarDataSet(list2, "收入");
                                BarDataSet barDataSet2 = new BarDataSet(list1, "支出");
                                barDataSet1.setColor(Color.rgb(73, 255, 204));
                                barDataSet2.setColor(Color.rgb(255, 116, 105));
                                barDataSet1.notifyDataSetChanged();
                                barDataSet2.notifyDataSetChanged();
                                BarData barData = new BarData(barDataSet1);
                                barData.addDataSet(barDataSet2);
                                bar.setTouchEnabled(false);
                                bar.setScaleYEnabled(false);
                                bar.setData(barData);
                                bar.getDescription().setEnabled(false);
                                bar.getXAxis().setPosition(XAxis.XAxisPosition.BOTTOM);
                                bar.getAxisRight().setEnabled(false);
                                float groupSpace = 0.2f;
                                float barSpace = 0f;
                                float barWidth = 0.4f;
                                barData.setBarWidth(barWidth);
                                barData.notifyDataChanged();
                                bar.groupBars(0.5f, groupSpace, barSpace);
                                bar.notifyDataSetChanged();
                                bar.invalidate();
                            }
                        }
                    });
                    dialog.dismiss();
                } else {
                    // 处理查询失败
                    Log.e("错误", "柱状图获取失败" + e.getMessage());
                }
            }
        });

我只做了支出的动态显示,收入的还没做,反正都是一样的道理 

bar是BarChart的id

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值