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
package cn.flightfeather.thirdappmodule.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 = "其余"
    }
}