COVID-19 cases in your state (province)

3 weeks ago

vmitchell85

Adapted from Marcel Pociot's: https://tinkerwell.app/shares/mpociot/2f51c001-f78a-481e-9ac7-352a9554b384

Data taken from: https://github.com/ExpDev07/coronavirus-tracker-api

Controller:

// Change this for your specific country
$countryCode = 'US';
$province = 'Ohio';

$confirmed = Http::get('https://coronavirus-tracker-api.herokuapp.com/confirmed')->json();
$confirmedData = collect($confirmed['locations'])
  	->where('country_code', $countryCode)
	->where('province', $province)
    ->first();

$chartData = [
	['Date', '# Confirmed']
];

collect($confirmedData['history'])->sortBy(function ($numConfirmed, $date) {
	return Carbon\Carbon::createFromFormat('n/j/y', $date)->startOfDay();
})->map(function ($numConfirmed, $date) use (&$chartData) {
  $chartData[] = [$date, $numConfirmed];
});

return view('__tinker__::tinker', [
  	'chartData' => $chartData,
	'country' => $confirmedData['country'],
  	'province' => $province
]);

View:

<body>
    <script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
      <script type="text/javascript">
        google.charts.load('current', {'packages': ['corechart']});
        google.charts.setOnLoadCallback(drawChart);
        function drawChart() {
            var data = google.visualization.arrayToDataTable(@json($chartData));
            var chart = new google.visualization.AreaChart(document.getElementById('chart'));
            chart.draw(data, {
                title: 'Corona cases in {{ $province }}',
                legend: {position: 'bottom'},
                width: '100%',
                height: '900'
            });
        }
    </script>
	<div style="height:100%;width:100%;" id="chart"></div>
</body>