book

Pokemon Viz

Compare attacks as a horizontal barchart[top]

Data [{"Name":"Squirtle","Type":"WATER","HP":44,"Attack":48,"Defense":65,"Special Attack":50,"Special Defense":64,"Speed":43},{"Name":"Mega Charizard Y","Type":"FLYING","HP":78,"Attack":104,"Defense":78,"Special Attack":159,"Special Defense":115,"Speed":100},{"Name":"Charmander","Type":"FIRE","HP":39,"Attack":52,"Defense":43,"Special Attack":60,"Special Defense":50,"Speed":65},{"Name":"Mega Venusaur","Type":"GRASS","HP":80,"Attack":100,"Defense":123,"Special Attack":122,"Special Defense":120,"Speed":80},{"Name":"Venusaur","Type":"GRASS","HP":80,"Attack":82,"Defense":83,"Special Attack":100,"Special Defense":100,"Speed":80},{"Name":"Ivysaur","Type":"GRASS","HP":60,"Attack":62,"Defense":63,"Special Attack":80,"Special Defense":80,"Speed":60},{"Name":"Bulbasaur","Type":"GRASS","HP":45,"Attack":49,"Defense":49,"Special Attack":65,"Special Defense":65,"Speed":45}]
Desired Viz
Actual Viz
<g transform="translate(0 0)">
    <rect
         width="48"
         height="20"
         style="fill:red;
                stroke-width:3;
                stroke:rgb(0,0,0)" />
</g>
<g transform="translate(0 20)">
    <rect
         width="104"
         height="20"
         style="fill:red;
                stroke-width:3;
                stroke:rgb(0,0,0)" />
</g>
<g transform="translate(0 40)">
    <rect
         width="52"
         height="20"
         style="fill:red;
                stroke-width:3;
                stroke:rgb(0,0,0)" />
</g>
<g transform="translate(0 60)">
    <rect
         width="100"
         height="20"
         style="fill:red;
                stroke-width:3;
                stroke:rgb(0,0,0)" />
</g>
<g transform="translate(0 80)">
    <rect
         width="82"
         height="20"
         style="fill:red;
                stroke-width:3;
                stroke:rgb(0,0,0)" />
</g>
<g transform="translate(0 100)">
    <rect
         width="62"
         height="20"
         style="fill:red;
                stroke-width:3;
                stroke:rgb(0,0,0)" />
</g>
<g transform="translate(0 120)">
    <rect
         width="49"
         height="20"
         style="fill:red;
                stroke-width:3;
                stroke:rgb(0,0,0)" />
</g>
<g transform="translate(0 0)">
    <rect
         width="48"
         height="20"
         style="fill:red;
                stroke-width:3;
                stroke:rgb(0,0,0)" />
</g>
<g transform="translate(0 20)">
    <rect
         width="104"
         height="20"
         style="fill:red;
                stroke-width:3;
                stroke:rgb(0,0,0)" />
</g>
<g transform="translate(0 40)">
    <rect
         width="52"
         height="20"
         style="fill:red;
                stroke-width:3;
                stroke:rgb(0,0,0)" />
</g>
<g transform="translate(0 60)">
    <rect
         width="100"
         height="20"
         style="fill:red;
                stroke-width:3;
                stroke:rgb(0,0,0)" />
</g>
<g transform="translate(0 80)">
    <rect
         width="82"
         height="20"
         style="fill:red;
                stroke-width:3;
                stroke:rgb(0,0,0)" />
</g>
<g transform="translate(0 100)">
    <rect
         width="62"
         height="20"
         style="fill:red;
                stroke-width:3;
                stroke:rgb(0,0,0)" />
</g>
<g transform="translate(0 120)">
    <rect
         width="49"
         height="20"
         style="fill:red;
                stroke-width:3;
                stroke:rgb(0,0,0)" />
</g>
Solution: SVG Template
<g transform="translate(0 ${d.y})">
    <rect
         width="${d.width}"
         height="20"
         style="fill:${d.color};
                stroke-width:3;
                stroke:rgb(0,0,0)" />
</g>
Solution: Javascript
function computeX(d, i) {
    return 0
}

function computeWidth(d, i) {
    return d.Attack
}

function computeY(d, i) {
    return i * 20
}

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

var viz = _.map(data, function(d, i){
            return {
                x: computeX(d, i),
                y: computeY(d, i),
                width: computeWidth(d, i),
                color: computeColor(d, i)
            }
         })
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
[{"x":0,"y":0,"width":48,"color":"red"},{"x":0,"y":20,"width":104,"color":"red"},{"x":0,"y":40,"width":52,"color":"red"},{"x":0,"y":60,"width":100,"color":"red"},{"x":0,"y":80,"width":82,"color":"red"},{"x":0,"y":100,"width":62,"color":"red"},{"x":0,"y":120,"width":49,"color":"red"}]

Place a label over each bar[top]

Data [{"Name":"Squirtle","Type":"WATER","HP":44,"Attack":48,"Defense":65,"Special Attack":50,"Special Defense":64,"Speed":43},{"Name":"Mega Charizard Y","Type":"FLYING","HP":78,"Attack":104,"Defense":78,"Special Attack":159,"Special Defense":115,"Speed":100},{"Name":"Charmander","Type":"FIRE","HP":39,"Attack":52,"Defense":43,"Special Attack":60,"Special Defense":50,"Speed":65},{"Name":"Mega Venusaur","Type":"GRASS","HP":80,"Attack":100,"Defense":123,"Special Attack":122,"Special Defense":120,"Speed":80},{"Name":"Venusaur","Type":"GRASS","HP":80,"Attack":82,"Defense":83,"Special Attack":100,"Special Defense":100,"Speed":80},{"Name":"Ivysaur","Type":"GRASS","HP":60,"Attack":62,"Defense":63,"Special Attack":80,"Special Defense":80,"Speed":60},{"Name":"Bulbasaur","Type":"GRASS","HP":45,"Attack":49,"Defense":49,"Special Attack":65,"Special Defense":65,"Speed":45}]
Desired Viz Mega Charizard Y Charmander Mega Venusaur Venusaur Ivysaur Bulbasaur
Actual Viz Squirtle Mega Charizard Y Charmander Mega Venusaur Venusaur Ivysaur Bulbasaur
<g transform="translate(0 0)">
    <rect         
         width="48"
         height="20"
         style="fill:red;
                stroke-width:3;
                stroke:rgb(0,0,0)" />
    <text transform="translate(0 15)">
        
    </text>
</g>
<g transform="translate(0 20)">
    <rect         
         width="104"
         height="20"
         style="fill:red;
                stroke-width:3;
                stroke:rgb(0,0,0)" />
    <text transform="translate(0 15)">
        Mega Charizard Y
    </text>
</g>
<g transform="translate(0 40)">
    <rect         
         width="52"
         height="20"
         style="fill:red;
                stroke-width:3;
                stroke:rgb(0,0,0)" />
    <text transform="translate(0 15)">
        Charmander
    </text>
</g>
<g transform="translate(0 60)">
    <rect         
         width="100"
         height="20"
         style="fill:red;
                stroke-width:3;
                stroke:rgb(0,0,0)" />
    <text transform="translate(0 15)">
        Mega Venusaur
    </text>
</g>
<g transform="translate(0 80)">
    <rect         
         width="82"
         height="20"
         style="fill:red;
                stroke-width:3;
                stroke:rgb(0,0,0)" />
    <text transform="translate(0 15)">
        Venusaur
    </text>
</g>
<g transform="translate(0 100)">
    <rect         
         width="62"
         height="20"
         style="fill:red;
                stroke-width:3;
                stroke:rgb(0,0,0)" />
    <text transform="translate(0 15)">
        Ivysaur
    </text>
</g>
<g transform="translate(0 120)">
    <rect         
         width="49"
         height="20"
         style="fill:red;
                stroke-width:3;
                stroke:rgb(0,0,0)" />
    <text transform="translate(0 15)">
        Bulbasaur
    </text>
</g>
<g transform="translate(0 0)">
    <rect         
         width="48"
         height="20"
         style="fill:red;
                stroke-width:3;
                stroke:rgb(0,0,0)" />
                 <text transform="translate(0 15)">
        Squirtle
    </text>
</g>
<g transform="translate(0 20)">
    <rect         
         width="104"
         height="20"
         style="fill:red;
                stroke-width:3;
                stroke:rgb(0,0,0)" />
                 <text transform="translate(0 15)">
        Mega Charizard Y
    </text>
</g>
<g transform="translate(0 40)">
    <rect         
         width="52"
         height="20"
         style="fill:red;
                stroke-width:3;
                stroke:rgb(0,0,0)" />
                 <text transform="translate(0 15)">
        Charmander
    </text>
</g>
<g transform="translate(0 60)">
    <rect         
         width="100"
         height="20"
         style="fill:red;
                stroke-width:3;
                stroke:rgb(0,0,0)" />
                 <text transform="translate(0 15)">
        Mega Venusaur
    </text>
</g>
<g transform="translate(0 80)">
    <rect         
         width="82"
         height="20"
         style="fill:red;
                stroke-width:3;
                stroke:rgb(0,0,0)" />
                 <text transform="translate(0 15)">
        Venusaur
    </text>
</g>
<g transform="translate(0 100)">
    <rect         
         width="62"
         height="20"
         style="fill:red;
                stroke-width:3;
                stroke:rgb(0,0,0)" />
                 <text transform="translate(0 15)">
        Ivysaur
    </text>
</g>
<g transform="translate(0 120)">
    <rect         
         width="49"
         height="20"
         style="fill:red;
                stroke-width:3;
                stroke:rgb(0,0,0)" />
                 <text transform="translate(0 15)">
        Bulbasaur
    </text>
</g>
Solution: SVG Template
<g transform="translate(0 ${d.y})">
    <rect         
         width="${d.width}"
         height="20"
         style="fill:${d.color};
                stroke-width:3;
                stroke:rgb(0,0,0)" />
                 <text transform="translate(0 15)">
        ${d.name}
    </text>
</g>
Solution: Javascript
function computeX(d, i) {
    return 0
}

function computeWidth(d, i) {
    return d.Attack
}

function computeY(d, i) {
    return i * 20
}

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

var viz = _.map(data, function(d, i){
            return {
                x: computeX(d, i),
                y: computeY(d, i),
                width: computeWidth(d, i),
                color: computeColor(d, i),
                name:  computeName(d, i)
            }
         })
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
[{"x":0,"y":0,"width":48,"color":"red","name":"Squirtle"},{"x":0,"y":20,"width":104,"color":"red","name":"Mega Charizard Y"},{"x":0,"y":40,"width":52,"color":"red","name":"Charmander"},{"x":0,"y":60,"width":100,"color":"red","name":"Mega Venusaur"},{"x":0,"y":80,"width":82,"color":"red","name":"Venusaur"},{"x":0,"y":100,"width":62,"color":"red","name":"Ivysaur"},{"x":0,"y":120,"width":49,"color":"red","name":"Bulbasaur"}]

Place a label to the left of each bar[top]

Data [{"Name":"Squirtle","Type":"WATER","HP":44,"Attack":48,"Defense":65,"Special Attack":50,"Special Defense":64,"Speed":43},{"Name":"Mega Charizard Y","Type":"FLYING","HP":78,"Attack":104,"Defense":78,"Special Attack":159,"Special Defense":115,"Speed":100},{"Name":"Charmander","Type":"FIRE","HP":39,"Attack":52,"Defense":43,"Special Attack":60,"Special Defense":50,"Speed":65},{"Name":"Mega Venusaur","Type":"GRASS","HP":80,"Attack":100,"Defense":123,"Special Attack":122,"Special Defense":120,"Speed":80},{"Name":"Venusaur","Type":"GRASS","HP":80,"Attack":82,"Defense":83,"Special Attack":100,"Special Defense":100,"Speed":80},{"Name":"Ivysaur","Type":"GRASS","HP":60,"Attack":62,"Defense":63,"Special Attack":80,"Special Defense":80,"Speed":60},{"Name":"Bulbasaur","Type":"GRASS","HP":45,"Attack":49,"Defense":49,"Special Attack":65,"Special Defense":65,"Speed":45}]
Desired Viz Squirtle Mega Charizard Y Charmander Mega Venusaur Venusaur Ivysaur Bulbasaur
Actual Viz Squirtle Mega Charizard Y Charmander Mega Venusaur Venusaur Ivysaur Bulbasaur
<g transform="translate(0 0)">
    <rect
         x="120"
         width="48"
         height="20"
         style="fill:red;
                stroke-width:3;
                stroke:rgb(0,0,0)" />
    <text transform="translate(0 15)">
        Squirtle
    </text>
</g>
<g transform="translate(0 20)">
    <rect
         x="120"
         width="104"
         height="20"
         style="fill:red;
                stroke-width:3;
                stroke:rgb(0,0,0)" />
    <text transform="translate(0 15)">
        Mega Charizard Y
    </text>
</g>
<g transform="translate(0 40)">
    <rect
         x="120"
         width="52"
         height="20"
         style="fill:red;
                stroke-width:3;
                stroke:rgb(0,0,0)" />
    <text transform="translate(0 15)">
        Charmander
    </text>
</g>
<g transform="translate(0 60)">
    <rect
         x="120"
         width="100"
         height="20"
         style="fill:red;
                stroke-width:3;
                stroke:rgb(0,0,0)" />
    <text transform="translate(0 15)">
        Mega Venusaur
    </text>
</g>
<g transform="translate(0 80)">
    <rect
         x="120"
         width="82"
         height="20"
         style="fill:red;
                stroke-width:3;
                stroke:rgb(0,0,0)" />
    <text transform="translate(0 15)">
        Venusaur
    </text>
</g>
<g transform="translate(0 100)">
    <rect
         x="120"
         width="62"
         height="20"
         style="fill:red;
                stroke-width:3;
                stroke:rgb(0,0,0)" />
    <text transform="translate(0 15)">
        Ivysaur
    </text>
</g>
<g transform="translate(0 120)">
    <rect
         x="120"
         width="49"
         height="20"
         style="fill:red;
                stroke-width:3;
                stroke:rgb(0,0,0)" />
    <text transform="translate(0 15)">
        Bulbasaur
    </text>
</g>
<g transform="translate(0 0)">
    <rect         
         x= "120"
         width="48"
         height="20"
         style="fill:red;
                stroke-width:3;
                stroke:rgb(0,0,0)" />
                 <text transform="translate(0 15)">
        Squirtle
    </text>
</g>
<g transform="translate(0 20)">
    <rect         
         x= "120"
         width="104"
         height="20"
         style="fill:red;
                stroke-width:3;
                stroke:rgb(0,0,0)" />
                 <text transform="translate(0 15)">
        Mega Charizard Y
    </text>
</g>
<g transform="translate(0 40)">
    <rect         
         x= "120"
         width="52"
         height="20"
         style="fill:red;
                stroke-width:3;
                stroke:rgb(0,0,0)" />
                 <text transform="translate(0 15)">
        Charmander
    </text>
