hostmonster-Host Unlimited Domains on 1 Account   coolhandle offering reliable webhosting since 2001
Unlimited Hosting Space - FREE Site Builder   Smart Website Solutions for Your Small Business=

Fun With Physics In Data Visualization

Fun With Physics In Data Visualization

Data visualization is on the rise. Publishers around the world — individual bloggers and major online publications alike — are realizing that charts, maps and combinations of the two can convey a message far more effectively than plain numbers can. From simple charts to fancy infographics to complex timeline animations, data visualizations are popping up all over the Internet. However, as in any other area, once everyone gets on the train, distinguishing yourself from the pack becomes hard.

Read on to find out how a physics engine can really set your efforts apart.

You don’t need to look far for examples of data visualization. Take Google Analytics’ real-time view, which places bubbles of various sizes over top a map to illustrate quantitative information:

Sample bubble map.1
A sample bubble map (Image: Google Analytics2) (View large version3)

Or take the “Map of the Oil World4” done by New York Times (which, by the way, does a lot of nice data visualizations5). That map does the job, but I’ll bet most of you have come across fancier maps, ones in which the bubbles never overlap to form a distorted map and where the bigger bubbles nudge smaller ones out of place, as in the map below, also by the New York Times:

Sample bubble map.6
Another bubble map (Image: New York Times7) (View large version8)

Maps like this are especially good when relatively few countries have assigned values — not all countries win a medal in the Olympics, after all. If the bubbles were to be placed directly over their respective countries, they would overlap and the result would not be as visually pleasing.

Having worked on the data visualization library amCharts9 for years, I’m always looking out for ways to help users visualize data in ways that are creative and aesthetically pleasing and that effectively convey their message. Maps like the ones discussed in this article are surely good means of relaying quantitative geographical information. Read on to find out how to make this happen.

The default features of our mapping library — JavaScript Maps — enable you to generate a standard bubble map quite easily:

See the Pen Map with Bubbles10 by amCharts (@amcharts2621181511) on CodePen2722191612.

The map above already looks nice and implementing it is very easy, but overlapping is still an issue. Let’s solve that.

I could try to write my own script to detect and resolve collisions (not an easy task). From my own experience in game development, I know that all physics simulation libraries have collision detection built in. Let’s use one of those.

But why stop there? Because we’re tapping into the power of these libraries, we can use their other features, too. Let’s make our charts animate with lifelike motion for a truly impressive result that will undoubtedly capture the user’s attention.

A bunch of ready-made JavaScript physics libraries are available. I’ve chosen probably the most famous one — one of the JavaScript ports of Box2D, box2dweb13. The idea is simple. We have a map with bubbles, and we have a box2d world (made invisible to the user) of the same size with the same bubbles replicated in it.

We’ll use the (invisible) box2d world to simulate bubble movement and interaction (such as collision detection and other stuff), to monitor the bubbles’ positions and to dynamically modify those positions on the map. I won’t get into the technical details — you can view the source code if you want. To prevent the bubbles from falling, we simply set the gravity to 0. And here is result:

See the Pen Bubbles instead of countries + Gravity14 by amCharts (@amcharts2621181511) on CodePen2722191612.

Great, isn’t it? Now, I’m thinking to change the size of some bubbles at runtime and let the other bubbles move accordingly. To do that, we have to scale the box2d world’s bubble and the map bubble together and simply observe what happens. After some testing, I see that this might work by increasing the sizes of bubbles, but not by reducing them — bubbles that are displaced before other big bubbles are reduced in size do not return back:

See the Pen Bubbles instead of countries + change size17 by amCharts (@amcharts2621181511) on CodePen2722191612.

We need a more complex solution. After some experimenting, I came to this:

  1. Create a static object in the middle of each country that always stay in position.
  2. Make this object a “sensor,” so that other objects don’t collide into it.
  3. Create a joint between the fixed sensor and bubble, and adjust the properties of this joint to allow for some movement but while always being attracted to the position of the sensor.

That works nicely:

See the Pen Bubbles instead of countries + working resize20 by amCharts (@amcharts2621181511) on CodePen2722191612.

Just to show what can be done with a physics engine, below is another demo we made, a map of worldwide coffee consumption. Before you ask, we completely made up the data.

