PatoCh

Coronavirus deaths / cases ratio

Mar 20th, 2020 (edited)
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
jQuery 3.76 KB | None | 0 0
  1.         function round_prct(number) {
  2.             return Math.round((number + Number.EPSILON) * 10000) / 100;
  3.         }
  4.        
  5.         function round_2d(number) {
  6.             return Math.round((number + Number.EPSILON) * 100) / 100;
  7.         }
  8.  
  9.         function get_number(cells, position) {
  10.             return cells.eq(position).text().replaceAll(",","");
  11.         }
  12.  
  13.         function build_cell(data_n, display_n, processing_func) {
  14.             var red_color = ((data_n <= 0 || isNaN(data_n)) ? 0 : processing_func(data_n));
  15.             var bg_color, black_text;
  16.             if (data_n < 0 || isNaN(data_n)) {
  17.                 bg_color = "white";
  18.                 black_text = true;
  19.             } else {
  20.                 bg_color = "rgb(" + red_color + ", " + (255 - red_color) + ", 0)";
  21.                 black_text = red_color > 210 || red_color < 80;
  22.             }
  23.             var text_color = black_text ? "black" : "white";
  24.             return '<td style="font-weight: bold; text-align:right; background-color:' + bg_color + '; color: ' + text_color + '">' + display_n + '</td>';
  25.         }
  26.  
  27.         function log(number) {
  28.             return Math.min((Math.log10(number) + 2) * 255);
  29.         }
  30.  
  31.         function arctan(number) {
  32.             return 510 * Math.atan(number) / Math.PI;
  33.         }
  34.        
  35.         function active_fun(number) {
  36.             if (number > 20000)
  37.                 return 255;
  38.             if (number > 10000)
  39.                 return 200;
  40.             if (number > 5000)
  41.                 return 150;
  42.             if (number > 1000)
  43.                 return 120;
  44.             if (number > 100)
  45.                 return 50;
  46.             return 0;
  47.         }
  48.        
  49.         function new_fun(number) {
  50.             if (number > 500)
  51.                 return 255;
  52.             if (number > 200)
  53.                 return 200;
  54.             if (number > 100)
  55.                 return 150;
  56.             if (number > 50)
  57.                 return 120;
  58.             if (number > 10)
  59.                 return 50;
  60.             return 0;
  61.         }
  62.  
  63.  
  64.         var countries_rows = $("#main_table_countries_yesterday tr");
  65.         var size = countries_rows.length;
  66.  
  67.         countries_rows.each(function(i, e) {
  68.             if (i == 0) {
  69.                 $(this).append('<th class="sorting">Deaths/ cases</th>');
  70.                 //$(this).append('<th class="sorting">Deaths/ 1M pop </th>');
  71.                 $(this).append('<th class="sorting">Active/ 1M pop </th>');
  72.                 $(this).append('<th class="sorting">New/ 1M pop </th>');
  73.             } else if (i == size - 1) {
  74.                 $(this).append('<td></td>');
  75.             } else {
  76.                 var cells = $(this).children("td");
  77.  
  78.                 var cases_total = get_number(cells, 2);
  79.                 var deaths = get_number(cells, 4);
  80.                 var cases_per_cap = get_number(cells, 9);
  81.                 var population = get_number(cells, 13);
  82.                 var active = get_number(cells, 7);
  83.                 var new_cases = get_number(cells, 3);
  84.                 var new_deaths = get_number(cells, 5);
  85.  
  86.                 var deaths_to_cases = deaths / cases_total;
  87.                 var deaths_to_pop = (deaths * cases_per_cap) / cases_total;
  88.                 var active_to_pop = active * (10 ** 6) / population;
  89.                 var new_to_pop = new_cases * (10 ** 6) / population;
  90.                 var new_deaths_to_pop = new_deaths * (10 ** 6) / population;
  91.                 var rounded_dtc = round_prct(deaths_to_cases);
  92.                 var rounded_dtp = round_2d(deaths_to_pop);
  93.                 var rounded_atp = round_2d(active_to_pop);
  94.                 var rounded_ntp = round_2d(new_to_pop);
  95.                 var rounded_ndtp = round_2d(new_deaths_to_pop);
  96.  
  97.                 if (deaths != "") {
  98.                     $(this).append(build_cell(deaths_to_cases, rounded_dtc + '%', log));
  99.                 } else {
  100.                     $(this).append(build_cell(-1, "N/A"));
  101.                 }
  102.                 //$(this).append(build_cell(deaths_to_pop, rounded_dtp, arctan));
  103.                 if (!isNaN(active) && active != "" && population > 0) {
  104.                     $(this).append(build_cell(active_to_pop, rounded_atp, active_fun));
  105.                 } else {
  106.                     $(this).append(build_cell(-1, "N/A"));
  107.                 }
  108.                 if (!isNaN(new_cases) && new_cases != "" && population > 0) {
  109.                     $(this).append(build_cell(new_to_pop, rounded_ntp, new_fun));
  110.                 } else {
  111.                     $(this).append(build_cell(-1, "N/A"));
  112.                 }
  113.                
  114.                 if (!isNaN(new_deaths) && new_deaths != "" && population > 0) {
  115.                     $(this).append(build_cell(new_deaths_to_pop, rounded_ndtp, new_fun));
  116.                 } else {
  117.                     $(this).append(build_cell(-1, "N/A"));
  118.                 }
  119.             }
  120.         });
Add Comment
Please, Sign In to add comment