Skip to content

Instantly share code, notes, and snippets.

@molliemarie
Last active March 6, 2019 21:49
Show Gist options
  • Save molliemarie/06630abcdb4bc9f2dccd206df7850a42 to your computer and use it in GitHub Desktop.
Save molliemarie/06630abcdb4bc9f2dccd206df7850a42 to your computer and use it in GitHub Desktop.
ChicagoCrimeFinished
license: mit
year Primary Type count
2001 ARSON 1010
2001 ASSAULT 31384
2001 BATTERY 93447
2001 BURGLARY 26011
2001 CRIM SEXUAL ASSAULT 1798
2001 CRIMINAL DAMAGE 55851
2001 CRIMINAL TRESPASS 13240
2001 DECEPTIVE PRACTICE 14906
2001 DOMESTIC VIOLENCE 1
2001 GAMBLING 934
2001 HOMICIDE 667
2001 INTERFERENCE WITH PUBLIC OFFICER 406
2001 INTIMIDATION 279
2001 KIDNAPPING 933
2001 LIQUOR LAW VIOLATION 1637
2001 MOTOR VEHICLE THEFT 27549
2001 NARCOTICS 50567
2001 OBSCENITY 19
2001 OFFENSE INVOLVING CHILDREN 2242
2001 OTHER NARCOTIC VIOLATION 6
2001 OTHER OFFENSE 29657
2001 PROSTITUTION 6026
2001 PUBLIC INDECENCY 9
2001 PUBLIC PEACE VIOLATION 2750
2001 RITUALISM 8
2001 ROBBERY 18441
2001 SEX OFFENSE 2231
2001 STALKING 203
2001 THEFT 99264
2001 WEAPONS VIOLATION 4274
2002 ARSON 1032
2002 ASSAULT 31521
2002 BATTERY 94153
2002 BURGLARY 25623
2002 CRIM SEXUAL ASSAULT 1830
2002 CRIMINAL DAMAGE 55940
2002 CRIMINAL TRESPASS 13880
2002 DECEPTIVE PRACTICE 13717
2002 GAMBLING 971
2002 HOMICIDE 657
2002 INTERFERENCE WITH PUBLIC OFFICER 361
2002 INTIMIDATION 337
2002 KIDNAPPING 829
2002 LIQUOR LAW VIOLATION 1414
2002 MOTOR VEHICLE THEFT 25121
2002 NARCOTICS 51789
2002 OBSCENITY 26
2002 OFFENSE INVOLVING CHILDREN 2577
2002 OTHER NARCOTIC VIOLATION 5
2002 OTHER OFFENSE 32599
2002 PROSTITUTION 6408
2002 PUBLIC INDECENCY 8
2002 PUBLIC PEACE VIOLATION 2457
2002 RITUALISM 2
2002 ROBBERY 18522
2002 SEX OFFENSE 2167
2002 STALKING 200
2002 THEFT 98327
2002 WEAPONS VIOLATION 4281
2003 ARSON 955
2003 ASSAULT 29477
2003 BATTERY 88378
2003 BURGLARY 25156
2003 CRIM SEXUAL ASSAULT 1597
2003 CRIMINAL DAMAGE 55011
2003 CRIMINAL TRESPASS 14807
2003 DECEPTIVE PRACTICE 13452
2003 GAMBLING 1088
2003 HOMICIDE 604
2003 INTERFERENCE WITH PUBLIC OFFICER 408
2003 INTIMIDATION 364
2003 KIDNAPPING 705
2003 LIQUOR LAW VIOLATION 1311
2003 MOTOR VEHICLE THEFT 22748
2003 NARCOTICS 54288
2003 NON-CRIMINAL 1
2003 OBSCENITY 16
2003 OFFENSE INVOLVING CHILDREN 3035
2003 OTHER NARCOTIC VIOLATION 2
2003 OTHER OFFENSE 31147
2003 PROSTITUTION 6214
2003 PUBLIC INDECENCY 6
2003 PUBLIC PEACE VIOLATION 2430
2003 RITUALISM 2
2003 ROBBERY 17332
2003 SEX OFFENSE 2073
2003 STALKING 247
2003 THEFT 98875
2003 WEAPONS VIOLATION 4211
2004 ARSON 778
2004 ASSAULT 28850
2004 BATTERY 87136
2004 BURGLARY 24564
2004 CRIM SEXUAL ASSAULT 1573
2004 CRIMINAL DAMAGE 53164
2004 CRIMINAL TRESPASS 15913
2004 DECEPTIVE PRACTICE 13247
2004 GAMBLING 1122
2004 HOMICIDE 454
2004 INTERFERENCE WITH PUBLIC OFFICER 531
2004 INTIMIDATION 349
2004 KIDNAPPING 481
2004 LIQUOR LAW VIOLATION 985
2004 MOTOR VEHICLE THEFT 22805
2004 NARCOTICS 57060
2004 OBSCENITY 13
2004 OFFENSE INVOLVING CHILDREN 3080
2004 OTHER NARCOTIC VIOLATION 9
2004 OTHER OFFENSE 29532
2004 PROSTITUTION 7476
2004 PUBLIC INDECENCY 9
2004 PUBLIC PEACE VIOLATION 2495
2004 RITUALISM 1
2004 ROBBERY 15978
2004 SEX OFFENSE 1803
2004 STALKING 215
2004 THEFT 95463
2004 WEAPONS VIOLATION 4297
2005 ARSON 691
2005 ASSAULT 27066
2005 BATTERY 83965
2005 BURGLARY 25504
2005 CRIM SEXUAL ASSAULT 1547
2005 CRIMINAL DAMAGE 54548
2005 CRIMINAL TRESPASS 16655
2005 DECEPTIVE PRACTICE 13570
2005 GAMBLING 1078
2005 HOMICIDE 453
2005 INTERFERENCE WITH PUBLIC OFFICER 615
2005 INTIMIDATION 258
2005 KIDNAPPING 389
2005 LIQUOR LAW VIOLATION 1005
2005 MOTOR VEHICLE THEFT 22497
2005 NARCOTICS 56234
2005 OBSCENITY 19
2005 OFFENSE INVOLVING CHILDREN 2885
2005 OTHER NARCOTIC VIOLATION 9
2005 OTHER OFFENSE 28028
2005 PROSTITUTION 6124
2005 PUBLIC INDECENCY 4
2005 PUBLIC PEACE VIOLATION 2730
2005 RITUALISM 2
2005 ROBBERY 16047
2005 SEX OFFENSE 1807
2005 STALKING 192
2005 THEFT 85685
2005 WEAPONS VIOLATION 4106
2006 ARSON 726
2006 ASSAULT 25945
2006 BATTERY 80666
2006 BURGLARY 24324
2006 CRIM SEXUAL ASSAULT 1468
2006 CRIMINAL DAMAGE 57124
2006 CRIMINAL TRESPASS 14505
2006 DECEPTIVE PRACTICE 13587
2006 GAMBLING 1368
2006 HOMICIDE 476
2006 INTERFERENCE WITH PUBLIC OFFICER 758
2006 INTIMIDATION 276
2006 KIDNAPPING 339
2006 LIQUOR LAW VIOLATION 1135
2006 MOTOR VEHICLE THEFT 21818
2006 NARCOTICS 55813
2006 OBSCENITY 17
2006 OFFENSE INVOLVING CHILDREN 2756
2006 OTHER NARCOTIC VIOLATION 9
2006 OTHER OFFENSE 27100
2006 PROSTITUTION 7034
2006 PUBLIC INDECENCY 4
2006 PUBLIC PEACE VIOLATION 3068
2006 RITUALISM 7
2006 ROBBERY 15968
2006 SEX OFFENSE 1565
2006 STALKING 186
2006 THEFT 86240
2006 WEAPONS VIOLATION 3821
2007 ARSON 712
2007 ASSAULT 26313
2007 BATTERY 79591
2007 BURGLARY 24858
2007 CRIM SEXUAL ASSAULT 1542
2007 CRIMINAL DAMAGE 53749
2007 CRIMINAL TRESPASS 13699
2007 DECEPTIVE PRACTICE 14150
2007 GAMBLING 1409
2007 HOMICIDE 447
2007 INTERFERENCE WITH PUBLIC OFFICER 677
2007 INTIMIDATION 255
2007 KIDNAPPING 330
2007 LIQUOR LAW VIOLATION 1170
2007 MOTOR VEHICLE THEFT 18573
2007 NARCOTICS 54454
2007 OBSCENITY 11
2007 OFFENSE INVOLVING CHILDREN 2884
2007 OTHER NARCOTIC VIOLATION 12
2007 OTHER OFFENSE 26863
2007 PROSTITUTION 6087
2007 PUBLIC INDECENCY 5
2007 PUBLIC PEACE VIOLATION 3315
2007 RITUALISM 1
2007 ROBBERY 15450
2007 SEX OFFENSE 1528
2007 STALKING 213
2007 THEFT 85156
2007 WEAPONS VIOLATION 3554
2008 ARSON 644
2008 ASSAULT 25447
2008 BATTERY 75922
2008 BURGLARY 26218
2008 CRIM SEXUAL ASSAULT 1531
2008 CRIMINAL DAMAGE 52841
2008 CRIMINAL TRESPASS 12310
2008 DECEPTIVE PRACTICE 14892
2008 GAMBLING 1199
2008 HOMICIDE 513
2008 INTERFERENCE WITH PUBLIC OFFICER 580
2008 INTIMIDATION 261
2008 KIDNAPPING 359
2008 LIQUOR LAW VIOLATION 912
2008 MOTOR VEHICLE THEFT 18881
2008 NARCOTICS 46507
2008 OBSCENITY 13
2008 OFFENSE INVOLVING CHILDREN 2622
2008 OTHER NARCOTIC VIOLATION 12
2008 OTHER OFFENSE 26533
2008 PROSTITUTION 5141
2008 PUBLIC INDECENCY 4
2008 PUBLIC PEACE VIOLATION 3013
2008 ROBBERY 16703
2008 SEX OFFENSE 1491
2008 STALKING 190
2008 THEFT 88433
2008 WEAPONS VIOLATION 3877
2009 ARSON 616
2009 ASSAULT 22861
2009 BATTERY 68462
2009 BURGLARY 26766
2009 CRIM SEXUAL ASSAULT 1418
2009 CRIMINAL DAMAGE 47724
2009 CRIMINAL TRESPASS 10851
2009 DECEPTIVE PRACTICE 13819
2009 GAMBLING 991
2009 HOMICIDE 460
2009 INTERFERENCE WITH PUBLIC OFFICER 573
2009 INTIMIDATION 231
2009 KIDNAPPING 293
2009 LIQUOR LAW VIOLATION 746
2009 MOTOR VEHICLE THEFT 15482
2009 NARCOTICS 43543
2009 NON-CRIMINAL 1
2009 OBSCENITY 21
2009 OFFENSE INVOLVING CHILDREN 2581
2009 OTHER NARCOTIC VIOLATION 7
2009 OTHER OFFENSE 25601
2009 PROSTITUTION 3940
2009 PUBLIC INDECENCY 10
2009 PUBLIC PEACE VIOLATION 3147
2009 ROBBERY 15980
2009 SEX OFFENSE 1254
2009 STALKING 167
2009 THEFT 80973
2009 WEAPONS VIOLATION 4158
2010 ARSON 522
2010 ASSAULT 21536
2010 BATTERY 65403
2010 BURGLARY 26422
2010 CRIM SEXUAL ASSAULT 1359
2010 CRIMINAL DAMAGE 40653
2010 CRIMINAL TRESPASS 9401
2010 DECEPTIVE PRACTICE 12500
2010 GAMBLING 927
2010 HOMICIDE 438
2010 INTERFERENCE WITH PUBLIC OFFICER 797
2010 INTIMIDATION 197
2010 KIDNAPPING 313
2010 LIQUOR LAW VIOLATION 736
2010 MOTOR VEHICLE THEFT 19029
2010 NARCOTICS 43393
2010 NON-CRIMINAL 1
2010 OBSCENITY 34
2010 OFFENSE INVOLVING CHILDREN 2554
2010 OTHER NARCOTIC VIOLATION 6
2010 OTHER OFFENSE 22013
2010 PROSTITUTION 2485
2010 PUBLIC INDECENCY 7
2010 PUBLIC PEACE VIOLATION 3538
2010 ROBBERY 14274
2010 SEX OFFENSE 1117
2010 STALKING 189
2010 THEFT 76754
2010 WEAPONS VIOLATION 3704
2011 ARSON 504
2011 ASSAULT 20411
2011 BATTERY 60458
2011 BURGLARY 26619
2011 CRIM SEXUAL ASSAULT 1481
2011 CRIMINAL DAMAGE 37332
2011 CRIMINAL TRESPASS 8659
2011 DECEPTIVE PRACTICE 12642
2011 GAMBLING 736
2011 HOMICIDE 438
2011 INTERFERENCE WITH PUBLIC OFFICER 1048
2011 INTIMIDATION 171
2011 KIDNAPPING 266
2011 LIQUOR LAW VIOLATION 619
2011 MOTOR VEHICLE THEFT 19388
2011 NARCOTICS 38605
2011 OBSCENITY 40
2011 OFFENSE INVOLVING CHILDREN 2352
2011 OTHER NARCOTIC VIOLATION 5
2011 OTHER OFFENSE 20191
2011 PROSTITUTION 2424
2011 PUBLIC INDECENCY 13
2011 PUBLIC PEACE VIOLATION 3095
2011 ROBBERY 13983
2011 SEX OFFENSE 1075
2011 STALKING 181
2011 THEFT 75149
2011 WEAPONS VIOLATION 3880
2012 ARSON 469
2012 ASSAULT 19898
2012 BATTERY 59135
2012 BURGLARY 22843
2012 CRIM SEXUAL ASSAULT 1423
2012 CRIMINAL DAMAGE 35854
2012 CRIMINAL TRESPASS 8216
2012 DECEPTIVE PRACTICE 13618
2012 GAMBLING 724
2012 HOMICIDE 506
2012 INTERFERENCE WITH PUBLIC OFFICER 1228
2012 INTIMIDATION 156
2012 KIDNAPPING 235
2012 LIQUOR LAW VIOLATION 573
2012 MOTOR VEHICLE THEFT 16492
2012 NARCOTICS 35488
2012 NON-CRIMINAL 6
2012 NON-CRIMINAL (SUBJECT SPECIFIED) 2
2012 OBSCENITY 26
2012 OFFENSE INVOLVING CHILDREN 2219
2012 OTHER NARCOTIC VIOLATION 6
2012 OTHER OFFENSE 17481
2012 PROSTITUTION 2204
2012 PUBLIC INDECENCY 17
2012 PUBLIC PEACE VIOLATION 3007
2012 ROBBERY 13485
2012 SEX OFFENSE 1063
2012 STALKING 207
2012 THEFT 75460
2012 WEAPONS VIOLATION 3907
2013 ARSON 364
2013 ASSAULT 17972
2013 BATTERY 54002
2013 BURGLARY 17894
2013 CRIM SEXUAL ASSAULT 1300
2013 CRIMINAL DAMAGE 30853
2013 CRIMINAL TRESPASS 8136
2013 DECEPTIVE PRACTICE 13726
2013 GAMBLING 596
2013 HOMICIDE 422
2013 HUMAN TRAFFICKING 2
2013 INTERFERENCE WITH PUBLIC OFFICER 1281
2013 INTIMIDATION 134
2013 KIDNAPPING 242
2013 LIQUOR LAW VIOLATION 465
2013 MOTOR VEHICLE THEFT 12582
2013 NARCOTICS 34127
2013 NON - CRIMINAL 3
2013 NON-CRIMINAL 4
2013 OBSCENITY 24
2013 OFFENSE INVOLVING CHILDREN 2373
2013 OTHER NARCOTIC VIOLATION 5
2013 OTHER OFFENSE 17994
2013 PROSTITUTION 1652
2013 PUBLIC INDECENCY 10
2013 PUBLIC PEACE VIOLATION 3135
2013 ROBBERY 11820
2013 SEX OFFENSE 1026
2013 STALKING 154
2013 THEFT 71530
2013 WEAPONS VIOLATION 3246
2014 ARSON 397
2014 ASSAULT 16901
2014 BATTERY 49447
2014 BURGLARY 14570
2014 CONCEALED CARRY LICENSE VIOLATION 15
2014 CRIM SEXUAL ASSAULT 1343
2014 CRIMINAL DAMAGE 27798
2014 CRIMINAL TRESPASS 7539
2014 DECEPTIVE PRACTICE 15679
2014 GAMBLING 393
2014 HOMICIDE 426
2014 HUMAN TRAFFICKING 2
2014 INTERFERENCE WITH PUBLIC OFFICER 1399
2014 INTIMIDATION 115
2014 KIDNAPPING 220
2014 LIQUOR LAW VIOLATION 397
2014 MOTOR VEHICLE THEFT 9912
2014 NARCOTICS 29117
2014 NON - CRIMINAL 10
2014 NON-CRIMINAL 17
2014 NON-CRIMINAL (SUBJECT SPECIFIED) 1
2014 OBSCENITY 38
2014 OFFENSE INVOLVING CHILDREN 2389
2014 OTHER NARCOTIC VIOLATION 10
2014 OTHER OFFENSE 16979
2014 PROSTITUTION 1626
2014 PUBLIC INDECENCY 10
2014 PUBLIC PEACE VIOLATION 2903
2014 ROBBERY 9799
2014 SEX OFFENSE 980
2014 STALKING 140
2014 THEFT 61562
2014 WEAPONS VIOLATION 3114
2015 ARSON 453
2015 ASSAULT 17045
2015 BATTERY 48920
2015 BURGLARY 13183
2015 CONCEALED CARRY LICENSE VIOLATION 34
2015 CRIM SEXUAL ASSAULT 1395
2015 CRIMINAL DAMAGE 28675
2015 CRIMINAL TRESPASS 6401
2015 DECEPTIVE PRACTICE 16003
2015 GAMBLING 310
2015 HOMICIDE 502
2015 HUMAN TRAFFICKING 13
2015 INTERFERENCE WITH PUBLIC OFFICER 1308
2015 INTIMIDATION 123
2015 KIDNAPPING 190
2015 LIQUOR LAW VIOLATION 292
2015 MOTOR VEHICLE THEFT 10068
2015 NARCOTICS 23883
2015 NON - CRIMINAL 20
2015 NON-CRIMINAL 16
2015 OBSCENITY 48
2015 OFFENSE INVOLVING CHILDREN 2322
2015 OTHER NARCOTIC VIOLATION 5
2015 OTHER OFFENSE 17553
2015 PROSTITUTION 1322
2015 PUBLIC INDECENCY 14
2015 PUBLIC PEACE VIOLATION 2422
2015 ROBBERY 9639
2015 SEX OFFENSE 997
2015 STALKING 154
2015 THEFT 57336
2015 WEAPONS VIOLATION 3364
2016 ARSON 516
2016 ASSAULT 18739
2016 BATTERY 50292
2016 BURGLARY 14287
2016 CONCEALED CARRY LICENSE VIOLATION 36
2016 CRIM SEXUAL ASSAULT 1560
2016 CRIMINAL DAMAGE 31018
2016 CRIMINAL TRESPASS 6308
2016 DECEPTIVE PRACTICE 18788
2016 GAMBLING 189
2016 HOMICIDE 786
2016 HUMAN TRAFFICKING 11
2016 INTERFERENCE WITH PUBLIC OFFICER 936
2016 INTIMIDATION 135
2016 KIDNAPPING 202
2016 LIQUOR LAW VIOLATION 227
2016 MOTOR VEHICLE THEFT 11294
2016 NARCOTICS 13287
2016 NON - CRIMINAL 5
2016 NON-CRIMINAL 49
2016 NON-CRIMINAL (SUBJECT SPECIFIED) 1
2016 OBSCENITY 51
2016 OFFENSE INVOLVING CHILDREN 2315
2016 OTHER NARCOTIC VIOLATION 4
2016 OTHER OFFENSE 17281
2016 PROSTITUTION 800
2016 PUBLIC INDECENCY 10
2016 PUBLIC PEACE VIOLATION 1607
2016 ROBBERY 11960
2016 SEX OFFENSE 984
2016 STALKING 175
2016 THEFT 61601
2016 WEAPONS VIOLATION 3449
2017 ARSON 444
2017 ASSAULT 19301
2017 BATTERY 49215
2017 BURGLARY 13000
2017 CONCEALED CARRY LICENSE VIOLATION 69
2017 CRIM SEXUAL ASSAULT 1616
2017 CRIMINAL DAMAGE 29041
2017 CRIMINAL TRESPASS 6812
2017 DECEPTIVE PRACTICE 18913
2017 GAMBLING 191
2017 HOMICIDE 674
2017 HUMAN TRAFFICKING 9
2017 INTERFERENCE WITH PUBLIC OFFICER 1086
2017 INTIMIDATION 151
2017 KIDNAPPING 190
2017 LIQUOR LAW VIOLATION 191
2017 MOTOR VEHICLE THEFT 11408
2017 NARCOTICS 11653
2017 NON-CRIMINAL 38
2017 NON-CRIMINAL (SUBJECT SPECIFIED) 2
2017 OBSCENITY 87
2017 OFFENSE INVOLVING CHILDREN 2266
2017 OTHER NARCOTIC VIOLATION 11
2017 OTHER OFFENSE 17223
2017 PROSTITUTION 735
2017 PUBLIC INDECENCY 10
2017 PUBLIC PEACE VIOLATION 1498
2017 ROBBERY 11877
2017 SEX OFFENSE 1020
2017 STALKING 188
2017 THEFT 64339
2017 WEAPONS VIOLATION 4686
2018 ARSON 372
2018 ASSAULT 20368
2018 BATTERY 49774
2018 BURGLARY 11709
2018 CONCEALED CARRY LICENSE VIOLATION 149
2018 CRIM SEXUAL ASSAULT 1597
2018 CRIMINAL DAMAGE 27792
2018 CRIMINAL TRESPASS 6899
2018 DECEPTIVE PRACTICE 18347
2018 GAMBLING 201
2018 HOMICIDE 585
2018 HUMAN TRAFFICKING 14
2018 INTERFERENCE WITH PUBLIC OFFICER 1305
2018 INTIMIDATION 167
2018 KIDNAPPING 172
2018 LIQUOR LAW VIOLATION 266
2018 MOTOR VEHICLE THEFT 9993
2018 NARCOTICS 12833
2018 NON-CRIMINAL 38
2018 NON-CRIMINAL (SUBJECT SPECIFIED) 3
2018 OBSCENITY 86
2018 OFFENSE INVOLVING CHILDREN 2239
2018 OTHER NARCOTIC VIOLATION 1
2018 OTHER OFFENSE 17061
2018 PROSTITUTION 718
2018 PUBLIC INDECENCY 14
2018 PUBLIC PEACE VIOLATION 1367
2018 ROBBERY 9688
2018 SEX OFFENSE 1067
2018 STALKING 202
2018 THEFT 64942
2018 WEAPONS VIOLATION 5445
2019 ARSON 19
2019 ASSAULT 1263
2019 BATTERY 3136
2019 BURGLARY 724
2019 CONCEALED CARRY LICENSE VIOLATION 9
2019 CRIM SEXUAL ASSAULT 118
2019 CRIMINAL DAMAGE 1617
2019 CRIMINAL TRESPASS 519
2019 DECEPTIVE PRACTICE 1041
2019 GAMBLING 4
2019 HOMICIDE 19
2019 HUMAN TRAFFICKING 3
2019 INTERFERENCE WITH PUBLIC OFFICER 88
2019 INTIMIDATION 21
2019 KIDNAPPING 8
2019 LIQUOR LAW VIOLATION 15
2019 MOTOR VEHICLE THEFT 579
2019 NARCOTICS 1065
2019 NON-CRIMINAL 2
2019 OBSCENITY 3
2019 OFFENSE INVOLVING CHILDREN 159
2019 OTHER OFFENSE 1171
2019 PROSTITUTION 63
2019 PUBLIC PEACE VIOLATION 80
2019 ROBBERY 617
2019 SEX OFFENSE 57
2019 STALKING 14
2019 THEFT 3981
2019 WEAPONS VIOLATION 420
<!DOCTYPE html>
<meta charset="utf-8">
<script src="https://d3js.org/d3.v4.min.js"></script>
<style type="text/css">
/*Add styling here */
/* svg {
border: 1px solid #f0f;
} */
.axis text {
text-transform: capitalize;
}
</style>
<body>
<!-- Add title here -->
<div id=#titleDiv>
<h1>Crime in Chicago in 2018</h1>
</div>
</body>
<script>
function titleCase(str) {
// lowercases each word, only capitalizing the first letter of each word
var splitStr = str.toLowerCase().split(' ');
for (var i = 0; i < splitStr.length; i++) {
// You do not need to check if i is larger than splitStr length, as your for does that for you
// Assign it back to the array
splitStr[i] = splitStr[i].charAt(0).toUpperCase() + splitStr[i].substring(1);
}
// Directly return the joined string
return splitStr.join(' ');
}
// Reading in data here, then calling "ready" function:
d3.csv("ChiCrime.csv", ready)
// Ready Function
function ready(error, data) {
if (error) return console.warn(error);
// format data
data.forEach(function(d) {
d.count = +d.count; //making sure count reads as a number
d.year = +d.year; //making sure year reads in as a number
d.violation = titleCase(d['Primary Type']); //changing to an easier to use variable name
});
// filtering for 2018 data
var data2018 = data.filter(function(d) { return d.year == 2018})
//Define Margins and svg here:
var margin = {top: 20, right: 50, bottom: 160, left: 40};
var width = 720 - margin.left - margin.right,
height = 400 - margin.top - margin.bottom;
var svg = d3.select("body").append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
//Define xScale, yScale, here:
var xScale = d3.scaleBand()
.domain(data2018.map(function(d) { return d.violation; }))
.padding([.1])
.rangeRound([0, width]);
var yScale = d3.scaleLinear()
.domain([0, d3.max(data2018, function(d) { return d.count; })])
.range([height, 0]);
// Define xAxis and yAxis generators here
var xAxis = d3.axisBottom(xScale);
var yAxis = d3.axisLeft(yScale)
.tickFormat(d3.format(`.2s`));
// append axes here:
var xAxisGroup = svg.append("g")
.attr("class","x axis")
.attr("transform","translate(0," + height + ")")
.call(xAxis);
xAxisGroup
.selectAll('text')
.attr('transform', 'rotate(45)')
.style('text-anchor', 'start');
var yAxisGroup = svg.append("g")
.attr("class","y axis")
.call(yAxis);
// Create bars here:
svg.selectAll(".bar")
.data(data2018)
.enter().append("rect")
.attr("class", "bar")
.attr("x", function(d) { return xScale(d.violation); })
.attr("y", function(d) { return yScale(d.count); })
.attr("width", xScale.bandwidth())
.attr("height", function(d) { return height - yScale(d.count); })
.attr("fill", "red")
.on("mouseenter", function(d) {
d3.select(this)
.transition()
.duration(500)
.attr("fill", "yellow");
//Get this bar's x/y values, then augment for the label
var xPosition = parseFloat(d3.select(this).attr('x')) + xScale.bandwidth() / 2;
var yPosition = parseFloat(d3.select(this).attr("y")) - 5;
//Create the label
svg.append("text")
.attr("id", "countLabel")
.attr("x", xPosition)
.attr("y", yPosition)
.attr("text-anchor", "middle")
.attr("font-family", "sans-serif")
.attr("font-size", "11px")
.attr("font-weight", "bold")
.text(d.count);
})
.on("mouseleave", function(d) {
d3.select("#countLabel").remove();
d3.select(this)
.transition()
.duration(500)
.attr("fill", "red");
});
}
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment