Recently I visited Austin and many of my friends had mentioned about the variety in food options here. So my wife and I decided to search for places to eat on the foursquare app. As a standard search filter with high rating we ended up at pretty good places and foursquare did alert us to checkins whenever we reached a place. Post the trip I wanted to see how many people do checkins using this app and how the checkins are correlated with the ratings.

The first step here is to get the data . So I started to play around with the foursquare API and started working around the URL on what category(food,places to see, etc) to get the data . The authentication process for the foursquare API was a bit tricky but with my google-fu (( and special mention to the GIS tribe ) I was able to get going. Below is how you would get the client id and client secret when you create a new app.

This is an image

The idea was how to do it for many places across the country. So I decided to use R to scrap and clean the data. You can find the code here.

Once this was done the next part was to how do I visualize this data . Since I have been trying my hands on d3js I used the cleaned output from R in CSV format to display how checkins and ratings vary for these places using bubble chart.

           <script> src = "https://d3js.org/d3.v4.min.js" ></script>
           <!-- <link href="//fonts.googleapis.com/css?family=Raleway:400,300,600" rel="stylesheet" type="text/css"> -->
           <script src="https://cdnjs.cloudflare.com/ajax/libs/d3/4.4.4/d3.min.js" type="text/JavaScript"></script>
           <script src="https://cdnjs.cloudflare.com/ajax/libs/d3-queue/3.0.3/d3-queue.min.js"></script>
           <script src="https://cdnjs.cloudflare.com/ajax/libs/topojson/2.2.0/topojson.min.js"></script>
           <!-- JS–––––––––––––––––––––––––––––––––––––––––––––––––– -->
           <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
           <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
           <script>
           var svg = d3.select("svg"),
               width = +svg.attr("width"),
               height = +svg.attr("height");

               var format = d3.format(",d");

               var color = d3.scaleOrdinal(d3.schemeCategory10);

               var pack = d3.pack()
                   .size([width, width])
                   .padding(1.5);

               var inputs = {};

               d3.csv("austin_fsq.csv", function(d) {
                   d.sno = +d.sno;
                   return d;
               }, function(error, data) {
                   if (error) throw error;

                   d3.selectAll("input").on("change", function(){
                     inputs[this.id] = +this.value;
                     console.log(inputs.myValue + "-" + inputs.myRating)
                     if(inputs.myValue && inputs.myRating){
                        var classes = data.filter(d => d.value < inputs.myValue && d.rating >= inputs.myRating);
                       draw(classes);
                     }
                   })

                   function draw(classes) {

                     d3.selectAll("svg > *").remove();

                       console.log(classes.length);
                       var root = d3.hierarchy({
                               children: classes
                           })
                           .sum(function(d) {
                               return d.value;
                           })
                           .each(function(d) {
                               if (id = d.data.id) {
                                   var id, i = id.lastIndexOf(".");
                                   d.id = id;
                                   d.package = id.slice(0, i);
                                   d.class = id.slice(i + 1);
                               }
                           });

                       var node = svg.selectAll(".node")
                           .data(pack(root).leaves())
                           .enter().append("g")
                           .attr("class", "node")
                           .attr("transform", function(d) {
                               return "translate(" + d.x + "," + d.y + ")";
                           });

                       node.append("circle")
                           .attr("id", function(d) {
                               return d.id;
                           })
                           .attr("r", function(d) {
                               return d.r;
                           })
                           .style("fill", function(d) {
                               return color(d.package);
                           });

                       node.append("clipPath")
                           .attr("id", function(d) {
                               return "clip-" + d.id;
                           })
                           .append("use")
                           .attr("xlink:href", function(d) {
                               return "#" + d.id;
                           });

                       node.append("text")
                           .attr("clip-path", function(d) {
                               return "url(#clip-" + d.id + ")";
                           })
                           .selectAll("tspan")
                           .data(function(d) {
                               return d.class.split(/(?=[A-Z][^A-Z])/g);
                           })
                           .enter().append("tspan")
                           .attr("x", 0)
                           .attr("y", function(d, i, nodes) {
                               return 13 + (i - nodes.length / 2 - 0.5) * 10;
                           })
                           .text(function(d) {
                               return d;
                           });

                       node.append("title")
                           .text(function(d) {
                               return d.data.id + "\n" + format(d.value);
                           });
                   }
               });
           </script>