albertlyu / shot-charts-site

A Rails web application for visualizing NCAA men's basketball play-by-play shot location data
MIT License
3 stars 1 forks source link

Add shot frequency heatmap #33

Closed albertlyu closed 10 years ago

albertlyu commented 10 years ago

The Highcharts shot charts with the tooltips are nice. However, there is a lot of noise with a scatter chart. A heatmap would be nice as a shot frequency chart. We can also create a shot efficiency chart by weighting the points by 2, 3, or 0 depending on if the shot was a 2-pt field goal made, a 3-pt field goal made, or field goal missed.

Perhaps heatmap.js would be nice for this - the downside is no tooltips. Maybe that's okay. We can use this example. http://www.patrick-wied.at/static/heatmapjs/examples.html

albertlyu commented 10 years ago

So I was able to create the heatmap for shot frequency, but it was a pretty hack-y way of doing it. And apparently, all the shots at the origin have disappeared, so I'll have to figure out how to fix that at some point. Will play around with the configuration though.

image

albertlyu commented 10 years ago

Also, rendering these charts - extremely poor performance. We'll have to get rid of this for loop

for (var ii = 0; ii < shots.length; ii++) {
    var xcoord = -shots[ii]['x_coord']*width/50+width/2;
    var ycoord = -shots[ii]['y_coord']*height/30+height-35;;
    heatmap.store.addDataPoint(xcoord, ycoord, [1]);
}
albertlyu commented 10 years ago

Alright, the heatmaps finally work in production. From what I can tell, here are the requirements:

  1. Include heatmap.js in the assets/javascripts directory.
  2. Add //= require heatmap to assets/javascripts/application.js.
  3. Add config.assets.precompile += %w( heatmap.js ) to config/environments/production.rb.
  4. Add config.serve_static_assets = true to config/application.rb.
  5. Finally, run rake assets:precompile locally to create public/assets/application-6ced7880aa6f677cc42c8fe8492515d6.js.
  6. Commit all files, push to GitHub, and push to Heroku.

Took awhile to figure out, but the shot frequency heat charts are now live on the player pages in production.

albertlyu commented 10 years ago

As nice as these heatmaps are, unfortunately, the DOM processing time significantly increased. I'm seeing 20-25 seconds per request for the player pages in production now, when it used to be 5 seconds per request for the player pages with only the scatter charts. To be fair, I tested this by opening up 10 player pages at the same time, asynchronously, whereas I did not try that method of performance testing before the heatmap rollouts. This isn't enough of an issue (yet) for me to rollback the most recent feature in production, but it's definitely something to be alerted of to be monitored closely...

albertlyu commented 10 years ago

Okay, I jumped the gun. Turns out player page loads one-at-a-time averages more around 5-6 seconds. Still worth monitoring, and it's still too slow. But it's not due to these new heatmaps, probably like more due to the queries and app's server. So continue the discussion on #41.