</g>
<g transform="translate(0 60)">
    <rect         
         x= "120"
         width="100"
         height="20"
         style="fill:red;
                stroke-width:3;
                stroke:rgb(0,0,0)" />
                 <text transform="translate(0 15)">
        Mega Venusaur
    </text>
</g>
<g transform="translate(0 80)">
    <rect         
         x= "120"
         width="82"
         height="20"
         style="fill:red;
                stroke-width:3;
                stroke:rgb(0,0,0)" />
                 <text transform="translate(0 15)">
        Venusaur
    </text>
</g>
<g transform="translate(0 100)">
    <rect         
         x= "120"
         width="62"
         height="20"
         style="fill:red;
                stroke-width:3;
                stroke:rgb(0,0,0)" />
                 <text transform="translate(0 15)">
        Ivysaur
    </text>
</g>
<g transform="translate(0 120)">
    <rect         
         x= "120"
         width="49"
         height="20"
         style="fill:red;
                stroke-width:3;
                stroke:rgb(0,0,0)" />
                 <text transform="translate(0 15)">
        Bulbasaur
    </text>
</g>
Solution: SVG Template
<g transform="translate(0 ${d.y})">
    <rect         
         x= "120"
         width="${d.width}"
         height="20"
         style="fill:${d.color};
                stroke-width:3;
                stroke:rgb(0,0,0)" />
                 <text transform="translate(0 15)">
        ${d.name}
    </text>
</g>
Solution: Javascript
function computeX(d, i) {
    return 0
}

function computeWidth(d, i) {
    return d.Attack
}

function computeY(d, i) {
    return i * 20
}

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

var viz = _.map(data, function(d, i){
            return {
                x: computeX(d, i),
                y: computeY(d, i),
                width: computeWidth(d, i),
                color: computeColor(d, i),
                name:  computeName(d, i)
            }
         })
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
[{"x":0,"y":0,"width":48,"color":"red","name":"Squirtle"},{"x":0,"y":20,"width":104,"color":"red","name":"Mega Charizard Y"},{"x":0,"y":40,"width":52,"color":"red","name":"Charmander"},{"x":0,"y":60,"width":100,"color":"red","name":"Mega Venusaur"},{"x":0,"y":80,"width":82,"color":"red","name":"Venusaur"},{"x":0,"y":100,"width":62,"color":"red","name":"Ivysaur"},{"x":0,"y":120,"width":49,"color":"red","name":"Bulbasaur"}]

Compare attack and defense points side by side[top]

Data [{"Name":"Squirtle","Type":"WATER","HP":44,"Attack":48,"Defense":65,"Special Attack":50,"Special Defense":64,"Speed":43},{"Name":"Mega Charizard Y","Type":"FLYING","HP":78,"Attack":104,"Defense":78,"Special Attack":159,"Special Defense":115,"Speed":100},{"Name":"Charmander","Type":"FIRE","HP":39,"Attack":52,"Defense":43,"Special Attack":60,"Special Defense":50,"Speed":65},{"Name":"Mega Venusaur","Type":"GRASS","HP":80,"Attack":100,"Defense":123,"Special Attack":122,"Special Defense":120,"Speed":80},{"Name":"Venusaur","Type":"GRASS","HP":80,"Attack":82,"Defense":83,"Special Attack":100,"Special Defense":100,"Speed":80},{"Name":"Ivysaur","Type":"GRASS","HP":60,"Attack":62,"Defense":63,"Special Attack":80,"Special Defense":80,"Speed":60},{"Name":"Bulbasaur","Type":"GRASS","HP":45,"Attack":49,"Defense":49,"Special Attack":65,"Special Defense":65,"Speed":45}]
Desired Viz Squirtle Mega Charizard Y Charmander Mega Venusaur Venusaur Ivysaur Bulbasaur
Actual Viz "Squirtle" "Mega Charizard Y" "Charmander" "Mega Venusaur" "Venusaur" "Ivysaur" "Bulbasaur"
<g transform="translate(120 0)">
    <rect
         x="-48"
         width="48"
         height="20"
         style="fill:red;
                stroke-width:1;
                stroke:rgb(0,0,0)" />
    <rect
         x="0"
         width="65"
         height="20"
         style="fill:blue;
                stroke-width:1;
                stroke:rgb(0,0,0)" />

    <text transform="translate(0 15)">
        Squirtle
    </text>
</g>
<g transform="translate(120 20)">
    <rect
         x="-104"
         width="104"
         height="20"
         style="fill:red;
                stroke-width:1;
                stroke:rgb(0,0,0)" />
    <rect
         x="0"
         width="78"
         height="20"
         style="fill:blue;
                stroke-width:1;
                stroke:rgb(0,0,0)" />

    <text transform="translate(0 15)">
        Mega Charizard Y
    </text>
</g>
<g transform="translate(120 40)">
    <rect
         x="-52"
         width="52"
         height="20"
         style="fill:red;
                stroke-width:1;
                stroke:rgb(0,0,0)" />
    <rect
         x="0"
         width="43"
         height="20"
         style="fill:blue;
                stroke-width:1;
                stroke:rgb(0,0,0)" />

    <text transform="translate(0 15)">
        Charmander
    </text>
