| | |
| | | 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.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.model.UserData; |
| | | |
| | | import java.util.ArrayList; |
| | | import java.util.HashMap; |
| | | import java.util.List; |
| | | import java.util.Random; |
| | | |
| | | import lecho.lib.hellocharts.formatter.LineChartValueFormatter; |
| | | import lecho.lib.hellocharts.formatter.SimpleLineChartValueFormatter; |
| | | import lecho.lib.hellocharts.gesture.ContainerScrollType; |
| | | import lecho.lib.hellocharts.gesture.ZoomType; |
| | | import lecho.lib.hellocharts.model.Axis; |
| | |
| | | private List<AxisValue> mAxisXValues = new ArrayList<>(); |
| | | |
| | | private RadioGroup rg_time; |
| | | private TextView tv_starttime,tv_endtime; |
| | | private TextView tv_starttime,tv_endtime,tv_value; |
| | | private ImageView iv_pre,iv_next; |
| | | private int type=0; |
| | | @Override |
| | |
| | | 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); |
| | | iv_pre = findView(R.id.iv_pre); |
| | | iv_next = findView(R.id.iv_next); |
| | | rg_time.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { |
| | |
| | | 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(),-7)); |
| | | getData(tv_starttime.getText().toString(),tv_endtime.getText().toString()); |
| | | break; |
| | | case R.id.rb_month: |
| | | type = 1; |
| | |
| | | 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; |
| | | } |
| | | } |
| | | }); |
| | | 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(),-7)); |
| | | getData(tv_starttime.getText().toString(),tv_endtime.getText().toString()); |
| | | } |
| | | |
| | | @Override |
| | |
| | | |
| | | @Override |
| | | protected void initData() { |
| | | reshowChart(); |
| | | reshowChart(dataList); |
| | | } |
| | | |
| | | @Override |
| | |
| | | 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)); |
| | | } |
| | | getData(tv_starttime.getText().toString(),tv_endtime.getText().toString()); |
| | | break; |
| | | case R.id.iv_next: |
| | | if(TimeUtil.getCurrentDate("yyyy-MM-dd").equals(TimeUtil.getAfterDate("yyyy-MM-dd",tv_endtime.getText().toString(),1))){ |
| | |
| | | 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)); |
| | | } |
| | | getData(tv_starttime.getText().toString(),tv_endtime.getText().toString()); |
| | | break; |
| | | } |
| | | |
| | |
| | | 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); |
| | |
| | | |
| | | //坐标轴 |
| | | axisX = new Axis(); //X轴 |
| | | axisX.setHasTiltedLabels(false); //X坐标轴字体是斜的显示还是直的,true是斜的显示 |
| | | axisX.setHasTiltedLabels(true); //X坐标轴字体是斜的显示还是直的,true是斜的显示 |
| | | axisX.setName("时间段(按周)"); //表格名称 |
| | | axisX.setTextSize(10);//设置字体大小 |
| | | axisX.setTextSize(8);//设置字体大小 |
| | | axisX.setTextColor(getResources().getColor(R.color.red_txt)); |
| | | // axisX.setMaxLabelChars(7); //最多几个X轴坐标,意思就是你的缩放让X轴上数据的个数7<=x<=mAxisXValues.length |
| | | axisX.setValues(mAxisXValues); //填充X轴的坐标名称 |
| | |
| | | //data.setAxisXTop(axisX); //x 轴在顶部 |
| | | |
| | | // Y轴是根据数据的大小自动设置Y轴上限(在下面我会给出固定Y轴数据个数的解决方案) |
| | | axisY = Axis.generateAxisFromRange(0,100,1); //Y轴 |
| | | axisY = Axis.generateAxisFromRange(0,100, (float) 0.01); //Y轴 |
| | | axisY.setAutoGenerated(false); |
| | | axisY.setHasLines(true); |
| | | axisY.setHasSeparationLine(true); |
| | |
| | | * 当时是为了解决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 = 0; |
| | | v.top = 100; |
| | | // // 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) { |
| | |
| | | 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++){ |
| | | 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.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 = 0; |
| | | v.top = 100; |
| | | //// // 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(); |
| | | } |
| | | ArrayList<UserData> temp = (ArrayList<UserData>) FastJsonTools.getArrayJson(res,UserData.class); |
| | | dataList.clear(); |
| | | |
| | | dataList.addAll(resetData(temp)); |
| | | |
| | | //重绘 |
| | | reshowChart(dataList); |
| | | } |
| | | |
| | | @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); |
| | | tv_value.setText(0+"uSv"); |
| | | } |
| | | }); |
| | | |
| | | } |
| | | |
| | | |
| | | 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); |
| | | } |
| | | } |
| | | //重构数据 |
| | | float totalValues = 0; |
| | | if(tempList!=null&&tempList.size()>0){ |
| | | for (UserData data : tempList){ |
| | | totalValues+= data.getDay_avg(); |
| | | for(UserData data1 : newList){ |
| | | 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; |
| | | } |
| | | } |