Public
Edited
Mar 1, 2023
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
continet_set = new Set()
Insert cell
Insert cell
continent_map = [
{
continent: "Oceania",
countries: ["Australia", "Fiji","New Zealand","Kiribati"]
},
{
continent: "South America",
countries: [
"Argentina",
"Bolivia",
"Brazil",
"Chile",
"Colombia",
"Ecuador",
"Guyana",
"Paraguay",
"Peru",
"Suriname",
"Uruguay",
"Venezuela"
]
},
{
continent: "North America",
countries: [
"Antigua and Barbuda",
"Bahamas",
"Barbados",
"Belize",
"Canada",
"Costa Rica",
"Cuba",
"Dominica",
"Dominican Republic",
"El Salvador",
"Grenada",
"Guatemala",
"Haiti",
"Honduras",
"Jamaica",
"Mexico",
"Nicaragua",
"Panama",
"Saint Kitts and Nevis",
"Saint Lucia",
"Saint Vincent and Grenadines",
"Trinidad and Tobago",
"United States",
"Puerto Rico"
]
},
{
continent: "Africa",
countries: [
"Algeria",
"Angola",
"Benin",
"Botswana",
"Burkina Faso",
"Burundi",
"Cabo Verde",
"Cameroon",
"Central African Republic (CAR)",
"Chad",
"Comoros",
"Democratic Republic of the Congo",
"Republic of the Congo",
"Cote d'Ivoire",
"Djibouti",
"Egypt",
"Equatorial Guinea",
"Eritrea",
"Eswatini (formerly Swaziland)",
"Ethiopia",
"Gabon",
"Gambia",
"Ghana",
"Guinea",
"Guinea-Bissau",
"Kenya",
"Lesotho",
"Liberia",
"Libya",
"Madagascar",
"Malawi",
"Mali",
"Mauritania",
"Mauritius",
"Morocco",
"Mozambique",
"Namibia",
"Niger",
"Nigeria",
"Rwanda",
"Sao Tome and Principe",
"Senegal",
"Seychelles",
"Sierra Leone",
"Somalia",
"South Africa",
"South Sudan",
"Sudan",
"Tanzania",
"Togo",
"Tunisia",
"Uganda",
"Zambia",
"Zimbabwe"
]
},
{
continent: "Europe",
countries: [
"Albania",
"Anguilla",
"Aruba",
"Austria",
"Azerbaijan",
"Belgium",
"Bulgaria",
"Croatia",
"Curacao",
"Cyprus",
"Denmark",
"Finland",
"France",
"Georgia",
"Germany",
"Greece",
"Italy",
"Luxembourg",
"Lithuania",
"Malta",
"Monaco",
"Montenegro",
"Netherlands",
"Netherlands Antilles",
"Norway",
"Poland",
"Portugal",
"Romania",
"Spain",
"Sweden",
"Switzerland",
"Ukraine",
"United Kingdom",
"Slovenia",
"Slovakia",
"Serbia",
"San Marino",
"Latvia",
"Ireland",
"Iceland",
"Hungary",
"Czech Republic",
"Estonia",
"Bosnia and Herzegovina",
"Belarus",
]
},
{
continent: "Asia",
countries: [
"Armenia",
"Bahrain",
"China",
"East Timor",
"India",
"Indonesia",
"Iran",
"Iraq",
"Israel",
"Japan",
"Jordan",
"Kazakhstan",
"Myanmar",
"Maldives",
"Mongolia",
"North Korea",
"South Korea",
"Kuwait",
"Kyrgyzstan",
"Laos",
"Malaysia",
"Macau",
"Oman",
"Pakistan",
"Qatar",
"Russian Federation",
"Saudi Arabia",
"Singapore",
"Taiwan",
"Thailand",
"Turkey",
"Turkmenistan",
"United Arab Emirates",
"Uzbekistan",
"Vietnam",
"Yemen",
"Sri Lanka",
"Republic of Korea",
"Philippines"
]
}
]
Insert cell
map = new Map()
Insert cell
Insert cell
function mapCountriesContinent() {
continent_map.forEach((c) => {
c.countries.forEach((cou) => {
map.set(cou, c.continent);
});
});

return map;
}
Insert cell
continent_mapped = mapCountriesContinent()
Insert cell
Insert cell
function countSuicideRateAcrossContinent() {
let map = new Map();
data.forEach((s) => {
let continent_name = continent_mapped.get(s.country);
if (map.has(continent_name)) {
map.set(
continent_name,
map.get(continent_name) + parseInt(s.suicides_no)
);
} else {
map.set(continent_name, parseInt(s.suicides_no));
}
});

return map;
}
Insert cell
suicide_count_continent = countSuicideRateAcrossContinent()
Insert cell
Insert cell
function average_gender_year(){
let obj = []
let country_set = new Set()
let year_set = new Set()
data.forEach((d) => {
country_set.add(d.country)
year_set.add(d.year)
})
country_set.forEach((d)=>{
year_set.forEach((y) => {
let male = 0
let male_pop = 0
let female = 0
let female_pop = 0
data.forEach((s) => {
if(s.country==d && s.year==y){
if (s.sex=="male"){
male = male+parseInt(s.suicides_no)
male_pop = male_pop+parseInt(s.population)
}
else{
female = female+parseInt(s.suicides_no)
female_pop = female_pop+parseInt(s.population)
}
}
})
let male_per_100k = ((male/male_pop)*100000).toFixed(2)
let female_per_100k = ((female/female_pop)*100000).toFixed(2)
if(male_per_100k=="NaN"){
male_per_100k = 0.0
console.log(male_per_100k)
}
if(female_per_100k=="NaN"){
female_per_100k = 0.0
}
obj.push({
country:d,
year:y,
male:parseFloat(male_per_100k),
female:parseFloat(female_per_100k)
})
})
})

let final_obj = []
country_set.forEach((c)=>{
let male_cnt = 0.0
let female_cnt = 0.0
obj.forEach((d) =>{
if(d.country==c){
male_cnt = male_cnt+d.male
female_cnt = female_cnt+d.female
}
})

final_obj.push({
country:c,
male:parseFloat((male_cnt/32.0).toFixed(2)),
female:parseFloat((female_cnt/32.0).toFixed(2))
})
})

return final_obj
}
Insert cell
per_100k_obj = average_gender_year()
Insert cell
Insert cell
function get_data_by_continent(){
let map = new Map()
per_100k_obj.forEach((d) => {
let continent_name = continent_mapped.get(d.country);
if (map.has(continent_name)) {
map.set(
continent_name,
{
male:parseFloat(map.get(continent_name).male + d.male),
female:parseFloat(map.get(continent_name).female + d.female),
total:parseInt(map.get(continent_name).total + 1)
}
);
} else {
map.set(continent_name, {
male:parseFloat(d.male),
female:parseFloat(d.female),
total:parseInt(1)
});
}
})

let final_data = []

for (let [key, value] of map.entries()) {
final_data.push({
continent: key,
male: parseFloat((value.male/value.total).toFixed(2)),
female:parseFloat((value.female/value.total).toFixed(2)),
count: parseFloat((value.male/value.total+value.female/value.total).toFixed(2))
});
}

return final_data
}
Insert cell
data_continent = get_data_by_continent()
Insert cell
viewof trigger = {
const element = html`<div id="trigger" style="display:none;">`;
element.value = true;
return element;
}
Insert cell

mutable gdp_data = filter_data_by_continent("Europe")


Insert cell
mutable continent_selected = "Europe"
Insert cell
Insert cell
function filter_data_by_continent(continent){
let obj = []
let country_set = new Set()
let year_set = new Set()
data.forEach((d) => {
country_set.add(d.country)
year_set.add(d.year)
})
country_set.forEach((d)=>{
let rates = 0
let tot_gdp = 0
let rec_time = 0
let is_present = false
if(continent_mapped.get(d)==continent){
year_set.forEach((y) => {
let pop_suicide = 0
let pop = 0
let gdp = 0
let year = 0
data.forEach((s) => {
if(s.country==d && s.year==y){
pop_suicide = pop_suicide+parseInt(s.suicides_no)
pop = pop+parseInt(s.population)
year = 1
gdp = parseInt(s["gdp_per_capita ($)"])
}
})
let pop_per_100k = parseFloat(((pop_suicide/pop)*100000).toFixed(2))
if(pop_per_100k){
rates = rates+pop_per_100k
}
tot_gdp = tot_gdp+gdp
if(year==1){
rec_time = rec_time+1
is_present = true
}
})
}
if (is_present){
obj.push({
country:d,
rates: parseFloat((rates/rec_time).toFixed(2)),
gdp_per_capita: parseFloat((tot_gdp/rec_time).toFixed(2))
})
}
})

return obj
}
Insert cell
Insert cell
function filter_data_by_continent_gender(continent,gender){
let obj = []
let country_set = new Set()
let year_set = new Set()
data.forEach((d) => {
country_set.add(d.country)
year_set.add(d.year)
})
country_set.forEach((d)=>{
let rates = 0
let tot_gdp = 0
let rec_time = 0
let is_present = false
if(continent_mapped.get(d)==continent){
year_set.forEach((y) => {
let pop_suicide = 0
let pop = 0
let gdp = 0
let year = 0
data.forEach((s) => {
if(s.country==d && s.year==y && s.sex==gender){
pop_suicide = pop_suicide+parseInt(s.suicides_no)
pop = pop+parseInt(s.population)
year = 1
gdp = parseInt(s["gdp_per_capita ($)"])
}
})
let pop_per_100k = parseFloat(((pop_suicide/pop)*100000).toFixed(2))
if(pop_per_100k){
rates = rates+pop_per_100k
}
tot_gdp = tot_gdp+gdp
if(year==1){
rec_time = rec_time+1
is_present = true
}
})
}
if (is_present){
obj.push({
country:d,
rates: parseFloat((rates/rec_time).toFixed(2)),
gdp_per_capita: parseFloat((tot_gdp/rec_time).toFixed(2))
})
}
})

return obj
}
Insert cell
margin = ({ left: 80, top: 20, right: 20, bottom: 20 })
Insert cell
function sortArrayDescending(arr, param) {
return arr.sort((a, b) => {
let x = a[param];
let y = b[param];

if (x > y) return -1;
if (x < y) return 1;
});
}
Insert cell
import {Legend, Swatches} from "@d3/color-legend"
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more