Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <p><em>Data from <a href="https://wikitech.wikimedia.org/wiki/Incident_documentation" target="_blank">wikitech.wikimedia.org</a>.</em></p>
<hr/>
<h2>Overview</h2>
<div id="x-overview" x-pending></div>
<hr style="clear: both;"/>
<label>Year <select id="x-year">
	<option value="" disabled selected>Select a year</option>
</select></label>
<hr/>
<h2>Breakdown for <span id="x-title-year">…</span></h2>
<div id="x-breakdown" x-pending></div>
<h3>Table</h3>
<div id="x-table" x-pending></div>
<h3>TSV (Tab-separated-values)</h3>
<div id="x-tsv" x-pending></div>
              
            
!

CSS

              
                html,
body {
	margin: 0;
	padding: 0;
}

body {
	background: #fff;
	padding: 0.5em 1em;
	font: 14px/21px sans-serif;
	color: #333;
}

[x-pending]:empty:before {
	display: inline-block;
	pointer-events: none;
	font-style: italic;
	opacity: 0.75;
	border: 1px solid #ccc;
	padding: 1em 2em;
	content: '(Loading…)';
}

.chart {
	display: inline-block;
	border: 1px solid #ccc;
	margin: 0 0.5em 0.5em 0;
	padding: 0 0 0 1em;
	position: relative;
}

.chart:before {
	display: inline-block;
	content: attr(data-chart-ymax);
	position: absolute;
	box-sizing: border-box;
	top: 2.4em;
	left: 0;
	padding-left: 0.5em;
	font-size: 0.7em;
	box-sizing: border-box;
	width: auto;
	border-top: 1px solid #eee;
}

.chart:after {
	display: block;
	content: '0';
	position: absolute;
	bottom: 2em;
	left: 0;
	padding-left: 0.5em;
	font-size: 0.7em;
	box-sizing: border-box;
	width: auto;
	border-bottom: 1px solid #eee;
}

.chart h3 {
	text-align: center;
	margin: 0.2em;
	color: #777;
	position: relative;
}
.chart h3:after {
	position: absolute;
	right: 0;
	float: right;
	content: "total: " attr(data-chart-total);
	font-weight: normal;
	font-size: 0.7rem;
}
.chart:first-of-type h3:after {
	content: "subtotal: " attr(data-chart-total);
}

.chart-column {
	display: inline-block;
	margin: 0 0.1em;
	color: #555;
}
.chart-column:hover:after,
.chart-column:focus:after {
	position: absolute;
	top: -1em;
	right: 0.5em;
	z-index: 1;
	display: block;
	background: rgba(255, 255, 255, 0.9);
	padding: 0.2em 0.3em;
	content: attr(data-title);
	white-space: nowrap;
	border: 1px solid #aaa;
	box-shadow: 1px 1px 10px #ccc;
}

.chart-column-bar {
	display: block;
	width: 1em;
	height: 1px;
	min-height: 1px;
	background: darkcyan;
}

.chart-column-label {
	font-size: 0.7em;
}

table {
	background-color: #f8f9fa;
	color: #222;
	margin: 1em 0;
	border: 1px solid #a2a9b1;
	border-collapse: collapse;
}
td,
th {
	border: 1px solid #a2a9b1;
	padding: 0.2em 0.4em;
}
th {
	background-color: #eaecf0;
}
pre {
	font-family: monospace, monospace;
	color: #000;
	background-color: #f8f9fa;
	border: 1px solid #eaecf0;
	padding: 1em;
	white-space: pre-wrap;
	line-height: 1.3em;
	overflow-wrap: break-word;
}

              
            
!

JS

              
                const DOM = {
	empty: function(node) {
		while (node.lastChild) {
			node.removeChild(node.lastChild);
		}
	},
	create: function(tagName, text) {
		const el = document.createElement(tagName);
		if (text !== undefined) el.textContent = text;
		return el;
	}
};

async function fetchIncidentStats(year) {
	// TODO: This may need to handle continuation at some point,
	// but for now the total fits within the max limit per query.
	// https://www.mediawiki.org/wiki/API:Query#Continuing_queries
	let prefix = "Incident_documentation/" + year;
	let apiUrl =
		"https://wikitech.wikimedia.org/w/api.php?origin=*&format=json&action=query&prop=info&list=prefixsearch&indexpageids=1&pssearch=" +
		encodeURIComponent(prefix) +
		"&psprofile=strict&pslimit=max";
	let resp = await fetch(apiUrl, { mode: "cors" });
	let data = await resp.json();
	if (data.error) {
		throw new Error("API Error: " + JSON.stringify(data.error));
	}

	let byMonth = {};
	// Fill with zeroes
	let monthIdx = 12;
	while (monthIdx--) {
		byMonth[1 + monthIdx] = 0;
	}
	let titles = data.query.prefixsearch.map(info => info.title);
	titles.forEach(title => {
		let monthIdx = Number(title.slice(prefix.length).slice(0, 2));
		byMonth[monthIdx]++;
	});
	return {
		year: year,
		total: titles.length,
		byMonth: byMonth
	};
}

