Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using it's URL and the proper URL extention.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <div class="container-fluid">
<div class="row">
  <div class="col-10">
<canvas id="myChart" width="200" height="90"></canvas>
  </div>
  <div class="col-2 ">
<!-- https://cdnjs.com/libraries/chosen -->
<div>
<!--select id="date"></select-->
  <br />
  <div>
<input type="button" id="btnLoad" value="load"/>
</div>
<select id="countries" multiple="" data-placeholder="select countries" class="select chosen-select"></select>
  <div class="card">
    <div class="card-body">
<label>
<input type="radio" id="cases" name="source" checked>&nbsp;Cases
</label><br />
<label>
  <input type="radio" id="deaths" name="source">&nbsp;Deaths
</label>
    </div>
  </div>
  <div class="card"><div class="card-body">
  <label><input type="radio" id="count" name="agg" checked>&nbsp;Count per day</label><br />
  <label><input type="radio" id="total" name="agg" >&nbsp;Total</label><br />
  <label><input type="radio" id="deltapct" name="agg" >&nbsp;Change%&nbsp;per&nbsp;day</label>
  <select id="deltapctdays" style="display: none">
    <option value="1" selected>1 day</option>
        <option value="2">2 days</option>
        <option value="3">3 days</option>
        <option value="4">4 days</option>
        <option value="5">5 days</option>
        <option value="6">6 days</option>
        <option value="7">7 days</option>
  </select>
    </div></div>
  <div class="card"><div class="card-body">
    <select id="range">
      <option value="0" selected>all</option>
      <option value="7">last 7 days</option>
      <option value="14">last 14 days</option>
      <option value="30">last 30 days</option>
      <option value="60">last 60 days</option>
    </select>
    </div></div>
</div>
  </div>
</div>
</div>
              
            
!

CSS

              
                #countries {
  width: 100%;
}

.chosen-container {
  width: 100%;
}
              
            
!

JS

              
                
$(function() {
  $(".chosen-select").chosen();
  $(".chosen-select").chosen().change(countriesChanged);
  $("input[type=radio]").click(countriesChanged);
  $("input[name=agg]").click(function() {
    if ($("#deltapct").is(":checked"))
    $("#deltapctdays").show();
    else
      $("#deltapctdays").hide();
  });
  $("#deltapctdays").change(countriesChanged);
  $("#range").change(countriesChanged);
  /*var date = moment();
console.log(date.format("YYYY-MM-DD"));
  console.log(date.year());

var $sel = $("#date");
while(date.year() >= 2020) {
//console.log(date.format("YYYY-MM-DD"));
  $sel.append($("<option>", { value: date.format("YYYY-MM-DD"), text: date.format("YYYY-MM-DD") }));
  date = date.subtract(1, "days");
}*/
});

$("#btnLoad").click(function() {
  var date = $("#date option:selected").val();
var url = "https://static.devio.at/covid-19-latest.csv";
//var url = urlBase + date + ".xlsx";
  
  // https://docs.sheetjs.com/ 
  
  /* set up async GET request *
var req = new XMLHttpRequest();
req.open("GET", url, true);
req.responseType = "arraybuffer";
  //req.setRequestHeader('Range', 'bytes=0-253572');

req.onload = function(e) {
  console.log("onload " + req.readyState);
  console.log("responsetype " + req.responseType + " length " + req.response.byteLength);
  console.log(req.response);
  console.log("type " + e.type + " loaded " + e.loaded);
  var data = new Uint8Array(req.response);
  console.log("ui8 length " + data.length);
  var workbook = XLSX.read(data, {type:"array"});

  // DO SOMETHING WITH workbook HERE 
  console.log(JSON.stringify(workbook.SheetNames));
  console.log(JSON.stringify(workbook.Sheets[workbook.SheetNames[0]]));
  
}

req.send();
});
/*/
$.ajax(
   url,
  { 
    type: "GET",
    dataType: "text",
    success: 
   function(data) {
     //console.log(data);
    console.log(data.length + " characters");
     
     // https://stackoverflow.com/questions/7431268/how-to-read-data-from-csv-file-using-javascript
     processData(data);
     
  }}
);
});
                    
var dCountries = null;