Our technical approach for this map is quite the same as the one for the last demo above. The only difference is that the bubbles initially are not joined to their destined locations, but rather are placed in the middle of the picture, just above the coffee pot. The coffee cup is made up of three rectangles in box2d. If you uncomment all of the lines about “debug draw” (two places in the JavaScript source and in the canvas element in HTML), you could get a box2d debug-draw view (very helpful when doing a job like this):

Debug wireframes for physics engine.23
A wireframe for debugging physics in our imaginary board (View large version24)

Awesome, isn’t it? And it’s only about 500 lines of code (not counting the data).

See the Pen Map+Physics+Coffee25 by amCharts (@amcharts2621181511) on CodePen2722191612.

Conclusion

Physics engines are not just for games and specialized simulations. They enable us to add stunning effects and, with just a few lines of code, to solve geometry problems that would otherwise have taken us weeks if not months to build on our own.

We’ve focused here on the box2d engine, which is robust and powerful, but it might get a bit slow on slower devices. Many engines are available. We encourage you to try some out to see which one clicks with you or is best suited to the task at hand. If you just need to solve a problem of overlapping, for example, then a lightweight option might do the trick just as well, and with a far smaller footprint and much lower processing overhead.

There is virtually no limit to what you can do with the physics of data visualizations. Create column charts and make them fall like dominos, or make pie charts roll, bounce and more. You can make objects in charts and maps roll, spin, bounce, change shape and morph in ways that will capture the viewer’s attention much more quickly than regular static or even interactive versions.

You will find more fancy examples and tools for data visualization on the amCharts28 website.

(rb, il, al)

Footnotes

  1. 1 http://www.smashingmagazine.com/wp-content/uploads/2015/03/Google-analytics-large-preview-opt.jpg
  2. 2 http://www.google.com/analytics/
  3. 3 http://www.smashingmagazine.com/wp-content/uploads/2015/03/Google-analytics-large-preview-opt.jpg
  4. 4 http://www.nytimes.com/interactive/2007/11/06/business/20071107_WINNERSLOSERS_GRAPHIC.html
  5. 5 http://www.nytimes.com/newsgraphics/2013/12/30/year-in-interactive-storytelling/
  6. 6 http://www.smashingmagazine.com/wp-content/uploads/2015/03/london-olympics-large-preview-opt.png
  7. 7 http://london2012.nytimes.com/results
  8. 8 http://www.smashingmagazine.com/wp-content/uploads/2015/03/london-olympics-large-preview-opt.png
  9. 9 http://www.amcharts.com/
  10. 10 ‘http://codepen.io/amcharts/pen/XJjGRM/’
  11. 11 ‘http://codepen.io/amcharts’
  12. 12 ‘http://codepen.io’
  13. 13 https://code.google.com/p/box2dweb/
  14. 14 ‘http://codepen.io/amcharts/pen/MYOWGr/’
  15. 15 ‘http://codepen.io/amcharts’
  16. 16 ‘http://codepen.io’
  17. 17 ‘http://codepen.io/amcharts/pen/RNjweN/’
  18. 18 ‘http://codepen.io/amcharts’
  19. 19 ‘http://codepen.io’
  20. 20 ‘http://codepen.io/amcharts/pen/yyPyQw/’
  21. 21 ‘http://codepen.io/amcharts’
  22. 22 ‘http://codepen.io’
  23. 23 http://www.smashingmagazine.com/wp-content/uploads/2015/03/box2d-large-preview-opt.png
  24. 24 http://www.smashingmagazine.com/wp-content/uploads/2015/03/box2d-large-preview-opt.png
  25. 25 ‘http://codepen.io/amcharts/pen/RNxrqo/’
  26. 26 ‘http://codepen.io/amcharts’
  27. 27 ‘http://codepen.io’
  28. 28 http://www.amcharts.com

↑ Back to top
Tweet itShare on Facebook

Article source: http://www.smashingmagazine.com/2015/03/09/fun-with-physics-in-data-visualization/

Tags:


Submit a Review




If you want a picture to show with your review, go get a Gravatar.

1&1 has shared hosting and dedicated hosting solutions for every budget and free domains with all hosting packages!  StartLogic - Affordable hosting: Free setup/domain, unlimited emails, PHP, mySQL, CGI, FrontPage. As low as $3.95/month
Cloud ecommerce platform delivers more traffic, higher conversion and unmatched performance

© Copyright 2008 Tyconia International, Inc. All Rights Reserved.