Created
June 13, 2020 14:35
-
-
Save ForceGT/fc6cadcf27c3d3ec60266024c1106df0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.gtxtreme.punecoronatracker | |
import android.os.Bundle | |
import android.view.LayoutInflater | |
import android.view.View | |
import android.view.ViewGroup | |
import android.widget.Toast | |
import androidx.fragment.app.Fragment | |
import com.anychart.AnyChart | |
import com.anychart.chart.common.dataentry.DataEntry | |
import com.anychart.chart.common.dataentry.ValueDataEntry | |
import com.anychart.charts.Cartesian | |
import com.anychart.charts.Pie | |
import com.anychart.core.cartesian.series.Bar | |
import com.anychart.data.Mapping | |
import com.anychart.data.Set | |
import com.anychart.enums.Anchor | |
import com.anychart.enums.ScaleStackMode | |
import kotlinx.android.synthetic.main.fragment_graphical_stats.* | |
import java.lang.Exception | |
class GraphicalStats : Fragment() { | |
private val urlArray = arrayOf( | |
"https://raw.githubusercontent.com/omkarmarkad/COVID19-Pune/master/age-wise.csv", | |
"https://raw.githubusercontent.com/omkarmarkad/COVID19-Pune/master/timeseries-case-counts.csv", | |
"https://raw.githubusercontent.com/omkarmarkad/COVID19-Pune/master/daily-wardwise.csv", | |
"https://raw.githubusercontent.com/omkarmarkad/COVID19-Pune/master/timeseries-ward-wise.csv" | |
) | |
override fun onCreateView( | |
inflater: LayoutInflater, container: ViewGroup?, | |
savedInstanceState: Bundle? | |
): View? { | |
// Inflate the layout for this fragment | |
return inflater.inflate(R.layout.fragment_graphical_stats, container, false) | |
} | |
override fun onViewCreated(view: View, savedInstanceState: Bundle?) { | |
super.onViewCreated(view, savedInstanceState) | |
pieChart.setProgressBar(progressBar) | |
pieChart.setChart(getSummary()) | |
stackedBarAge.setProgressBar(ageProgressBar) | |
stackedBarAge.setChart(getAgeWiseBar()) | |
dailyWard.setProgressBar(wardProgressBar) | |
dailyWard.setChart(getDailyWardWiseBar()) | |
} | |
private fun getSummary(): Pie { | |
val pie: Pie = AnyChart.pie() | |
val dataHandlerInstance = object : APIDataEventHandler { | |
override fun timeSeriesCaseCounts(map: Map<String, ArrayList<String>>) { | |
val totalDeaths = map["Total Deaths"]?.lastOrNull() | |
val totalActive = map["Total Active"]?.lastOrNull() | |
val totalRecovered = map["Total Recovered"]?.lastOrNull() | |
val data: MutableList<DataEntry> = ArrayList() | |
data.add(ValueDataEntry("Total Deaths", totalDeaths?.toInt())) | |
data.add(ValueDataEntry("Total Active", totalActive?.toInt())) | |
data.add(ValueDataEntry("Total Recovered", totalRecovered?.toInt())) | |
pie.data(data) | |
pie.title("Summary of Total Deaths,Active,Recovered") | |
pie.labels().position("inside") | |
pie.animation(true, 800) | |
} | |
override fun ageWiseData(map: Map<String, ArrayList<String>>) { | |
TODO("Not yet implemented") | |
} | |
override fun dailyWardWiseData(map: Map<String, ArrayList<String>>) { | |
TODO("Not yet implemented") | |
} | |
override fun timeSeriesWardWise(map: Map<String, ArrayList<String>>) { | |
TODO("Not yet implemented") | |
} | |
override fun onFailure(e: Exception) { | |
Toast.makeText(context, e.message, Toast.LENGTH_SHORT).show() | |
} | |
} | |
ApiFetcher(dataHandlerInstance, 1).execute(urlArray[1]) | |
return pie | |
} | |
fun getAgeWiseBar(): Cartesian? { | |
val column = AnyChart.column() | |
val dataHandlerInstance2 = object : APIDataEventHandler { | |
override fun timeSeriesCaseCounts(map: Map<String, ArrayList<String>>) { | |
TODO("Not yet implemented") | |
} | |
override fun ageWiseData(map: Map<String, ArrayList<String>>) { | |
column.animation(true) | |
column.yScale().stackMode(ScaleStackMode.VALUE) | |
column.padding(10.0, 10.0, 10.0, 10.0) | |
column.yAxis(0).labels().format("{%value}") | |
val ageGroup = map["Age"] | |
val maleCount = map["Male"] | |
val femaleCount = map["Female"] | |
val deaths = map["Deaths"] | |
val seriesData: MutableList<DataEntry> = ArrayList() | |
if (ageGroup != null) { | |
for (i in 1 until ageGroup.size) { | |
seriesData.add( | |
AgeCustomDataEntry( | |
ageGroup[i], | |
maleCount?.get(i)?.toInt(), | |
femaleCount?.get(i)?.toInt(), | |
deaths?.get(i)?.toInt() | |
) | |
) | |
} | |
} | |
val set = Set.instantiate() | |
set.data(seriesData) | |
val series1Data: Mapping = set.mapAs("{ x: 'x', value: 'value' }") | |
val series2Data: Mapping = set.mapAs("{ x: 'x', value: 'value2' }") | |
val series3Data: Mapping = set.mapAs("{x: 'x', value: 'value3' }") | |
val series1 = column.column(series1Data) | |
series1.name("Males") | |
series1.tooltip() | |
.position("right") | |
.anchor(Anchor.LEFT_CENTER) | |
val series2 = column.column(series2Data) | |
series2.name("Females") | |
series2.tooltip() | |
.position("left") | |
.anchor(Anchor.RIGHT_CENTER) | |
val series3 = column.column(series3Data) | |
series3.name("Deaths") | |
series3.tooltip() | |
.position("left") | |
.anchor(Anchor.RIGHT_CENTER) | |
column.title("Age Wise Stats") | |
column.legend().enabled(true) | |
column.legend().fontSize(13.0) | |
column.legend().padding(0.0, 0.0, 20.0, 0.0) | |
} | |
override fun dailyWardWiseData(map: Map<String, ArrayList<String>>) { | |
TODO("Not yet implemented") | |
} | |
override fun timeSeriesWardWise(map: Map<String, ArrayList<String>>) { | |
TODO("Not yet implemented") | |
} | |
override fun onFailure(e: Exception) { | |
Toast.makeText(context, e.message, Toast.LENGTH_LONG).show() | |
} | |
} | |
ApiFetcher(dataHandlerInstance2, 4).execute(urlArray[0]) | |
return column | |
} | |
fun getDailyWardWiseBar(): Cartesian? { | |
val bar = AnyChart.bar() | |
val dataHandlerInstance3 = object :APIDataEventHandler{ | |
override fun timeSeriesCaseCounts(map: Map<String, ArrayList<String>>) { | |
TODO("Not yet implemented") | |
} | |
override fun ageWiseData(map: Map<String, ArrayList<String>>) { | |
TODO("Not yet implemented") | |
} | |
override fun dailyWardWiseData(map: Map<String, ArrayList<String>>) { | |
bar.animation(true) | |
bar.yScale().stackMode(ScaleStackMode.VALUE) | |
bar.padding(10.0, 10.0, 10.0, 10.0) | |
bar.yAxis(0).labels().format("{%value}") | |
val ward = map["Ward"] | |
val tCases = map["Total Cases"] | |
val tDeaths = map ["Total Deaths"] | |
val tActive = map["Total Active"] | |
val seriesData: MutableList<DataEntry> = ArrayList() | |
if (ward != null) { | |
for (i in 1 until ward.size){ | |
seriesData.add(AgeCustomDataEntry(ward[i], tCases?.get(i)?.toInt(), tDeaths?.get(i)?.toInt(), tActive?.get(i)?.toInt())) | |
} | |
} | |
val set = Set.instantiate() | |
set.data(seriesData) | |
val series1Data: Mapping = set.mapAs("{ x: 'x', value: 'value' }") | |
val series2Data: Mapping = set.mapAs("{ x: 'x', value: 'value2' }") | |
val series3Data: Mapping = set.mapAs("{x: 'x', value: 'value3' }") | |
val series1 = bar.bar(series1Data) | |
series1.name("Total Cases") | |
series1.tooltip() | |
.position("right") | |
.anchor(Anchor.LEFT_CENTER) | |
val series2 = bar.bar(series2Data) | |
series2.name("Total Deaths") | |
series2.tooltip() | |
.position("left") | |
.anchor(Anchor.RIGHT_CENTER) | |
val series3 = bar.bar(series3Data) | |
series3.name("Total Active") | |
series3.tooltip() | |
.position("left") | |
.anchor(Anchor.RIGHT_CENTER) | |
//TODO Add Date of Last Update | |
bar.title("Daily Ward Data") | |
bar.legend().enabled(true) | |
bar.legend().fontSize(13.0) | |
bar.legend().padding(0.0, 0.0, 20.0, 0.0) | |
} | |
override fun timeSeriesWardWise(map: Map<String, ArrayList<String>>) { | |
TODO("Not yet implemented") | |
} | |
override fun onFailure(e: Exception) { | |
TODO("Not yet implemented") | |
} | |
} | |
ApiFetcher(dataHandlerInstance3, 3).execute(urlArray[2]) | |
return bar | |
} | |
} | |
class AgeCustomDataEntry( | |
x: String?, | |
value: Number?, | |
value2: Number?, | |
value3: Number? | |
) : | |
ValueDataEntry(x, value) { | |
init { | |
setValue("value2", value2) | |
setValue("value3", value3) | |
} | |
} | |
//class WardCustomDataEntry( | |
// x: String?, | |
// value: Number?, | |
// value2: Number?, | |
// value3: Number?, | |
// value4: Number? | |
//):ValueDataEntry(x,value){ | |
// init{ | |
// setValue("value2", value2) | |
// setValue("value3", value3) | |
// setValue("value4", value4) | |
// | |
// } | |
//} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment