Data Visualization

“Data visualization is both an art and a science”

Presenter:Chi-Hsuan, Huang @ Netdb.NCKU

What is data visualization?

“Transformation of the symbolic into the geometric”
[McCormick et al. 1987]
Video Link

Why Data Visualization?


About 70% of all the sensory receptors in the body are in the eyes.
And raw data is usually not good for human-readable.
(JSON、CSV、XML)
|Table | vs | Chart |---|---|---| |Specific||General| |Measure、Calculate||Patterns、Relationships| |More Machine-readable|| More Human-readable
Besides, visualization is more...

The Goal of Data Visualization

A primary goal of data visualization is to communicate information clearly and efficiently to users.

Effective visualization also helps
users in analyzing and reasoning about data.

and makes complex data more
accessible, understandable and usable.

Turn data into information, and information into insight.

--Carly Fiorina, Former CEO of HP
At its best, data visualization is expert storytelling.

How to Creating a Visualization?

# Your Purpose? (questions、hypotheses、audience)
# Your data? (categorical, ordinal and interval)
# Domain Knowledge?

Which Chart?

# There are many tools - Excel - R - Python - ..., etc
Why Web?

After creating visualizations,
should be notice that does it convey the data honestly?

D3.js

Data-Driven Documents
D3.js is a JavaScript library for manipulating documents based on data.

Quora:How do I learn D3.js?

HTML


              <html>
              <head>
                
              </head>
              <body>
                Hello, world!
                
              </body>
              </html>
            

CSS


              <style>
                body {
                  background-color: black;
                }
              </style>
            

Javascript


              <script>
                // Array
                var arr = [1, 2, 3, 4, 5];

                // Object
                var obj = {
                  key: 'value'
                };

                // Also have if control, for loop...
                for (var i = 0; i < 10; ++i) {}

                // Print
                console.log(arr);
              </script>
            
A web server is required when loading external data (e.g., d3.csv). ```python > python -m SimpleHTTPServer 8888 & ```
#D3 Basic - SVG - Binding Data - Scales - Axes - Time - Transitions - Layouts - Geomapping

SVG - Three Little Circles


            
              
              
              
            
            
So, how can we bind data to elements in the DOM?
Data ←→ DOM

Selection

With one element selected, adds one element.

              var svg = d3.select("body").append("svg");
            
Set some attributes and styles

            d3.selectAll("circle")
              .attr("cx", 20)
              .attr("cy", 12)
              .attr("r", 24)
              .style("fill", "red");
            

Binding Data

Data are Arrays


            var data = [1, 1, 2, 3, 5, 8];
            

Concept of Data Join


            var svg = d3.select('#circles');
            var circle = svg.selectAll("circle")
            .data([32, 57, 293]);

            circle.enter().append("circle")
                .attr("cy", 60)
                .attr("cx", function(d, i) { return i * 100 + 30; })
                .attr("r", function(d) { return Math.sqrt(d); })
                .style("fill", "white");
            
The data method computes the join, defining enter and exit.

            var circle = svg.selectAll("circle")
              data([32, 57, 293], function(d) { return d; });
            
Appending to the enter selection creates the missing elements.

            circle.enter().append("circle");
            

Enter

DataElements

Key Function

Data

          var data = [
            {name: "Alice", x: 10.0, y: 9.14},
            {name:   "Bob", x:  8.0, y: 8.14},
            {name: "Carol", x: 13.0, y: 8.74},
            {name:  "Dave", x:  9.0, y: 8.77},
            {name: "Edith", x: 11.0, y: 9.26}
          ];
          

         function key(d) { return d.name; }

         var circle = svg.selectAll("circle")
            .data(data, key)
            .attr("cx", x)
            .attr("cy", y)
            .attr("r", 2.5);
         

Loading Data

  • csv -> d3.csv
  • json -> d3.json

            d3.csv("stocks.csv", function(stocks) {
              // your code
            });
          

Scale

Scale


         var x = d3.scale.linear()
          .domain([12, 24])
          .range([0, 720]);
         

Axes


          var yAxis = d3.svg.axis()
            .scale(y)
            .orient("left");
          
https://rawgit.com/mbostock/d3/wiki/Gallery

Transition (animation)

Transition


             svg.on("click", function(){
                path
                  .transition()
                    .duration(2000)
                    .ease("linear")
                    .attr("stroke-dashoffset", totalLength);
              })
          

HTML5 Canvas

Dynamic rendering of 2D shapes and bitmap images
Added in HTML5, the HTML canvas element can be used to draw graphics via scripting in JavaScript.

SVG vs Canvas


        function draw() {
          var canvas = document.getElementById("canvas");
          if (canvas.getContext) {
            var ctx = canvas.getContext("2d");

            ctx.fillStyle = "rgb(200,0,0)";
            ctx.fillRect (10, 10, 55, 50);

            ctx.fillStyle = "rgba(0, 0, 200, 0.5)";
            ctx.fillRect (30, 30, 55, 50);
          }
        }
            

Leaflet

A JavaScript library for interactive maps
An open-source JavaScript library for mobile-friendly interactive maps
- Map Layer - Marker - Vector Layers (Path、Polyline、Polygon、Rectangle、Circle) - GeoJSON - Controls - Plugins
Create a simple map with marker

 var map = L.map('map').setView([51.505, -0.09], 13);
 L.tileLayer('http://{s}.tile.osm.org/{z}/{x}/{y}.png', {
    attribution: '© OpenStreetMap contributors'
 }).addTo(map);

 L.marker([51.5, -0.09]).addTo(map)
    .bindPopup('A pretty CSS3 popup.
Easily customizable.') .openPopup();

Summary

  • Data visualization is a process of mapping information to visuals.
  • The power of data visualization as a way to influence and persuade their audience.
  • D3:A powerful tool for custom, web-based visualization.
  • Canvus:A javascript graph visualization toolkit with high performance.
  • leafletjs:An open-source JavaScript library for mobile-friendly interactive maps.
Have fun, learning by doing
# Reference - D3 * [d3.org](http://d3js.org) * [github wiki](https://github.com/mbostock/d3/wiki) * [Interactive Data Visualization for the Web](http://chimera.labs.oreilly.com/books/1230000000345/index.html) * [d3 workshop slides](https://bost.ocks.org/mike/d3/workshop/) - HTML5 Canvas * [Canvas 教學文件](https://developer.mozilla.org/zh-TW/docs/Web/Guide/HTML/Canvas_tutorial) - [Leaflet](http://leafletjs.com) - [Google map API](https://developers.google.com/maps/documentation/javascript/) - [React-D3](http://www.reactd3.org)
# Reference - [Course: CSE512 Data Visualization](http://courses.cs.washington.edu/courses/cse512/14wi/) - [Website: Why your Brain Craves Infographics](http://neomam.com/interactive/13reasons) - [TED Talk: The best stats you've ever seen](http://www.ted.com/talks/hans_rosling_shows_the_best_stats_you_ve_ever_seen)
# Thank you : ) Welcome to join us ## Q&A