package cn.flightfeather.thirdappmodule.module.dataanalysis
|
|
import android.app.Fragment
|
import android.graphics.Color
|
import android.icu.text.DecimalFormat
|
import android.os.Build
|
import android.os.Bundle
|
import android.support.annotation.RequiresApi
|
import android.support.v7.widget.DividerItemDecoration
|
import android.support.v7.widget.LinearLayoutManager
|
import android.view.LayoutInflater
|
import android.view.View
|
import android.view.ViewGroup
|
import butterknife.ButterKnife
|
import butterknife.Unbinder
|
import cn.flightfeather.thirdappmodule.R
|
import cn.flightfeather.thirdappmodule.adapter.AllRecyclerViewAdapter
|
import cn.flightfeather.thirdappmodule.bean.vo.ProblemlistVo
|
import cn.flightfeather.thirdappmodule.bean.vo.TaskVo
|
import com.github.mikephil.charting.animation.Easing
|
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 kotlinx.android.synthetic.main.fragment_analysis_problem_1.*
|
import java.util.*
|
|
/**
|
* 2018.11.14
|
* @author riku
|
* 顶层任务的问题类型统计
|
*/
|
class ProblemDetailFragment : Fragment() {
|
private var taskProgressVo //需要activity传入一个具体的顶层任务对象,用于显示其详细进度信息
|
: TaskVo? = null
|
private var unbinder: Unbinder? = null
|
override fun onCreate(savedInstanceState: Bundle) {
|
super.onCreate(savedInstanceState)
|
if (arguments != null) {
|
taskProgressVo = arguments.getSerializable(ARG_PARAM1) as TaskVo
|
}
|
}
|
|
override fun onCreateView(
|
inflater: LayoutInflater, container: ViewGroup,
|
savedInstanceState: Bundle
|
): View {
|
val 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 fun onViewCreated(view: View, savedInstanceState: Bundle?) {
|
super.onViewCreated(view, savedInstanceState)
|
initPieChart(view)
|
initRecyclerView(view)
|
}
|
|
override fun onDestroyView() {
|
super.onDestroyView()
|
unbinder!!.unbind()
|
}
|
|
//<editor-fold desc="初始化饼图以及对应的列表">
|
private val problemlists //问题列表
|
: List<ProblemlistVo>? = null
|
private val entries = ArrayList<PieEntry>() //饼图数据
|
private var entryAdapter: AllRecyclerViewAdapter<PieEntry>? = null //list列表适配器(问题统计数据)
|
|
/**
|
* 问题类型分布的列表
|
* @param view 问题类型分布fragment
|
*/
|
@RequiresApi(api = Build.VERSION_CODES.N)
|
private fun initRecyclerView(view: View) {
|
val decimalFormat = DecimalFormat("#0.00")
|
entryAdapter = object : AllRecyclerViewAdapter<PieEntry>(entries, R.layout.item_day_task_progress, context) {
|
override fun bindView(holder: MyViewHolder, obj: PieEntry, bool: Boolean, position: Int) {
|
var total = 0f
|
for (entry in entries) {
|
total += entry.value
|
}
|
holder.setText(R.id.tv_item1, entries[position].label)
|
.setText(R.id.tv_item2, entries[position].value.toString() + "")
|
.setText(R.id.tv_item3, decimalFormat.format((entries[position].value / total * 100).toDouble()) + "%")
|
}
|
}
|
val layoutManager = LinearLayoutManager(context)
|
layoutManager.orientation = LinearLayoutManager.VERTICAL
|
rv_problem_type!!.layoutManager = layoutManager
|
rv_problem_type!!.addItemDecoration(DividerItemDecoration(context, DividerItemDecoration.VERTICAL))
|
rv_problem_type!!.adapter = entryAdapter
|
}
|
|
private fun initPieChart(view: View) {
|
|
//饼图样式设置***************************************************************************************************
|
PC_problem!!.setUsePercentValues(true)
|
PC_problem!!.description.isEnabled = false
|
PC_problem!!.setExtraOffsets(5f, 10f, 5f, 5f)
|
// mChart.setExtraOffsets(20.f, 0.f, 20.f, 0.f);
|
PC_problem!!.dragDecelerationFrictionCoef = 0.95f
|
|
// mChart.setCenterTextTypeface(mTfLight);
|
// mChart.setCenterText(generateCenterSpannableText());
|
PC_problem!!.isDrawHoleEnabled = true
|
PC_problem!!.setHoleColor(Color.WHITE)
|
PC_problem!!.setTransparentCircleColor(Color.WHITE)
|
PC_problem!!.setTransparentCircleAlpha(110)
|
PC_problem!!.holeRadius = 39f
|
PC_problem!!.transparentCircleRadius = 41f
|
PC_problem!!.setDrawCenterText(true)
|
PC_problem!!.rotationAngle = 0f
|
// enable rotation of the chart by touch
|
PC_problem!!.isRotationEnabled = true
|
PC_problem!!.isHighlightPerTapEnabled = true
|
val l = PC_problem!!.legend
|
l.verticalAlignment = Legend.LegendVerticalAlignment.TOP
|
l.horizontalAlignment = Legend.LegendHorizontalAlignment.LEFT
|
l.orientation = Legend.LegendOrientation.VERTICAL
|
l.setDrawInside(false)
|
l.xEntrySpace = 7f
|
l.yEntrySpace = 0f
|
l.yOffset = 0f
|
|
// entry label styling
|
PC_problem!!.setEntryLabelColor(Color.WHITE)
|
// mChart.setEntryLabelTypeface(mTfRegular);
|
PC_problem!!.setEntryLabelTextSize(12f)
|
PC_problem!!.setDrawEntryLabels(false)
|
|
//test
|
// setData(new ArrayList<ProblemlistVo>());
|
//
|
}
|
|
private fun setData(problemlists: MutableList<ProblemlistVo>) {
|
//数据初始化***************************************************************************************************
|
entries.clear()
|
val dataSet = 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]));
|
// }
|
//*********
|
|
//初始化数据
|
val problemTypeMap = HashMap<String, Int>()
|
while (!problemlists.isEmpty()) {
|
val problemType = problemlists[0].typename
|
if (problemType == null) {
|
problemlists.removeAt(0)
|
continue
|
}
|
var problemCount = 0
|
var i = 0
|
while (i < problemlists.size) {
|
if (problemType == problemlists[i].typename) {
|
problemCount++
|
problemlists.removeAt(i)
|
i--
|
}
|
i++
|
}
|
problemTypeMap[problemType] = problemCount
|
}
|
//按HashMap的value降序排序
|
val list: List<Map.Entry<String, Int>> = ArrayList<Map.Entry<String, Int>>(problemTypeMap.entries)
|
Collections.sort(list) { o1, o2 -> o2.value.compareTo(o1.value) }
|
// problemTypeMap.clear();
|
// problemTypeMap.entrySet().addAll(list);
|
//初始化Entry,限定最多显示5个扇区,超出的合并为第5个扇区
|
var remainProblemType = ""
|
var remainProblemCount = 0
|
var chartcount = 0
|
for ((key, value) in list) {
|
if (chartcount < MAX_CHART_COUNT - 1) {
|
entries.add(PieEntry(value.toFloat(), key))
|
} else {
|
remainProblemCount = remainProblemCount + value
|
remainProblemType = key
|
}
|
chartcount++
|
}
|
if (chartcount == MAX_CHART_COUNT) {
|
entries.add(PieEntry(remainProblemCount.toFloat(), remainProblemType))
|
} else if (chartcount > MAX_CHART_COUNT) {
|
entries.add(PieEntry(remainProblemCount.toFloat(), REMAIN_PROBLEM))
|
}
|
|
// add a lot of colors
|
val colors = ArrayList<Int>()
|
for (c in ColorTemplate.JOYFUL_COLORS) colors.add(c)
|
for (c in ColorTemplate.COLORFUL_COLORS) colors.add(c)
|
for (c in ColorTemplate.VORDIPLOM_COLORS) colors.add(c)
|
for (c in ColorTemplate.LIBERTY_COLORS) colors.add(c)
|
for (c in ColorTemplate.PASTEL_COLORS) colors.add(c)
|
colors.add(ColorTemplate.getHoloBlue())
|
dataSet.colors = colors
|
|
//value line
|
dataSet.valueLinePart1OffsetPercentage = 80f
|
dataSet.valueLinePart1Length = 0.2f
|
dataSet.valueLinePart2Length = 0.4f
|
//dataSet.setXValuePosition(PieDataSet.ValuePosition.OUTSIDE_SLICE);
|
dataSet.yValuePosition = PieDataSet.ValuePosition.OUTSIDE_SLICE
|
|
//
|
val data = PieData(dataSet)
|
data.setValueFormatter(PercentFormatter())
|
data.setValueTextSize(11f)
|
data.setValueTextColor(Color.BLACK)
|
// data.setValueTypeface(mTfLight);
|
if (!entries.isEmpty()) { //数据全为空时,不显示
|
PC_problem!!.data = data
|
} else {
|
PC_problem!!.data = null
|
}
|
PC_problem!!.setNoDataText("暂无数据")
|
|
// undo all highlights
|
PC_problem!!.highlightValues(null)
|
PC_problem!!.animateY(1400, Easing.EasingOption.EaseInOutQuad)
|
} //</editor-fold>
|
|
/**
|
* 按照搜索条件查询问题分布结果
|
*/
|
// 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<List<ProblemlistVo>> getProblemByArea = problemListService.getByArea(areaVo);
|
// getProblemByArea.enqueue(new Callback<List<ProblemlistVo>>() {
|
// @Override
|
// public void onResponse(Call<List<ProblemlistVo>> call, Response<List<ProblemlistVo>> response) {
|
// if (response.body() != null) {
|
// problemlists = response.body();
|
// }
|
// setData(problemlists);
|
// entryAdapter.notifyDataSetChanged();//刷新列表
|
// DialogUtil.closeDialog(dialog);
|
// }
|
//
|
// @Override
|
// public void onFailure(Call<List<ProblemlistVo>> call, Throwable t) {
|
// setData(problemlists);
|
// DialogUtil.closeDialog(dialog);
|
// }
|
// });
|
//
|
// setChartTitle();//刷新标题
|
// setDateDisplay();//刷新时间
|
// }
|
companion object {
|
private const val ARG_PARAM1 = "taskProgressVo" //参数标签
|
fun newInstance(taskProgressVo: TaskVo?): ProblemDetailFragment {
|
val fragment = ProblemDetailFragment()
|
val args = Bundle()
|
args.putSerializable(ARG_PARAM1, taskProgressVo)
|
fragment.arguments = args
|
return fragment
|
}
|
|
private const val MAX_CHART_COUNT = 20 //饼图最多显示区块数
|
private const val REMAIN_PROBLEM = "其余"
|
}
|
}
|