Skip to content

Instantly share code, notes, and snippets.

@patilv
Created May 5, 2015 23:17
Show Gist options
  • Save patilv/00277bc2ab8bfffa7e9d to your computer and use it in GitHub Desktop.
Save patilv/00277bc2ab8bfffa7e9d to your computer and use it in GitHub Desktop.
crimebarplot
<!doctype HTML>
<meta charset = 'utf-8'>
<html>
<head>
<link rel='stylesheet' href="http://netdna.bootstrapcdn.com/bootstrap/3.0.3/css/bootstrap.min.css">
<link rel='stylesheet' href='//cdnjs.cloudflare.com/ajax/libs/nvd3/1.1.15-beta/nv.d3.min.css'>
<script src='//ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js' type='text/javascript'></script>
<script src='//d3js.org/d3.v3.min.js' type='text/javascript'></script>
<script src='//cdnjs.cloudflare.com/ajax/libs/nvd3/1.1.15-beta/nv.d3.min.js' type='text/javascript'></script>
<script src='//nvd3.org/assets/lib/fisheye.js' type='text/javascript'></script>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.2.6/angular.min.js"></script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/lodash.js/2.4.1/lodash.min.js"></script>
<style>
.rChart {
display: block
margin: auto auto;
width: 100%;
height: 400px;
}
.bs-docs-example:after {
content: "";
background: transparent;
border: none;
}
</style>
</head>
<body ng-app>
<div class='container' ng-controller="DemoCtrl">
<div class='row'>
<div class='col-md-3'>
<form class='well'>
<label><b>Select y :</b></label>
<select class='form-control' ng-model="opts.y"
ng-options="ctl for ctl in controls.y.values">
</select><br>
</form>
</div>
<div class='col-md-8'>
<div class="bs-docs-example">
<div id='chart1824126d6da' class='rChart '>
<svg></svg>
</div>
</div>
</div>
</div>
</div>
<script type="text/javascript">
function DemoCtrl($scope){
$scope.opts = {
"dom": "chart1824126d6da",
"width": 1200,
"height": 600,
"x": "State",
"y": "ViolentCrime",
"type": "discreteBarChart",
"id": "chart1824126d6da"
}
$scope.data = [
{
"State": " Alabama",
"ViolentCrime": 449.9,
"MurderNonNegligentManslaughter": 7.1,
"ForcibleRape": 26.9,
"Robbery": 104.1,
"AggravatedAssault": 311.8,
"PropertyCrime": 3502.2,
"Burglary": 984.7,
"LarcenyTheft": 2312.8,
"MotorVehicleTheft": 204.8
},
{
"State": " Alaska",
"ViolentCrime": 603.2,
"MurderNonNegligentManslaughter": 4.1,
"ForcibleRape": 79.7,
"Robbery": 86.1,
"AggravatedAssault": 433.2,
"PropertyCrime": 2739.4,
"Burglary": 403.3,
"LarcenyTheft": 2128,
"MotorVehicleTheft": 208.1
},
{
"State": " Arizona",
"ViolentCrime": 428.9,
"MurderNonNegligentManslaughter": 5.5,
"ForcibleRape": 34.7,
"Robbery": 112.7,
"AggravatedAssault": 276,
"PropertyCrime": 3539.2,
"Burglary": 807.8,
"LarcenyTheft": 2439.1,
"MotorVehicleTheft": 292.3
},
{
"State": " Arkansas",
"ViolentCrime": 469.1,
"MurderNonNegligentManslaughter": 5.9,
"ForcibleRape": 42.3,
"Robbery": 78.7,
"AggravatedAssault": 342.3,
"PropertyCrime": 3660.1,
"Burglary": 1081.3,
"LarcenyTheft": 2384.7,
"MotorVehicleTheft": 194.1
},
{
"State": " California",
"ViolentCrime": 423.1,
"MurderNonNegligentManslaughter": 5,
"ForcibleRape": 20.6,
"Robbery": 148.6,
"AggravatedAssault": 248.9,
"PropertyCrime": 2758.7,
"Burglary": 646.1,
"LarcenyTheft": 1669.5,
"MotorVehicleTheft": 443.2
},
{
"State": " Colorado",
"ViolentCrime": 308.9,
"MurderNonNegligentManslaughter": 3.1,
"ForcibleRape": 40.7,
"Robbery": 65.4,
"AggravatedAssault": 199.6,
"PropertyCrime": 2684.7,
"Burglary": 504.2,
"LarcenyTheft": 1947.4,
"MotorVehicleTheft": 233.1
},
{
"State": " Connecticut",
"ViolentCrime": 283,
"MurderNonNegligentManslaughter": 4.1,
"ForcibleRape": 25.6,
"Robbery": 102.7,
"AggravatedAssault": 150.6,
"PropertyCrime": 2140,
"Burglary": 409.7,
"LarcenyTheft": 1550.7,
"MotorVehicleTheft": 179.6
},
{
"State": " Delaware",
"ViolentCrime": 547.4,
"MurderNonNegligentManslaughter": 6.2,
"ForcibleRape": 26.5,
"Robbery": 162.7,
"AggravatedAssault": 352,
"PropertyCrime": 3340.9,
"Burglary": 803.7,
"LarcenyTheft": 2380.6,
"MotorVehicleTheft": 156.6
},
{
"State": " Florida",
"ViolentCrime": 487.1,
"MurderNonNegligentManslaughter": 5.2,
"ForcibleRape": 27.2,
"Robbery": 123.7,
"AggravatedAssault": 330.9,
"PropertyCrime": 3276.7,
"Burglary": 794.9,
"LarcenyTheft": 2288.6,
"MotorVehicleTheft": 193.2
},
{
"State": " Georgia",
"ViolentCrime": 378.9,
"MurderNonNegligentManslaughter": 5.9,
"ForcibleRape": 21.4,
"Robbery": 125.6,
"AggravatedAssault": 226,
"PropertyCrime": 3410.6,
"Burglary": 874.9,
"LarcenyTheft": 2248,
"MotorVehicleTheft": 287.7
},
{
"State": " Hawaii",
"ViolentCrime": 239.2,
"MurderNonNegligentManslaughter": 2.1,
"ForcibleRape": 20.5,
"Robbery": 74.7,
"AggravatedAssault": 141.9,
"PropertyCrime": 3075.2,
"Burglary": 573.1,
"LarcenyTheft": 2243.2,
"MotorVehicleTheft": 258.9
},
{
"State": " Idaho",
"ViolentCrime": 207.9,
"MurderNonNegligentManslaughter": 1.8,
"ForcibleRape": 30,
"Robbery": 15.2,
"AggravatedAssault": 160.9,
"PropertyCrime": 1983.5,
"Burglary": 450.3,
"LarcenyTheft": 1447.7,
"MotorVehicleTheft": 85.5
},
{
"State": " Illinois",
"ViolentCrime": 414.8,
"MurderNonNegligentManslaughter": 5.8,
"ForcibleRape": 27.7,
"Robbery": 151.2,
"AggravatedAssault": 230,
"PropertyCrime": 2578.7,
"Burglary": 552.2,
"LarcenyTheft": 1826.9,
"MotorVehicleTheft": 199.5
},
{
"State": " Indiana",
"ViolentCrime": 345.7,
"MurderNonNegligentManslaughter": 4.7,
"ForcibleRape": 25.5,
"Robbery": 100.9,
"AggravatedAssault": 214.6,
"PropertyCrime": 3029.2,
"Burglary": 728.3,
"LarcenyTheft": 2092,
"MotorVehicleTheft": 209
},
{
"State": " Iowa",
"ViolentCrime": 263.9,
"MurderNonNegligentManslaughter": 1.5,
"ForcibleRape": 28.3,
"Robbery": 31.3,
"AggravatedAssault": 202.8,
"PropertyCrime": 2271.8,
"Burglary": 556.1,
"LarcenyTheft": 1584.8,
"MotorVehicleTheft": 130.9
},
{
"State": " Kansas",
"ViolentCrime": 354.6,
"MurderNonNegligentManslaughter": 2.9,
"ForcibleRape": 36.5,
"Robbery": 52,
"AggravatedAssault": 263.2,
"PropertyCrime": 3143.2,
"Burglary": 650.3,
"LarcenyTheft": 2258,
"MotorVehicleTheft": 234.9
},
{
"State": " Kentucky",
"ViolentCrime": 222.6,
"MurderNonNegligentManslaughter": 4.5,
"ForcibleRape": 29,
"Robbery": 80.7,
"AggravatedAssault": 108.4,
"PropertyCrime": 2552.9,
"Burglary": 675.4,
"LarcenyTheft": 1725.2,
"MotorVehicleTheft": 152.2
},
{
"State": " Louisiana",
"ViolentCrime": 496.9,
"MurderNonNegligentManslaughter": 10.8,
"ForcibleRape": 25.2,
"Robbery": 119,
"AggravatedAssault": 342,
"PropertyCrime": 3540.6,
"Burglary": 915.7,
"LarcenyTheft": 2453.7,
"MotorVehicleTheft": 171.3
},
{
"State": " Maine",
"ViolentCrime": 122.7,
"MurderNonNegligentManslaughter": 1.9,
"ForcibleRape": 28,
"Robbery": 31.8,
"AggravatedAssault": 61,
"PropertyCrime": 2509.9,
"Burglary": 561.3,
"LarcenyTheft": 1873.7,
"MotorVehicleTheft": 74.9
},
{
"State": " Maryland",
"ViolentCrime": 476.8,
"MurderNonNegligentManslaughter": 6.3,
"ForcibleRape": 21,
"Robbery": 172.3,
"AggravatedAssault": 277.2,
"PropertyCrime": 2753.5,
"Burglary": 573.2,
"LarcenyTheft": 1924.9,
"MotorVehicleTheft": 255.3
},
{
"State": " Massachusetts",
"ViolentCrime": 405.5,
"MurderNonNegligentManslaughter": 1.8,
"ForcibleRape": 24.7,
"Robbery": 98.6,
"AggravatedAssault": 280.4,
"PropertyCrime": 2153,
"Burglary": 519.7,
"LarcenyTheft": 1494.4,
"MotorVehicleTheft": 138.9
},
{
"State": " Michigan",
"ViolentCrime": 454.5,
"MurderNonNegligentManslaughter": 7,
"ForcibleRape": 46.4,
"Robbery": 105.6,
"AggravatedAssault": 295.5,
"PropertyCrime": 2530.5,
"Burglary": 664.4,
"LarcenyTheft": 1612,
"MotorVehicleTheft": 254.1
},
{
"State": " Minnesota",
"ViolentCrime": 230.9,
"MurderNonNegligentManslaughter": 1.8,
"ForcibleRape": 30.5,
"Robbery": 64.6,
"AggravatedAssault": 134,
"PropertyCrime": 2568.3,
"Burglary": 471.8,
"LarcenyTheft": 1939.3,
"MotorVehicleTheft": 157.2
},
{
"State": " Mississippi",
"ViolentCrime": 260.8,
"MurderNonNegligentManslaughter": 7.4,
"ForcibleRape": 27.5,
"Robbery": 76.5,
"AggravatedAssault": 149.4,
"PropertyCrime": 2811,
"Burglary": 940.6,
"LarcenyTheft": 1725.6,
"MotorVehicleTheft": 144.8
},
{
"State": " Missouri",
"ViolentCrime": 450.9,
"MurderNonNegligentManslaughter": 6.5,
"ForcibleRape": 25.1,
"Robbery": 96,
"AggravatedAssault": 323.4,
"PropertyCrime": 3314.4,
"Burglary": 705.2,
"LarcenyTheft": 2338.4,
"MotorVehicleTheft": 270.8
},
{
"State": " Montana",
"ViolentCrime": 272.2,
"MurderNonNegligentManslaughter": 2.7,
"ForcibleRape": 37.7,
"Robbery": 19,
"AggravatedAssault": 212.8,
"PropertyCrime": 2583.7,
"Burglary": 387.4,
"LarcenyTheft": 2028.3,
"MotorVehicleTheft": 168
},
{
"State": " Nebraska",
"ViolentCrime": 259.4,
"MurderNonNegligentManslaughter": 2.9,
"ForcibleRape": 38.3,
"Robbery": 60.9,
"AggravatedAssault": 157.4,
"PropertyCrime": 2754.9,
"Burglary": 470.8,
"LarcenyTheft": 2060.7,
"MotorVehicleTheft": 223.5
},
{
"State": " Nevada",
"ViolentCrime": 607.6,
"MurderNonNegligentManslaughter": 4.5,
"ForcibleRape": 33.7,
"Robbery": 178.3,
"AggravatedAssault": 391.1,
"PropertyCrime": 2809.4,
"Burglary": 801.8,
"LarcenyTheft": 1644.6,
"MotorVehicleTheft": 363.1
},
{
"State": " New Hampshire",
"ViolentCrime": 187.9,
"MurderNonNegligentManslaughter": 1.1,
"ForcibleRape": 34,
"Robbery": 35.7,
"AggravatedAssault": 117,
"PropertyCrime": 2324,
"Burglary": 412.2,
"LarcenyTheft": 1834.3,
"MotorVehicleTheft": 77.5
},
{
"State": " New Jersey",
"ViolentCrime": 290.2,
"MurderNonNegligentManslaughter": 4.4,
"ForcibleRape": 11.7,
"Robbery": 128.4,
"AggravatedAssault": 145.7,
"PropertyCrime": 2047.3,
"Burglary": 477.6,
"LarcenyTheft": 1383.7,
"MotorVehicleTheft": 185.9
},
{
"State": " New Mexico",
"ViolentCrime": 559.1,
"MurderNonNegligentManslaughter": 5.6,
"ForcibleRape": 45.9,
"Robbery": 88.6,
"AggravatedAssault": 419.1,
"PropertyCrime": 3600.7,
"Burglary": 1025.3,
"LarcenyTheft": 2313.4,
"MotorVehicleTheft": 261.9
},
{
"State": " New York",
"ViolentCrime": 406.8,
"MurderNonNegligentManslaughter": 3.5,
"ForcibleRape": 14.6,
"Robbery": 146.4,
"AggravatedAssault": 242.3,
"PropertyCrime": 1922,
"Burglary": 329.9,
"LarcenyTheft": 1503.5,
"MotorVehicleTheft": 88.6
},
{
"State": " North Carolina",
"ViolentCrime": 353.4,
"MurderNonNegligentManslaughter": 4.9,
"ForcibleRape": 20.3,
"Robbery": 96.3,
"AggravatedAssault": 231.8,
"PropertyCrime": 3369.5,
"Burglary": 1018.5,
"LarcenyTheft": 2185.7,
"MotorVehicleTheft": 165.3
},
{
"State": " North Dakota",
"ViolentCrime": 244.7,
"MurderNonNegligentManslaughter": 4,
"ForcibleRape": 38.9,
"Robbery": 18.7,
"AggravatedAssault": 183.1,
"PropertyCrime": 2010.1,
"Burglary": 339.8,
"LarcenyTheft": 1505.8,
"MotorVehicleTheft": 164.5
},
{
"State": " Ohio",
"ViolentCrime": 299.7,
"MurderNonNegligentManslaughter": 4.3,
"ForcibleRape": 31.7,
"Robbery": 132,
"AggravatedAssault": 131.7,
"PropertyCrime": 3117.4,
"Burglary": 895.9,
"LarcenyTheft": 2052.5,
"MotorVehicleTheft": 169
},
{
"State": " Oklahoma",
"ViolentCrime": 469.3,
"MurderNonNegligentManslaughter": 5.7,
"ForcibleRape": 41.6,
"Robbery": 84.7,
"AggravatedAssault": 337.3,
"PropertyCrime": 3401,
"Burglary": 936.6,
"LarcenyTheft": 2161.3,
"MotorVehicleTheft": 303.1
},
{
"State": " Oregon",
"ViolentCrime": 247.6,
"MurderNonNegligentManslaughter": 2.4,
"ForcibleRape": 29.2,
"Robbery": 61.9,
"AggravatedAssault": 154.1,
"PropertyCrime": 3224.2,
"Burglary": 561.7,
"LarcenyTheft": 2400.9,
"MotorVehicleTheft": 261.7
},
{
"State": " Pennsylvania",
"ViolentCrime": 348.7,
"MurderNonNegligentManslaughter": 5.4,
"ForcibleRape": 26.1,
"Robbery": 122.8,
"AggravatedAssault": 194.4,
"PropertyCrime": 2166.3,
"Burglary": 445.5,
"LarcenyTheft": 1603.1,
"MotorVehicleTheft": 117.7
},
{
"State": " Rhode Island",
"ViolentCrime": 252.4,
"MurderNonNegligentManslaughter": 3.2,
"ForcibleRape": 27.4,
"Robbery": 67.9,
"AggravatedAssault": 153.9,
"PropertyCrime": 2572.3,
"Burglary": 566.4,
"LarcenyTheft": 1755.7,
"MotorVehicleTheft": 250.2
},
{
"State": " South Carolina",
"ViolentCrime": 558.8,
"MurderNonNegligentManslaughter": 6.9,
"ForcibleRape": 35.5,
"Robbery": 95,
"AggravatedAssault": 421.4,
"PropertyCrime": 3822.2,
"Burglary": 954.5,
"LarcenyTheft": 2588.3,
"MotorVehicleTheft": 279.5
},
{
"State": " South Dakota",
"ViolentCrime": 321.8,
"MurderNonNegligentManslaughter": 3,
"ForcibleRape": 70.2,
"Robbery": 19,
"AggravatedAssault": 229.7,
"PropertyCrime": 2060.1,
"Burglary": 391,
"LarcenyTheft": 1541.4,
"MotorVehicleTheft": 127.8
},
{
"State": " Tennessee",
"ViolentCrime": 643.6,
"MurderNonNegligentManslaughter": 6,
"ForcibleRape": 31.5,
"Robbery": 126.5,
"AggravatedAssault": 479.6,
"PropertyCrime": 3371.4,
"Burglary": 870.2,
"LarcenyTheft": 2300.1,
"MotorVehicleTheft": 201
},
{
"State": " Texas",
"ViolentCrime": 408.6,
"MurderNonNegligentManslaughter": 4.4,
"ForcibleRape": 29.6,
"Robbery": 116.6,
"AggravatedAssault": 258,
"PropertyCrime": 3361.8,
"Burglary": 785.9,
"LarcenyTheft": 2326.4,
"MotorVehicleTheft": 249.4
},
{
"State": " Utah",
"ViolentCrime": 205.8,
"MurderNonNegligentManslaughter": 1.8,
"ForcibleRape": 33,
"Robbery": 38.5,
"AggravatedAssault": 132.5,
"PropertyCrime": 2991.8,
"Burglary": 453.3,
"LarcenyTheft": 2328.4,
"MotorVehicleTheft": 210.1
},
{
"State": " Vermont",
"ViolentCrime": 142.6,
"MurderNonNegligentManslaughter": 1.3,
"ForcibleRape": 19.3,
"Robbery": 17.9,
"AggravatedAssault": 104.2,
"PropertyCrime": 2398.7,
"Burglary": 633.4,
"LarcenyTheft": 1695.8,
"MotorVehicleTheft": 69.5
},
{
"State": " Virginia",
"ViolentCrime": 190.1,
"MurderNonNegligentManslaughter": 3.8,
"ForcibleRape": 17.7,
"Robbery": 57.5,
"AggravatedAssault": 111.1,
"PropertyCrime": 2162.1,
"Burglary": 360.5,
"LarcenyTheft": 1690.7,
"MotorVehicleTheft": 110.9
},
{
"State": " Washington",
"ViolentCrime": 295.6,
"MurderNonNegligentManslaughter": 3,
"ForcibleRape": 31.8,
"Robbery": 83.3,
"AggravatedAssault": 177.5,
"PropertyCrime": 3658.6,
"Burglary": 880.5,
"LarcenyTheft": 2395.3,
"MotorVehicleTheft": 382.8
},
{
"State": " West Virginia",
"ViolentCrime": 316.3,
"MurderNonNegligentManslaughter": 3.9,
"ForcibleRape": 22.7,
"Robbery": 45.2,
"AggravatedAssault": 244.6,
"PropertyCrime": 2364.9,
"Burglary": 608.5,
"LarcenyTheft": 1637.3,
"MotorVehicleTheft": 119.1
},
{
"State": " Wisconsin",
"ViolentCrime": 280.5,
"MurderNonNegligentManslaughter": 3,
"ForcibleRape": 21.3,
"Robbery": 80.7,
"AggravatedAssault": 175.5,
"PropertyCrime": 2453.8,
"Burglary": 488,
"LarcenyTheft": 1822.3,
"MotorVehicleTheft": 143.5
},
{
"State": " Wyoming",
"ViolentCrime": 201.4,
"MurderNonNegligentManslaughter": 2.4,
"ForcibleRape": 26.7,
"Robbery": 10.6,
"AggravatedAssault": 161.7,
"PropertyCrime": 2293.8,
"Burglary": 368.7,
"LarcenyTheft": 1823.9,
"MotorVehicleTheft": 101.3
}
]
$scope.controls = {
"y": {
"name": "y",
"value": "ViolentCrime",
"values": [ "ViolentCrime", "MurderNonNegligentManslaughter", "ForcibleRape", "Robbery", "AggravatedAssault", "PropertyCrime", "Burglary", "LarcenyTheft", "MotorVehicleTheft" ],
"label": "Select y :"
}
}
$scope.filters = []
$scope.drawChart = function(){
drawChart($scope.opts, $scope.data)
}
$scope.$watch('selected', function(){
var keys = _.pluck($scope.selected, "variable")
var values = _.pluck($scope.selected, "value")
$scope.opts.selected = _.zipObject(keys, values)
})
$scope.$watch('opts',function(){
$scope.drawChart()
}, true)
}
function drawChart(opts, data){
if (Object.keys(opts.selected).length > 0){
data = _.filter(data, opts.selected)
}
if(!(opts.type==="pieChart" || opts.type==="sparklinePlus" || opts.type==="bulletChart")) {
var data = d3.nest()
.key(function(d){
//return opts.group === undefined ? 'main' : d[opts.group]
//instead of main would think a better default is opts.x
return opts.group === undefined ? opts.y : d[opts.group];
}).entries(data);
}
if (opts.disabled != undefined){
data.map(function(d, i){
d.disabled = opts.disabled[i]
})
}
nv.addGraph(function() {
var chart = nv.models[opts.type]()
.x(function(d) { return d[opts.x] })
.y(function(d) { return d[opts.y] })
.width(opts.width)
.height(opts.height)
chart.xAxis
.rotateLabels( -90)
d3.select("#" + opts.id + ' svg')
// .empty()
.datum(data)
.transition().duration(500)
.call(chart);
nv.utils.windowResize(chart.update);
return chart;
});
};
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment