Published
Edited
Mar 14, 2022
2 stars
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
dms = {
let dimensions = {
width: width,
height: Math.min(width * (16 / 9), 900),
heightSmall: width * (9 / 16),
marginTop: 30
};

dimensions.marginLeft = dimensions.marginTop;
dimensions.marginBottom = dimensions.marginTop;
dimensions.marginRight = dimensions.marginTop;

dimensions.chartWidth =
dimensions.width - dimensions.marginLeft - dimensions.marginRight;
dimensions.chartHeight =
dimensions.height - dimensions.marginTop - dimensions.marginBottom;
dimensions.chartHeightSmall =
dimensions.heightSmall - dimensions.marginTop - dimensions.marginBottom;

return dimensions;
}
Insert cell
colors = {
return {
deaths: '#ff3333',
deathsNone: '#ff9191',
land: '#F7F7F7',
boundary: '#C0C0C0'
};
}
Insert cell
Insert cell
timeParser = function(date) {
if (date.includes('/')) {
return d3.timeParse("%d/%m/%Y")(date);
} else {
return d3.timeParse("%d %b %Y")(date);
}
}
Insert cell
timeFormatter = function(date) {
const months = [
"Jan.",
"Feb.",
"March",
"April",
"May",
"June",
"July",
"Aug.",
"Sept.",
"Oct.",
"Nov.",
"Dec."
];
const monthIndex = +d3.timeFormat('%m')(date) - 1;
const day = +d3.timeFormat('%e')(date);
let remainingDate = "";

if (day < 9) {
remainingDate = d3.timeFormat(`${months[monthIndex]}%e, %Y`)(date);
} else {
remainingDate = d3.timeFormat(`${months[monthIndex]} %e, %Y`)(date);
}

return remainingDate;
}
Insert cell
timeParserInput = d3.timeParse("%Y-%m-%d")
Insert cell
timeFormatterInput = d3.timeFormat("%Y-%m-%d")
Insert cell
Insert cell
townsAAPP = [
...d3
.rollup(
dataDeaths.filter(f => f['Town_eng'].length > 0),
v => {
return {
township: v[0]['Town_eng'],
region: v[0]['State_Region']
};
},
d => d['Town_eng']
)
.values()
].sort((a, b) => d3.ascending(a['township'], b['township']))
Insert cell
townsMIMU = {
const states = geoMyanmarTownships['features'].map(d => {
return {
township: d['properties']['TS'],
district: d['properties']['DT'],
state: d['properties']['ST']
};
});

return states.sort((a, b) => d3.ascending(a['township'], b['township']));
}
Insert cell
crosswalk = {
const attachment = await FileAttachment("crosswalkTownships@1.csv").text();
return d3.csvParse(attachment).map(d => {
const townshipAAPP = [];

for (let i = 1; i <= 6; i++) {
if (d['townshipAAPP-v' + i].length > 0) {
townshipAAPP.push(d['townshipAAPP-v' + i]);
}
}

const e = {
district: d['district'],
state: d['state'],
townshipMIMU: d['townshipMIMU']
};

d['townshipSFM'].length > 0 ? (e['townshipSFM'] = d['townshipSFM']) : null;
townshipAAPP.length > 0 ? (e['townshipAAPP'] = townshipAAPP) : null;

return e;
});
}
Insert cell
crosswalkSFM = {
const attachment = await FileAttachment(
"SecurityForceMonitorLocationsCrosswalk.csv"
).text();

return d3.rollup(
d3.csvParse(attachment),
v => v[0],
d => d['location:humane_id:admin']
);
}
Insert cell
Insert cell
dataLegend = [0, 30, 60, 120]
Insert cell
dataDeaths = {
const attachment = await FileAttachment(
"Myanmar Spring Revolution Data - death_cases.csv"
).text();

return d3
.csvParse(attachment)
.map(d => {
d["DateRaw"] = timeParser(d["Incident Date"]);
d["DateFormatted"] = timeFormatter(d["DateRaw"]);

d['Location'] = {
township: d['Town_eng'] === "Mandalay" ? 'Mahaaungmyay' : d['Town_eng'],
state: d['State_Region']
};

return d;
})
.filter(d => d["DateRaw"] != null);
}
Insert cell
dataDeathsPerDayAndTown = {
const totals = d3.rollups(
dataDeaths
.sort((a, b) => d3.ascending(a['DateRaw'], b['DateRaw']))
.filter(f => f["Location"]["township"]),
v => v.length,
d => d['DateRaw'],
d => d['Location']['township']
);

const totalsProcessed = totals.map(d =>
d[1].map(e => {
return { date: d[0], township: e[0], deaths: e[1] };
})
);

return d3
.merge(totalsProcessed)
.sort((a, b) => d3.descending(a['deaths'], b['deaths']));
}
Insert cell
dataDeathsPerDay = d3.rollup(
dataDeaths.sort((a, b) => d3.ascending(a['DateRaw'], b['DateRaw'])),
v => {
return {
date: v[0]['DateRaw'],
total: v.length,
gunshot: v.filter(f => f['Type'].includes('gunshot')).length,
other: v.filter(
f => f['Type'].includes('gunshot') == false && f['Type'].length > 0
).length,
unknown: v.filter(f => f['Type'].length < 1).length
};
},
d => d["DateRaw"]
)
Insert cell
dataDeathsPerTown = [
...d3
.rollup(
dataDeaths
.filter(f => f["Location"]["township"])
.filter(
f =>
timeParserInput(dateStart) <= f['DateRaw'] &&
f['DateRaw'] <= timeParserInput(dateEnd)
),
v => {
const deaths = v.length;
const deathsGunshot = v.filter(
f => f['Type'].includes('gunshot') && f['Type'].length > 0
).length;
const township = v[0]["Location"]["township"];
const state = v[0]["Location"]["state"];

return { township, state, deaths, deathsGunshot };
},
d => d["Location"]["township"]
)
.values()
]
Insert cell
dataDeathsPerTownTotal = [
...d3
.rollup(
dataDeaths.filter(f => f["Location"]["township"]),
v => {
const deaths = v.length;
const deathsGunshot = v.filter(
f => f['Type'].includes('gunshot') && f['Type'].length > 0
).length;
const township = v[0]["Location"]["township"];
const state = v[0]["Location"]["state"];
const deathsArray = v;

return { township, state, deaths, deathsGunshot, deathsArray };
},
d => d["Location"]["township"]
)
.values()
]
Insert cell
Insert cell
geoMyanmarBorder = {
const attachment = await FileAttachment("ne10.topo.json").json();

// return attachment;

const converted = topojson.feature(
attachment,
attachment['objects']['ne_10m_admin_0_countries']
);

// return converted;

return converted['features'].filter(
f => f['properties']['ISO_A3'] == 'MMR'
)[0];
}
Insert cell
geoMyanmarTownships = {
const attachment = await FileAttachment(
"MyanmarTownships.topo@2.json"
).json();

return topojson.feature(
attachment,
attachment['objects']['MyanmarTownships']
);
}
Insert cell
Insert cell
projection = {
// "+proj=laea +lon_0=96.328125 +lat_0=18.2744487 +x_0=0 +y_0=0 +datum=WGS84 +units=m +no_defs"

// return d3
// .geoMercator()
// .fitExtent(
// [[dms.marginLeft, dms.marginTop], [dms.chartWidth, dms.chartHeight]],
// geoMyanmarBorder
// );

return proj4d3(
`+proj=laea +lon_0=96.328125 +lat_0=18.2744487 +x_0=0 +y_0=0 +datum=WGS84 +units=m +no_defs`
).fitExtent(
[[dms.marginLeft, dms.marginTop], [dms.chartWidth, dms.chartHeight]],
geoMyanmarBorder
);
}
Insert cell
path = d3.geoPath(projection)
Insert cell
Insert cell
// {
// const dates = [...dataDeathsPerDay.keys()];
// // return dates[dates.length - 1];
// return d3.timeDay.offset(dates[dates.length - 1], 1);
// }
Insert cell
scaleTime = {
const dates = [...dataDeathsPerDay.keys()];

return d3
.scaleTime()
.domain([dates[0], d3.timeDay.offset(dates[dates.length - 1], 1)])
.range([0, dms.chartWidth]);
}
Insert cell
rangeDates = {
const range = d3.timeDay.range(
scaleTime.domain()[0],
scaleTime.domain()[1],
1
);

range.push(scaleTime.domain()[1]);

return range;
}
Insert cell
scaleDeaths = {
const deaths = [...dataDeathsPerDay.values()];

return d3
.scaleLinear()
.domain([0, d3.max(deaths, d => d['total'])])
.range([dms.chartHeightSmall, 0])
.nice();
}
Insert cell
scaleDeathsGradient = {
const deaths = dataDeathsPerTown;

// return d3.max(deaths, d => +d['deaths']);

return d3
.scaleLinear()
.domain([0, d3.max(deaths, d => +d['deaths'])])
.interpolate(d3.interpolateRgb)
.range(['#fff', colors.deaths]);
}
Insert cell
scaleDeathsRadius = {
const deaths = dataDeathsPerTownTotal;

// return d3.max(deaths, d => +d['deaths']);

return d3
.scaleLinear()
.domain([0, d3.max(deaths, d => +d['deaths'])])
.range([0, 300]);
}
Insert cell
scaleDeathsSpike = {
const deaths = dataDeathsPerTownTotal;

// return d3.max(deaths, d => +d['deaths']);

return d3
.scaleLinear()
.domain([0, d3.max(deaths, d => +d['deaths'])])
.range([0, 50]);
}
Insert cell
Insert cell
spike = function(heightInput, widthInput) {
return `M ${-widthInput / 2},0 0,${-heightInput} ${widthInput / 2},0`;
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more