</g>
<g transform="translate(120 60)">
    <rect
         x="-100"
         width="100"
         height="20"
         style="fill:red;
                stroke-width:1;
                stroke:rgb(0,0,0)" />
    <rect
         x="0"
         width="123"
         height="20"
         style="fill:blue;
                stroke-width:1;
                stroke:rgb(0,0,0)" />

    <text transform="translate(0 15)">
        Mega Venusaur
    </text>
</g>
<g transform="translate(120 80)">
    <rect
         x="-82"
         width="82"
         height="20"
         style="fill:red;
                stroke-width:1;
                stroke:rgb(0,0,0)" />
    <rect
         x="0"
         width="83"
         height="20"
         style="fill:blue;
                stroke-width:1;
                stroke:rgb(0,0,0)" />

    <text transform="translate(0 15)">
        Venusaur
    </text>
</g>
<g transform="translate(120 100)">
    <rect
         x="-62"
         width="62"
         height="20"
         style="fill:red;
                stroke-width:1;
                stroke:rgb(0,0,0)" />
    <rect
         x="0"
         width="63"
         height="20"
         style="fill:blue;
                stroke-width:1;
                stroke:rgb(0,0,0)" />

    <text transform="translate(0 15)">
        Ivysaur
    </text>
</g>
<g transform="translate(120 120)">
    <rect
         x="-49"
         width="49"
         height="20"
         style="fill:red;
                stroke-width:1;
                stroke:rgb(0,0,0)" />
    <rect
         x="0"
         width="49"
         height="20"
         style="fill:blue;
                stroke-width:1;
                stroke:rgb(0,0,0)" />

    <text transform="translate(0 15)">
        Bulbasaur
    </text>
</g>
<g transform="translate(120 0)">
    <rect
         x="-48"
         width="48"
         height="20"
         style="fill:red;
                stroke-width:1;
                stroke:rgb(0,0,0)" />


    <rect
         x="0"
         width="65"
         height="20"
         style="fill:blue;
                stroke-width:1;
                stroke:rgb(0,0,0)" />

    <text transform="translate(0 15)">
        "Squirtle"
    </text>
</g>
<g transform="translate(120 20)">
    <rect
         x="-104"
         width="104"
         height="20"
         style="fill:red;
                stroke-width:1;
                stroke:rgb(0,0,0)" />


    <rect
         x="0"
         width="78"
         height="20"
         style="fill:blue;
                stroke-width:1;
                stroke:rgb(0,0,0)" />

    <text transform="translate(0 15)">
        "Mega Charizard Y"
    </text>
</g>
<g transform="translate(120 40)">
    <rect
         x="-52"
         width="52"
         height="20"
         style="fill:red;
                stroke-width:1;
                stroke:rgb(0,0,0)" />


    <rect
         x="0"
         width="43"
         height="20"
         style="fill:blue;
                stroke-width:1;
                stroke:rgb(0,0,0)" />

    <text transform="translate(0 15)">
        "Charmander"
    </text>
</g>
<g transform="translate(120 60)">
    <rect
         x="-100"
         width="100"
         height="20"
         style="fill:red;
                stroke-width:1;
                stroke:rgb(0,0,0)" />


    <rect
         x="0"
         width="123"
         height="20"
         style="fill:blue;
                stroke-width:1;
                stroke:rgb(0,0,0)" />

    <text transform="translate(0 15)">
        "Mega Venusaur"
    </text>
</g>
<g transform="translate(120 80)">
    <rect
         x="-82"
         width="82"
         height="20"
         style="fill:red;
                stroke-width:1;
                stroke:rgb(0,0,0)" />


    <rect
         x="0"
         width="83"
         height="20"
         style="fill:blue;
                stroke-width:1;
                stroke:rgb(0,0,0)" />

    <text transform="translate(0 15)">
        "Venusaur"
    </text>
</g>
<g transform="translate(120 100)">
    <rect
         x="-62"
         width="62"
         height="20"
         style="fill:red;
                stroke-width:1;
                stroke:rgb(0,0,0)" />


    <rect
         x="0"
         width="63"
         height="20"
         style="fill:blue;
                stroke-width:1;
                stroke:rgb(0,0,0)" />

    <text transform="translate(0 15)">
        "Ivysaur"
    </text>
</g>
<g transform="translate(120 120)">
    <rect
         x="-49"
         width="49"
         height="20"
         style="fill:red;
                stroke-width:1;
                stroke:rgb(0,0,0)" />


    <rect
         x="0"
         width="49"
         height="20"
         style="fill:blue;
                stroke-width:1;
                stroke:rgb(0,0,0)" />

    <text transform="translate(0 15)">
        "Bulbasaur"
    </text>
</g>
Solution: SVG Template
<g transform="translate(120 ${d.y})">
    <rect
         x="-${d.width}"
         width="${d.width}"
         height="20"
         style="fill:${d.color};
                stroke-width:1;
                stroke:rgb(0,0,0)" />


    <rect
         x="0"
         width="${d.height}"
         height="20"
         style="fill:blue;
                stroke-width:1;
                stroke:rgb(0,0,0)" />

    <text transform="translate(0 15)">
        "${d.name}"
    </text>
