|
|
|
|
@ -7,26 +7,37 @@ import android.view.ViewGroup |
|
|
|
|
import com.github.mikephil.charting.charts.BarChart |
|
|
|
|
import com.github.mikephil.charting.charts.BarLineChartBase |
|
|
|
|
import com.github.mikephil.charting.charts.LineChart |
|
|
|
|
import com.github.mikephil.charting.data.* |
|
|
|
|
import com.github.mikephil.charting.data.BarData |
|
|
|
|
import com.github.mikephil.charting.data.Entry |
|
|
|
|
import com.github.mikephil.charting.data.LineData |
|
|
|
|
import com.github.mikephil.charting.highlight.Highlight |
|
|
|
|
import com.github.mikephil.charting.interfaces.datasets.IBarLineScatterCandleBubbleDataSet |
|
|
|
|
import com.github.mikephil.charting.listener.OnChartValueSelectedListener |
|
|
|
|
import kotlinx.android.synthetic.main.fragment_graph.* |
|
|
|
|
import net.pokeranalytics.android.R |
|
|
|
|
import net.pokeranalytics.android.calculus.Stat |
|
|
|
|
import net.pokeranalytics.android.exceptions.PAIllegalStateException |
|
|
|
|
import net.pokeranalytics.android.model.interfaces.ObjectIdentifier |
|
|
|
|
import net.pokeranalytics.android.ui.fragment.components.RealmFragment |
|
|
|
|
import net.pokeranalytics.android.ui.graph.AxisFormatting |
|
|
|
|
import net.pokeranalytics.android.ui.graph.GraphUnderlyingEntry |
|
|
|
|
import net.pokeranalytics.android.ui.graph.setStyle |
|
|
|
|
import net.pokeranalytics.android.ui.view.LegendView |
|
|
|
|
import net.pokeranalytics.android.ui.view.MultiLineLegendView |
|
|
|
|
import net.pokeranalytics.android.ui.viewmodel.GraphDataProvider |
|
|
|
|
import net.pokeranalytics.android.ui.viewmodel.ViewModelHolder |
|
|
|
|
import net.pokeranalytics.android.util.extensions.findById |
|
|
|
|
import timber.log.Timber |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class GraphFragment : RealmFragment(), OnChartValueSelectedListener { |
|
|
|
|
|
|
|
|
|
private lateinit var graphDataProvider: GraphDataProvider |
|
|
|
|
|
|
|
|
|
private val stat: Stat |
|
|
|
|
get() { |
|
|
|
|
return this.graphDataProvider.stat |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
enum class Style { |
|
|
|
|
LINE, |
|
|
|
|
BAR, |
|
|
|
|
@ -38,12 +49,12 @@ class GraphFragment : RealmFragment(), OnChartValueSelectedListener { |
|
|
|
|
/** |
|
|
|
|
* Create new instance |
|
|
|
|
*/ |
|
|
|
|
fun newInstance(lineDataSets: List<LineDataSet>? = null, barDataSets: List<BarDataSet>? = null, style: Style = Style.LINE): GraphFragment { |
|
|
|
|
fun newInstance(style: Style = Style.LINE): GraphFragment { |
|
|
|
|
val fragment = GraphFragment() |
|
|
|
|
fragment.style = style |
|
|
|
|
fragment.lineDataSetList = lineDataSets |
|
|
|
|
fragment.barDataSetList = barDataSets |
|
|
|
|
// fragment.lineDataSetList = lineDataSets |
|
|
|
|
// fragment.barDataSetList = barDataSets |
|
|
|
|
val bundle = Bundle() |
|
|
|
|
bundle.putSerializable(BundleKey.STYLE.value, style.ordinal) |
|
|
|
|
fragment.arguments = bundle |
|
|
|
|
return fragment |
|
|
|
|
} |
|
|
|
|
@ -51,16 +62,11 @@ class GraphFragment : RealmFragment(), OnChartValueSelectedListener { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private var style: Style = Style.LINE |
|
|
|
|
private lateinit var legendView: LegendView |
|
|
|
|
|
|
|
|
|
private var lineDataSetList: List<LineDataSet>? = null |
|
|
|
|
private var barDataSetList: List<BarDataSet>? = null |
|
|
|
|
private lateinit var legendView: LegendView |
|
|
|
|
|
|
|
|
|
private var chartView: BarLineChartBase<*>? = null |
|
|
|
|
|
|
|
|
|
private var stat: Stat = Stat.NET_RESULT |
|
|
|
|
private var axisFormatting: AxisFormatting = AxisFormatting.DEFAULT |
|
|
|
|
|
|
|
|
|
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? { |
|
|
|
|
super.onCreateView(inflater, container, savedInstanceState) |
|
|
|
|
return inflater.inflate(R.layout.fragment_graph, container, false) |
|
|
|
|
@ -68,31 +74,16 @@ class GraphFragment : RealmFragment(), OnChartValueSelectedListener { |
|
|
|
|
|
|
|
|
|
override fun onViewCreated(view: View, savedInstanceState: Bundle?) { |
|
|
|
|
super.onViewCreated(view, savedInstanceState) |
|
|
|
|
initData() |
|
|
|
|
initUI() |
|
|
|
|
loadGraph() |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Set data |
|
|
|
|
*/ |
|
|
|
|
fun setLineData(lineDataSets: List<LineDataSet>, stat: Stat, axisFormatting: AxisFormatting = AxisFormatting.DEFAULT) { |
|
|
|
|
this.lineDataSetList = lineDataSets |
|
|
|
|
this.stat = stat |
|
|
|
|
this.axisFormatting = axisFormatting |
|
|
|
|
|
|
|
|
|
if (isAdded && !isDetached) { |
|
|
|
|
loadGraph() |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
fun setBarData(barDataSets: List<BarDataSet>, stat: Stat, axisFormatting: AxisFormatting = AxisFormatting.DEFAULT) { |
|
|
|
|
this.barDataSetList = barDataSets |
|
|
|
|
this.stat = stat |
|
|
|
|
this.axisFormatting = axisFormatting |
|
|
|
|
private fun initData() { |
|
|
|
|
val styleOrdinal = this.arguments?.getInt(BundleKey.STYLE.value) ?: throw PAIllegalStateException("Missing style key in bundle") |
|
|
|
|
this.style = Style.values()[styleOrdinal] |
|
|
|
|
|
|
|
|
|
if (isAdded && !isDetached) { |
|
|
|
|
loadGraph() |
|
|
|
|
} |
|
|
|
|
this.graphDataProvider = (requireActivity() as ViewModelHolder).viewModel as GraphDataProvider |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
@ -108,6 +99,13 @@ class GraphFragment : RealmFragment(), OnChartValueSelectedListener { |
|
|
|
|
this.legendContainer.addView(this.legendView) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
fun reload(style: Style) { |
|
|
|
|
this.style = style |
|
|
|
|
if (isAdded && !isDetached) { |
|
|
|
|
loadGraph() |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Load graph |
|
|
|
|
*/ |
|
|
|
|
@ -115,7 +113,14 @@ class GraphFragment : RealmFragment(), OnChartValueSelectedListener { |
|
|
|
|
|
|
|
|
|
this.chartContainer.removeAllViews() |
|
|
|
|
|
|
|
|
|
this.lineDataSetList?.let { dataSets -> |
|
|
|
|
this.chartView = when (this.style) { |
|
|
|
|
Style.LINE, Style.MULTILINE -> { |
|
|
|
|
|
|
|
|
|
val dataSets = when (this.style) { |
|
|
|
|
Style.LINE -> listOf(this.graphDataProvider.lineDataSet(requireContext())) |
|
|
|
|
Style.MULTILINE -> this.graphDataProvider.multiLineDataSet(requireContext()) |
|
|
|
|
else -> throw PAIllegalStateException("Cannot happen") |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
val lineChart = LineChart(context) |
|
|
|
|
lineChart.setOnChartValueSelectedListener(this) |
|
|
|
|
@ -123,20 +128,21 @@ class GraphFragment : RealmFragment(), OnChartValueSelectedListener { |
|
|
|
|
val lineData = LineData(dataSets) |
|
|
|
|
lineChart.data = lineData |
|
|
|
|
|
|
|
|
|
this.chartView = lineChart |
|
|
|
|
|
|
|
|
|
dataSets.firstOrNull()?.let { dataSet -> |
|
|
|
|
this.legendView.prepareWithStat(this.stat, dataSet.entryCount, this.style) |
|
|
|
|
this.legendView.prepareWithStat(stat, dataSet.entryCount, this.style) |
|
|
|
|
if (dataSet.entryCount > 0) { |
|
|
|
|
val entry = dataSet.getEntryForIndex(dataSet.entryCount - 1) |
|
|
|
|
this.selectValue(entry, dataSet) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
lineChart |
|
|
|
|
} |
|
|
|
|
Style.BAR -> { |
|
|
|
|
|
|
|
|
|
this.barDataSetList?.let { dataSets -> |
|
|
|
|
val dataSets = listOf(this.graphDataProvider.barDataSet(requireContext())) |
|
|
|
|
|
|
|
|
|
this.legendView.prepareWithStat(this.stat, style = this.style) |
|
|
|
|
this.legendView.prepareWithStat(stat, style = this.style) |
|
|
|
|
val barChart = BarChart(context) |
|
|
|
|
barChart.setOnChartValueSelectedListener(this) |
|
|
|
|
|
|
|
|
|
@ -146,7 +152,6 @@ class GraphFragment : RealmFragment(), OnChartValueSelectedListener { |
|
|
|
|
if (stat.graphShowsYAxisZero) { |
|
|
|
|
barChart.axisLeft.axisMinimum = 0.0f |
|
|
|
|
} |
|
|
|
|
this.chartView = barChart |
|
|
|
|
|
|
|
|
|
val barData = BarData(dataSets) |
|
|
|
|
barChart.data = barData |
|
|
|
|
@ -154,14 +159,17 @@ class GraphFragment : RealmFragment(), OnChartValueSelectedListener { |
|
|
|
|
dataSets.firstOrNull()?.let { |
|
|
|
|
if (it.entryCount > 0) { |
|
|
|
|
val entry = it.getEntryForIndex(0) |
|
|
|
|
this.chartView?.highlightValue(entry.x, 0) |
|
|
|
|
barChart.highlightValue(entry.x, 0) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
barChart |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
this.chartContainer.addView(this.chartView) |
|
|
|
|
|
|
|
|
|
this.chartView?.setStyle(false, axisFormatting, requireContext()) |
|
|
|
|
this.chartView?.setStyle(false, this.graphDataProvider.axisFormatting, requireContext()) |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|