Skip to content
This repository has been archived by the owner on Jan 10, 2024. It is now read-only.

Commit

Permalink
Bugfix/grades bar chart (#1506)
Browse files Browse the repository at this point in the history
* removed chart icon in edit mode

* adapted styling of the grades bar chart

* Apply suggestions from code review

Co-authored-by: Benjamin Pabst <[email protected]>

* Addressed code review feedback

* added weight grades in percentages

Co-authored-by: Benjamin Pabst <[email protected]>
  • Loading branch information
tobiasjungmann and Bentipa authored Nov 4, 2022
1 parent 6095d5b commit 56947a4
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,12 @@ import android.widget.EditText
import androidx.appcompat.app.AlertDialog
import androidx.core.content.ContextCompat
import androidx.core.content.res.ResourcesCompat.getColor
import com.github.mikephil.charting.components.Description
import com.github.mikephil.charting.components.Legend
import com.github.mikephil.charting.components.LegendEntry
import com.github.mikephil.charting.components.XAxis
import com.github.mikephil.charting.data.*
import com.github.mikephil.charting.formatter.ValueFormatter
import com.github.mikephil.charting.formatter.IndexAxisValueFormatter
import com.google.gson.*
import com.google.gson.reflect.TypeToken
import com.zhuinden.fragmentviewbindingdelegatekt.viewBinding
Expand All @@ -36,7 +38,7 @@ import org.joda.time.format.DateTimeFormat
import org.joda.time.format.DateTimeFormatter
import java.lang.reflect.Type
import java.text.NumberFormat
import java.util.Locale
import java.util.*
import javax.inject.Inject

class GradesFragment : FragmentForAccessingTumOnline<ExamList>(
Expand Down Expand Up @@ -242,7 +244,7 @@ class GradesFragment : FragmentForAccessingTumOnline<ExamList>(
*
* @param gradeDistribution An [ArrayMap] mapping grades to number of occurrences
*/
private fun displayPieChart(gradeDistribution: ArrayMap<String, Int>) {
private fun displayPieChart(gradeDistribution: ArrayMap<String, Double>) {
val entries = grades.map { grade ->
val count = gradeDistribution[grade] ?: 0
PieEntry(count.toFloat(), grade)
Expand Down Expand Up @@ -283,47 +285,41 @@ class GradesFragment : FragmentForAccessingTumOnline<ExamList>(
*
* @param gradeDistribution An [ArrayMap] mapping grades to number of occurrence
*/
private fun displayBarChart(gradeDistribution: ArrayMap<String, Int>) {
val entries = grades.mapIndexed { index, grade ->
val value = gradeDistribution[grade] ?: 0
BarEntry(index.toFloat(), value.toFloat())
private fun displayBarChart(gradeDistribution: ArrayMap<String, Double>) {
val sum = gradeDistribution.values.sum()
val entries: List<BarEntry>
if (adaptDiagramToWeights) {
entries = grades.mapIndexed { index, grade ->
val value: Double = gradeDistribution[grade] ?: 0.0
BarEntry(index.toFloat(), ((value*100) / sum).toFloat())
}
} else {
entries = grades.mapIndexed { index, grade ->
val value = gradeDistribution[grade] ?: 0
BarEntry(index.toFloat(), value.toFloat())
}
}

var annotation = ""
if (!adaptDiagramToWeights) {
annotation = getString(R.string.grades_without_weight)
}
val set = BarDataSet(entries, annotation).apply {
val set = BarDataSet(entries, "").apply {
setColors(GRADE_COLORS, requireContext())
valueTextColor = resources.getColor(R.color.text_primary)
}
set.setDrawValues(false)

with(binding) {
barChartView.apply {

data = BarData(set)
setFitBars(true)

// only label grades that are associated with at least one grade
data.setValueFormatter(object : ValueFormatter() {
override fun getFormattedValue(value: Float): String {
if (value > 0.0)
return value.toString()
return ""
}
})

description = null
setTouchEnabled(false)

axisLeft.granularity = 1f
axisRight.granularity = 1f
xAxis.granularity = 1f
legend.isEnabled = true

description = null
setTouchEnabled(false)
legend.setCustom(
arrayOf(
LegendEntry(
getString(R.string.grades_without_weight),
context.getString(R.string.grade_passed_annotation),
Legend.LegendForm.SQUARE,
10f,
0f,
Expand All @@ -335,9 +331,30 @@ class GradesFragment : FragmentForAccessingTumOnline<ExamList>(

legend.textColor = getColor(resources, R.color.text_primary, null)
xAxis.textColor = getColor(resources, R.color.text_primary, null)
axisLeft.textColor = getColor(resources, R.color.text_primary, null)
axisRight.textColor = getColor(resources, R.color.text_primary, null)

axisLeft.isEnabled = true

if (adaptDiagramToWeights) {
description.isEnabled = true
val desc = Description()
desc.text = context.getString(R.string.grade_percentages)

desc.setTextSize(11f)
desc.setPosition(540F, 50F)
description = desc
axisRight.disableGridDashedLine()
axisLeft.disableGridDashedLine()
} else {
description.isEnabled = false
axisLeft.granularity = 1f
axisRight.granularity = 1f
}
val labels = (10..51).map { i -> "" + (i / 10.0) }.toList()

xAxis.valueFormatter = IndexAxisValueFormatter(labels)
xAxis.position = XAxis.XAxisPosition.BOTTOM
xAxis.disableGridDashedLine()
xAxis.disableAxisLineDashedLine()
invalidate()
}
}
Expand Down Expand Up @@ -373,24 +390,26 @@ class GradesFragment : FragmentForAccessingTumOnline<ExamList>(
* @param exams List of [Exam] objects
* @return An [ArrayMap] mapping grades to number of occurrence
*/
private fun calculateGradeDistribution(exams: List<Exam>): ArrayMap<String, Int> {
val gradeDistribution = ArrayMap<String, Int>()
private fun calculateGradeDistribution(exams: List<Exam>): ArrayMap<String, Double> {
val gradeDistribution = ArrayMap<String, Double>()
exams.forEach { exam ->
// The grade distribution now takes grades with more than one decimal place into account as well
if (exam.gradeUsedInAverage) {
var cleanGrade = exam.grade!!
if (cleanGrade.contains(longGradeRe)) {
cleanGrade = cleanGrade.subSequence(0, 3) as String
}
val count = gradeDistribution[cleanGrade] ?: 0
val count = gradeDistribution[cleanGrade] ?: 0.0

if (adaptDiagramToWeights) {
gradeDistribution[cleanGrade] = count + (exam.credits_new * exam.weight).toInt()
gradeDistribution[cleanGrade] =
count + (exam.credits_new * exam.weight).toInt()
} else {
gradeDistribution[cleanGrade] = count + 1
gradeDistribution[cleanGrade] = count + 1.0
}
}
}

return gradeDistribution
}

Expand Down Expand Up @@ -672,13 +691,17 @@ class GradesFragment : FragmentForAccessingTumOnline<ExamList>(
binding.floatingButtonAddExamGrade.visibility = View.VISIBLE
binding.chartsContainer.visibility = View.GONE
binding.checkboxUseDiagrams.visibility = View.VISIBLE
barMenuItem?.isVisible = false
pieMenuItem?.isVisible = false
param.setMargins(0, ((32 * density + 0.5f).toInt()), 0, 0)
binding.gradesListView.setPadding(0, 0, 0, 0)
} else {
showExams(exams)
binding.frameLayoutAverageGrade?.visibility = View.VISIBLE
binding.floatingButtonAddExamGrade.visibility = View.GONE
binding.chartsContainer.visibility = View.VISIBLE
pieMenuItem?.isVisible = binding.barChartView.visibility != View.GONE
barMenuItem?.isVisible = binding.barChartView.visibility == View.GONE
binding.checkboxUseDiagrams.visibility = View.GONE
param.setMargins(0, 0, 0, 0)
binding.gradesListView.setPadding(0, ((256 * density + 0.5f).toInt()), 0, 0)
Expand Down
3 changes: 2 additions & 1 deletion app/src/main/res/values-de/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,8 @@
<string name="add_grade_error_invalid_weight_less_than_zero">Ungültige Anzahl an ECTS. Die ECTS-Zahl muss größer als 0 sein.</string>
<string name="add_exam_dialog_title">Eine neue Prüfung hinzufügen</string>
<string name="add_exam_dialog_message">Die Noten werden normalerweise automatisch hinzugefügt. Falls spezielle Noten hinzugefügt werden sollen, kann dies hier geschehen. Manuell hinzugefügte Prüfungen können später wieder gelöscht werden.</string>

<string name="grade_passed_annotation">Ohne Note Bestanden.</string>
<string name="grade_percentages">Prozentsatz der Noten.</string>

<!-- Preferences -->
<string name="lrz_id">TUM Kennung</string>
Expand Down
6 changes: 3 additions & 3 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:tools="http://schemas.android.com/tools">

<!-- START Translated, please do not change identifiers -->
Expand Down Expand Up @@ -128,7 +128,8 @@
<string name="add_grade_error_invalid_weight_less_than_zero">Invalid amount of credits. Must be greater than 0.</string>
<string name="add_exam_dialog_title">Add a New Exam</string>
<string name="add_exam_dialog_message">Grades are normally added automatically. In case special grades should be added, this can be done here. Manually added Exams can be deleted later on.</string>

<string name="grade_passed_annotation">Exam passed without a grade</string>
<string name="grade_percentages">Percentage of each grade</string>

<!-- Preferences -->
<string name="lrz_id">TUM Identification</string>
Expand Down Expand Up @@ -846,5 +847,4 @@ Signature: %5$s</string>
<string name="not_implemented">Feature not implemented yet</string>
<string name="not_implemented_interactive_map">Interactive map is not yet implemented. You can access it on NavigaTum website.</string>
<string name="redirect">Redirect</string>

</resources>

0 comments on commit 56947a4

Please sign in to comment.