//*/
// https://stackoverflow.com/questions/7431268/how-to-read-data-from-csv-file-using-javascript
function processData(allText) {
    //var record_num = 5;  // or however many elements there are in each row
    var allTextLines = allText.split(/\r\n|\n/);
    var headings = allTextLines[0].split(',');
  
  console.log(JSON.stringify(headings));

   var iDateRep = headings.indexOf("DateRep");
   var iDay = headings.indexOf("Day");
   var iMonth = headings.indexOf("Month");
   var iYear = headings.indexOf("Year");
   var iCases = headings.indexOf("Cases");
   var iDeaths = headings.indexOf("Deaths");
   var iCountry = headings.indexOf("Countries and territories");
  var iGeoId = headings.indexOf("GeoId"); 
    
  //console.log("iGeoId " + iGeoId);
  
    //var lines = [];

    dCountries = {
      meta: { minDate: new Date(2019, 11, 31),
            maxDate: new Date(2019, 11, 31)}
    };  // by GeoId
  
  var totalCases = 0, totalDeaths = 0;
  
    for (var i=1; i<allTextLines.length; i++) {
        var data = allTextLines[i].split(',');
        if (data.length == headings.length) {

          var geoId = data[iGeoId];
          if (!dCountries[geoId]) {
            dCountries[geoId] = {
              geoId : geoId,
              name: data[iCountry],
              dates : {}
          };        
            totalCases = 0; totalDeaths = 0;
        }
      
      var c = dCountries[geoId];
      
          var dt = new Date(data[iYear], data[iMonth]-1, data[iDay]);
          
       var d = {
              dateRep : data[iDateRep],
              day : data[iDay],
               month: data[iMonth],
                year : data[iYear],
                date : dt,
                cases : parseInt(data[iCases]),
                deaths : parseInt(data[iDeaths]),
                changePctCases: [0],
          changePctDeaths: [0]
            };
          totalCases += d.cases;
          totalDeaths += d.deaths;
          
          d.totalCases = totalCases;
          d.totalDeaths = totalDeaths;
          
          c.dates[data[iDateRep]] = d;
          if (dCountries.meta.maxDate < dt) dCountries.meta.maxDate = dt;
          };
        }
  
  console.log(Object.keys(dCountries).length + " countries");

    for(var key in dCountries) {
    if (key=="meta") continue;

      var c= dCountries[key];
      var totalCases = 0;
      var totalDeaths = 0;
      
  for(var dt = dCountries.meta.minDate; dt <= dCountries.meta.maxDate; dt = dt.addDays(1)) {
    //console.log(moment(dt).format("YYYY-MM-DD"));
      var idate = moment(dt).format("DD/MM/YYYY");
      //console.log(idate);
      var val;
    if (c.dates[idate]) {
          totalCases += c.dates[idate].cases;
      totalDeaths += c.dates[idate].deaths;
      
      c.dates[idate].totalCases = totalCases;
      c.dates[idate].totalDeaths = totalDeaths;
      } else {
       c.dates[idate] = {
         totalCases: totalCases,
         totalDeaths: totalDeaths     ,
 changePctCases: [0],
          changePctDeaths: [0]     
       };
      }
    
    // calc %
    if (c.dates[idate].totalCases || c.dates[idate].totalDeaths) {
    for(var i=1; i<8; i++) {
      var pdate = moment(dt.addDays(-i)).format("DD/MM/YYYY");
      if (c.dates[pdate]) {
//                changePctCases: [0],
//          changePctDeaths: [0]
        if (c.dates[pdate].totalCases) {
        c.dates[idate].changePctCases.push(
          Math.pow((c.dates[idate].totalCases / c.dates[pdate].totalCases) , 1 / i));
        
        } else {
                  c.dates[idate].changePctCases.push(null);
        }
        if (c.dates[pdate].totalDeaths) {
        c.dates[idate].changePctDeaths.push(
          Math.pow((c.dates[idate].totalDeaths / c.dates[pdate].totalDeaths) , 1 / i));
        
        } else {
          c.dates[idate].changePctDeaths.push(null);
        }
      }
    }
  }
    }
    }
 
  $("#countries").empty();
  
  //console.log(JSON.stringify(Object.keys(dCountries)));
  for(var key in dCountries) {
    if (key=="meta") continue;
    //console.log(key);
    $("#countries").append($("<option>", { text: dCountries[key]["name"], value: key,
selected: ["AT", "IT", "US", "CN"].indexOf(key) > -1 }));
  }
  $("#countries").trigger("chosen:updated");
  $("#countries").trigger("change");
}

