陈奇
2018-11-01 cdbaf79d124d46b69dc62b653fde55f9d40b5534
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
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 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;
import lecho.lib.hellocharts.model.AxisValue;
import lecho.lib.hellocharts.model.Line;
import lecho.lib.hellocharts.model.LineChartData;
import lecho.lib.hellocharts.model.PointValue;
import lecho.lib.hellocharts.model.ValueShape;
import lecho.lib.hellocharts.model.Viewport;
import lecho.lib.hellocharts.view.LineChartView;
 
/**
 * Created by haijiang on 2017/7/13.
 */
 
public class ChartActivity extends BaseActivity {
    private LineChartView chartView;
    Line line;
    LineChartData data;
    Axis axisX;
    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) {
 
    }
 
    @Override
    protected int getLayoutId() {
        return R.layout.ac_charts_layout;
    }
 
    @Override
    protected void initViews() {
        initTopBar();
        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(dataList);
        getAd();
    }
 
    @Override
    protected void processClick(View view) {
        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;
        }
 
    }
 
    @Override
    protected void onErrorPageClick() {
 
    }
 
    private void initLineChart() {
        line = new Line(mPointValues).setColor(Color.parseColor("#9EF5DF"));  //折线的颜色
        List<Line> lines = new ArrayList<>();
        line.setShape(ValueShape.CIRCLE);//折线图上每个数据点的形状  这里是圆形 (有三种 :ValueShape.SQUARE  ValueShape.CIRCLE  ValueShape.DIAMOND)
        line.setPointColor(Color.parseColor("#20DAE5"));
        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);
        data.setValueLabelBackgroundColor(Color.TRANSPARENT);//此处设置坐标点旁边的文字背景
        data.setValueLabelBackgroundEnabled(false);
        data.setValueLabelsTextColor(Color.GRAY);
 
        //坐标轴
        axisX = new Axis(); //X轴
        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.blue_txt));
        axisX.setHasSeparationLine(true);
        data.setAxisXBottom(axisX); //x 轴在底部
        //data.setAxisXTop(axisX);  //x 轴在顶部
 
        // Y轴是根据数据的大小自动设置Y轴上限(在下面我会给出固定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.blue_txt));
        axisY.setLineColor(getResources().getColor(R.color.blue_txt));
        data.setAxisYLeft(axisY);  //Y轴设置在左边
 
        //设置行为属性,支持缩放、滑动以及平移
        chartView.setInteractive(true);
        chartView.setZoomType(ZoomType.HORIZONTAL);
        chartView.setMaxZoom((float) 2);//最大方法比例
        chartView.setContainerScrollEnabled(true, ContainerScrollType.HORIZONTAL);
        chartView.setLineChartData(data);
        chartView.setVisibility(View.VISIBLE);
        /**注:下面的7,10只是代表一个数字去类比而已
         * 当时是为了解决X轴固定数据个数。见(http://forum.xda-developers.com/tools/programming/library-hellocharts-charting-library-t2904456/page2);
         */
//        chartView.setCurrentViewport(initViewPort(0,7));
        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) {
        Viewport port = new Viewport();
        port.left = left;
        port.right = right;
        return port;
    }
 
    private void reshowChart(final ArrayList<UserData> dataList) {
 
        chartView.post(new Runnable() {
            @Override
            public void run() {
                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);
                data.setAxisXBottom(axisX);
                line.setValues(mPointValues);
                List<Line> lines = new ArrayList<Line>();
                lines.add(line);
                data.setLines(lines);
                chartView.setLineChartData(data);
                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);
            }
        });
    }
 
    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) {
 
            }
        });
    }
}