A look into Chicago Police Department’s gun related incident reports from January 2008 through February 2016. The data is from data.cityofchicago.org.
Stacked bar chart of monthly incidents by primary type.
Click chart image to expand
Line chart of monthly incidents by primary type.
Click chart image to expand
Animated Map
C# Console Application used to create the JSON for the Stacked Bar Chart and Line Chart.
int counter = 0; string line; bool header = true; List primaryCrimeType = new List(); Dictionary<string, Dictionary<string, int>> dictDate_PriCount = new Dictionary<string, Dictionary<string, int>>(); System.IO.StreamReader file = new System.IO.StreamReader("C:\\CHICAGOGUN\\2008-present GUN RELATED CHICAGO CRIME INCIDENTREPORTS.csv"); while ((line = file.ReadLine()) != null) { counter++; if (header) header = false; else { string[] parts = line.Split(','); DateTime date = DateTime.Parse(parts[0]); string datekey = date.ToString("MMM yy"); string primary = parts[1].Trim(); if ( dictDate_PriCount.ContainsKey(datekey) ) { Dictionary<string, int> dictExistingInner = new Dictionary<string, int>(); dictExistingInner = dictDate_PriCount[datekey]; int existing_count = dictExistingInner[primary]; existing_count += 1; dictExistingInner[primary] = existing_count; dictDate_PriCount[datekey] = dictExistingInner; } else { Dictionary<string, int> dictFreshInner = new Dictionary<string, int>(); dictFreshInner.Add("BATTERY", 0); dictFreshInner.Add("WEAPONS VIOLATION", 0); dictFreshInner.Add("ASSAULT", 0); dictFreshInner.Add("ROBBERY", 0); dictFreshInner.Add("CRIM SEXUAL ASSAULT", 0); dictFreshInner.Add("OTHER OFFENSE", 0); dictFreshInner.Add("NON-CRIMINAL", 0); dictFreshInner[primary] = 1; dictDate_PriCount.Add(datekey, dictFreshInner); } } } file.Close(); string json = ""; foreach (string keydate in dictDate_PriCount.Keys) { json += "['" + keydate + "' "; foreach( string pri in dictDate_PriCount[keydate].Keys) { json += "," + dictDate_PriCount[keydate][pri]; } json += "],\r\n"; } StreamWriter streamWrite; streamWrite = File.AppendText("C:\\Airplanes\\2008-2016ChicagoGunJSON.txt"); streamWrite.WriteLine(json); streamWrite.Close(); |