From 50aacfed3ceb60997c2c9a4cc0f9e6fe6b5ed718 Mon Sep 17 00:00:00 2001
From: 陈奇 <1650699704@qq.com>
Date: Mon, 26 Nov 2018 15:17:11 +0800
Subject: [PATCH] [*]修改主页指针跳转动画

---
 app/src/main/java/com/moral/yunfushao/activity/ChartActivity.java |  339 ++++++++++++++++++++++++++++++++++++++++++++++++++++----
 1 files changed, 314 insertions(+), 25 deletions(-)

diff --git a/app/src/main/java/com/moral/yunfushao/activity/ChartActivity.java b/app/src/main/java/com/moral/yunfushao/activity/ChartActivity.java
index 9bfeb76..6a5e22a 100644
--- a/app/src/main/java/com/moral/yunfushao/activity/ChartActivity.java
+++ b/app/src/main/java/com/moral/yunfushao/activity/ChartActivity.java
@@ -1,16 +1,43 @@
 package com.moral.yunfushao.activity;
 
+import android.app.AlertDialog;
 import android.graphics.Color;
+import android.graphics.drawable.ColorDrawable;
 import android.os.Bundle;
+import android.support.annotation.IdRes;
+import android.text.TextUtils;
+import android.view.LayoutInflater;
 import android.view.View;
+import android.view.Window;
+import android.widget.Button;
+import android.widget.ImageView;
+import android.widget.RadioGroup;
+import android.widget.TextView;
 
+import com.lzy.okgo.cache.CacheMode;
+import com.moral.andbrickslib.utils.FastJsonTools;
+import com.moral.andbrickslib.utils.NumberUtils;
+import com.moral.andbrickslib.utils.TimeUtil;
+import com.moral.yunfushao.MainApp;
 import com.moral.yunfushao.R;
 import com.moral.yunfushao.base.BaseActivity;
+import com.moral.yunfushao.common.API;
+import com.moral.yunfushao.httputils.HttpCallBack;
+import com.moral.yunfushao.httputils.HttpUtils;
+import com.moral.yunfushao.imageload.ImageLoader;
+import com.moral.yunfushao.model.UserData;
+
+import org.json.JSONException;
+import org.json.JSONObject;
 
 import java.util.ArrayList;
+import java.util.HashMap;
 import java.util.List;
-import java.util.Random;
 
+import lecho.lib.hellocharts.formatter.AxisValueFormatter;
+import lecho.lib.hellocharts.formatter.LineChartValueFormatter;
+import lecho.lib.hellocharts.formatter.SimpleAxisValueFormatter;
+import lecho.lib.hellocharts.formatter.SimpleLineChartValueFormatter;
 import lecho.lib.hellocharts.gesture.ContainerScrollType;
 import lecho.lib.hellocharts.gesture.ZoomType;
 import lecho.lib.hellocharts.model.Axis;
@@ -34,6 +61,12 @@
     Axis axisY;
     private List<PointValue> mPointValues = new ArrayList<>();
     private List<AxisValue> mAxisXValues = new ArrayList<>();
