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

              
                <script src="https://d3js.org/d3.v5.min.js"></script>
<script src="http://unpkg.com/topojson@3/dist/topojson.min.js"></script>
<body>
  <div id="main-div">
    <button onclick='myFunction()' id="search-button">Search By State</button>
    <div id="nav-div"></div><hr id="horizontal-row">
    <div id="sum-data-element"></div><hr id="horizontal-row2">
    <div id="list-element"></div>
    <div id="district-element"></div>
  </div>
</body>
              
            
!

CSS

              
                .main-list{
  font-size : 20px;
}

#main-div{
  background : #242424;
  color : #edebeb;
}

.secondary-list{
  font-size : 16px;
  animation: 3s ease-out 0s 1 slideInFromLeft;
}

#nav-div{
  display : flex;
  flex-wrap : wrap;
  justify-content : flex-start;
  animation: 2s ease-in-out 0s 1 slideInFromTop;
}

#list-element{
  display : grid;
  grid-template-columns : 1fr 1fr;
  margin : 15px;
  text-align : center;
}

@media (max-width : 500px){
  #list-element{
  grid-template-columns : 1fr;
  }
}

#search-button{
  width : 100%;
  border-radius : 10px;
  background : #121212;
  color : white;
}

#search-button:hover{
  background : #424242;
}

a{
  color : white;
}

#horizontal-row{
  display : none;
  animation: 2s ease-out 0s 1 slideInFromLeft;
}

#horizontal-row2{
  animation: 2s ease-out 0s 1 slideInFromLeft;
}

@keyframes slideInFromLeft{
  0% {
    transform: translateX(-200%);
  }
  100% {
    transform: translateX(0);
  }
}

@keyframes slideInFromTop{
  0% {
    transform: translateY(-80%);
  }
  100% {
    transform: translateY(0);
  }
}

#sum-div{
  text-align : center;  
    animation: 3s ease-out 0s 1 slideInFromLeft;

}

              
            
!

JS

              
                let todaysData = [];
var x = document.getElementById("nav-div");

x.style.display = "none";
d3.json('https://api.covid19india.org/state_district_wise.json').then(data => {
    let totalConfirmed =0;
    let totalDeceased =0;
    let totalActive = 0;
    let totalRecovered = 0;
    let keys = Object.keys(data);
    let districtKeys = [];
    let subDistrictKeys = [];
    Object.keys(keys.map(d => districtKeys.push(data[d]['districtData'])));
    //keys.map(d => console.log(data[d]['districtData']));
    districtKeys.map(d => subDistrictKeys.push(Object.keys(d)));
    console.log(data[keys[1]]['districtData'][subDistrictKeys[1][1]]);
    for(let i=0; i<keys.length;i++){
      let confirmed =0;
      let deceased =0;
      let active = 0;
      let recovered = 0;
      let state = '';
      let temp = [];
      for(let j=0;j<subDistrictKeys[i].length;j++){
        active += +(data[keys[i]]['districtData'][subDistrictKeys[i][j]]['active']);
        deceased += +(data[keys[i]]['districtData'][subDistrictKeys[i][j]]['deceased']);
        confirmed += +(data[keys[i]]['districtData'][subDistrictKeys[i][j]]['confirmed']);
        recovered += +(data[keys[i]]['districtData'][subDistrictKeys[i][j]]['recovered']);
      }
      state = keys[i];
      todaysData.push({active : active , deceased : deceased, confirmed : confirmed, recovered : recovered , state : state});
      totalConfirmed += confirmed;
      totalDeceased += deceased;
      totalActive += active;
      totalRecovered += recovered;
    }
    console.log(todaysData);


  let index = 0;
  let sumElement = document.getElementById('sum-data-element');
  let listElement = document.getElementById('list-element');
  let navDiv = document.getElementById('nav-div');
  let districtElement = document.getElementById('district-element');
  
  let headConfirmed = document.createElement('UL');
  headConfirmed.appendChild(document.createTextNode('Total Confirmed : '+totalConfirmed));
  headConfirmed.appendChild(document.createElement('BR'));
  
  let headActive = document.createElement('UL');
  headActive.appendChild(document.createTextNode('Total Active : '+totalActive));
  headActive.appendChild(document.createElement('BR'));
  
  let headDeceased = document.createElement('UL');
  headDeceased.appendChild(document.createTextNode('Total Deceased : '+totalDeceased));
  headDeceased.appendChild(document.createElement('BR'));
  let headRecovered = document.createElement('UL');
  headRecovered.appendChild(document.createTextNode('Total Recovered : '+totalRecovered));
  headRecovered.appendChild(document.createElement('BR'));
  
  let sumDiv = document.createElement('DIV');
  sumDiv.id = "sum-div";
  
  sumDiv.appendChild(headConfirmed);
  sumDiv.appendChild(headActive);
  sumDiv.appendChild(headRecovered);
  sumDiv.appendChild(headDeceased);
  
  sumElement.appendChild(sumDiv);
  
  todaysData.forEach(d => {
      let listDiv = document.createElement('DIV');
      let node = document.createElement("LI"); 
      let textnode1 = document.createTextNode(keys[index]);
      let nodeDiv = document.createElement('DIV');
      let node2 = document.createElement("UL"); 
      let node3 = document.createElement("UL");
      let node4 = document.createElement("UL");
      let node5 = document.createElement("UL");

      let textNode2 = document.createTextNode(" Confirmed : "+d['confirmed']);
      let textNode3 = document.createTextNode(" Deaths : "+d['deceased']);
      let textNode4 = document.createTextNode(" Recovered : "+d['recovered']);
      let textNode5 = document.createTextNode(" Active : "+d['active']);

      
      node.className = "main-list";
      nodeDiv.className = "secondary-list";
      listDiv.id = d['state'];

      node.appendChild(textnode1);
      node2.appendChild(textNode2);
      node3.appendChild(textNode3);
      node4.appendChild(textNode4);
      node5.appendChild(textNode5);
      nodeDiv.appendChild(node2);
      nodeDiv.appendChild(node3);
      nodeDiv.appendChild(node4);
      nodeDiv.appendChild(node5);

      node.appendChild(nodeDiv);
      listDiv.appendChild(node);
      listDiv.appendChild(document.createElement('BR'));
      
      //Navigation
      let navnode = document.createElement('A');
      navnode.href=`#${d['state']}`;
      let navUl = document.createElement('UL');
      navUl.appendChild(document.createTextNode(`${d['state']}`));
      navnode.appendChild(navUl);
      navDiv.appendChild(navnode);

      listElement.appendChild(listDiv);
    index++;
    });

 

});

//console.log(listElement);

let index =0;

//d3.select('ul').data(todaysData).enter().append('text').text(d => d)

function myFunction() {
  let hr = document.getElementById('horizontal-row');
  if (x.style.display == "none") {
    x.style.display = "flex";
    hr.style.display = 'flex';
  } else {
    x.style.display = "none";
    hr.style.display = "none";
  }
}
              
            
!
999px

Console