When Art Meets Data: Flowers as Visual Metaphor
Tags: javascript, design, visualization, showdev
Published: 2020-07-05 14:55:27

When it comes to data visualization, we have grown accustomed to the good old charts and maps. Seldomly do we pause and realize that these are merely shapes, serving as visual metaphors to help us digest information with more ease.

So I was delighted to find [Shirley Wu's Film Flowers project](https://sxywu.com/filmflowers/), which uses flowers as a medium to visualize top summer blockbusters.

![filmflowers example](https://dev-to-uploads.s3.amazonaws.com/i/3h8fna3d5jkbe31lqg1c.png)

Granted, it does not seem as easily digestible as a simple chart would. From an artistic standpoint, though, it definitely stands out as an alternative approach to data visualization. 

Inspired by the brilliance of combining data with aesthetics, I decided to use the same visual metaphor for [my first d3 data visualization](https://baby-name-blossoms.netlify.app/).

## Same Dataset, Different Interpretations

Popular baby names have been one of my favorite annual reports. Naturally, the most common way to visualize such data would be bar charts.

![Top names bar chart](https://dev-to-uploads.s3.amazonaws.com/i/8pjv77ehvhr9czw2dnyy.png)

I also found this cool map, which leverages animated GIFs to display the most popular names over the years.

![Top girl names in map](https://dev-to-uploads.s3.amazonaws.com/i/d5vp2ro70qj25umrf37p.gif)

## Popular Baby Names, Reimagined as Flowers

Similar to Film Flowers, in my [Baby Name Blossoms](https://baby-name-blossoms.netlify.app/) project, I used petals to quantify data: the popularity of the names. 

![Baby Name Blossoms not colored](https://dev-to-uploads.s3.amazonaws.com/i/b09ewnu74c7krxj740u8.png)

The magic behind this visualization is D3's [quantize scale](https://github.com/d3/d3-scale/blob/master/README.md#quantize-scales), which allowed me to transform popularity (`d.count`) into the number of petals (`numPetalScale`):
```js
const countMinMax = d3.extent(data, d => d.count)
const numPetalScale = d3.scaleQuantize().domain(countMinMax).range([7, 10, 12, 15, 20])
```

Since the data I grabbed were the top 10 names for both genders, the values share more similarity than, say, the values of Film Flowers' IMDb votes, making it a bit challenging to show big variations between the names.

So, how might we add features that distinguish between them? What makes each name unique? While asking myself the very question, I noticed that Film Flowers use colors to display different genres for each movie. 

Perhaps I can use colors as well? How about taking the vowels and painting them accordingly?

![Baby Name Blossoms legend](https://dev-to-uploads.s3.amazonaws.com/i/95slv08erumofjrsq16j.png)

Here, I used D3's [ordinal scale](https://github.com/d3/d3-scale/blob/master/README.md#ordinal-scales) to connect vowels with a range of colors:
```js
const vowels = ['a', 'e', 'i', 'o', 'u', 'y']
const petalColors = d3.scaleOrdinal().range(['#E44F5D', '#F6B06E', '#EFCB64', '#F8765C', '#E5D35F', '#1DDCCA'])
  petalColors.domain(vowels)
```

Then, inside the function that converts each dataset into flower scale, I passed in a new object containing the vowels of each name:
```js
  const flowersData = _.map(data, d => {
    const numPetals = numPetalScale(d.count)
    const name = d.name
    const vowelGenres = name.split('').filter(char => vowels.includes(char.toLowerCase()))
    return {
      name,
      vowelGenres,
      petals: _.times(numPetals, i => {
        return {
          angle: 360 * i / numPetals,
          petalPath
        }
      })
    }
  })
```

And finally, I appended the circles(s) inside the flowers.

Thanks to the varying vowels in each name, now we can see more contrasts between individual names. The resulting visual effect was more than satisfactory :)

![Baby Name Blossoms colored1](https://dev-to-uploads.s3.amazonaws.com/i/pl87zaq9axeqi0gnq7kv.png)
![Baby Name Blossoms colored1.5](https://dev-to-uploads.s3.amazonaws.com/i/xskxelqnbrmz29bzzer4.png)

## Lessons Learned

As magical as d3.js can be, there were plenty of unexpected side effects that turned my little 3-day project into a weeklong one.

The biggest unexpected challenge for me was **layout**. 

I originally used flexbox for [Baby Name Blossoms](https://baby-name-blossoms.netlify.app/)' CSS structure. However, the SVG element didn't seem to work well with the responsive flex design.

As this is a React project, I also thought of leveraging any existing library, which led me to [react-svg-flexbox](https://github.com/zoopoetics/react-svg-flexbox). Unfortunately, it didn't deliver. To make matters worse, its babel-eslint version conflicted with the existing one in my project, causing the initial errors during deployment. 

Looking at the Film Flowers, which actually laid out specific widths for each section, I hastily reverted back to using relative/absolute positioning for this project.

Nonetheless, I feel proud of the end product and will keep exploring new ways to visualize information with an artistic mind. 

*Special thanks to @jessesbyers for also inspiring me with her amazingly built [COVID-19 World Tracker](https://github.com/jessesbyers/covid19_world_tracker_frontend/)!*