+
+    private RadioGroup rg_time;
+    private TextView tv_starttime, tv_endtime, tv_value, tv_status;
+    private ImageView iv_pre, iv_next;
+    private int type = 2;
+
     @Override
     protected void getBundleExtras(Bundle extras) {
 
@@ -47,26 +80,108 @@
     @Override
     protected void initViews() {
         initTopBar();
-        tv_title.setText("������������");
+        tv_title.setText("������������");
         chartView = findView(R.id.chart);
         initLineChart();
+
+        rg_time = findView(R.id.rg_time);
+        tv_starttime = findView(R.id.tv_starttime);
+        tv_endtime = findView(R.id.tv_endtime);
+        tv_value = findView(R.id.tv_value);
+        tv_status = findView(R.id.tv_status);
+        iv_pre = findView(R.id.iv_pre);
+        iv_next = findView(R.id.iv_next);
+        rg_time.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
+            @Override
+            public void onCheckedChanged(RadioGroup radioGroup, @IdRes int i) {
+                switch (i) {
+                    case R.id.rb_week:
+                        type = 0;
+                        axisX.setName("���������������������");  //������������
+                        data.setAxisXBottom(axisX); //x ������������
+                        chartView.setLineChartData(data);
+                        tv_endtime.setText(TimeUtil.getAfterDate("yyyy-MM-dd", TimeUtil.getCurrentDate("yyyy-MM-dd"), -1));
+                        tv_starttime.setText(TimeUtil.getAfterDate("yyyy-MM-dd", tv_endtime.getText().toString(), -6));
+                        getData(tv_starttime.getText().toString(), tv_endtime.getText().toString());
+                        break;
+                    case R.id.rb_month:
+                        type = 1;
+                        axisX.setName("���������������������");  //������������
+                        data.setAxisXBottom(axisX); //x ������������
+                        chartView.setLineChartData(data);
+                        tv_endtime.setText(TimeUtil.getAfterDate("yyyy-MM-dd", TimeUtil.getCurrentDate("yyyy-MM-dd"), -1));
+                        tv_starttime.setText(TimeUtil.getAfterDate("yyyy-MM-dd", tv_endtime.getText().toString(), -28));
+                        getData(tv_starttime.getText().toString(), tv_endtime.getText().toString());
+                        break;
+                    case R.id.rb_day:
+                        type = 2;
+                        axisX.setName("���������������������");  //������������
+                        data.setAxisXBottom(axisX); //x ������������
+                        chartView.setLineChartData(data);
+                        tv_endtime.setText(TimeUtil.getAfterDate("yyyy-MM-dd", TimeUtil.getCurrentDate("yyyy-MM-dd"), 0));
+                        tv_starttime.setText(tv_endtime.getText().toString());
+                        getData(tv_starttime.getText().toString(), tv_endtime.getText().toString());
+                        break;
+                }
+            }
+        });
+        tv_endtime.setText(TimeUtil.getAfterDate("yyyy-MM-dd", TimeUtil.getCurrentDate("yyyy-MM-dd"), 0));
+        tv_starttime.setText(tv_endtime.getText().toString());
+        getData(tv_starttime.getText().toString(), tv_endtime.getText().toString());
     }
 
     @Override
     protected void initListener() {
-
+        iv_pre.setOnClickListener(this);
+        iv_next.setOnClickListener(this);
     }
 
     @Override
     protected void initData() {
-        reshowChart();
+        reshowChart(dataList);
+        getAd();
     }
 
     @Override
     protected void processClick(View view) {
-        switch (view.getId()){
+        switch (view.getId()) {
             case R.id.tv_left:
                 finish();
+                break;
+            case R.id.iv_pre:
+                if (type == 0) {
+                    tv_endtime.setText(TimeUtil.getAfterDate("yyyy-MM-dd", tv_starttime.getText().toString(), -1));
+                    tv_starttime.setText(TimeUtil.getAfterDate("yyyy-MM-dd", tv_endtime.getText().toString(), -6));
+                } else if (type == 1) {
+                    tv_endtime.setText(TimeUtil.getAfterDate("yyyy-MM-dd", tv_starttime.getText().toString(), -1));
+                    tv_starttime.setText(TimeUtil.getAfterDate("yyyy-MM-dd", tv_endtime.getText().toString(), -28));
+                } else if (type == 2) {
+                    tv_endtime.setText(TimeUtil.getAfterDate("yyyy-MM-dd", tv_starttime.getText().toString(), -1));
+                    tv_starttime.setText(tv_endtime.getText().toString());
+                }
+                getData(tv_starttime.getText().toString(), tv_endtime.getText().toString());
+                break;
+            case R.id.iv_next:
+                if (type == 0) {
+                    if (TimeUtil.getCurrentDate("yyyy-MM-dd").equals(TimeUtil.getAfterDate("yyyy-MM-dd", tv_endtime.getText().toString(), 1))) {
+                        return;
+                    }
+                    tv_starttime.setText(TimeUtil.getAfterDate("yyyy-MM-dd", tv_endtime.getText().toString(), 1));
+                    tv_endtime.setText(TimeUtil.getAfterDate("yyyy-MM-dd", tv_starttime.getText().toString(), 6));
+                } else if (type == 1) {
+                    if (TimeUtil.getCurrentDate("yyyy-MM-dd").equals(TimeUtil.getAfterDate("yyyy-MM-dd", tv_endtime.getText().toString(), 1))) {
+                        return;
+                    }
+                    tv_starttime.setText(TimeUtil.getAfterDate("yyyy-MM-dd", tv_endtime.getText().toString(), 1));
+                    tv_endtime.setText(TimeUtil.getAfterDate("yyyy-MM-dd", tv_starttime.getText().toString(), 28));
+                } else if (type == 2) {
+                    if (TimeUtil.getCurrentDate("yyyy-MM-dd").equals(TimeUtil.getAfterDate("yyyy-MM-dd", tv_endtime.getText().toString(), 0))) {
+                        return;
+                    }
+                    tv_starttime.setText(TimeUtil.getAfterDate("yyyy-MM-dd", tv_endtime.getText().toString(), 1));
+                    tv_endtime.setText(tv_starttime.getText().toString());
+                }
+                getData(tv_starttime.getText().toString(), tv_endtime.getText().toString());
                 break;
         }
 
@@ -82,12 +197,14 @@
         List<Line> lines = new ArrayList<>();
         line.setShape(ValueShape.CIRCLE);//������������������������������������  ��������������� ������������ ���ValueShape.SQUARE  ValueShape.CIRCLE  ValueShape.DIAMOND���
         line.setPointColor(Color.parseColor("#20DAE5"));
-        line.setCubic(true);//���������������������������������������������
+        line.setCubic(false);//���������������������������������������������
         line.setFilled(false);//���������������������������
         line.setHasLabels(true);//���������������������������������������
 //      line.setHasLabelsOnlyForSelected(true);//������������������������������������������������line.setHasLabels(true);������������
         line.setHasLines(true);//������������������������������false ������������������������������
         line.setHasPoints(true);//������������������ ���������false ������������������������������������������������������������������������
+        LineChartValueFormatter chartValueFormatter = new SimpleLineChartValueFormatter(3);
+        line.setFormatter(chartValueFormatter);//���������������
         lines.add(line);
         data = new LineChartData();
         data.setLines(lines);
@@ -97,27 +214,29 @@
 
         //���������
         axisX = new Axis(); //X���
-        axisX.setHasTiltedLabels(false);  //X���������������������������������������������true���������������
-        axisX.setName("���������");  //������������
-        axisX.setTextSize(10);//������������������
-        axisX.setTextColor(getResources().getColor(R.color.red_txt));
+        axisX.setHasTiltedLabels(true);  //X���������������������������������������������true���������������
+        axisX.setName("���������������������");  //������������
+        axisX.setTextSize(8);//������������������
+        axisX.setTextColor(getResources().getColor(R.color.blue_txt));
 //        axisX.setMaxLabelChars(7); //������������X���������������������������������������X���������������������7<=x<=mAxisXValues.length
         axisX.setValues(mAxisXValues);  //������X������������������
         axisX.setHasLines(true); //x ������������
-        axisX.setLineColor(getResources().getColor(R.color.red_txt));
+        axisX.setLineColor(getResources().getColor(R.color.blue_txt));
         axisX.setHasSeparationLine(true);
         data.setAxisXBottom(axisX); //x ������������
         //data.setAxisXTop(axisX);  //x ������������
 
         // Y���������������������������������������Y���������(���������������������������Y������������������������������)
-        axisY = Axis.generateAxisFromRange(0,100,1);  //Y���
+        axisY = Axis.generateAxisFromRange((float) 0.0, (float) 4.0, (float) 0.25);  //Y���
         axisY.setAutoGenerated(false);
+        AxisValueFormatter yvalue = new SimpleAxisValueFormatter(1);
+        axisY.setFormatter(yvalue);
         axisY.setHasLines(true);
         axisY.setHasSeparationLine(true);
         axisY.setName("������");//y���������
         axisY.setTextSize(10);//������������������
-        axisY.setTextColor(getResources().getColor(R.color.red_txt));
-        axisY.setLineColor(getResources().getColor(R.color.red_txt));
+        axisY.setTextColor(getResources().getColor(R.color.blue_txt));
+        axisY.setLineColor(getResources().getColor(R.color.blue_txt));
         data.setAxisYLeft(axisY);  //Y������������������
 
         //������������������������������������������������������
@@ -131,7 +250,15 @@
          * ���������������������X������������������������������http://forum.xda-developers.com/tools/programming/library-hellocharts-charting-library-t2904456/page2���;
          */
 //        chartView.setCurrentViewport(initViewPort(0,7));
-        chartView.startDataAnimation();
+        final Viewport v = new Viewport(chartView.getMaximumViewport());
+        v.bottom = (float) 0.0;
+        v.top = (float) 4.0;
+//                // You have to set max and current viewports separately.
+        chartView.setMaximumViewport(v);
+//                // I changing current viewport with animation in this case.
+////                                        lineChartCO2.setCurrentViewportWithAnimation(v);
+        chartView.setCurrentViewport(v);
+//        chartView.startDataAnimation();
     }
 
     private Viewport initViewPort(float left, float right) {
@@ -141,15 +268,23 @@
         return port;
     }
 
-    Random random = new Random();
-    private void reshowChart() {
+    private void reshowChart(final ArrayList<UserData> dataList) {
 
         chartView.post(new Runnable() {
             @Override
             public void run() {
-                for (int i=0;i<10;i++){
-                    mPointValues.add(new PointValue(i,random.nextInt(100)%(100-0+1) + 0));
-                    mAxisXValues.add(new AxisValue(i).setLabel(i + ""));
+                mPointValues.clear();
+                mAxisXValues.clear();
+                for (int i = 0; i < dataList.size(); i++) {
+                    if (type == 2) {
+                        mPointValues.add(new PointValue(i, dataList.get(i).getHour_avg()));
+                        mAxisXValues.add(new AxisValue(i).setLabel((dataList.get(i).getTime_level() - 1) * 3 + "���"));
+                    } else {
+                        mPointValues.add(new PointValue(i, dataList.get(i).getDay_avg()));
+                        mAxisXValues.add(new AxisValue(i).setLabel(dataList.get(i).getDate().substring(5, 10)));
+                    }
+
+//                    mAxisXValues.add(new AxisValue(i).setLabel(i+""));
                 }
 
                 axisX.setValues(mAxisXValues);
@@ -160,14 +295,168 @@
                 data.setLines(lines);
                 chartView.setLineChartData(data);
                 final Viewport v = new Viewport(chartView.getMaximumViewport());
-                v.bottom = -5;
-                v.top = 100 + 5;
-                // You have to set max and current viewports separately.
+                v.bottom = (float) 0.0;
+                v.top = (float) 4.0;
+////                // You have to set max and current viewports separately.
                 chartView.setMaximumViewport(v);
-                // I changing current viewport with animation in this case.
-//                                        lineChartCO2.setCurrentViewportWithAnimation(v);
+//                // I changing current viewport with animation in this case.
+////                                        lineChartCO2.setCurrentViewportWithAnimation(v);
                 chartView.setCurrentViewport(v);
             }
         });
     }
+
+    private ArrayList<UserData> dataList = new ArrayList<>();
+
+    private void getData(String startTime, String endTime) {
+        HashMap<String, String> params = new HashMap<>();
+        params.put("user_id", MainApp.userId);
+        params.put("start_time", startTime);
+        params.put("end_time", endTime);
+        HttpUtils.doPost(API.GETUSERDATALIST, params, CacheMode.DEFAULT, true, new HttpCallBack() {
+            @Override
+            public void onSuccess(String res, String msg) {
+                if (progressDialog.isShowing()) {
+                    progressDialog.dismiss();
+                }
+                try {
+                    JSONObject jsonObject = new JSONObject(res);
+                    String data = jsonObject.optString("data");
+                    ArrayList<UserData> temp = (ArrayList<UserData>) FastJsonTools.getArrayJson(data, UserData.class);
+                    dataList.clear();
+                    dataList.addAll(resetData(temp));
+                    //������
+                    reshowChart(dataList);
+                    tv_value.setText(NumberUtils.getBigDecimalValue(jsonObject.optDouble("avg"), 3) + "uSv");
+                    tv_status.setText(jsonObject.optString("status"));
+                } catch (JSONException e) {
+                    e.printStackTrace();
+                }
+            }
+
+            @Override
+            public void showLoadingDialog() {
+                progressDialog.show();
+            }
+
+            @Override
+            public void onFail(int errno, String s) {
+                if (progressDialog.isShowing()) {
+                    progressDialog.dismiss();
+                }
+                dataList.clear();
+
+                dataList.addAll(resetData(new ArrayList<UserData>()));
+
+                //������
+                reshowChart(dataList);
+            }
+        });
+
+    }
+
+
+    private ArrayList<UserData> resetData(ArrayList<UserData> tempList) {
+        ArrayList<UserData> newList = new ArrayList<>();
+        if (type == 0) {//���
+            for (int i = 0; i < 7; i++) {
+                UserData data = new UserData();
+                data.setDate(TimeUtil.getAfterDate("yyyy-MM-dd", tv_starttime.getText().toString(), i));
+                data.setDay_avg(0);
+                newList.add(data);
+            }
+        } else if (type == 1) {//���
+            for (int i = 0; i < 7; i++) {
+                UserData data = new UserData();
+                data.setDate(TimeUtil.getAfterDate("yyyy-MM-dd", tv_starttime.getText().toString(), i * 4));
+                data.setDay_avg(0);
+                newList.add(data);
+            }
+        } else if (type == 2) {//���
+            for (int i = 0; i < 8; i++) {
+                UserData data = new UserData();
+                data.setTime_level(i + 1);
+                data.setHour_avg(0);
+                newList.add(data);
+            }
+        }
+        //������������
+        float totalValues = 0;
+        if (tempList != null && tempList.size() > 0) {
+            for (UserData data : tempList) {
+                totalValues += data.getDay_avg();
+                for (UserData data1 : newList) {
+                    if (type == 2) {
+                        if (data.getTime_level() == data1.getTime_level()) {
+                            data1.setHour_avg(data.getHour_avg());
+                        }
+                    } else {
+                        if (data.getDate().equals(data1.getDate())) {
+                            data1.setDay_avg(data.getDay_avg());
+                        }
+                    }
+
+                }
+            }
+//            tv_value.setText(totalValues/tempList.size()+"uSv");
+        } else {
+//            tv_value.setText(0+"uSv");
+        }
+        return newList;
+    }
+
+    private AlertDialog adDialog;
+    private View adView;
+    private TextView tv_content, tv_name;
+    private ImageView iv_logo;
+    private Button bt_cancel, bt_ok;
+
+    private void showAd(String content, String logo, String name) {
+        adView = LayoutInflater.from(this).inflate(R.layout.dialog_ad_layout, null);
+        tv_content = (TextView) adView.findViewById(R.id.tv_content);
+        tv_name = (TextView) adView.findViewById(R.id.tv_name);
+        bt_cancel = (Button) adView.findViewById(R.id.bt_cancel);
+        bt_ok = (Button) adView.findViewById(R.id.bt_ok);
+        iv_logo = (ImageView) adView.findViewById(R.id.iv_logo);
+        tv_content.setText(content);
+        tv_name.setText(name);
+        ImageLoader.setCircleImageView(this, logo, iv_logo, R.mipmap.circle);
+        bt_cancel.setOnClickListener(new View.OnClickListener() {
+            @Override
+            public void onClick(View view) {
+                adDialog.dismiss();
+            }
+        });
+        adDialog = new AlertDialog.Builder(this).setView(adView).create();
+        Window window = adDialog.getWindow();
+        window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
+        adDialog.show();
+    }
+
+
+    private void getAd() {
+        HttpUtils.doPost(API.GETAD, null, CacheMode.DEFAULT, false, new HttpCallBack() {
+            @Override
+            public void onSuccess(String res, String msg) {
+                if (TextUtils.isEmpty(res)) {
+                    return;
+                }try {
+                    JSONObject json = new JSONObject(res);
+                    showAd(json.optString("content"), json.optString("logo"), json.optString("name"));
+                } catch (JSONException e) {
+                    e.printStackTrace();
+                }
+            }
+
+            @Override
+            public void showLoadingDialog() {
+
+            }
+
+            @Override
+            public void onFail(int errno, String s) {
+
+            }
+        });
+    }
 }

--
Gitblit v1.8.0