</g>
Solution: Javascript
function computeX(d, i) {
    return 0
}

function computeWidth(d, i) {
    return d.Attack
}

function computeY(d, i) {
    return i * 20
}

function computeColor(d, i) {
    return 'red'
}
function computeName(d, i) {
    return d.Name
}
function computeHeight(d, i) {
    return d.Defense
}
var viz = _.map(data, function(d, i){
            return {
                x: computeX(d, i),
                y: computeY(d, i),
                width: computeWidth(d, i),
                color: computeColor(d, i),
                name:  computeName(d,i),
                height:computeHeight(d,i)
                
                
            }
         })
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
[{"x":0,"y":0,"width":48,"color":"red","name":"Squirtle","height":65},{"x":0,"y":20,"width":104,"color":"red","name":"Mega Charizard Y","height":78},{"x":0,"y":40,"width":52,"color":"red","name":"Charmander","height":43},{"x":0,"y":60,"width":100,"color":"red","name":"Mega Venusaur","height":123},{"x":0,"y":80,"width":82,"color":"red","name":"Venusaur","height":83},{"x":0,"y":100,"width":62,"color":"red","name":"Ivysaur","height":63},{"x":0,"y":120,"width":49,"color":"red","name":"Bulbasaur","height":49}]

Compare attack points and represent defense points using bar heights[top]

Data [{"Name":"Squirtle","Type":"WATER","HP":44,"Attack":48,"Defense":65,"Special Attack":50,"Special Defense":64,"Speed":43},{"Name":"Mega Charizard Y","Type":"FLYING","HP":78,"Attack":104,"Defense":78,"Special Attack":159,"Special Defense":115,"Speed":100},{"Name":"Charmander","Type":"FIRE","HP":39,"Attack":52,"Defense":43,"Special Attack":60,"Special Defense":50,"Speed":65},{"Name":"Mega Venusaur","Type":"GRASS","HP":80,"Attack":100,"Defense":123,"Special Attack":122,"Special Defense":120,"Speed":80},{"Name":"Venusaur","Type":"GRASS","HP":80,"Attack":82,"Defense":83,"Special Attack":100,"Special Defense":100,"Speed":80},{"Name":"Ivysaur","Type":"GRASS","HP":60,"Attack":62,"Defense":63,"Special Attack":80,"Special Defense":80,"Speed":60},{"Name":"Bulbasaur","Type":"GRASS","HP":45,"Attack":49,"Defense":49,"Special Attack":65,"Special Defense":65,"Speed":45}]
Desired Viz Squirtle Mega Charizard Y Charmander Mega Venusaur Venusaur Ivysaur Bulbasaur
Actual Viz Squirtle Mega Charizard Y Charmander Mega Venusaur Venusaur Ivysaur Bulbasaur
<g transform="translate(0 0)">
    <rect width="48"
         height="65"
         style="fill:red;
                stroke-width:1;
                stroke:rgb(0,0,0)" />
    <text transform="translate(0 15)">
        Squirtle
    </text>
</g>
<g transform="translate(0 65)">
    <rect width="104"
         height="78"
         style="fill:red;
                stroke-width:1;
                stroke:rgb(0,0,0)" />
    <text transform="translate(0 15)">
        Mega Charizard Y
    </text>
</g>
<g transform="translate(0 143)">
    <rect width="52"
         height="43"
         style="fill:red;
                stroke-width:1;
                stroke:rgb(0,0,0)" />
    <text transform="translate(0 15)">
        Charmander
    </text>
</g>
<g transform="translate(0 186)">
    <rect width="100"
         height="123"
         style="fill:red;
                stroke-width:1;
                stroke:rgb(0,0,0)" />
    <text transform="translate(0 15)">
        Mega Venusaur
    </text>
</g>
<g transform="translate(0 309)">
    <rect width="82"
         height="83"
         style="fill:red;
                stroke-width:1;
                stroke:rgb(0,0,0)" />
    <text transform="translate(0 15)">
        Venusaur
    </text>
</g>
<g transform="translate(0 392)">
    <rect width="62"
         height="63"
         style="fill:red;
                stroke-width:1;
                stroke:rgb(0,0,0)" />
    <text transform="translate(0 15)">
        Ivysaur
    </text>
</g>
<g transform="translate(0 455)">
    <rect width="49"
         height="49"
         style="fill:red;
                stroke-width:1;
                stroke:rgb(0,0,0)" />
    <text transform="translate(0 15)">
        Bulbasaur
    </text>
</g>
<g transform="translate(0 0)">
    <rect width="48"
         height="65"
         style="fill:red;
                stroke-width:1;
                stroke:rgb(0,0,0)" />
                <text transform="translate(0 15)">
        Squirtle
    </text>
</g>
<g transform="translate(0 65)">
    <rect width="104"
         height="78"
         style="fill:red;
                stroke-width:1;
                stroke:rgb(0,0,0)" />
                <text transform="translate(0 15)">
        Mega Charizard Y
    </text>
