package cn.flightfeather.thirdapp.dataanalysis; import android.app.Fragment; import android.content.Context; import android.graphics.Color; import android.icu.text.DecimalFormat; import android.os.Build; import android.os.Bundle; import android.support.annotation.Nullable; import android.support.annotation.RequiresApi; 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 com.github.mikephil.charting.animation.Easing; import com.github.mikephil.charting.charts.PieChart; import com.github.mikephil.charting.components.Legend; import com.github.mikephil.charting.data.PieData; import com.github.mikephil.charting.data.PieDataSet; import com.github.mikephil.charting.data.PieEntry; import com.github.mikephil.charting.formatter.PercentFormatter; import com.github.mikephil.charting.utils.ColorTemplate; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.HashMap; import java.util.List; import java.util.Objects; 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.ProblemlistVo; import cn.flightfeather.thirdapp.bean.vo.TaskVo; /** * 2018.11.14 * @author riku * 顶层任务的问题类型统计 */ public class ProblemDetailFragment extends Fragment { private Context context; private static final String ARG_PARAM1 = "taskProgressVo";//参数标签 private TaskVo taskProgressVo;//需要activity传入一个具体的顶层任务对象,用于显示其详细进度信息 private Unbinder unbinder; public ProblemDetailFragment() { // Required empty public constructor } public static ProblemDetailFragment newInstance(TaskVo taskProgressVo) { ProblemDetailFragment fragment = new ProblemDetailFragment(); 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.fragment_analysis_problem_1, container, false); unbinder = ButterKnife.bind(this, view); return view; } @RequiresApi(api = Build.VERSION_CODES.N) @Override public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); context = getActivity(); initPieChart(view); initRecyclerView(view); } @Override public void onDestroyView() { super.onDestroyView(); unbinder.unbind(); } // private List problemlists;//问题列表 private ArrayList entries = new ArrayList<>();//饼图数据 private AllRecyclerViewAdapter entryAdapter = null;//list列表适配器(问题统计数据) private final static int MAX_CHART_COUNT = 20; //饼图最多显示区块数 private final static String REMAIN_PROBLEM = "其余"; @BindView(R.id.rv_problem_type) RecyclerView rv_problem_type; @BindView(R.id.PC_problem) PieChart mChart; /** * 问题类型分布的列表 * @param view 问题类型分布fragment */ @RequiresApi(api = Build.VERSION_CODES.N) private void initRecyclerView(View view) { final DecimalFormat decimalFormat = new DecimalFormat("#0.00"); rv_problem_type = (RecyclerView) view.findViewById(R.id.rv_problem_type); entryAdapter = new AllRecyclerViewAdapter(entries, R.layout.item_day_task_progress, context) { @Override public void bindView(AllRecyclerViewAdapter.MyViewHolder holder, PieEntry obj, boolean bool, int position) { float total = 0; for (PieEntry entry : entries) { total = total + entry.getValue(); } holder.setText(R.id.tv_item1, entries.get(position).getLabel()) .setText(R.id.tv_item2, entries.get(position).getValue() + "") .setText(R.id.tv_item3, decimalFormat.format(entries.get(position).getValue()/ total * 100) + "%"); } }; LinearLayoutManager layoutManager = new LinearLayoutManager(context); layoutManager.setOrientation(LinearLayoutManager.VERTICAL); rv_problem_type.setLayoutManager(layoutManager); rv_problem_type.addItemDecoration(new DividerItemDecoration(context, DividerItemDecoration.VERTICAL)); rv_problem_type.setAdapter(entryAdapter); } private void initPieChart(View view) { //饼图样式设置*************************************************************************************************** mChart.setUsePercentValues(true); mChart.getDescription().setEnabled(false); mChart.setExtraOffsets(5, 10, 5, 5); // mChart.setExtraOffsets(20.f, 0.f, 20.f, 0.f); mChart.setDragDecelerationFrictionCoef(0.95f); // mChart.setCenterTextTypeface(mTfLight); // mChart.setCenterText(generateCenterSpannableText()); mChart.setDrawHoleEnabled(true); mChart.setHoleColor(Color.WHITE); mChart.setTransparentCircleColor(Color.WHITE); mChart.setTransparentCircleAlpha(110); mChart.setHoleRadius(39f); mChart.setTransparentCircleRadius(41f); mChart.setDrawCenterText(true); mChart.setRotationAngle(0); // enable rotation of the chart by touch mChart.setRotationEnabled(true); mChart.setHighlightPerTapEnabled(true); Legend l = mChart.getLegend(); l.setVerticalAlignment(Legend.LegendVerticalAlignment.TOP); l.setHorizontalAlignment(Legend.LegendHorizontalAlignment.LEFT); l.setOrientation(Legend.LegendOrientation.VERTICAL); l.setDrawInside(false); l.setXEntrySpace(7f); l.setYEntrySpace(0f); l.setYOffset(0f); // entry label styling mChart.setEntryLabelColor(Color.WHITE); // mChart.setEntryLabelTypeface(mTfRegular); mChart.setEntryLabelTextSize(12f); mChart.setDrawEntryLabels(false); //test // setData(new ArrayList()); // } private void setData(List problemlists) { //数据初始化*************************************************************************************************** entries.clear(); PieDataSet dataSet = new PieDataSet(entries, "问题类型"); //test***** // int count = 5; // int mult = 100; // String[] mParties = {"路面积尘", "地面局部未硬化", "站厂出入口无外围护", "无喷淋装置", REMAIN_PROBLEM}; // for (int i = 0; i < count; i++) { // entries.add(new PieEntry((int) (Math.random() * mult) + mult / 5, mParties[i])); // } //********* //初始化数据 HashMap problemTypeMap = new HashMap<>(); while (!problemlists.isEmpty()) { String problemType = problemlists.get(0).getTypename(); if (problemType == null) { problemlists.remove(0); continue; } int problemCount = 0; for (int i = 0; i < problemlists.size(); i++) { if (Objects.equals(problemType, problemlists.get(i).getTypename())) { problemCount++; problemlists.remove(i); i--; } } problemTypeMap.put(problemType, problemCount); } //按HashMap的value降序排序 List> list = new ArrayList<>(problemTypeMap.entrySet()); Collections.sort(list, new Comparator>() { @Override public int compare(HashMap.Entry o1, HashMap.Entry o2) { return o2.getValue().compareTo(o1.getValue()); } }); // problemTypeMap.clear(); // problemTypeMap.entrySet().addAll(list); //初始化Entry,限定最多显示5个扇区,超出的合并为第5个扇区 String remainProblemType = ""; int remainProblemCount = 0; int chartcount = 0; for (HashMap.Entry entry : list) { if (chartcount < MAX_CHART_COUNT - 1) { entries.add(new PieEntry((float)entry.getValue(), entry.getKey())); } else { remainProblemCount = remainProblemCount + entry.getValue(); remainProblemType = entry.getKey(); } chartcount++; } if (chartcount == MAX_CHART_COUNT) { entries.add(new PieEntry((float) remainProblemCount, remainProblemType)); } else if (chartcount > MAX_CHART_COUNT) { entries.add(new PieEntry((float) remainProblemCount, REMAIN_PROBLEM)); } // add a lot of colors ArrayList colors = new ArrayList<>(); for (int c : ColorTemplate.JOYFUL_COLORS) colors.add(c); for (int c : ColorTemplate.COLORFUL_COLORS) colors.add(c); for (int c : ColorTemplate.VORDIPLOM_COLORS) colors.add(c); for (int c : ColorTemplate.LIBERTY_COLORS) colors.add(c); for (int c : ColorTemplate.PASTEL_COLORS) colors.add(c); colors.add(ColorTemplate.getHoloBlue()); dataSet.setColors(colors); //value line dataSet.setValueLinePart1OffsetPercentage(80.f); dataSet.setValueLinePart1Length(0.2f); dataSet.setValueLinePart2Length(0.4f); //dataSet.setXValuePosition(PieDataSet.ValuePosition.OUTSIDE_SLICE); dataSet.setYValuePosition(PieDataSet.ValuePosition.OUTSIDE_SLICE); // PieData data = new PieData(dataSet); data.setValueFormatter(new PercentFormatter()); data.setValueTextSize(11f); data.setValueTextColor(Color.BLACK); // data.setValueTypeface(mTfLight); if (!entries.isEmpty()) {//数据全为空时,不显示 mChart.setData(data); } else { mChart.setData(null); } mChart.setNoDataText("暂无数据"); // undo all highlights mChart.highlightValues(null); mChart.animateY(1400, Easing.EasingOption.EaseInOutQuad); } // /** * 按照搜索条件查询问题分布结果 */ // private void search() { // final SweetAlertDialog dialog = DialogUtil.createSweetLoadingDialog(getActivity(), "Loading..."); // AreaVo areaVo = new AreaVo(); // areaVo.setProvincecode(((Province)SP_province.getSelectedItem()).getProvincecode()); // areaVo.setProvincename(((Province)SP_province.getSelectedItem()).getProvincename()); // areaVo.setCitycode(((City)SP_city.getSelectedItem()).getCitycode()); // areaVo.setCityname(((City)SP_city.getSelectedItem()).getCityname()); // areaVo.setDistrictcode(((District)SP_district.getSelectedItem()).getDistrictcode()); // areaVo.setDistrictname(((District)SP_district.getSelectedItem()).getDistrictname()); // areaVo.setTowncode(((Town) SP_town.getSelectedItem()).getTowncode()); // areaVo.setTownname(((Town) SP_town.getSelectedItem()).getTownname()); // areaVo = gettime(areaVo); // int position = SP_scense_type.getSelectedItemPosition(); // areaVo.setScensetypeid(scenseTypeData.get(position).getValue()); // Call> getProblemByArea = problemListService.getByArea(areaVo); // getProblemByArea.enqueue(new Callback>() { // @Override // public void onResponse(Call> call, Response> response) { // if (response.body() != null) { // problemlists = response.body(); // } // setData(problemlists); // entryAdapter.notifyDataSetChanged();//刷新列表 // DialogUtil.closeDialog(dialog); // } // // @Override // public void onFailure(Call> call, Throwable t) { // setData(problemlists); // DialogUtil.closeDialog(dialog); // } // }); // // setChartTitle();//刷新标题 // setDateDisplay();//刷新时间 // } }