package jackall.moncalc.viewmodel import android.arch.lifecycle.ViewModel import android.arch.lifecycle.ViewModelProvider import android.databinding.BindingAdapter import android.graphics.Color 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.utils.ColorTemplate import jackall.moncalc.App import jackall.moncalc.R import jackall.moncalc.db.MonstDataRealmHelper import jackall.moncalc.db.QuestRecordRealmHelper /** * Created by matsumoto_k on 2017/11/04. */ class TempleAnalyzeViewModel : LifecycleViewModel() { val questRealmHelper = QuestRecordRealmHelper() val monstDataRealmHelper = MonstDataRealmHelper() val baseTempleName by lazy { App.instance.resources.getStringArray(R.array.base_temple_name) } var pieData: PieData init { val hoge = HashMap<Int, Float>() val entries = ArrayList<PieEntry>() val labels = ArrayList<String>() questRealmHelper.findAll().forEach { if (hoge.get(it.attribute) == null) { hoge.put(it.attribute, 1f) } else { hoge.put(it.attribute, hoge.get(it.attribute)!! + 1f) } } hoge.forEach { entries.add(PieEntry(it.value, baseTempleName.get(it.key))) } val dataSets = PieDataSet(entries, "") dataSets.setSliceSpace(2f) dataSets.setValueTextSize(15f) dataSets.setXValuePosition(PieDataSet.ValuePosition.INSIDE_SLICE); val colors = ArrayList<Int>() for (c in ColorTemplate.VORDIPLOM_COLORS) colors.add(c) dataSets.colors = colors dataSets.valueTextColor = Color.BLACK val pieData = PieData(dataSets) pieData.setValueTextColor(Color.BLACK) this.pieData = pieData } object Adapter { @JvmStatic @BindingAdapter("android:pieChart") fun hoge(mChart: PieChart, pieData: PieData) { mChart.setEntryLabelColor(Color.BLACK) mChart.isDrawHoleEnabled = false mChart.legend.verticalAlignment = Legend.LegendVerticalAlignment.TOP mChart.legend.horizontalAlignment = Legend.LegendHorizontalAlignment.RIGHT mChart.legend.orientation = Legend.LegendOrientation.VERTICAL mChart.isRotationEnabled = false mChart.description.text = "神殿周回割合" mChart.data = pieData } } override fun onDestroy() { super.onDestroy() questRealmHelper.close() monstDataRealmHelper.close() } class Factory() : ViewModelProvider.NewInstanceFactory() { override fun <T : ViewModel?> create(modelClass: Class<T>): T { return TempleAnalyzeViewModel() as T } } }