// https://stackoverflow.com/questions/4413590/javascript-get-array-of-dates-between-2-dates
Date.prototype.addDays = function(days) {
    var date = new Date(this.valueOf());
    date.setDate(date.getDate() + days);
    return date;
}

// https://stackoverflow.com/questions/25594478/different-color-for-each-bar-in-a-bar-chart-chartjs
function getRandomColor() {
    var letters = '0123456789ABCDEF'.split('');
    var color = '#';
    for (var i = 0; i < 6; i++ ) {
        color += letters[Math.floor(Math.random() * 16)];
    }
    return color;
}
// https://harvesthq.github.io/chosen/
// https://harvesthq.github.io/chosen/options.html

var myLineChart;
// https://personal.sron.nl/~pault/
var lineColors = ["#4477AA", "#66CCEE", "#228833", "#CCBB44", "#EE6677", "#AA3377", "#BBBBBB",
"#EE3377", "#009988", "#EE7733", "#CC3311", "#BB5566", "#DDAA33", "#004488", "#000000"];

function countriesChanged() {
  if (!dCountries) return;
  
    var range = parseInt($("#range option:selected").val());

    var minDate = range == 0 ? dCountries.meta.minDate : dCountries.meta.maxDate.addDays(1-range);

  var ctx = document.getElementById('myChart');
  var dates = [];
  //console.log(dCountries.meta.minDate);
  for(var dt = minDate; dt <= dCountries.meta.maxDate; dt = dt.addDays(1)) {
    //console.log(moment(dt).format("YYYY-MM-DD"));
    dates.push(moment(dt).format("YYYY-MM-DD"));
  }
  
  var selectedCountries = $("#countries option:selected").map(function(i,e){return e.value; });
  var source = $("input[name=source]:checked")[0].id;
                 // "cases", "deaths"
  var aggMode = $("input[name=agg]:checked")[0].id;
  // "count", "total", "deltapct";
  var deltaDays = parseInt( $("#deltapctdays option:selected").val());
  
  console.log("source " + source + " aggMode " + aggMode + " range " + range);
  
  var datasets = [];
  for(var ic = 0; ic < selectedCountries.length; ic++) {
    var geoId = selectedCountries[ic];
    var dsdata = [];
    var c = dCountries[geoId];
    //var total = 0;
    //console.log(geoId + ": " + JSON.stringify(c));
    for(var dt = minDate; dt <= dCountries.meta.maxDate; dt = dt.addDays(1)) {
    //console.log(moment(dt).format("YYYY-MM-DD"));
      var idate = moment(dt).format("DD/MM/YYYY");
      //console.log(idate);
      var val;
      switch(aggMode) {
        case "count":
          val = c.dates[idate] ? (source=="cases" ? c.dates[idate].cases : c.dates[idate].deaths) : null;
          break;
        case "total":
          //total += c.dates[idate] ? (source=="cases" ? c.dates[idate].cases : c.dates[idate].deaths) : 0;
          //val = total;
          val = c.dates[idate] ? (source=="cases" ? c.dates[idate].totalCases : c.dates[idate].totalDeaths) : null;
          break;
          
        case "deltapct":
          val = c.dates[idate] ? ((source=="cases" ? c.dates[idate].changePctCases[deltaDays] : c.dates[idate].changePctDeaths[deltaDays]) )  : null;
          if (val) val = (val-1) * 100;
          break;
      }
      dsdata.push(val);
  }
    datasets.push({ label: geoId, data: dsdata, borderColor: lineColors[datasets.length % lineColors.length], // getRandomColor(),

                   fill: false
                  });
    console.log(JSON.stringify({ label: geoId, data: dsdata }));
  }
  
  if (myLineChart) {
    myLineChart.data.datasets = datasets;
    myLineChart.data.labels = dates;
    myLineChart.update();
  } else {
  myLineChart = new Chart(ctx, {
    type: 'line',
    data: {
      labels: dates,
      datasets: datasets
    },
    options: {
}});
  }
}
              
            
!
999px

Console