Merge branch 'master' of https://gitea.kerboul.me/timelapse/timelapse-android
This commit is contained in:
@@ -8,13 +8,14 @@ import android.widget.TextView
|
||||
import androidx.appcompat.app.AppCompatActivity
|
||||
import androidx.recyclerview.widget.GridLayoutManager
|
||||
import androidx.recyclerview.widget.RecyclerView
|
||||
import androidx.recyclerview.widget.StaggeredGridLayoutManager
|
||||
import com.dreamteam.timelapse.data.ApiService
|
||||
import com.dreamteam.timelapse.data.Measurement
|
||||
import com.dreamteam.timelapse.data.Project
|
||||
import com.dreamteam.timelapse.data.ProjectRepository
|
||||
import com.dreamteam.timelapse.data.VideoRepository
|
||||
import com.dreamteam.timelapse.databinding.ProjectBinding
|
||||
import com.github.mikephil.charting.components.Legend
|
||||
import com.github.mikephil.charting.components.XAxis
|
||||
import com.github.mikephil.charting.data.Entry
|
||||
import com.github.mikephil.charting.data.LineData
|
||||
import com.github.mikephil.charting.data.LineDataSet
|
||||
@@ -124,55 +125,106 @@ class ProjectActivity : AppCompatActivity() {
|
||||
}
|
||||
}
|
||||
|
||||
fun initGraph(lm : List<Measurement>){
|
||||
fun initGraph(lm: List<Measurement>) {
|
||||
val temperatureHumidityChart = this.binding.temperatureHumidityChart
|
||||
|
||||
val temperatures = lm.sortedBy { p -> p.order_id }
|
||||
.map { m -> Pair(m.temperature, m.timestamp) }
|
||||
val humidities = lm.sortedBy { p -> p.order_id }
|
||||
.map { m -> Pair(m.humidity, m.timestamp) }
|
||||
// Sort measurements by order_id and map to pairs of value and timestamp
|
||||
val temperatures = lm.sortedBy { it.order_id }
|
||||
.map { m -> Pair(m.temperature, m.order_id) }
|
||||
val humidities = lm.sortedBy { it.order_id }
|
||||
.map { m -> Pair(m.humidity, m.order_id) }
|
||||
|
||||
// Ajouter des points de données (exemples)
|
||||
val temperatureEntries = temperatures.map { pair ->
|
||||
Entry((pair.second.time).toFloat(), pair.first) // (timestamp, température)
|
||||
// Create entries for temperature and humidity using timestamps
|
||||
val temperatureEntries = temperatures.mapIndexed { index, pair ->
|
||||
Entry(index.toFloat(), pair.first) // Use index for order, but display timestamp
|
||||
}
|
||||
|
||||
val humidityEntries = humidities.map { pair ->
|
||||
Entry((pair.second.time).toFloat(), pair.first) // (timestamp, humidité)
|
||||
val humidityEntries = humidities.mapIndexed { index, pair ->
|
||||
Entry(index.toFloat(), pair.first) // Use index for order, but display timestamp
|
||||
}
|
||||
|
||||
// Créer des LineDataSet pour chaque série de données
|
||||
val temperatureDataSet = LineDataSet(temperatureEntries, "Température")
|
||||
val humidityDataSet = LineDataSet(humidityEntries, "Hygrométrie")
|
||||
Log.i("ProjectActivity", temperatureEntries.toString())
|
||||
val minX = temperatureEntries.minOf { it.x } // La valeur minimale de x
|
||||
val maxX = temperatureEntries.maxOf { it.x } // La valeur maximale de x
|
||||
// Create LineDataSet for each data series
|
||||
val temperatureDataSet = LineDataSet(temperatureEntries, "Température").apply {
|
||||
color = Color.RED // Keep color red
|
||||
valueTextColor = Color.BLACK
|
||||
valueTextSize = 12f
|
||||
}
|
||||
|
||||
val humidityDataSet = LineDataSet(humidityEntries, "Hygrométrie").apply {
|
||||
color = Color.BLUE // Keep color blue
|
||||
valueTextColor = Color.BLACK
|
||||
valueTextSize = 12f
|
||||
}
|
||||
|
||||
// Set up the x-axis to display timestamps
|
||||
val xAxis = temperatureHumidityChart.xAxis
|
||||
xAxis.valueFormatter = object : ValueFormatter() {
|
||||
override fun getFormattedValue(value: Float): String {
|
||||
val date = Date(value.toLong())
|
||||
val sdf = SimpleDateFormat("hh:mm", Locale.getDefault())
|
||||
// Map the index back to the corresponding timestamp
|
||||
val timestamp = lm.sortedBy { it.order_id }[value.toInt()].timestamp
|
||||
val date = Date(timestamp.time)
|
||||
val sdf = SimpleDateFormat("dd/MM/yyyy:HH:mm:ss", Locale.getDefault())
|
||||
return sdf.format(date)
|
||||
}
|
||||
}
|
||||
|
||||
// Set the minimum and maximum x-axis values based on indices
|
||||
val minX = 0f
|
||||
val maxX = (lm.size - 1).toFloat()
|
||||
xAxis.axisMinimum = minX
|
||||
xAxis.axisMaximum = maxX
|
||||
// Ajouter les datasets au graphique
|
||||
xAxis.granularity = 1f // Ensure each index is plotted
|
||||
xAxis.labelRotationAngle = -90f
|
||||
xAxis.position = XAxis.XAxisPosition.BOTTOM
|
||||
|
||||
// Configure the left y-axis
|
||||
val leftYAxis = temperatureHumidityChart.axisLeft
|
||||
leftYAxis.labelCount = 6 // Set the number of labels
|
||||
leftYAxis.setLabelCount(6, true)
|
||||
leftYAxis.setDrawLabels(true)
|
||||
leftYAxis.setDrawAxisLine(true)
|
||||
leftYAxis.setDrawGridLines(true)
|
||||
leftYAxis.valueFormatter = object : ValueFormatter() {
|
||||
override fun getFormattedValue(value: Float): String {
|
||||
return "${value.toInt()} C°" // Append "C°" to the temperature values
|
||||
}
|
||||
}
|
||||
|
||||
// Configure the right y-axis
|
||||
val rightYAxis = temperatureHumidityChart.axisRight
|
||||
rightYAxis.labelCount = 6 // Set the number of labels
|
||||
rightYAxis.axisMinimum = 0f // Set minimum value if needed
|
||||
rightYAxis.axisMaximum = 100f // Set maximum value to 100%
|
||||
rightYAxis.setLabelCount(6, true)
|
||||
rightYAxis.setDrawLabels(true)
|
||||
rightYAxis.setDrawAxisLine(true)
|
||||
rightYAxis.setDrawGridLines(false)
|
||||
rightYAxis.valueFormatter = object : ValueFormatter() {
|
||||
override fun getFormattedValue(value: Float): String {
|
||||
return "${value.toInt()} %" // Append "%" to the humidity values
|
||||
}
|
||||
}
|
||||
|
||||
// Add datasets to the chart
|
||||
val dataSets: MutableList<ILineDataSet> = ArrayList()
|
||||
dataSets.add(temperatureDataSet)
|
||||
dataSets.add(humidityDataSet)
|
||||
// val lineData = LineData(temperatureDataSet, humidityDataSet)
|
||||
//Log.e("Graph", lineData.getDataSetByIndex(0).entryCount.toString())
|
||||
|
||||
temperatureHumidityChart.data = LineData(dataSets)
|
||||
val description = temperatureHumidityChart.description
|
||||
description.text = " "
|
||||
|
||||
// Customize the legend
|
||||
val legend: Legend = temperatureHumidityChart.legend
|
||||
legend.textColor = Color.BLACK
|
||||
legend.textSize = 12f
|
||||
|
||||
// Personnaliser le graphique (par exemple, couleur, légende, etc.)
|
||||
temperatureDataSet.color = Color.RED
|
||||
humidityDataSet.color = Color.BLUE
|
||||
|
||||
temperatureHumidityChart.invalidate() // Rafraîchir le graphique
|
||||
// Refresh the chart
|
||||
temperatureHumidityChart.invalidate()
|
||||
}
|
||||
|
||||
|
||||
|
||||
fun initProjectInfo(){
|
||||
val nameview = findViewById<TextView>(R.id.project_name)
|
||||
val descriptionview = findViewById<TextView>(R.id.project_description)
|
||||
|
||||
Reference in New Issue
Block a user