</g>
<g transform="translate(0 143)">
    <rect width="52"
         height="43"
         style="fill:red;
                stroke-width:1;
                stroke:rgb(0,0,0)" />
                <text transform="translate(0 15)">
        Charmander
    </text>
</g>
<g transform="translate(0 186)">
    <rect width="100"
         height="123"
         style="fill:red;
                stroke-width:1;
                stroke:rgb(0,0,0)" />
                <text transform="translate(0 15)">
        Mega Venusaur
    </text>
</g>
<g transform="translate(0 309)">
    <rect width="82"
         height="83"
         style="fill:red;
                stroke-width:1;
                stroke:rgb(0,0,0)" />
                <text transform="translate(0 15)">
        Venusaur
    </text>
</g>
<g transform="translate(0 392)">
    <rect width="62"
         height="63"
         style="fill:red;
                stroke-width:1;
                stroke:rgb(0,0,0)" />
                <text transform="translate(0 15)">
        Ivysaur
    </text>
</g>
<g transform="translate(0 455)">
    <rect width="49"
         height="49"
         style="fill:red;
                stroke-width:1;
                stroke:rgb(0,0,0)" />
                <text transform="translate(0 15)">
        Bulbasaur
    </text>
</g>
Solution: SVG Template
<g transform="translate(0 ${d.translate})">
    <rect width="${d.width}"
         height="${d.height}"
         style="fill:${d.color};
                stroke-width:1;
                stroke:rgb(0,0,0)" />
                <text transform="translate(0 15)">
        ${d.name}
    </text>
</g>
Solution: Javascript
function computeX(d, i) {
    return 0
}

function computeWidth(d, i) {
    return d.Attack
}

function computeHeight(d, i) {
    return d.Defense
}
function computeName(d,i)
 {
    return d.Name
}

// HINT: figure out a way to compute a Y offset on each call to return the sum of of the heights
// of all previous bars
function computeY(d, i) {
    return i * 20
}

function computeColor(d, i) {
    return 'red'
}
s=_.pluck(data,'Defense')
function computeTranslate(d, i) {
    var sum=0
    for(j=0;j<i;j++)
        sum= sum+s[j]
     return sum 
    }
var viz = _.map(data, function(d, i){
            return {
                x: computeX(d, i),
                y: computeY(d, i),
                height: computeHeight(d, i),
                width: computeWidth(d, i),
                color: computeColor(d, i),
                name: computeName(d,i),
                translate: computeTranslate(d,i)
            }
         })
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
[{"x":0,"y":0,"height":65,"width":48,"color":"red","name":"Squirtle","translate":0},{"x":0,"y":20,"height":78,"width":104,"color":"red","name":"Mega Charizard Y","translate":65},{"x":0,"y":40,"height":43,"width":52,"color":"red","name":"Charmander","translate":143},{"x":0,"y":60,"height":123,"width":100,"color":"red","name":"Mega Venusaur","translate":186},{"x":0,"y":80,"height":83,"width":82,"color":"red","name":"Venusaur","translate":309},{"x":0,"y":100,"height":63,"width":62,"color":"red","name":"Ivysaur","translate":392},{"x":0,"y":120,"height":49,"width":49,"color":"red","name":"Bulbasaur","translate":455}]

Compare attack points and represent defense points using colors[top]

Data [{"Name":"Squirtle","Type":"WATER","HP":44,"Attack":48,"Defense":65,"Special Attack":50,"Special Defense":64,"Speed":43},{"Name":"Mega Charizard Y","Type":"FLYING","HP":78,"Attack":104,"Defense":78,"Special Attack":159,"Special Defense":115,"Speed":100},{"Name":"Charmander","Type":"FIRE","HP":39,"Attack":52,"Defense":43,"Special Attack":60,"Special Defense":50,"Speed":65},{"Name":"Mega Venusaur","Type":"GRASS","HP":80,"Attack":100,"Defense":123,"Special Attack":122,"Special Defense":120,"Speed":80},{"Name":"Venusaur","Type":"GRASS","HP":80,"Attack":82,"Defense":83,"Special Attack":100,"Special Defense":100,"Speed":80},{"Name":"Ivysaur","Type":"GRASS","HP":60,"Attack":62,"Defense":63,"Special Attack":80,"Special Defense":80,"Speed":60},{"Name":"Bulbasaur","Type":"GRASS","HP":45,"Attack":49,"Defense":49,"Special Attack":65,"Special Defense":65,"Speed":45}]
Desired Viz Squirtle Mega Charizard Y Charmander Mega Venusaur Venusaur Ivysaur Bulbasaur
Actual Viz Squirtle Mega Charizard Y Charmander Mega Venusaur Venusaur Ivysaur Bulbasaur
<g transform="translate(0 0)">
    <rect width="48"
         height="20"
         style="fill:rgb(135,0,0);
                stroke-width:1;
                stroke:rgb(0,0,0)" />    

    <text transform="translate(0 15)">
        Squirtle
    </text>
</g>
<g transform="translate(0 20)">
    <rect width="104"
         height="20"
         style="fill:rgb(162,0,0);
                stroke-width:1;
                stroke:rgb(0,0,0)" />    

    <text transform="translate(0 15)">
        Mega Charizard Y
    </text>
