Individual 1 of 3
Is there a relationship between Review Count and Star Count?
Enter State Abbreviation
(SC, IL, WI, NC, PA, AZ or ALL)Enter City Name
(City Name or ALL)Enter Category
(Category or ALL)Enter Maximum Review Count
(Number or NONE)The Chart, below shows the distribution of Review Counts (X axis), across Star Count (Y axis). In general, the star counts range from 1-5.
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)
})
.error(function(e){
console.error(e)
})
function viz(state, city, category, reviews){
// define a template string
var tplString = '<g> \
<circle cx="${d.x}" \
cy="${d.y}" \
r="4" \
stroke="black" \
fill="red" /> \
</g>'
// compile the string to get a template function
var template = _.template(tplString)
// Filter all entries for the specified state, city and category
var itemsFiltered = _.filter(items, function(d) {
return (state == 'ALL' || d.state == state) &&
(city == 'ALL' || d.city == city ) &&
(category == 'ALL' || _.includes(d.categories, category))
})
// Next, extract star rating and review count
var reviewRatingPairs = _.map(itemsFiltered, function(d) {
return [d.review_count, d.stars]
})
var maxReview =
reviews == 'NONE' ? _.max(_.unzip(reviewRatingPairs)[0]): reviews
// X coordinate is the scaled review count, with jitter
function computeX(d, i) {
return (d[0] + Math.random() -.5) * 600 / maxReview
}
// Y coordinate is the scaled star count, translated to an origin at
// lower left corner, with jitter
function computeY(d, i) {
return 600 - 100 * (d[1] + Math.random() - .5)
}
var viz = _.map(reviewRatingPairs, function(d, i){
return {
x: computeX(d, i),
y: computeY(d, i)
}
})
var result = _.map(viz, function(d){
// invoke the compiled template function on each viz data
return template({d: d})
})
$('.myviz').html('<svg width="100%" height="100%">' + result + '</svg>')
}
$('button#viz').click(function(){
var state = $('input#state').val()
var city = $('input#city').val()
var category = $('input#category').val()
var reviews = $('input#reviews').val()
viz(state, city, category, reviews)
})