package cn.flightfeather.thirdapp.dataanalysis;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import com.github.mikephil.charting.animation.Easing;
import com.github.mikephil.charting.charts.BarChart;
import com.github.mikephil.charting.components.AxisBase;
import com.github.mikephil.charting.components.Description;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.BarData;
import com.github.mikephil.charting.data.BarDataSet;
import com.github.mikephil.charting.data.BarEntry;
import com.github.mikephil.charting.formatter.IAxisValueFormatter;
import com.github.mikephil.charting.formatter.PercentFormatter;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import cn.flightfeather.thirdapp.R;
import cn.flightfeather.thirdapp.adapter.AllRecyclerViewAdapter;
import cn.flightfeather.thirdapp.bean.vo.TaskVo;
import cn.flightfeather.thirdapp.util.CommonUtils;
import cn.flightfeather.thirdapp.view.ScrollTextView;
/**
* 2018.11.09
* @author riku
* 顶层任务的详细进度信息
*/
public class ProgressDetailFragment extends Fragment {
private static final String ARG_PARAM1 = "taskProgressVo";//参数标签
private TaskVo taskProgressVo;//需要activity传入一个具体的顶层任务对象,用于显示其详细进度信息
private Unbinder unbinder;
public ProgressDetailFragment() {
// Required empty public constructor
}
/**
* Use this factory method to create a new instance of
* this fragment using the provided parameters.
*
* @param taskProgressVo
* @return A new instance of fragment ProgressDetailFragment.
*/
// TODO: Rename and change types and number of parameters
public static ProgressDetailFragment newInstance(TaskVo taskProgressVo) {
ProgressDetailFragment fragment = new ProgressDetailFragment();
Bundle args = new Bundle();
args.putSerializable(ARG_PARAM1, taskProgressVo);
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
taskProgressVo = (TaskVo) getArguments().getSerializable(ARG_PARAM1);
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.dialog_anysis_sub_progress, container, false);
unbinder = ButterKnife.bind(this, view);
initUI();
initStandardBarchart();
return view;
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
}
@Override
public void onDetach() {
super.onDetach();
}
@Override
public void onDestroyView() {
super.onDestroyView();
unbinder.unbind();
}
//
private final static String NOTASK = "无任务";
@BindView(R.id.tv_top_task)
ScrollTextView tv_top_task;
@BindView(R.id.iv_top_task)
ImageView iv_top_task;
@BindView(R.id.tv_total_progress)
TextView tv_total_progress;
@BindView(R.id.tv_my_progress)
TextView tv_my_progress;
@BindView(R.id.rv_dialog_task_progress)
RecyclerView rv_dialog_task_progress;
private void initUI(){
final DecimalFormat decimalFormat = new DecimalFormat("#0.00");
tv_total_progress.setSelected(true);
tv_top_task.setText(taskProgressVo.getName());
//根据任务类型选择不同的图标
iv_top_task.setImageResource(CommonUtils.getIconByTaskType(taskProgressVo.getTypeno().toString()));
//数据
ArrayList daytaskprogresslist = new ArrayList<>();
daytaskprogresslist.addAll(taskProgressVo.getDaytaskList());//当前顶层任务的所有日任务
//设置列表显示对其方式
LinearLayoutManager layoutManager1 = new LinearLayoutManager(getActivity());
layoutManager1.setOrientation(LinearLayoutManager.VERTICAL);
rv_dialog_task_progress.setLayoutManager(layoutManager1);
rv_dialog_task_progress.addItemDecoration(new DividerItemDecoration(getActivity(), DividerItemDecoration.VERTICAL));
//**总进度列表
final AllRecyclerViewAdapter adapter1 = new AllRecyclerViewAdapter(daytaskprogresslist, R.layout.item_day_task_progress, getActivity()) {
@Override
public void bindView(AllRecyclerViewAdapter.MyViewHolder holder, TaskVo obj, boolean bool, int position) {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MM月dd日");
holder.setText(R.id.tv_item1, simpleDateFormat.format(obj.getStarttime()));
holder.setText(R.id.tv_item2, String.valueOf(obj.getCompletetask()) + "/" + String.valueOf(obj.getTotaltask()));
if (obj.getTotaltask() == 0)
holder.setText(R.id.tv_item3, NOTASK);
else
holder.setText(R.id.tv_item3, decimalFormat.format(((float)obj.getCompletetask() / (float)obj.getTotaltask() * 100)) + "%");
}
};
//**用户进度列表
final AllRecyclerViewAdapter adapter2 = new AllRecyclerViewAdapter(daytaskprogresslist, R.layout.item_day_task_progress, getActivity()) {
@Override
public void bindView(AllRecyclerViewAdapter.MyViewHolder holder, TaskVo obj, boolean bool, int position) {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MM月dd日");
holder.setText(R.id.tv_item1, simpleDateFormat.format(obj.getStarttime()));
holder.setText(R.id.tv_item2, String.valueOf(obj.getMycompletetask()) + "/" + String.valueOf(obj.getMytotaltask()));
if (obj.getMytotaltask() == 0)
holder.setText(R.id.tv_item3, NOTASK);
else
holder.setText(R.id.tv_item3, decimalFormat.format(((float)obj.getMycompletetask() / (float)obj.getMytotaltask() * 100)) + "%");
}
};
rv_dialog_task_progress.setAdapter(adapter1);
tv_total_progress.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
tv_total_progress.setSelected(true);
tv_my_progress.setSelected(false);
tv_total_progress.setTextColor(Color.WHITE);
tv_my_progress.setTextColor(Color.BLACK);
rv_dialog_task_progress.setAdapter(adapter1);
}
});
tv_my_progress.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
tv_my_progress.setSelected(true);
tv_total_progress.setSelected(false);
tv_my_progress.setTextColor(Color.WHITE);
tv_total_progress.setTextColor(Color.BLACK);
rv_dialog_task_progress.setAdapter(adapter2);
}
});
}
//
//
@BindView(R.id.barChart_progress)
BarChart mStandardChart;//每个顶层任务的具体直方图数据
/*
柱状图,顶层任务的完成百分比
*/
private void initStandardBarchart() {
List daytaskprogresslist = taskProgressVo.getDaytaskList();//当前顶层任务的所有日任务
//坐标轴
XAxis xAxis = mStandardChart.getXAxis();
xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
xAxis.setDrawGridLines(false);
xAxis.setGranularity(1f);//坐标间隔
// xAxis.setLabelCount(2, true);//进入时的标签个数
xAxis.setCenterAxisLabels(true);//标签处于图表中间
xAxis.setAxisMinimum(0f);
xAxis.setValueFormatter(new MyDateFormmater(daytaskprogresslist));
YAxis rightAxis = mStandardChart.getAxisRight();
rightAxis.setEnabled(false);
// rightAxis.setDrawGridLines(false);
// rightAxis.setDrawAxisLine(false);
// rightAxis.setDrawLabels(false);
YAxis leftAxis = mStandardChart.getAxisLeft();
leftAxis.setDrawGridLines(false);
leftAxis.setDrawAxisLine(false);
leftAxis.setDrawLabels(false);
leftAxis.setDrawTopYLabelEntry(true);
leftAxis.setZeroLineColor(Color.BLACK);
leftAxis.setDrawZeroLine(true);
leftAxis.setAxisMinimum(0f);
leftAxis.setAxisMaximum(100f);
leftAxis.setValueFormatter(new PercentFormatter());
// LimitLine limitLine = new LimitLine(100f);
// limitLine.setLineColor(Color.BLACK);
// leftAxis.addLimitLine(limitLine);
//Entry数据
List totalEntries = new ArrayList<>();;//每日总任务进度
List userEntries = new ArrayList<>();;//每日用户进度
//Test*********
// totalEntries.add(new BarEntry(0f, 0f));
// totalEntries.add(new BarEntry(1f, 0f));
// totalEntries.add(new BarEntry(2f, 40f));
// totalEntries.add(new BarEntry(3f, 70f));
// totalEntries.add(new BarEntry(4f, 80f));
// totalEntries.add(new BarEntry(5f, 50f));
// totalEntries.add(new BarEntry(6f, 30f));
// totalEntries.add(new BarEntry(7f, 10f));
//
// userEntries.add(new BarEntry(0f, 0f));
// userEntries.add(new BarEntry(1f, 60f));
// userEntries.add(new BarEntry(2f, 70));
// userEntries.add(new BarEntry(3f, 70));
// userEntries.add(new BarEntry(4f, 35));
// userEntries.add(new BarEntry(5f, 35));
// userEntries.add(new BarEntry(6f, 35));
// userEntries.add(new BarEntry(7f, 35));
//*************
for (int i = 0; i < daytaskprogresslist.size(); i++){//初始化数据
float y1 = daytaskprogresslist.get(i).getTotaltask();
if (y1 != 0)
y1 = ((float)daytaskprogresslist.get(i).getCompletetask() / (float) daytaskprogresslist.get(i).getTotaltask() * 100);
else
y1 = 100f;
totalEntries.add(new BarEntry((float)i, y1));
float y2 = daytaskprogresslist.get(i).getMytotaltask();
if (y2 != 0)
y2 = ((float)daytaskprogresslist.get(i).getMycompletetask() / (float)daytaskprogresslist.get(i).getMytotaltask() * 100);
else
y2 = 100f;
userEntries.add(new BarEntry((float)i, y2));
}
BarDataSet barDataSetTotal = new BarDataSet(totalEntries, "总进度");
BarDataSet barDataSetUser = new BarDataSet(userEntries, "我的进度");
barDataSetTotal.setAxisDependency(YAxis.AxisDependency.LEFT);
// barDataSetTotal.setAxisDependency(YAxis.AxisDependency.RIGHT);
barDataSetUser.setAxisDependency(YAxis.AxisDependency.LEFT);
// barDataSetUser.setAxisDependency(YAxis.AxisDependency.RIGHT);
barDataSetTotal.setColor(Color.argb(128, 255, 0, 0));
barDataSetUser.setColor(Color.argb(128, 0, 255, 0));
float groupSpace = 0.06f;
float barSpace = 0.02f; // x2 dataset
float barWidth = 0.45f; // x2 dataset
// (0.02 + 0.45) * 2 + 0.06 = 1.00 -> interval per "group"
BarData barData = new BarData(barDataSetTotal, barDataSetUser);
// BarData barData = new BarData(barDataSetTotal);
barData.setValueFormatter(new PercentFormatter());
barData.setValueTextSize(8f);
barData.setHighlightEnabled(false);//高亮
barData.setBarWidth(barWidth);
if (!totalEntries.isEmpty() || !userEntries.isEmpty()) {//数据全为空时,不显示
mStandardChart.setData(barData);
mStandardChart.setFitBars(true);//x轴坐标显示对应每个bar
mStandardChart.groupBars(0f, groupSpace, barSpace);
}
//General Chart Settings & Styling
mStandardChart.setNoDataText("任务还未开始执行,暂无数据");//Sets the title that should appear if the chart is empty.
// mStandardChart.setDrawBorders(false);//Enables / disables drawing the chart borders (lines surrounding the chart).
//图标描述
Description description = new Description();
description.setText("");
mStandardChart.setDescription(description);
// mStandardChart.setTouchEnabled(true);//触摸
// mStandardChart.setDragEnabled(true);//拖拽
mStandardChart.setScaleEnabled(false);//缩放
mStandardChart.animateY(1400, Easing.EasingOption.EaseOutSine);
mStandardChart.fitScreen();
//视图设置
mStandardChart.setVisibleXRangeMaximum(6f);
mStandardChart.setVisibleXRangeMinimum(6f);
// mStandardChart.invalidate();
}
//
class MyDateFormmater implements IAxisValueFormatter {
DecimalFormat mFormat;
List daytaskprogresslist;
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd日");
MyDateFormmater(List daytaskprogresslist) {
mFormat = new DecimalFormat("###,###,##0");
this.daytaskprogresslist = daytaskprogresslist;
}
@Override
public String getFormattedValue(float value, AxisBase axis) {
try {
return simpleDateFormat.format(daytaskprogresslist.get((int)value).getStarttime());
}catch (Exception e){
e.printStackTrace();
return "";
}
}
}
}