Individual 3 of 3
How many establishments of category (X) are in each city of state (Y) with at least a rating of (Z)?
Category
State (SC, IL, WI, PA, NC, AZ)
Minimum Rating (0.0 - 5.0)
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="150" \
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) {
// FIXME: Need to scale
return d.count * 5
}
function computeY(d, i) {
return i * 20
}
function computeColor(d, i) {
return 'yellow'
}
function computeLabel(d, i) {
return d.city + ' (' + d.count + ')'
}
// UI processing logic
console.log(arg0); console.log(arg1); console.log(arg2);
var filter_state = _.filter(items, function(d) {
return (d.state == arg1)
})
var filter_stars = _.filter(filter_state, function(d) {
return (d.stars >= parseFloat(arg2))
})
// Have filtered 'items' based on city, #reviews and rating (stars)
// Find all objects with a category element matching user input 'arg0'
var filter_categories = _.filter(filter_stars, function(f) {
return _.some(f.categories, function(d) {
return d == arg0
})
})
var groups = _.groupBy(filter_categories, 'city')
var results = _.mapValues(groups, function(d) {
return d.length
})
var new_results = _.map(_.pairs(results), function(d) {
return { city: d[0], count: 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)
}
})
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