function getMonthName(monthNr, monthType) {
	monthType = monthType || "long";
	let fmt = getMonthName.formatters[monthType];
	if (fmt === undefined) {
		if (window.Intl && Intl.DateTimeFormat) {
			fmt = new Intl.DateTimeFormat("en-US", {
				month: monthType
			});
		} else {
			fmt = null;
		}
		getMonthName.formatters[monthType] = fmt;
	}
	if (fmt) {
		return fmt.format(new Date(2011, monthNr - 1, 1));
	} else {
		return "Month " + monthNr;
	}
}
getMonthName.formatters = Object.create(null);

function renderStats(stats) {
	// Update heading
	let yearPos = document.querySelector("#x-title-year");
	yearPos.textContent = stats.year;

	// Render paragraph
	let numbers = Object.values(stats.byMonth);
	numbers.sort((a, b) => a - b);
	// Assume even count, which months of a year are...
	let median = numbers[Math.floor(numbers.length / 2)];
	let max = Math.max(...numbers);
	// Insert paragraph
	let overviewPos = document.querySelector("#x-breakdown");
	overviewPos.innerHTML = `
<p>${stats.year} had ${stats.total} documented incidents.</p>
<p>Max: ${max} per month.</p>
<p>Median: ${median} per month</p>
`;

	// Render table
	let tableNode = document.createElement("table");
	let row, cell;

	row = tableNode.insertRow();
	cell = DOM.create("th", "Month in " + stats.year);
	row.append(cell);
	cell = DOM.create("th", "# of incidents");
	row.append(cell);
	for (let [key, value] of Object.entries(stats.byMonth)) {
		row = tableNode.insertRow();
		cell = document.createElement("th");
		cell = row.insertCell();
		cell.textContent = getMonthName(key);
		cell = row.insertCell();
		cell.textContent = value;
	}

	// Insert table
	let tablePos = document.querySelector("#x-table");
	DOM.empty(tablePos);
	tablePos.append(tableNode);

	// Render TSV
	let tsvNode = document.createElement("pre");
	let preText = "Month in " + stats.year + "\t" + "Incidents" + "\n";
	for (let [key, value] of Object.entries(stats.byMonth)) {
		preText += getMonthName(key) + "\t" + value + "\n";
	}
	tsvNode.textContent = preText;

	// Insert TSV
	let tsvPos = document.querySelector("#x-tsv");
	DOM.empty(tsvPos);
	tsvPos.append(tsvNode);
}

function addOverviewChart(year) {
	let overviewNode = document.querySelector("#x-overview");
	let chartNode = DOM.create("div");
	chartNode.className = "chart";
	// Append early so as to ensure order
	overviewNode.append(chartNode);

	fetchIncidentStats(year).then(function(stats) {
		const head = DOM.create("h3", stats.year);
		head.setAttribute('data-chart-total', stats.total)
		chartNode.append(head);
		const maxHeightEm = 4;
		// Get highest
		let actualHighest = 1;
		for (let [, value] of Object.entries(stats.byMonth)) {
			actualHighest = Math.max(actualHighest, value);
		}
		chartNode.setAttribute("data-chart-ymax", actualHighest);
		for (let [monthNum, value] of Object.entries(stats.byMonth)) {
			let column = DOM.create("span");
			column.className = "chart-column";
			column.dataset.title = getMonthName(monthNum) + ': ' + value + ' incidents';
			column.tabIndex = '0';

			let bar = DOM.create("span");
			bar.className = "chart-column-bar";
			let height = value / actualHighest * maxHeightEm;
			bar.style.height = height + "em";

			let label = DOM.create("span", getMonthName(monthNum, "short"));
			label.className = "chart-column-label";
			column.append(bar, label);
			chartNode.append(column);
		}
		/*byMonth: {1: 0, 2: 0, 3: 0, 4: 0, 5: 1, 6: 1, 7: 1, 8: 0, 9: 4, 10: 0, 11: 1, 12: 0}
total: 8
year: 2011
	for (let [key, value] of Object.entries(stats.byMonth)) {
		row = tableNode.insertRow();
		cell = document.createElement("th");
		cell = row.insertCell();
		cell = row.insertCell();
		cell.textContent = value;
*/
	});
}

function init() {
	document.documentElement.lang = "en-US";

	let currentYear = new Date().getUTCFullYear();
	let minYear = 2011;

	let yearSelect = document.querySelector("#x-year");
	let year = currentYear;
	do {
		let option = document.createElement("option");
		option.value = option.textContent = year;
		option.selected = year === currentYear;
		yearSelect.add(option);
		addOverviewChart(year);
		year--;
	} while (year >= minYear);

	yearSelect.oninput = function() {
		fetchIncidentStats(this.value).then(renderStats);
	};

	fetchIncidentStats(currentYear).then(renderStats);
}

init();

              
            
!
999px

Console