Individual 2 of 3
What cities in state (X) have the highest average rating for category (Y) and are open on Day (Z)?
Category
State (SC, IL, WI, PA, NC, AZ)
Day of Week (e.g., Wednesday)
Data is not loaded yet
items = 'not loaded yet'
$.get('http://bigdatahci2015.github.io/data/yelp/yelp_academic_dataset_business.5000.json.lines.txt')
.success(function(data){
var lines = data.trim().split('\n')
// Convert text lines to json arrays and save them in `items`
items = _.map(lines, JSON.parse)
console.log('number of items loaded:', items.length)
// Show in the myviz that the data is loaded
$('.myviz').html('number of records load:' + data.length)
console.log('first item', items[0])
})
.error(function(e){
console.error(e)
})
function viz(arg0, arg1, arg2) {
// Define a template string
var tplString = '<g transform="translate(0 ${d.y})"> \
<text y="15">${d.label}</text> \
<rect x="210" \
width="${d.width}" \
height="20" \
style="fill:${d.color}; \
stroke-width:3; \
stroke:rgb(0,0,0)" /> \
<text x=220 y="15">${d.label3}</text> \
<text x=600 y="15">${d.label2}</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.ave * 60
}
function computeY(d, i) {
return i * 20
}
function computeColor(d, i, max_reviews) {
return 'red'
}
function computeLabel(d, i) {
return d.city
}
function computeLabel2(d, i) {
return 'Open on ' + arg2
}
function computeLabel3(d, i) {
return 'Rating: ' + d.ave
}
// UI processing logic
// console.log(arg0); console.log(arg1); console.log(arg2)
var filter_state = _.filter(items, function(d) {
return (d.state == arg1)
})
// Have filtered 'items' based on state
// Find all objects with a category element matching user input 'arg0'
var filter_categories = _.filter(filter_state, function(f) {
return _.some(f.categories, function(d) {
return d == arg0
})
})
// Find all objects open on user-specified day of week (arg2)
var filter_days = _.filter(filter_categories, function(d) {
if (arg2 == 'Sunday') {
if ( _.isUndefined(d.hours.Sunday) ) { return 0 }
else { return 1 }
}
else if (arg2 == 'Monday') {
if ( _.isUndefined(d.hours.Monday) ) { return 0 }
else { return 1 }
}
else if (arg2 == 'Tuesday') {
if ( _.isUndefined(d.hours.Tuesday) ) { return 0 }
else { return 1 }
}
else if (arg2 == 'Wednesday') {
if ( _.isUndefined(d.hours.Wednesday) ) { return 0 }
else { return 1 }
}
else if (arg2 == 'Thursday') {
if ( _.isUndefined(d.hours.Thursday) ) { return 0 }
else { return 1 }
}
else if (arg2 == 'Friday') {
if ( _.isUndefined(d.hours.Friday) ) { return 0 }
else { return 1 }
}
else if (arg2 == 'Saturday') {
if ( _.isUndefined(d.hours.Saturday) ) { return 0 }
else { return 1 }
}
})
var groups = _.groupBy(filter_days, 'city')
var results = _.mapValues(groups, function(d) {
var value = _.pluck(d, 'stars')
var filtered_value = _.filter(value, function(d) {
return d != null
})
var filtered_average = _.round(_.sum(filtered_value)/filtered_value.length, 2)
return filtered_average
})
var new_results = _.map(_.pairs(results), function(d) {
return { city: d[0], ave: d[1] }
})
var vizData = new_results
// Take the first 20 items to visualize
var vizItems = _.take(vizData, 20)
var viz = _.map(vizItems, function(d, i) {
return {
x: computeX(d, i),
y: computeY(d, i),
width: computeWidth(d, i),
color: computeColor(d, i),
label: computeLabel(d, i),
label2: computeLabel2(d, i),
label3: computeLabel3(d, i)
}
})
var result = _.map(viz, function(d){
return template({d: d}) // invoke the compiled template function on each viz data
})
$('.myviz').html('<svg width="100%" height="100%">' + result + '</svg>')
}
$('button#viz').click(function(){
var arg0 = $('input#arg0').val()
var arg1 = $('input#arg1').val()
var arg2 = $('input#arg2').val()
viz(arg0, arg1, arg2)
})
Authors
This UI is developed by