diff --git a/js/graph.js b/js/graph.js index 09c4a4b..277b5ea 100644 --- a/js/graph.js +++ b/js/graph.js @@ -18,165 +18,165 @@ function getGraphDataset(tableData, colsForGraph, type, crimes) { rate_type = "_percent"; } checkbox_names = ["Actual Offenses", - "Total Offenses Cleared", - "Offenses Cleared Involving Only Persons Under age 18", - "Unfounded Offenses" - ]; - if (type == "hate") { - checkbox_names = ["Arson", "Destruction of Property/Vandalism", "Other", "Property/Financial", "Violent", "Total"]; - } - if (type == "leoka") { + "Total Offenses Cleared", + "Offenses Cleared Involving Only Persons Under age 18", + "Unfounded Offenses" +]; +if (type == "hate") { + checkbox_names = ["Arson", "Destruction of Property/Vandalism", "Other", "Property/Financial", "Violent", "Total"]; +} +if (type == "leoka") { + checkbox_names = ["Female", "Male", "Total"]; +} +if (type == "leoka" & $("#crime_dropdown").val() == "officers_assaulted") { + checkbox_names = ["Gun", "Knife", "Other Weapon", "Unarmed", "Total"]; +} +if (type == "leoka" & $("#crime_dropdown").val() == "officers_killed") { + checkbox_names = ["Killed by Felony", "Killed by Accident"]; +} +if (type == "arrests") { + if ($("#subsubcategory_dropdown").val() == "Sex") { checkbox_names = ["Female", "Male", "Total"]; + } else if ($("#subsubcategory_dropdown").val() == "Race") { + checkbox_names = ["American Indian", "Asian", "Black", "White", "Total"]; + } else if ($("#subsubcategory_dropdown").val() == "Ethnicity") { + checkbox_names = ["Hispanic", "Non-Hispanic", "Total"]; } - if (type == "leoka" & $("#crime_dropdown").val() == "officers_assaulted") { - checkbox_names = ["Gun", "Knife", "Other Weapon", "Unarmed", "Total"]; - } - if (type == "leoka" & $("#crime_dropdown").val() == "officers_killed") { - checkbox_names = ["Killed by Felony", "Killed by Accident"]; - } - if (type == "arrests") { - if ($("#subsubcategory_dropdown").val() == "Sex") { - checkbox_names = ["Female", "Male", "Total"]; - } else if ($("#subsubcategory_dropdown").val() == "Race") { - checkbox_names = ["American Indian", "Asian", "Black", "White", "Total"]; - } else if ($("#subsubcategory_dropdown").val() == "Ethnicity") { - checkbox_names = ["Hispanic", "Non-Hispanic", "Total"]; - } - } +} - if (type == "nibrs") { - if ($("#subcategory_dropdown").val() == "sex") { - checkbox_names = ["Total", "Female", "Male", "Unknown"]; - } else if ($("#subcategory_dropdown").val() == "race") { - checkbox_names = ["Total", "Asian", "American Indian", "Black", "White", "Unknown"]; - } else if ($("#subcategory_dropdown").val() == "ethnicity") { - checkbox_names = ["Total", "Hispanic", "Non-Hispanic", "Unknown"]; - } else if ($("#subcategory_dropdown").val() == "age") { - checkbox_names = ["Total", "Adult", "Minor (<18 years)", "Unknown"]; - } else if ($("#subcategory_dropdown").val() == "injury") { - checkbox_names = ["Total", "No Injury", "Minor Injury", "Serious Injury"]; - } else if ($("#subcategory_dropdown").val() == "subtype" && $("#crime_dropdown").val() != "animal_cruelty") { - checkbox_names = ["Total", "Buy/Possess/Consume", "Sell/Create/Operate"]; - } else if ($("#subcategory_dropdown").val() == "subtype" && $("#crime_dropdown").val() == "animal_cruelty") { - checkbox_names = ["Total", "Abuse/Torture", "Animal Fighting", "Bestiality", "Neglect"]; - } else if ($("#subcategory_dropdown").val() == "relationship") { - checkbox_names = ["Total", "Intimdate Partner (include exes)", "Other Family", "Other", "Stranger", "Unknown"]; - } else if ($("#subcategory_dropdown").val() == "location") { - checkbox_names = ["Total", "Bar/Nightclub", "Home", "Other/Unknown", "Outside", "School"]; - } else if ($("#subcategory_dropdown").val() == "gun") { - checkbox_names = ["Total", "Gun Not Used", "Handgun used", "Other/Unknown Type Gun Used"]; - } else if ($("#subcategory_dropdown").val() == "arrest_type") { - checkbox_names = ["Total", "On-View", "Summoned/Cited", "Taken Into Custody"]; - } else if ($("#subcategory_dropdown").val() == "clearance") { - checkbox_names = ["Total", "Cleared by Arrest", "Death of Suspect", "Extradition Denied", - "Juvenile/No Custody", "Prosecution Denied", "Victim Refused to Cooperate" - ]; - } else if ($("#subcategory_dropdown").val() == "total" && $("#category_dropdown").val() != "property") { - checkbox_names = ["Total"]; - } else if ($("#subcategory_dropdown").val() == "sex" && $("#category_dropdown").val() == "arrestee") { - checkbox_names = ["Total", "Female", "Male"]; - } else if ($("#subcategory_dropdown").val() == "race" && $("#category_dropdown").val() == "arrestee") { - checkbox_names = ["Total", "Asian", "American Indian", "Black", "White"]; - } else if ($("#subcategory_dropdown").val() == "ethnicity" && $("#category_dropdown").val() == "arrestee") { - checkbox_names = ["Total", "Hispanic", "Non-Hispanic"]; - } else if ($("#category_dropdown").val() == "property" & $("#subcategory_dropdown").val() != "drugs") { - checkbox_names = ["Burned", "Counterfeited/Forged", "Destroyed/Damaged/Vandalized", "Recovered", "Seized", "Stolen/Robbed/Defrauded/Etc."]; - } else if ($("#category_dropdown").val() == "property" & $("#subcategory_dropdown").val() == "drugs") { - checkbox_names = ["Total Incidents With Drug Seized"]; +if (type == "nibrs") { + if ($("#subcategory_dropdown").val() == "sex") { + checkbox_names = ["Total", "Female", "Male", "Unknown"]; + } else if ($("#subcategory_dropdown").val() == "race") { + checkbox_names = ["Total", "Asian", "American Indian", "Black", "White", "Unknown"]; + } else if ($("#subcategory_dropdown").val() == "ethnicity") { + checkbox_names = ["Total", "Hispanic", "Non-Hispanic", "Unknown"]; + } else if ($("#subcategory_dropdown").val() == "age") { + checkbox_names = ["Total", "Adult", "Minor (<18 years)", "Unknown"]; + } else if ($("#subcategory_dropdown").val() == "injury") { + checkbox_names = ["Total", "No Injury", "Minor Injury", "Serious Injury"]; + } else if ($("#subcategory_dropdown").val() == "subtype" && $("#crime_dropdown").val() != "animal_cruelty") { + checkbox_names = ["Total", "Buy/Possess/Consume", "Sell/Create/Operate"]; + } else if ($("#subcategory_dropdown").val() == "subtype" && $("#crime_dropdown").val() == "animal_cruelty") { + checkbox_names = ["Total", "Abuse/Torture", "Animal Fighting", "Bestiality", "Neglect"]; + } else if ($("#subcategory_dropdown").val() == "relationship") { + checkbox_names = ["Total", "Intimdate Partner (include exes)", "Other Family", "Other", "Stranger", "Unknown"]; + } else if ($("#subcategory_dropdown").val() == "location") { + checkbox_names = ["Total", "Bar/Nightclub", "Home", "Other/Unknown", "Outside", "School"]; + } else if ($("#subcategory_dropdown").val() == "gun") { + checkbox_names = ["Total", "Gun Not Used", "Handgun used", "Other/Unknown Type Gun Used"]; + } else if ($("#subcategory_dropdown").val() == "arrest_type") { + checkbox_names = ["Total", "On-View", "Summoned/Cited", "Taken Into Custody"]; + } else if ($("#subcategory_dropdown").val() == "clearance") { + checkbox_names = ["Total", "Cleared by Arrest", "Death of Suspect", "Extradition Denied", + "Juvenile/No Custody", "Prosecution Denied", "Victim Refused to Cooperate" + ]; +} else if ($("#subcategory_dropdown").val() == "total" && $("#category_dropdown").val() != "property") { + checkbox_names = ["Total"]; +} else if ($("#subcategory_dropdown").val() == "sex" && $("#category_dropdown").val() == "arrestee") { + checkbox_names = ["Total", "Female", "Male"]; +} else if ($("#subcategory_dropdown").val() == "race" && $("#category_dropdown").val() == "arrestee") { + checkbox_names = ["Total", "Asian", "American Indian", "Black", "White"]; +} else if ($("#subcategory_dropdown").val() == "ethnicity" && $("#category_dropdown").val() == "arrestee") { + checkbox_names = ["Total", "Hispanic", "Non-Hispanic"]; +} else if ($("#category_dropdown").val() == "property" & $("#subcategory_dropdown").val() != "drugs") { + checkbox_names = ["Burned", "Counterfeited/Forged", "Destroyed/Damaged/Vandalized", "Recovered", "Seized", "Stolen/Robbed/Defrauded/Etc."]; +} else if ($("#category_dropdown").val() == "property" & $("#subcategory_dropdown").val() == "drugs") { + checkbox_names = ["Total Incidents With Drug Seized"]; +} +} + +if ((get_rate_type(type, binary = true) || (type == "offenses" && $("#clearance_rate").is(":checked")))) { + colsForGraph = _.map(colsForGraph, function(x) { + if (type == "offenses" && $("#clearance_rate").is(":checked") && x.includes("clearance")) { + return x + "_clearance_rate"; + } else { + return x + rate_type; } - } + }); + colsForGraph[0] = "year"; +} - if ((get_rate_type(type, binary = true) || (type == "offenses" && $("#clearance_rate").is(":checked")))) { - colsForGraph = _.map(colsForGraph, function(x) { - if (type == "offenses" && $("#clearance_rate").is(":checked") && x.includes("clearance")) { - return x + "_clearance_rate"; - } else { - return x + rate_type; - } - }); - colsForGraph[0] = "year"; - } +data = _.map(tableData, function(currentObject) { + return _.pick(currentObject, colsForGraph); +}); + +years = []; +data1 = []; +data2 = []; +data3 = []; +data4 = []; +data5 = []; +data6 = []; +data7 = []; + +for (var i = 0; i < data.length; i++) { + years.push(data[i][colsForGraph[0]]); + data1.push(data[i][colsForGraph[1]]); + data2.push(data[i][colsForGraph[2]]); + data3.push(data[i][colsForGraph[3]]); + data4.push(data[i][colsForGraph[4]]); + data5.push(data[i][colsForGraph[5]]); + data6.push(data[i][colsForGraph[6]]); + data7.push(data[i][colsForGraph[7]]); +} - data = _.map(tableData, function(currentObject) { - return _.pick(currentObject, colsForGraph); - }); +if (["offenses", "arrests", "nibrs", "arson", "hate"].includes(type) || type == "leoka") { - years = []; - data1 = []; - data2 = []; - data3 = []; - data4 = []; - data5 = []; - data6 = []; - data7 = []; - - for (var i = 0; i < data.length; i++) { - years.push(data[i][colsForGraph[0]]); - data1.push(data[i][colsForGraph[1]]); - data2.push(data[i][colsForGraph[2]]); - data3.push(data[i][colsForGraph[3]]); - data4.push(data[i][colsForGraph[4]]); - data5.push(data[i][colsForGraph[5]]); - data6.push(data[i][colsForGraph[6]]); - data7.push(data[i][colsForGraph[7]]); + final_data = [ + makeGraphObjects(data1, "#ca0020", checkbox_names[0]), + makeGraphObjects(data2, "#f4a582", checkbox_names[1]), + makeGraphObjects(data3, "#92c5de", checkbox_names[2]), + makeGraphObjects(data4, "#0571b0", checkbox_names[3]), + makeGraphObjects(data5, "#008837", checkbox_names[4]), + makeGraphObjects(data6, "#000000", checkbox_names[5]), + makeGraphObjects(data7, "#800080", checkbox_names[6]) + ]; + for (var i = 0; i < 7; i++) { + final_data[i].hidden = false; } - - if (["offenses", "arrests", "nibrs", "arson", "hate"].includes(type) || type == "leoka") { - - final_data = [ - makeGraphObjects(data1, "#ca0020", checkbox_names[0]), - makeGraphObjects(data2, "#f4a582", checkbox_names[1]), - makeGraphObjects(data3, "#92c5de", checkbox_names[2]), - makeGraphObjects(data4, "#0571b0", checkbox_names[3]), - makeGraphObjects(data5, "#008837", checkbox_names[4]), - makeGraphObjects(data6, "#000000", checkbox_names[5]), - makeGraphObjects(data7, "#800080", checkbox_names[6]) - ]; - for (var i = 0; i < 7; i++) { - final_data[i].hidden = false; - } - for (var i = 0; i < 7; i++) { - if (!$("#checkbox_" + (i + 1)).is(':checked')) { - final_data = _.filter(final_data, function(x) { - return x.label != checkbox_names[i]; - }); - } + for (var i = 0; i < 7; i++) { + if (!$("#checkbox_" + (i + 1)).is(':checked')) { + final_data = _.filter(final_data, function(x) { + return x.label != checkbox_names[i]; + }); } + } - } else { - years = []; - data1 = []; +} else { + years = []; + data1 = []; - for (var n = 0; n < data.length; n++) { - years.push(data[n][colsForGraph[0]]); - data1.push(data[n][colsForGraph[1]]); - } + for (var n = 0; n < data.length; n++) { + years.push(data[n][colsForGraph[0]]); + data1.push(data[n][colsForGraph[1]]); + } - label = colsForGraph[1] - label = label.replace(/_age_adjusted_rate/g, ""); - label = label.replace(/_crude_rate/g, ""); - if (type == "leoka") { - crimes = leoka_subcategories[$("#crime_dropdown").val()]; - } + label = colsForGraph[1] + label = label.replace(/_age_adjusted_rate/g, ""); + label = label.replace(/_crude_rate/g, ""); + if (type == "leoka") { + crimes = leoka_subcategories[$("#crime_dropdown").val()]; + } - if (type == "nibrs" && $("#rate").is(':checked')) { - label = label.replace(/_rate/g, ""); - label = label.replace(/_percent/g, ""); - label = crimes[label] - label += " Rate" - // Temp fix since crimes variable doesn't exist and is causing issues for leoka page - } else if (type == "leoka" && ["officers_assaulted", "officers_killed"].includes($("#crime_dropdown").val())) { - label = "Incidents" - } else if (type == "hate") { - label = "# of Incidents" - } else { - label = crimes[label] - } - final_data = [makeGraphObjects(data1, "#ca0020", label)]; - final_data[0].hidden = false; + if (type == "nibrs" && $("#rate").is(':checked')) { + label = label.replace(/_rate/g, ""); + label = label.replace(/_percent/g, ""); + label = crimes[label] + label += " Rate" + // Temp fix since crimes variable doesn't exist and is causing issues for leoka page + } else if (type == "leoka" && ["officers_assaulted", "officers_killed"].includes($("#crime_dropdown").val())) { + label = "Incidents" + } else if (type == "hate") { + label = "# of Incidents" + } else { + label = crimes[label] } - return final_data; + final_data = [makeGraphObjects(data1, "#ca0020", label)]; + final_data[0].hidden = false; +} +return final_data; } @@ -194,7 +194,6 @@ function makeGraphObjects(data, color, label) { data: cleanedData, onAnimationComplete: allowSaveGraph, hidden: true, - yAxisID: "A", position: "left", spanGaps: false, radius: 1.5, // Dot size @@ -259,103 +258,78 @@ function makeGraph(type, crimes) { graph_datasets = getGraphDataset(table_data, graph_headers, type, crimes); if (type == "offenses" && $("#clearance_rate").is(":checked")) { - const cleared_data = graph_datasets.filter(x => x.label.includes("Clear")); - const not_cleared_data = graph_datasets.filter(x => !x.label.includes("Clear")); - graph_datasets = not_cleared_data; - } xaxis_label = "Year"; opts = { - watermark: { - image: "crimedatatool_watermark.jpg", - x: -333, - y: 33, - opacity: 0.75, - alignToChartArea: true, - position: "back" - }, - animation: true, - responsive: true, - plugins: { - title: { - display: true, - position: 'top', - text: title, - font: { - size: 22 - } - } - }, - scales: { - x: { - display: true, - ticks: { - autoSkip: true, - maxTicksLimit: 10, - font: { - size: 15 - } - }, - title: { - display: true, - text: xaxis_label, - font: { - size: 22 - } - } + watermark: { + + image: "crimedatatool_watermark.jpg", + x: -333, + y: 53, + // width: 53, + // height: 60, + opacity: 0.75, + alignToChartArea: true, + position: "back" + + }, + animation: true, + responsive: true, + plugins: { + title: { + display: true, + position: 'top', + text: title, + font: { + size: 22 + }, + } + }, + scales: { + x: { + display: true, + ticks: { + autoSkip: true, + maxTicksLimit: 10, + fontSize: 15 + }, + title: { + display: true, + text: xaxis_label, + font: { + size: 22, }, - y: { - beginAtZero: true, // Ensures the y-axis starts at zero - display: true, - ticks: { - autoSkip: true, - maxTicksLimit: 10, - font: { - size: 15 - } - }, - title: { - display: true, - text: yaxis_label, - font: { - size: 22 - } - } - } - }, - interaction: { - mode: 'nearest', - intersect: true + } }, - plugins: { - tooltip: { - callbacks: { - label: function(tooltipItem) { - let value = tooltipItem.raw; - value = value.toLocaleString(); - return tooltipItem.dataset.label + ": " + value; - } - } - } + y: { + beginAtZero: true, + title: { + display: true, + text: yaxis_label, + font: { + size: 22, + }, + } } + }, }; - - -Chart.defaults.color = 'black' + Chart.defaults.color = 'black' + console.log(graph_datasets) myLineChart = new Chart(ctx, { type: 'line', data: { labels: years, - datasets: graph_datasets + datasets: graph_datasets, + yAxisID: 'y' }, options: opts }); -// disable_animation_on_mobile(myLineChart); + // disable_animation_on_mobile(myLineChart); return (myLineChart); } @@ -426,15 +400,15 @@ function getTitle(data, type) { switch (type) { case "offenses": - subtitle = crime_values[selectedCrime]; - break; + subtitle = crime_values[selectedCrime]; + break; case "arson": - subtitle = arson_values[selectedCrime]; - break; + subtitle = arson_values[selectedCrime]; + break; case "arrests": - subtitle = `Arrests for: ${arrest_values[selectedCrime]}, Breakdown: ${arrests_breakdown[selectedSubsubcategory]}, Age: ${arrest_age_categories[selectedSubcategory]}`; - break; + subtitle = `Arrests for: ${arrest_values[selectedCrime]}, Breakdown: ${arrests_breakdown[selectedSubsubcategory]}, Age: ${arrest_age_categories[selectedSubcategory]}`; + break; case "nibrs": nibrs_crimes_temp = nibrs_crime_values["offense"] @@ -461,24 +435,24 @@ function getTitle(data, type) { } - subtitle = nibrs_crimes_temp[$("#crime_dropdown").val()] + ", " + nibrs_categories[$("#category_dropdown").val()] + " " + nibrs_subcategories[$("#category_dropdown").val()][$("#subcategory_dropdown").val()]; + subtitle = nibrs_crimes_temp[$("#crime_dropdown").val()] + ", " + nibrs_categories[$("#category_dropdown").val()] + " " + nibrs_subcategories[$("#category_dropdown").val()][$("#subcategory_dropdown").val()]; - if (selectedCategory === "property") { - subtitle = `Property Data: ${nibrs_property_values[selectedCrime]}`; - } - break; + if (selectedCategory === "property") { + subtitle = `Property Data: ${nibrs_property_values[selectedCrime]}`; + } + break; case "leoka": - subtitle = `${leoka_categories[selectedCrime]}: ${leoka_subcategories[selectedCrime][selectedSubcategory]}`; - if (leoka_categories[selectedCrime] === "Officers Assaulted") { - // const weapon = ", Weapon: " + leoka_weapons[selectedSubsubcategory]; - // subtitle += `${weapon}`; - } - break; + subtitle = `${leoka_categories[selectedCrime]}: ${leoka_subcategories[selectedCrime][selectedSubcategory]}`; + if (leoka_categories[selectedCrime] === "Officers Assaulted") { + // const weapon = ", Weapon: " + leoka_weapons[selectedSubsubcategory]; + // subtitle += `${weapon}`; + } + break; case "hate": - subtitle = `Bias Motivation: ${hate_bias_motivations[selectedCrime]}`; - break; + subtitle = `Bias Motivation: ${hate_bias_motivations[selectedCrime]}`; + break; } return [title, subtitle]; diff --git a/js/utils.js b/js/utils.js index a95035e..2e8485b 100644 --- a/js/utils.js +++ b/js/utils.js @@ -65,16 +65,16 @@ function exportToCsv(tableData, type, states) { /* function make_dropdown(dropdown_id, dropdown_values, starter, starter_div) { - $(dropdown_id).empty(); - _.each(dropdown_values, function(val, text) { - $(dropdown_id).append(new Option(val, text)); - }); +$(dropdown_id).empty(); +_.each(dropdown_values, function(val, text) { +$(dropdown_id).append(new Option(val, text)); +}); - if (Array.isArray(starter)) { - starter = starter[$(starter_div).val()] - } +if (Array.isArray(starter)) { +starter = starter[$(starter_div).val()] +} - $(dropdown_id).val(starter); +$(dropdown_id).val(starter); } */ @@ -115,7 +115,7 @@ function toggle_leoka_display() { $("label[for='checkbox_5']").hide() -$('#subcategory_dropdown').next(".select2-container").show(); + $('#subcategory_dropdown').next(".select2-container").show(); $("h3").eq(4).html("Subcategory:"); } else if ($("#crime_dropdown").val() == "officers_assaulted") { @@ -133,7 +133,7 @@ $('#subcategory_dropdown').next(".select2-container").show(); $("#checkbox_3").prop("checked", false); $("#checkbox_5").prop("checked", true); -$('#subcategory_dropdown').next(".select2-container").show(); + $('#subcategory_dropdown').next(".select2-container").show(); $("h3").eq(4).html("Subcategory:"); } else if ($("#crime_dropdown").val() == "officers_killed") { @@ -150,7 +150,7 @@ $('#subcategory_dropdown').next(".select2-container").show(); $("#checkbox_1").prop("checked", true); $("#checkbox_2").prop("checked", true); -$('#subcategory_dropdown').next(".select2-container").hide(); + $('#subcategory_dropdown').next(".select2-container").hide(); $("h3").eq(4).html(""); } } @@ -316,12 +316,12 @@ function toggle_nibrs_display() { $("label[for='checkbox_4']").html("Black") $("label[for='checkbox_5']").html("White") $("label[for='checkbox_6']").html("Unknown") - // if ($('#category_dropdown').val() == "arrestee") { - // $("#checkbox_6").hide(); - // $("#checkbox_7").hide(); - // $("label[for='checkbox_6']").hide() - // $("label[for='checkbox_7']").hide() - // } + // if ($('#category_dropdown').val() == "arrestee") { + // $("#checkbox_6").hide(); + // $("#checkbox_7").hide(); + // $("label[for='checkbox_6']").hide() + // $("label[for='checkbox_7']").hide() + // } } else if ($("#subcategory_dropdown").val() == "ethnicity") { $("#checkbox_5").hide(); $("#checkbox_6").hide(); @@ -334,11 +334,11 @@ function toggle_nibrs_display() { $("label[for='checkbox_2']").html("Hispanic") $("label[for='checkbox_3']").html("Non-Hispanic") $("label[for='checkbox_4']").html("Unknown") -// if ($('#category_dropdown').val() == "arrestee") { -// $("label[for='checkbox_3']").html("Not Hispanic") -// $("#checkbox_4").hide(); -// $("label[for='checkbox_4']").hide() -// } + // if ($('#category_dropdown').val() == "arrestee") { + // $("label[for='checkbox_3']").html("Not Hispanic") + // $("#checkbox_4").hide(); + // $("label[for='checkbox_4']").hide() + // } } else if ($("#subcategory_dropdown").val() == "sex") { $("#checkbox_5").hide(); $("#checkbox_6").hide(); @@ -466,7 +466,7 @@ function countToRate(data, type, per_officer = false) { for (var i = 0; i < data_keys.length; i++) { if (!["agency", "year", "state", "population", "ORI", "school_name", "school_unique_ID", "number_of_students"].includes(data_keys[i]) && !data_keys[i].startsWith("population_")) { - if (type == "arrests" && $("#percent_of_arrests").is(':checked')) { + if (type == "arrests" && $("#percent_of_arrests").is(':checked')) { rate_val = data[data_keys[i]] / data[population_column] * 100; } else if (type == "arrests" && $("#percent_of_all_arrests").is(':checked')) { rate_val = data[data_keys[i]] / data[population_column] * 100; @@ -489,233 +489,233 @@ function countToRate(data, type, per_officer = false) { new_key = data_keys[i] + rate_type; Object.defineProperty(data, new_key, Object.getOwnPropertyDescriptor(data, data_keys[i])); - delete data[data_keys[i]]; + delete data[data_keys[i]]; + } } + return data; } - return data; -} -function makeCrimeClearanceRates(data) { - storage = data - console.log(storage) - data_keys = _.keys(data); - clearance_starters = ["total_cleared", "cleared_18"]; + function makeCrimeClearanceRates(data) { + storage = data + console.log(storage) + data_keys = _.keys(data); + clearance_starters = ["total_cleared", "cleared_18"]; - crime_column = data_keys.filter(function(element) { - return element.includes("actual"); - }); + crime_column = data_keys.filter(function(element) { + return element.includes("actual"); + }); - for (var i = 0; i < crime_column.length; i++) { - for (var n = 0; n < clearance_starters.length; n++) { - rate_val = data[crime_column[i].replace("actual", clearance_starters[n])] / data[crime_column[i]]; - rate_val = parseFloat(rate_val).toFixed(2); // Rounds to 2 decimals + for (var i = 0; i < crime_column.length; i++) { + for (var n = 0; n < clearance_starters.length; n++) { + rate_val = data[crime_column[i].replace("actual", clearance_starters[n])] / data[crime_column[i]]; + rate_val = parseFloat(rate_val).toFixed(2); // Rounds to 2 decimals - if (!isFinite(rate_val)) { - rate_val = NaN; - } - data[crime_column[i].replace("actual", clearance_starters[n])] = rate_val * 100; - rate_type = "_clearance_rate"; + if (!isFinite(rate_val)) { + rate_val = NaN; + } + data[crime_column[i].replace("actual", clearance_starters[n])] = rate_val * 100; + rate_type = "_clearance_rate"; - new_key = crime_column[i].replace("actual", clearance_starters[n]) + rate_type; - new_key = new_key.replace("_rate_clearance", "_clearance"); - Object.defineProperty(data, new_key, - Object.getOwnPropertyDescriptor(data, crime_column[i].replace("actual", clearance_starters[n]))); - delete data[crime_column[i].replace("actual", clearance_starters[n])]; + new_key = crime_column[i].replace("actual", clearance_starters[n]) + rate_type; + new_key = new_key.replace("_rate_clearance", "_clearance"); + Object.defineProperty(data, new_key, + Object.getOwnPropertyDescriptor(data, crime_column[i].replace("actual", clearance_starters[n]))); + delete data[crime_column[i].replace("actual", clearance_starters[n])]; + } + } + return data; } - } - return data; -} -function getStateAgencies(type, states = state_values, largest_agencies = false) { - url = "https://raw.githubusercontent.com/jacobkap/crimedatatool_helper/master/data/"; - if (type == "nibrs") { - url = "https://raw.githubusercontent.com/jacobkap/crimedatatool_helper_nibrs/master/data/"; - } - if ($("#monthly").is(':checked')) { - type += "_monthly"; - } + function getStateAgencies(type, states = state_values, largest_agencies = false) { + url = "https://raw.githubusercontent.com/jacobkap/crimedatatool_helper/master/data/"; + if (type == "nibrs") { + url = "https://raw.githubusercontent.com/jacobkap/crimedatatool_helper_nibrs/master/data/"; + } + if ($("#monthly").is(':checked')) { + type += "_monthly"; + } - url += type + "/"; - final_url = url + states[$("#state_dropdown").val()] + "_agency_choices.json"; + url += type + "/"; + final_url = url + states[$("#state_dropdown").val()] + "_agency_choices.json"; - if (largest_agencies) { - final_url = url + "largest_agency_choices.json"; - } - var state_agencies = $.ajax({ - url: final_url, - type: 'get', - dataType: 'json', - async: false, - success: function(data) { - result = data; - } - }); + if (largest_agencies) { + final_url = url + "largest_agency_choices.json"; + } + var state_agencies = $.ajax({ + url: final_url, + type: 'get', + dataType: 'json', + async: false, + success: function(data) { + result = data; + } + }); - state_agencies = state_agencies.responseJSON; - return (state_agencies); -} + state_agencies = state_agencies.responseJSON; + return (state_agencies); + } -function makeDataSourceDropdown() { - page_temp = window.location.pathname + function makeDataSourceDropdown() { + page_temp = window.location.pathname - // Use a document fragment to batch DOM updates - const $dropdown = $("#data_source"); - $dropdown.empty(); // Clear existing options + // Use a document fragment to batch DOM updates + const $dropdown = $("#data_source"); + $dropdown.empty(); // Clear existing options - const fragment = document.createDocumentFragment(); // Create a document fragment + const fragment = document.createDocumentFragment(); // Create a document fragment - Object.entries(data_sources).forEach(([text, val]) => { - const option = new Option(val, text); // Create a new option element - fragment.appendChild(option); // Append the option to the fragment - }); + Object.entries(data_sources).forEach(([text, val]) => { + const option = new Option(val, text); // Create a new option element + fragment.appendChild(option); // Append the option to the fragment + }); - $dropdown.append(fragment); // Append the fragment to the dropdown in one go + $dropdown.append(fragment); // Append the fragment to the dropdown in one go - if (page_temp == "/arrest.html" || page_temp == "/E:/Dropbox/crimedatatool/arrest.html" || page_temp == "/C:/Users/jkkap/Dropbox/crimedatatool/arrest.html") { - current_page = 0 - } - if (page_temp == "/arson.html" || page_temp == "/E:/Dropbox/crimedatatool/arson.html" || page_temp == "/C:/Users/jkkap/Dropbox/crimedatatool/arson.html") { - current_page = 1 - } - if (page_temp == "/hate.html" || page_temp == "/E:/Dropbox/crimedatatool/hate.html" || page_temp == "/C:/Users/jkkap/Dropbox/crimedatatool/hate.html") { - current_page = 2 - } - if (page_temp == "/leoka.html" || page_temp == "/E:/Dropbox/crimedatatool/leoka.html" || page_temp == "/C:/Users/jkkap/Dropbox/crimedatatool/leoka.html") { - current_page = 3 - } - if (page_temp == "/nibrs.html" || page_temp == "/E:/Dropbox/crimedatatool/nibrs.html" || page_temp == "/C:/Users/jkkap/Dropbox/crimedatatool/nibrs.html") { - current_page = 4 - } - if (page_temp == "/index.html" || page_temp == "/" || page_temp == "/E:/Dropbox/crimedatatool/" || page_temp == "/E:/Dropbox/crimedatatool/index.html" || page_temp == "/C:/Users/jkkap/Dropbox/crimedatatool/" || page_temp == "/C:/Users/jkkap/Dropbox/crimedatatool/index.html") { - current_page = 5 - } - $("#data_source").val(current_page) -} + if (page_temp == "/arrest.html" || page_temp == "/E:/Dropbox/crimedatatool/arrest.html" || page_temp == "/C:/Users/jkkap/Dropbox/crimedatatool/arrest.html") { + current_page = 0 + } + if (page_temp == "/arson.html" || page_temp == "/E:/Dropbox/crimedatatool/arson.html" || page_temp == "/C:/Users/jkkap/Dropbox/crimedatatool/arson.html") { + current_page = 1 + } + if (page_temp == "/hate.html" || page_temp == "/E:/Dropbox/crimedatatool/hate.html" || page_temp == "/C:/Users/jkkap/Dropbox/crimedatatool/hate.html") { + current_page = 2 + } + if (page_temp == "/leoka.html" || page_temp == "/E:/Dropbox/crimedatatool/leoka.html" || page_temp == "/C:/Users/jkkap/Dropbox/crimedatatool/leoka.html") { + current_page = 3 + } + if (page_temp == "/nibrs.html" || page_temp == "/E:/Dropbox/crimedatatool/nibrs.html" || page_temp == "/C:/Users/jkkap/Dropbox/crimedatatool/nibrs.html") { + current_page = 4 + } + if (page_temp == "/index.html" || page_temp == "/" || page_temp == "/E:/Dropbox/crimedatatool/" || page_temp == "/E:/Dropbox/crimedatatool/index.html" || page_temp == "/C:/Users/jkkap/Dropbox/crimedatatool/" || page_temp == "/C:/Users/jkkap/Dropbox/crimedatatool/index.html") { + current_page = 5 + } + $("#data_source").val(current_page) + } -function updateDataSource() { + function updateDataSource() { - if (data_sources[$("#data_source").val()] == "Offenses Known and Clearances by Arrest") { - new_url_path = "https://crimedatatool.com/" - } - if (data_sources[$("#data_source").val()] == "Arson") { - new_url_path = "https://crimedatatool.com/arson.html" - } - if (data_sources[$("#data_source").val()] == "Hate Crime Statistics") { - new_url_path = "https://crimedatatool.com/hate.html" - } - if (data_sources[$("#data_source").val()] == "Arrests by Age, Sex, and Race") { - new_url_path = "https://crimedatatool.com/arrest.html" - } - if (data_sources[$("#data_source").val()] == "Law Enforcement Officers Killed and Assaulted (LEOKA)") { - new_url_path = "https://crimedatatool.com/leoka.html" - } - if (data_sources[$("#data_source").val()] == "National Incident-Based Reporting System (NIBRS)") { - new_url_path = "https://crimedatatool.com/nibrs.html" - } + if (data_sources[$("#data_source").val()] == "Offenses Known and Clearances by Arrest") { + new_url_path = "https://crimedatatool.com/" + } + if (data_sources[$("#data_source").val()] == "Arson") { + new_url_path = "https://crimedatatool.com/arson.html" + } + if (data_sources[$("#data_source").val()] == "Hate Crime Statistics") { + new_url_path = "https://crimedatatool.com/hate.html" + } + if (data_sources[$("#data_source").val()] == "Arrests by Age, Sex, and Race") { + new_url_path = "https://crimedatatool.com/arrest.html" + } + if (data_sources[$("#data_source").val()] == "Law Enforcement Officers Killed and Assaulted (LEOKA)") { + new_url_path = "https://crimedatatool.com/leoka.html" + } + if (data_sources[$("#data_source").val()] == "National Incident-Based Reporting System (NIBRS)") { + new_url_path = "https://crimedatatool.com/nibrs.html" + } -current_page = data_sources[$("#data_source").val()] - page_temp = window.location.pathname - if (current_page == "Offenses Known and Clearances by Arrest" & !["/index.html", "/"].includes(page_temp)) { - window.location.href = new_url_path; - } - if (current_page == "Hate Crime Statistics" & page_temp != "/hate.html") { - window.location.href = new_url_path; - } - if (current_page == "Arrests by Age, Sex, and Race" & page_temp != "/arrest.html") { - window.location.href = new_url_path; - } - if (current_page == "Law Enforcement Officers Killed and Assaulted (LEOKA)" & page_temp != "/leoka.html") { - window.location.href = new_url_path; - } - if (current_page == "National Incident-Based Reporting System (NIBRS)" & page_temp != "/nibrs.html") { - window.location.href = new_url_path; - } - if (current_page == "Arson" & page_temp != "/arson.html") { - window.location.href = new_url_path; - } - makeDataSourceDropdown() -} + current_page = data_sources[$("#data_source").val()] + page_temp = window.location.pathname + if (current_page == "Offenses Known and Clearances by Arrest" & !["/index.html", "/"].includes(page_temp)) { + window.location.href = new_url_path; + } + if (current_page == "Hate Crime Statistics" & page_temp != "/hate.html") { + window.location.href = new_url_path; + } + if (current_page == "Arrests by Age, Sex, and Race" & page_temp != "/arrest.html") { + window.location.href = new_url_path; + } + if (current_page == "Law Enforcement Officers Killed and Assaulted (LEOKA)" & page_temp != "/leoka.html") { + window.location.href = new_url_path; + } + if (current_page == "National Incident-Based Reporting System (NIBRS)" & page_temp != "/nibrs.html") { + window.location.href = new_url_path; + } + if (current_page == "Arson" & page_temp != "/arson.html") { + window.location.href = new_url_path; + } + makeDataSourceDropdown() + } -function updateAgencies(type, states) { - agencies = getStateAgencies(type, states); - agencies.sort(); - // Use a document fragment to batch DOM updates - const $dropdown = $("#agency_dropdown"); - $dropdown.empty(); // Clear existing options + function updateAgencies(type, states) { + agencies = getStateAgencies(type, states); + agencies.sort(); + // Use a document fragment to batch DOM updates + const $dropdown = $("#agency_dropdown"); + $dropdown.empty(); // Clear existing options - const fragment = document.createDocumentFragment(); // Create a document fragment + const fragment = document.createDocumentFragment(); // Create a document fragment - agencies.forEach((val, text) => { - const option = new Option(val, text); // Create a new option element - fragment.appendChild(option); // Append the option to the fragment - }); + agencies.forEach((val, text) => { + const option = new Option(val, text); // Create a new option element + fragment.appendChild(option); // Append the option to the fragment + }); - $dropdown.append(fragment); // Append the fragment to the dropdown in one go - largest_agency_temp = states[$("#state_dropdown").val()]; - largest_agency_temp = _.filter(largest_agency, function(x) { - return x.state == largest_agency_temp; - }); - largest_agency_temp = largest_agency_temp[0].agency; - largest_agency_temp = _.indexOf(agencies, largest_agency_temp); - $("#agency_dropdown").val(largest_agency_temp); - return agencies; -} + $dropdown.append(fragment); // Append the fragment to the dropdown in one go + largest_agency_temp = states[$("#state_dropdown").val()]; + largest_agency_temp = _.filter(largest_agency, function(x) { + return x.state == largest_agency_temp; + }); + largest_agency_temp = largest_agency_temp[0].agency; + largest_agency_temp = _.indexOf(agencies, largest_agency_temp); + $("#agency_dropdown").val(largest_agency_temp); + return agencies; + } -function main(type, states, state_default, crimes, crime_starter) { + function main(type, states, state_default, crimes, crime_starter) { - state_default = Math.floor(Math.random()*states.length) - makeDataSourceDropdown() - make_dropdown('#state_dropdown', states, state_default) - make_dropdown("#crime_dropdown", crimes, crime_starter); + state_default = Math.floor(Math.random()*states.length) + makeDataSourceDropdown() + make_dropdown('#state_dropdown', states, state_default) + make_dropdown("#crime_dropdown", crimes, crime_starter); - if (type == "arrests") { - make_dropdown("#subcategory_dropdown", arrest_age_categories, "total") - make_dropdown("#subsubcategory_dropdown", arrests_breakdown, "Race") - toggle_arrest_display(); - } else if (type == "nibrs") { - make_dropdown('#category_dropdown', nibrs_categories, "offense") - make_dropdown("#subcategory_dropdown", nibrs_subcategories[$('#category_dropdown').val()], nibrs_starts[$('#category_dropdown').val()]) - toggle_nibrs_display() - } else if (type == "leoka") { + if (type == "arrests") { + make_dropdown("#subcategory_dropdown", arrest_age_categories, "total") + make_dropdown("#subsubcategory_dropdown", arrests_breakdown, "Race") + toggle_arrest_display(); + } else if (type == "nibrs") { + make_dropdown('#category_dropdown', nibrs_categories, "offense") + make_dropdown("#subcategory_dropdown", nibrs_subcategories[$('#category_dropdown').val()], nibrs_starts[$('#category_dropdown').val()]) + toggle_nibrs_display() + } else if (type == "leoka") { - make_dropdown('#subcategory_dropdown', leoka_subcategories[$('#crime_dropdown').val()], leoka_categories_starts[$('#crime_dropdown').val()], '#crime_dropdown'); - // make_dropdown("#subsubcategory_dropdown", leoka_weapons, "total_assaults"); - // toggle_display("#weaponsDiv", ["officers_assaulted"]); - // $("#leokaSex").show(); - toggle_leoka_display() - } + make_dropdown('#subcategory_dropdown', leoka_subcategories[$('#crime_dropdown').val()], leoka_categories_starts[$('#crime_dropdown').val()], '#crime_dropdown'); + // make_dropdown("#subsubcategory_dropdown", leoka_weapons, "total_assaults"); + // toggle_display("#weaponsDiv", ["officers_assaulted"]); + // $("#leokaSex").show(); + toggle_leoka_display() + } - largest_agency = getStateAgencies(type, states, true); - agencies = updateAgencies(type, states); + largest_agency = getStateAgencies(type, states, true); + agencies = updateAgencies(type, states); -// if (window.location.hash == "") { -// change_url(type) - //} else { - // change_data_from_url(type); - //} + // if (window.location.hash == "") { + // change_url(type) + //} else { + // change_data_from_url(type); + //} document.addEventListener("DOMContentLoaded", function() { - ctx = document.getElementById("graph").getContext('2d'); - main_results = get_data(type, states); - table_data = main_results[0]; - graph_headers = main_results[1]; - table_headers = main_results[2]; - all_data = main_results[3]; - graph = makeGraph(type, crimes); - table = makeTable(type); + ctx = document.getElementById("graph").getContext('2d'); + main_results = get_data(type, states); + table_data = main_results[0]; + graph_headers = main_results[1]; + table_headers = main_results[2]; + all_data = main_results[3]; + graph = makeGraph(type, crimes); + table = makeTable(type); }); -// makeDataSourceDropdown() -} + // makeDataSourceDropdown() + } diff --git a/nibrs.html b/nibrs.html index 8463458..087dbea 100644 --- a/nibrs.html +++ b/nibrs.html @@ -35,7 +35,7 @@ - +