</g>
<g transform="translate(0 40)">
    <rect width="52"
         height="20"
         style="fill:rgb(89,0,0);
                stroke-width:1;
                stroke:rgb(0,0,0)" />    

    <text transform="translate(0 15)">
        Charmander
    </text>
</g>
<g transform="translate(0 60)">
    <rect width="100"
         height="20"
         style="fill:rgb(255,0,0);
                stroke-width:1;
                stroke:rgb(0,0,0)" />    

    <text transform="translate(0 15)">
        Mega Venusaur
    </text>
</g>
<g transform="translate(0 80)">
    <rect width="82"
         height="20"
         style="fill:rgb(172,0,0);
                stroke-width:1;
                stroke:rgb(0,0,0)" />    

    <text transform="translate(0 15)">
        Venusaur
    </text>
</g>
<g transform="translate(0 100)">
    <rect width="62"
         height="20"
         style="fill:rgb(131,0,0);
                stroke-width:1;
                stroke:rgb(0,0,0)" />    

    <text transform="translate(0 15)">
        Ivysaur
    </text>
</g>
<g transform="translate(0 120)">
    <rect width="49"
         height="20"
         style="fill:rgb(102,0,0);
                stroke-width:1;
                stroke:rgb(0,0,0)" />    

    <text transform="translate(0 15)">
        Bulbasaur
    </text>
</g>
<g transform="translate(0 0)">
    <rect width="48"
         height="20"
         style="fill:rgb(135,0,0);
                stroke-width:1;
                stroke:rgb(0,0,0)" />    
                
    <text transform="translate(0 15)">
       Squirtle
    </text>
</g>
<g transform="translate(0 20)">
    <rect width="104"
         height="20"
         style="fill:rgb(162,0,0);
                stroke-width:1;
                stroke:rgb(0,0,0)" />    
                
    <text transform="translate(0 15)">
       Mega Charizard Y
    </text>
</g>
<g transform="translate(0 40)">
    <rect width="52"
         height="20"
         style="fill:rgb(89,0,0);
                stroke-width:1;
                stroke:rgb(0,0,0)" />    
                
    <text transform="translate(0 15)">
       Charmander
    </text>
</g>
<g transform="translate(0 60)">
    <rect width="100"
         height="20"
         style="fill:rgb(255,0,0);
                stroke-width:1;
                stroke:rgb(0,0,0)" />    
                
    <text transform="translate(0 15)">
       Mega Venusaur
    </text>
</g>
<g transform="translate(0 80)">
    <rect width="82"
         height="20"
         style="fill:rgb(172,0,0);
                stroke-width:1;
                stroke:rgb(0,0,0)" />    
                
    <text transform="translate(0 15)">
       Venusaur
    </text>
</g>
<g transform="translate(0 100)">
    <rect width="62"
         height="20"
         style="fill:rgb(131,0,0);
                stroke-width:1;
                stroke:rgb(0,0,0)" />    
                
    <text transform="translate(0 15)">
       Ivysaur
    </text>
</g>
<g transform="translate(0 120)">
    <rect width="49"
         height="20"
         style="fill:rgb(102,0,0);
                stroke-width:1;
                stroke:rgb(0,0,0)" />    
                
    <text transform="translate(0 15)">
       Bulbasaur
    </text>
</g>
Solution: SVG Template
<g transform="translate(0 ${d.y})">
    <rect width="${d.width}"
         height="20"
         style="fill:rgb(${d.color},0,0);
                stroke-width:1;
                stroke:rgb(0,0,0)" />    
                
    <text transform="translate(0 15)">
       ${d.name}
    </text>
</g>
Solution: Javascript
function computeX(d, i) {
    return 0
}

function computeWidth(d, i) {
    return d['Attack']
}

function computeY(d, i) {
    return i * 20
}


function computeName(d, i) {
    return d.Name
}

// HINT: find the max Defense point (_.max, _.pluck) so that you can scale
//       each Pokemon's defense point with respect to it to obtain a value 'r'
//       between 0 and 255. Generate a different rgb(r,0,0) string

def =_.pluck(data,'Defense')
function computeColor(d, i) {
    return _.round(255*d.Defense/_.max(def))
    
}

var viz = _.map(data, function(d, i){
            return {
                x: computeX(d, i),
                y: computeY(d, i),
                width: computeWidth(d, i),
                color: computeColor(d, i),
                name: computeName(d, i)                 
            }
         })
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
[{"x":0,"y":0,"width":48,"color":135,"name":"Squirtle"},{"x":0,"y":20,"width":104,"color":162,"name":"Mega Charizard Y"},{"x":0,"y":40,"width":52,"color":89,"name":"Charmander"},{"x":0,"y":60,"width":100,"color":255,"name":"Mega Venusaur"},{"x":0,"y":80,"width":82,"color":172,"name":"Venusaur"},{"x":0,"y":100,"width":62,"color":131,"name":"Ivysaur"},{"x":0,"y":120,"width":49,"color":102,"name":"Bulbasaur"}]