Extract graph creation duplicated code to function
Clean up code based on http://bost.ocks.org/mike/bar/3/