React and D3: Dynamic COVID-19 Visualizations (Part 1: World Map)
Tags: react, d3, codenewbie, visualization
Published: 2020-05-23 16:26:22

As a science teacher, I LOVE data visualizations. I love how a strong visualization combines the fields of art, science, mathematics, and humanities to tell a story, and I love the interactive and exploratory aspect of visualizations. However, in a lot of visualizations, the designer makes all/most of the decisions about what data to display, and how to display it to tell a specific story from a specific point of view. 

But as a teacher, I prefer the visualizations that allow the user to make more of these choices themselves, guided by their own questions, seeking answers and further inquiry. So as I started my journey in creating data visualizations, I knew I needed to make something that would render the data based on user input, and that sparked the concept for my current project-in-progress, the COVID-19 World Tracker.

![Imgur](https://i.imgur.com/VSivqho.png)

First, [check out a quick video demo](https://drive.google.com/file/d/12GCIO429NXrMwQIdbDXs9e1kyj6jMbch/view) and skim through the [README.md file](https://github.com/jessesbyers/covid19_world_tracker_frontend/blob/demo_5_23_20/README.md) for a quick tour of the project.

### Designing and Planning User Interaction Goals

My design choices were centered around this guiding question: 

>How can I display data and options that will allow the user to generate open-ended questions and freely investigate those questions to better understand the COVID-19 outbreak?

My goal was to develop a simple platform in which users could easily explore and compare the COVID-19 outbreak on a global scale, using real-time global data as well as looking at historical data throughout the outbreak. The map visualization portion of the app allows for quick analysis of current case data, and allows users to explore possible correlations between case numbers, severity of cases, and how the data looks when overall population is taken into account. The bar chart portion of the app allows users to compare the evolution of the outbreak from the first reported case in each country, through selecting a "collection" of countries for comparison. The two halves of the app operate together - ideally, after exploration of the global map, a user would be curious to dig deeper and compare certain countries based on what they saw in the map. 

The remainder of this post will explain how I combined the React.js user interface with D3.js to achieve this goal, allowing users to be more in control of the data they explore, the questions they ask, and the understandings they walk away with. This post will focus on the map visualization, while my next post will focus on the bar charts.

### Exploring Real-Time Global Data Using the World Map Visualization
![Imgur](https://i.imgur.com/rkq732f.png)

#### The Available Data

The map data was fetched from the [Novel COVID API](https://corona.lmao.ninja/). I chose this API because it pulls data from a variety of reputable sources, and includes consistent current data for every country. Here is an example of the data entry for Afghanistan:
![Imgur](https://i.imgur.com/xAUDgai.png)

#### Creating Options Based on Available Data

First, this data is fetched using the useEffect() hook in the MapInput component (React.js). 
![Imgur](https://i.imgur.com/Y15IpkB.png)

I used the keys for each of the data points (cases, deaths, population, etc) to organize a series of user input buttons in the MapInput component. The component renders a series of buttons, which when clicked, call the DisplayMapData() function (D3.js) with arguments related to the specific data the user wants to display on the map. For example:

```jsx
<button 
    onClick={event => DisplayMapData(event.target.value, event.target.innerText, countryResults)} 
    className="cases block" 
    value="cases">
        Total Cases
</button>
```

With a little bit of css button styling and react-bootstrap Row-Col-Card formatting, I created the left sidebar menu of options:
![Imgur](https://i.imgur.com/xOK8DTL.png)

#### Visualizing the Data Selected by the User
From here, the DisplayMapData(caseType, caseTitle, data) function uses the arguments passed down to parse the data by the user input (caseType) to plot only that selected data on the map, using each country's geographic coordinates supplied by the countryInfo object within the data object. This function first removes any data related to the last selection (country circles, the legend, and the title), and then renders that information for the most recently-selected caseType. 

[You can view the code for the DisplayMapData() function in my repository](https://github.com/jessesbyers/covid19_world_tracker_frontend/blob/demo_5_23_20/src/d3/DisplayMapData.js)

#### Adding Additional Interactive Features 

Finally, I built in additional interactions so that the user could further explore the map and the data. This included: 

- Zooming and Panning the map view
- Highlighting country boundaries and displaying country names  
- Displaying country data when hovering over each data circle

### Comparing Country-Specific Historical Data Using the Bar Chart Visualizations
![Imgur](https://i.imgur.com/RDcbDAU.png)

*Keep an eye out for Part 2 of this post, describing the Bar Chart Portion of the App.*

### Final Thoughts
As a novice programmer, this project has forced me to dramatically grow my skill set in the past two weeks. I have a lot more to learn about D3.js. I plan to continue practicing making different types of visualizations with complex data sets, while always keeping the user experience and opportunities for self-directed inquiry at the forefront of my designs.

This project is by no means complete, and I will share a link to the live project in a few weeks. In the meantime, keep an eye out for my weekly updates to see how the project evolves. 

And if you made it this far - Thank you for reading! 

*Leave a comment below and let me know:* 
- *What kind of user experience would you like to see in visualizations like this?* 
- *What additional features or interactions should be added?*
- *Is there other COVID-19 data you would like to explore in this way?*