book

Warmup

Complete this warmup exercise to get an idea how to put all the different pieces together to generate an end-to-end data analysis viz report.

What is the distribution of courses across colleges?[top]

Viz AS 3237 BU 378 EB 139 EN 573 GR 51 JR 96 LW 176 MB 241 XX 30 undefined 19 AP 60
<rect x="0"
      y="76.30000000000001"
      height="323.7"
      width="20"
      style="fill:red;
             stroke-width:3;
             stroke:rgb(0,0,0)" />
<text x="0" y="76.30000000000001">AS</text>
<text x="0" y="66.30000000000001">3237</text>
<rect x="50"
      y="362.2"
      height="37.8"
      width="20"
      style="fill:red;
             stroke-width:3;
             stroke:rgb(0,0,0)" />
<text x="50" y="362.2">BU</text>
<text x="50" y="352.2">378</text>
<rect x="100"
      y="386.1"
      height="13.9"
      width="20"
      style="fill:red;
             stroke-width:3;
             stroke:rgb(0,0,0)" />
<text x="100" y="386.1">EB</text>
<text x="100" y="376.1">139</text>
<rect x="150"
      y="342.7"
      height="57.3"
      width="20"
      style="fill:red;
             stroke-width:3;
             stroke:rgb(0,0,0)" />
<text x="150" y="342.7">EN</text>
<text x="150" y="332.7">573</text>
<rect x="200"
      y="394.9"
      height="5.1"
      width="20"
      style="fill:red;
             stroke-width:3;
             stroke:rgb(0,0,0)" />
<text x="200" y="394.9">GR</text>
<text x="200" y="384.9">51</text>
<rect x="250"
      y="390.4"
      height="9.6"
      width="20"
      style="fill:red;
             stroke-width:3;
             stroke:rgb(0,0,0)" />
<text x="250" y="390.4">JR</text>
<text x="250" y="380.4">96</text>
<rect x="300"
      y="382.4"
      height="17.6"
      width="20"
      style="fill:red;
             stroke-width:3;
             stroke:rgb(0,0,0)" />
<text x="300" y="382.4">LW</text>
<text x="300" y="372.4">176</text>
<rect x="350"
      y="375.9"
      height="24.1"
      width="20"
      style="fill:red;
             stroke-width:3;
             stroke:rgb(0,0,0)" />
<text x="350" y="375.9">MB</text>
<text x="350" y="365.9">241</text>
<rect x="400"
      y="397"
      height="3"
      width="20"
      style="fill:red;
             stroke-width:3;
             stroke:rgb(0,0,0)" />
<text x="400" y="397">XX</text>
<text x="400" y="387">30</text>
<rect x="450"
      y="398.1"
      height="1.9"
      width="20"
      style="fill:red;
             stroke-width:3;
             stroke:rgb(0,0,0)" />
<text x="450" y="398.1">undefined</text>
<text x="450" y="388.1">19</text>
<rect x="500"
      y="394"
      height="6"
      width="20"
      style="fill:red;
             stroke-width:3;
             stroke:rgb(0,0,0)" />
<text x="500" y="394">AP</text>
<text x="500" y="384">60</text>
Solution: SVG Template
<rect x="${d.x}"
      y="${d.y}"
      height="${d.height}"
      width="${d.width}"
      style="fill:${d.color};
             stroke-width:3;
             stroke:rgb(0,0,0)" />
<text x="${d.x}" y="${d.y}">${d.label.name}</text>
<text x="${d.x}" y="${d.y-10}">${d.label.count}</text>
Solution: Javascript
var groups = _.groupBy(data, function(d){
    return d['CrsPBAColl']
})

// TODO: add real code to convert groups (which is an object) into an array like below
var countsJson=_.mapValues(groups, function(group){
    return _.size(group)
})
var keys=_.keys(countsJson)
var counts=_.map(keys, function(key){
    return {"name": key, "count": countsJson[key]}
})
// This array should have a lot more elements.
/*var counts = [{"name": "AS","count": 3237},
    {"name": "BU","count": 378},
    {"name": "EB","count": 139},
    {"name": "EN","count": 573}]*/

console.log(counts)

// TODO: modify the code below to produce a nice vertical bar charts

function computeX(d, i) {
    return 50*i
}

function computeHeight(d, i) {
    return d['count']/10
}

function computeWidth(d, i) {
    return 20 
}

function computeY(d, i) {
    return 400-d['count']/10
}

function computeColor(d, i) {
    return 'red'
}



var viz = _.map(counts, function(d, i){
            return {
                x: computeX(d, i),
                y: computeY(d, i),
                height: computeHeight(d, i),
                width: computeWidth(d, i),
                color: computeColor(d, i),
                label: d
            }
         })
console.log(viz)

var result = _.map(viz, function(d){
         // invoke the compiled template function on each viz data
         return template({d: d})
     })
return result.join('\n')
Console
[{"name":"AS","count":3237},{"name":"BU","count":378},{"name":"EB","count":139},{"name":"EN","count":573},{"name":"GR","count":51},{"name":"JR","count":96},{"name":"LW","count":176},{"name":"MB","count":241},{"name":"XX","count":30},{"name":"undefined","count":19},{"name":"AP","count":60}]
[{"x":0,"y":76.30000000000001,"height":323.7,"width":20,"color":"red","label":{"name":"AS","count":3237}},{"x":50,"y":362.2,"height":37.8,"width":20,"color":"red","label":{"name":"BU","count":378}},{"x":100,"y":386.1,"height":13.9,"width":20,"color":"red","label":{"name":"EB","count":139}},{"x":150,"y":342.7,"height":57.3,"width":20,"color":"red","label":{"name":"EN","count":573}},{"x":200,"y":394.9,"height":5.1,"width":20,"color":"red","label":{"name":"GR","count":51}},{"x":250,"y":390.4,"height":9.6,"width":20,"color":"red","label":{"name":"JR","count":96}},{"x":300,"y":382.4,"height":17.6,"width":20,"color":"red","label":{"name":"LW","count":176}},{"x":350,"y":375.9,"height":24.1,"width":20,"color":"red","label":{"name":"MB","count":241}},{"x":400,"y":397,"height":3,"width":20,"color":"red","label":{"name":"XX","count":30}},{"x":450,"y":398.1,"height":1.9,"width":20,"color":"red","label":{"name":"undefined","count":19}},{"x":500,"y":394,"height":6,"width":20,"color":"red","label":{"name":"AP","count":60}}]