corona.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. <?php
  2. ini_set('display_errors', 1);
  3. ini_set('display_startup_errors', 1);
  4. error_reporting(E_ALL);
  5. $_gitlink = 'https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_time_series/';
  6. $_files = [
  7. 'confirmed' => [
  8. 'url' => $_gitlink.'time_series_19-covid-Confirmed.csv',
  9. 'path' => 'data/covid_confirmed.csv'
  10. ],
  11. 'deaths' => [
  12. 'url' => $_gitlink.'time_series_19-covid-Deaths.csv',
  13. 'path' => 'data/covid_deaths.csv'
  14. ],
  15. 'recovered' => [
  16. 'url' => $_gitlink.'time_series_19-covid-Recovered.csv',
  17. 'path' => 'data/covid_recovered.csv'
  18. ]
  19. ];
  20. $_start_index = 40;
  21. if($_start_index < 4) { $_start_index = 4; }
  22. $sync_output = sync_data();
  23. $data = [];
  24. foreach($_files as $k => $v) {
  25. $ts = file_get_contents($v['path']);
  26. $ts = condition_csv($ts);
  27. $data[$k] = parse_ts_csv($ts);
  28. }
  29. //print_r($data,1);
  30. //$data['current'] = $data['confirmed'];
  31. $data['current'] = get_current_factors($data);
  32. function get_current_factors($data) {
  33. //Get numbers of people that were infected on each day.
  34. foreach($data['confirmed']['series'] as $country => $states) {
  35. foreach($states as $state => $d) {
  36. foreach($states[$state]['data'] as $k => $v) {
  37. $infected = (int) $data['confirmed']['series'][$country][$state]['data'][$k];
  38. $cured = (int) $data['recovered']['series'][$country][$state]['data'][$k];
  39. $dead = (int) $data['deaths']['series'][$country][$state]['data'][$k];
  40. $data['current']['series'][$country][$state]['data'][$k] = ($infected-($cured));
  41. }
  42. }
  43. }
  44. return $data['current'];
  45. }
  46. ?>
  47. <!DOCTYPE html>
  48. <html lang="en">
  49. <head>
  50. <meta charset="utf-8">
  51. <title>COVID-19</title>
  52. <meta name="description" content="Page Titile">
  53. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  54. <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no, user-scalable=no, minimal-ui">
  55. <!-- Call App Mode on ios devices -->
  56. <meta name="apple-mobile-web-app-capable" content="yes" />
  57. <!-- Remove Tap Highlight on Windows Phone IE -->
  58. <meta name="msapplication-tap-highlight" content="no">
  59. <!-- base css -->
  60. <link rel="stylesheet" media="screen, print" href="css/vendors.bundle.css">
  61. <link rel="stylesheet" media="screen, print" href="css/app.bundle.css">
  62. <!-- Place favicon.ico in the root directory -->
  63. <link rel="apple-touch-icon" sizes="180x180" href="img/eq2emu_labs_logo_180.png">
  64. <link rel="icon" type="image/png" sizes="32x32" href="img/favicon/eq2emu_labs_favico_32.png">
  65. <link rel="mask-icon" href="img/favicon/safari-pinned-tab.svg" color="#5bbad5">
  66. <link rel="stylesheet" href="css/animate.min.css">
  67. <link rel="stylesheet" href="css/fa-light.css">
  68. <link rel="stylesheet" href="css/fa-regular.css">
  69. <link rel="stylesheet" href="css/fa-solid.css">
  70. <link rel="stylesheet" href="css/fa-duotone.css">
  71. <link rel="stylesheet" href="css/fa-brands.css">
  72. <link rel="stylesheet" href="css/eq2emu.css">
  73. <link rel="stylesheet" media="screen, print" href="css/statistics/chartist/chartist.css">
  74. <script>
  75. var cvd = <?php echo json_encode($data); ?>;
  76. </script>
  77. <!--<link rel="stylesheet" media="screen, print" href="css/your_styles.css">-->
  78. <script src="js/vendors.bundle.js"></script>
  79. <script src="js/app.bundle.js"></script>
  80. <script src="js/eq2emu.js"></script>
  81. <script src="js/statistics/chartist/chartist.js"></script>
  82. <style>
  83. .ct-chart-line {
  84. overflow: visible;
  85. margin : 0 0 30px 0;
  86. }
  87. .ct-chart .ct-label.ct-horizontal.ct-end {
  88. position: relative;
  89. justify-content: flex-end;
  90. text-align: right;
  91. transform-origin: 100% 0;
  92. transform: translate(-100%) rotate(-45deg);
  93. white-space:nowrap;
  94. }
  95. </style>
  96. </head>
  97. <body class="mod-bg-1 ">
  98. <!-- BEGIN Page Wrapper -->
  99. <div class="page-wrapper">
  100. <div class="page-inner">
  101. <!-- BEGIN Left Aside -->
  102. <!-- END Left Aside -->
  103. <div class="page-content-wrapper">
  104. <div class="row">
  105. <div class="col-xl-3">
  106. <select id="country" class="form-control">
  107. </select>
  108. <label>Country</label>
  109. </div>
  110. <div class="col-xl-3">
  111. <select id="states" class="form-control">
  112. </select>
  113. <label>State/Province</label>
  114. </div>
  115. <div class="col-xl-3">
  116. <button id="load" class="btn btn-md btn-default">
  117. <i class="fa fa-bolt text-warning"></i>&nbsp;&nbsp;Run Chart
  118. </button>
  119. </div>
  120. <div class="col-xl-12"><h4><hr></hr></h4></div>
  121. <div class="col-xl-12">
  122. <div class="ct-chart nyc">
  123. </div>
  124. </div>
  125. </div>
  126. <div class="row">
  127. <div class="col-xl-4">
  128. <p id="tooltip">Test!</p>
  129. </div>
  130. <div class="col-xl-8">
  131. <?php
  132. //echo '<pre>'.$sync_output.'</pre>';
  133. //echo '<pre>'.print_r($data, 1).'</pre>';
  134. //echo '<pre>'.json_encode($data, JSON_PRETTY_PRINT).'</pre>';
  135. //echo '<pre>'.$ts.'</pre>';
  136. ?>
  137. </div>
  138. </div>
  139. <script>
  140. load_countries();
  141. function load_countries() {
  142. $.each(cvd.current.series, function(k, v) {
  143. if($('option[value="'+k+'"]').length < 1) {
  144. $('#country').append('<option value="'+k+'">'+k+'</option>');
  145. }
  146. });
  147. sort_options('.countries');
  148. $('#country').prepend('<option class="countries" value="US" selected="selected" disabled>- Select a Country -</option>');
  149. $('#country').prepend('<option class="countries" value="US">United States</option>');
  150. }
  151. function load_states(c) {
  152. var s = $('#states');
  153. s.empty();
  154. $.each(cvd.current.series[c], function(k, v) {
  155. s.append('<option class="states" value="'+k+'">'+k.replace('|', ', ')+'</option>');
  156. });
  157. sort_options('.states');
  158. }
  159. function sort_options(e) {
  160. var options = $(e);
  161. var arr = options.map(function(_, o) { return { t: $(o).text(), v: o.value }; }).get();
  162. arr.sort(function(o1, o2) { return o1.t > o2.t ? 1 : o1.t < o2.t ? -1 : 0; });
  163. options.each(function(i, o) {
  164. o.value = arr[i].v;
  165. $(o).text(arr[i].t);
  166. });
  167. }
  168. $(document).on('change', '#country', function() {
  169. var c = $(this, 'option:selected').val();
  170. load_states(c);
  171. });
  172. $(document).on('click', '#load', function() {
  173. var c = $('#country option:selected').val(), s = $('#states option:selected').val();
  174. load_chart(c,s);
  175. });
  176. function load_chart(c, s) {
  177. //chart.clear();
  178. chart = new Chartist.Line('.nyc', {
  179. labels: cvd.confirmed.labels,
  180. series: [
  181. cvd.current.series[c][s].data,
  182. cvd.deaths.series[c][s].data,
  183. cvd.recovered.series[c][s].data
  184. ]
  185. }, {
  186. height:'300px',
  187. fullWidth: true,
  188. chartPadding: { right: 40 },
  189. axisX: {
  190. labelInterpolationFnc: function(value, index) {
  191. return index % 2 === 0 ? value : null;
  192. }
  193. }
  194. });
  195. }
  196. var chart = new Chartist.Line('.nyc', {
  197. labels: cvd.confirmed.labels,
  198. series: [
  199. cvd.current.series["Italy"]['country']
  200. ]
  201. }, {
  202. height:'300px',
  203. fullWidth: true,
  204. chartPadding: { right: 40 },
  205. axisX: {
  206. labelInterpolationFnc: function(value, index) {
  207. return index % 2 === 0 ? value : null;
  208. }
  209. }
  210. });
  211. // Based on ty's comment
  212. chart.on('created', function(bar) {
  213. $('.ct-point').on('mouseover', function() {
  214. $('#tooltip').html('<b>Selected Value: </b>' + $(this).attr('ct:value'));
  215. });
  216. $('.ct-point').on('mouseout', function() {
  217. $('#tooltip').html('<b>Selected Value:</b>');
  218. });
  219. });
  220. </script>
  221. <!-- END Color profile -->
  222. </div>
  223. </div>
  224. </div>
  225. </body>
  226. </html>
  227. <?php
  228. function parse_ts_csv($s, $return = [ 'labels'=>[], 'series' => [] ]) {
  229. global $_start_index;
  230. $lines = explode("\n", $s);
  231. $hdr = explode(",",$lines[0]);
  232. foreach($hdr as $hk => $hv) {
  233. if($hk > $_start_index) {
  234. $return['labels'][] = $hv;
  235. }
  236. }
  237. foreach($lines as $row => $csv) {
  238. if($row > 0) {
  239. $l = explode(",",$csv);
  240. if(!isset($return['series'][$l[1]])) { $return['series'][$l[1]] = []; }
  241. if(strlen($l[0]) < 1) {
  242. $key = 'country';
  243. } else {
  244. $key = $l[0];
  245. }
  246. $return['series'][$l[1]][$key] = ['coords'=>[$l[2],$l[3]], 'data'=>[]];
  247. foreach($l as $k => $v) {
  248. if($k > $_start_index) {
  249. $return['series'][$l[1]][$key]['data'][] = $v;
  250. }
  251. }
  252. }
  253. }
  254. return $return;
  255. }
  256. function condition_csv($r) {
  257. return str_replace(['"', ', '], ['', '|'], $r);
  258. }
  259. function sync_data($return = NULL) {
  260. global $_files;
  261. $hours_til_sync = 6;
  262. foreach($_files as $k => $v) {
  263. $sync = false;
  264. if(file_exists($v['path'])) {
  265. $return .= $v['path'].' exists! Checking last update time.'.PHP_EOL;
  266. //time interval for deletion to occur...
  267. $x = (3600*$hours_til_sync); //6 hours
  268. //timestamp
  269. $current_time = time();
  270. //timestamp
  271. $file_creation_time = filemtime($v['path']);
  272. //extract difference
  273. $difference = $current_time - $file_creation_time;
  274. //if difference = $x...then delete file
  275. if ($difference >= $x) {
  276. $return .= $v['path'].' needs to be updated!'.PHP_EOL;
  277. unlink($v['path']);
  278. $sync = true;
  279. } else {
  280. $return .= $v['path'].' is still fresh. Skipping update!'.PHP_EOL;
  281. }
  282. } else {
  283. $sync = true;
  284. $return .= $v['path'].' does not exist. Downloading file!'.PHP_EOL;
  285. }
  286. if($sync) {
  287. $csv = file_get_contents($v['url']);
  288. if (!file_exists($v['path'])) { touch($v['path']); }
  289. file_put_contents($v['path'], $csv);
  290. $return .= $v['path'].' has been updated!'.PHP_EOL;
  291. }
  292. }
  293. return $return;
  294. }
  295. ?>