Pokemon
Create interactive visualizations for the Pokemon data. Complete the script so that when a user clicks on each menu button, there will be a different bar chart shown in the Viz block.
You will be reusing the code you wrote for generating SVG bar charts a couple weeks ago. The main challenge is to figure out how to connect your visualization code with the front-end code (event handlers ... etc).
Menu
Viz
Data is not loaded yet
// pokemonData is a global variable
pokemonData = 'not loaded yet'
$.get('/data/pokemon-small.json')
.success(function(data){
console.log('data loaded', data)
// TODO: show in the myviz that the data is loaded
pokemonData = data
$('.myviz').html('number of records load:' + pokemonData.length)
})
function vizAsHorizontalBars(){
// TODO: modify this function to visualize the data as horizontal
// bars to compare attack points
// define a template string
var tplString = '<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>'
// compile the string to get a template function
var template = _.template(tplString)
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(pokemonData, function(d, i){
return {
x: computeX(d, i),
y: computeY(d, i),
width: computeWidth(d, i),
color: computeColor(d, i)
}
})
console.log('viz', viz)
var result = _.map(viz, function(d){
// invoke the compiled template function on each viz data
return template({d: d})
})
console.log('result', result)
$('.myviz').html('<svg>' + result + '</svg>')
}
$('button#viz-horizontal').click(vizAsHorizontalBars)
// for each of the TODOs below, you will write a "callback function" similar
// to vizAsHorizontalBars and a $('something').click to add this callback
// function to respond to the click event of a particular button
// TODO: add code to visualize the attack points as a series
// of vertical bars (without labels)
function vizAsVerticalBars(){
// TODO: modify this function to visualize the data as horizontal
// bars to compare attack points
// define a template string
var tplString = '<g transform="translate(${d.x} 0)"> \
<rect \
width="20" \
height="${d.height}" \
style="fill:${d.color}; \
stroke-width:3; \
stroke:rgb(0,0,0)" /> \
</g>'
// compile the string to get a template function
var template = _.template(tplString)
function computeX(d, i) {
return i * 20
}
function computeWidth(d, i) {
return 20
}
function computeHeight(d, i) {
return d.Attack
}
function computeY(d, i) {
return 0
}
function computeColor(d, i) {
return 'red'
}
var viz = _.map(pokemonData, function(d, i){
return {
x: computeX(d, i),
y: computeY(d, i),
height: computeHeight(d, i),
color: computeColor(d, i)
}
})
console.log('viz', viz)
var result = _.map(viz, function(d){
// invoke the compiled template function on each viz data
return template({d: d})
})
console.log('result', result)
$('.myviz').html('<svg>' + result + '</svg>')
}
$('button#viz-vertical').click(vizAsVerticalBars)
// TODO: add code visualize the attack points vs. defense
// points as side-by-side horizontal bar charts (with labels)
function vizAttackDefense(){
// TODO: modify this function to visualize the data as horizontal
// bars to compare attack points
// define a template string
var tplString = '<g transform="translate(0 ${d.atty})"> \
<rect \
width="${d.attwidth}" \
height="20" \
style="fill:${d.attcolor}; \
stroke-width:3; \
stroke:rgb(0,0,0)" /> \
</g>'
tplString += '<g transform="translate(0 ${d.defy})"> \
<rect \
width="${d.defwidth}" \
height="20" \
style="fill:${d.defcolor}; \
stroke-width:3; \
stroke:rgb(0,0,0)" /> \
<text transform="translate(${d.width} 15)"> \
${d.label} \
</text> \
</g>'
// compile the string to get a template function
var template = _.template(tplString)
function computeX(d, i, attack) {
return 0
}
function computeWidth(d, i, attack) {
if(attack) {return d.Attack}
return d.Defense
}
function computeY(d, i, attack) {
if(attack) {return i * 40}
return 20 + i * 40
}
function computeColor(d, i, attack) {
if(attack) {return 'red'}
return 'blue'
}
var viz = _.map(pokemonData, function(d, i){
return {
attx: computeX(d, i, true),
atty: computeY(d, i, true),
attwidth: computeWidth(d, i, true),
attcolor: computeColor(d, i, true),
defx: computeX(d, i, false),
defy: computeY(d, i, false),
defwidth: computeWidth(d, i, false),
defcolor: computeColor(d, i, false)
}
})
console.log('viz', viz)
var result = _.map(viz, function(d){
// invoke the compiled template function on each viz data
return template({d: d})
})
console.log('result', result)
$('.myviz').html('<svg>' + result + '</svg>')
}
$('button#viz-attack-defense').click(vizAttackDefense)
// TODO: add code visualize the speed points vs. defense
// points as side-by-side horizontal bar charts (with labels)
function vizSpeedDefense(){
// TODO: modify this function to visualize the data as horizontal
// bars to compare attack points
// define a template string
var tplString = '<g transform="translate(0 ${d.speedy})"> \
<rect \
width="${d.speedwidth}" \
height="20" \
style="fill:${d.speedcolor}; \
stroke-width:3; \
stroke:rgb(0,0,0)" /> \
<text transform="translate(${d.speedy} 15)"> \
${d.label} \
</text> \
</g>'
tplString += '<g transform="translate(0 ${d.defy})"> \
<rect \
width="${d.defwidth}" \
height="20" \
style="fill:${d.defcolor}; \
stroke-width:3; \
stroke:rgb(0,0,0)" /> \
<text transform="translate(${d.defy} 15)"> \
${d.label} \
</text> \
</g>'
// compile the string to get a template function
var template = _.template(tplString)
function computeX(d, i, speed) {
return 0
}
function computeWidth(d, i, speed) {
if(speed) {return d.Speed}
return d.Defense
}
function computeY(d, i, speed) {
if(speed) {return i * 40}
return 20 + i * 40
}
function computeColor(d, i, speed) {
if(speed) {return 'green'}
return 'blue'
}
function label(d, i){
return d.Name
}
var viz = _.map(pokemonData, function(d, i){
return {
speedx: computeX(d, i, true),
speedy: computeY(d, i, true),
speedwidth: computeWidth(d, i, true),
speedcolor: computeColor(d, i, true),
defx: computeX(d, i, false),
defy: computeY(d, i, false),
label: label(d, i),
defwidth: computeWidth(d, i, false),
defcolor: computeColor(d, i, false)
}
})
console.log('viz', viz)
var result = _.map(viz, function(d){
// invoke the compiled template function on each viz data
return template({d: d})
})
console.log('result', result)
$('.myviz').html('<svg>' + result + '</svg>')
}
$('button#viz-speed-defense').click(vizSpeedDefense)
// TODO: add code to visualize the attack points in ascending order as a
// series of horizontal bar charts (with labels)
function vizAsHorizontalBarsSort(){
// define a template string
var tplString = '<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(${d.width} 15)"> \
${d.label} \
</text> \
</g>'
// compile the string to get a template function
var template = _.template(tplString)
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 label(d, i) {
return d['Name']
}
var viz = _.chain(pokemonData)
.sortBy(function(obj) {
return obj['Attack'];
}).map(function(d, i){
return {
x: computeX(d, i),
y: computeY(d, i),
label: label(d, i),
width: computeWidth(d, i),
color: computeColor(d, i)
}
}).value()
console.log('viz', viz)
var result = _.map(viz, function(d){
// invoke the compiled template function on each viz data
return template({d: d})
})
console.log('result', result)
$('.myviz').html('<svg>' + result + '</svg>')
}
$('button#viz-horizontal-sorted').click(vizAsHorizontalBarsSort)
// TODO: add code to visualize the attack points in descending order as a
// series of horizontal bar charts (with labels)
function vizAsHorizontalBarsSortReverse(){
// define a template string
var tplString = '<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(${d.width} 15)"> \
${d.label} \
</text> \
</g>'
// compile the string to get a template function
var template = _.template(tplString)
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 computeLabel(d, i) {
return d['Name']
}
var viz = _.chain(pokemonData)
.sortBy(function(obj) {
return -obj['Attack'];
}).map(function(d, i){
return {
x: computeX(d, i),
y: computeY(d, i),
label: computeLabel(d, i),
width: computeWidth(d, i),
color: computeColor(d, i)
}
}).value()
console.log('viz', viz)
var result = _.map(viz, function(d){
// invoke the compiled template function on each viz data
return template({d: d})
})
console.log('result', result)
$('.myviz').html('<svg>' + result + '</svg>')
}
$('button#viz-horizontal-sorted-desc').click(vizAsHorizontalBarsSortReverse)
// TODO: add code to visualize the attack points as a series of horizontal bar
// charts (with labels), and using the brightness of red to represent defense
// points
function vizAsHorizontalBarsSpeedRed(){
// TODO: modify this function to visualize the data as horizontal
// bars to compare attack points
// define a template string
var tplString = '<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(${d.width} 15)"> \
${d.label} \
</text> \
</g>'
// compile the string to get a template function
var template = _.template(tplString)
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 'rgb('+d.Speed+', 0, 0)'
}
function label(d, i){
return d.Name
}
var viz = _.map(pokemonData, function(d, i){
return {
x: computeX(d, i),
y: computeY(d, i),
label: label(d, i),
width: computeWidth(d, i),
color: computeColor(d, i)
}
})
console.log('viz', viz)
var result = _.map(viz, function(d){
// invoke the compiled template function on each viz data
return template({d: d})
})
console.log('result', result)
$('.myviz').html('<svg>' + result + '</svg>')
}
$('button#viz-attack-speed').click(vizAsHorizontalBarsSpeedRed)