Jump to: navigation, search

Difference between revisions of "Widget:PnChoropleths"

m
m
Line 12: Line 12:
 
};
 
};
 
 
 +
}() );
 +
 +
 +
//Data
 +
( function() {
 +
 +
window.IRHB = window.IRHB || {};
 +
var w = window.IRHB;
 +
w.data = {};
 +
var d = w.data;
 +
d.stats = false;
 +
var get = w.get;
 +
 +
d.colours = [
 +
{limit: 10, rgb: "a30000"}, {limit: 20, rgb: "b70000"}, {limit: 30, rgb: "d11919"}, {limit: 40, rgb: "db4c4c"}, {limit: 50, rgb: "e06666"}, {limit: 60, rgb: "e57f7f"},
 +
{limit: 70, rgb: "ea9999"}, {limit: 80, rgb: "efb2b2"}, {limit: 90, rgb: "f4cccc"}, {limit: 100, rgb: "f9e5e5"}, {limit: 110, rgb: "ffffff"}, {limit: 120, rgb: "f9faf9"},
 +
{limit: 130, rgb: "f3f5f3"}, {limit: 140, rgb: "ecefec"}, {limit: 150, rgb: "d9e0da"}, {limit: 160, rgb: "c7d0c7"}, {limit: 170, rgb: "b4c1b5"}, {limit: 180, rgb: "a2b1a3"},
 +
{limit: 190, rgb: "8fa290"}, {limit: 200, rgb: "7c927e"}, {limit: 300, rgb: "6a836b"}, {limit: 400, rgb: "577359"}, {limit: 500, rgb: "456447"}, {limit: 600, rgb: "3e5a3f"},
 +
{limit: 700, rgb: "375038"}, {limit: 800, rgb: "304631"}, {limit: 900, rgb: "293c2a"}, {limit: 1000, rgb: "223223"}, {limit: 1500, rgb: "1b281c"}, {limit: 2000, rgb: "141e15"}
 +
];
 +
var nCols = d.colours.length;
 +
 +
d.England = {'Robin Hood name': 0, 'Local tradition': 0, 'Literary locale': 0, 'Artifacts': 0, 'Miscellaneous': 0, 'total': 0};
 +
 +
d.counties = {
 +
'Bedfordshire': {'path': 1, 'Robin Hood name': 0, 'Local tradition': 0, 'Literary locale': 0, 'Artifacts': 0, 'Miscellaneous': 0, 'total': 0},
 +
'Berkshire': {'path': 2, 'Robin Hood name': 0, 'Local tradition': 0, 'Literary locale': 0, 'Artifacts': 0, 'Miscellaneous': 0, 'total': 0},
 +
'Buckinghamshire': {'path': 3, 'Robin Hood name': 0, 'Local tradition': 0, 'Literary locale': 0, 'Artifacts': 0, 'Miscellaneous': 0, 'total': 0},
 +
'Cambridgeshire': {'path': 4, 'Robin Hood name': 0, 'Local tradition': 0, 'Literary locale': 0, 'Artifacts': 0, 'Miscellaneous': 0, 'total': 0},
 +
'Cheshire': {'path': 5, 'Robin Hood name': 0, 'Local tradition': 0, 'Literary locale': 0, 'Artifacts': 0, 'Miscellaneous': 0, 'total': 0},
 +
'Cornwall': {'path': 6, 'Robin Hood name': 0, 'Local tradition': 0, 'Literary locale': 0, 'Artifacts': 0, 'Miscellaneous': 0, 'total': 0},
 +
'Cumberland': {'path': 0, 'Robin Hood name': 0, 'Local tradition': 0, 'Literary locale': 0, 'Artifacts': 0, 'Miscellaneous': 0, 'total': 0},
 +
'Derbyshire': {'path': 7, 'Robin Hood name': 0, 'Local tradition': 0, 'Literary locale': 0, 'Artifacts': 0, 'Miscellaneous': 0, 'total': 0},
 +
'Devon': {'path': 8, 'Robin Hood name': 0, 'Local tradition': 0, 'Literary locale': 0, 'Artifacts': 0, 'Miscellaneous': 0, 'total': 0},
 +
'Dorset': {'path': 9, 'Robin Hood name': 0, 'Local tradition': 0, 'Literary locale': 0, 'Artifacts': 0, 'Miscellaneous': 0, 'total': 0},
 +
'Durham': {'path': 10, 'Robin Hood name': 0, 'Local tradition': 0, 'Literary locale': 0, 'Artifacts': 0, 'Miscellaneous': 0, 'total': 0},
 +
'Essex': {'path': 11, 'Robin Hood name': 0, 'Local tradition': 0, 'Literary locale': 0, 'Artifacts': 0, 'Miscellaneous': 0, 'total': 0},
 +
'Gloucestershire': {'path': 12, 'Robin Hood name': 0, 'Local tradition': 0, 'Literary locale': 0, 'Artifacts': 0, 'Miscellaneous': 0, 'total': 0},
 +
'Hampshire': {'path': 13, 'Robin Hood name': 0, 'Local tradition': 0, 'Literary locale': 0, 'Artifacts': 0, 'Miscellaneous': 0, 'total': 0},
 +
'Herefordshire': {'path': 14, 'Robin Hood name': 0, 'Local tradition': 0, 'Literary locale': 0, 'Artifacts': 0, 'Miscellaneous': 0, 'total': 0},
 +
'Hertfordshire': {'path': 15, 'Robin Hood name': 0, 'Local tradition': 0, 'Literary locale': 0, 'Artifacts': 0, 'Miscellaneous': 0, 'total': 0},
 +
'Huntingdonshire': {'path': 16, 'Robin Hood name': 0, 'Local tradition': 0, 'Literary locale': 0, 'Artifacts': 0, 'Miscellaneous': 0, 'total': 0},
 +
'Kent': {'path': 17, 'Robin Hood name': 0, 'Local tradition': 0, 'Literary locale': 0, 'Artifacts': 0, 'Miscellaneous': 0, 'total': 0},
 +
'Lancashire': {'path': 18, 'Robin Hood name': 0, 'Local tradition': 0, 'Literary locale': 0, 'Artifacts': 0, 'Miscellaneous': 0, 'total': 0},
 +
'Leicestershire': {'path': 19, 'Robin Hood name': 0, 'Local tradition': 0, 'Literary locale': 0, 'Artifacts': 0, 'Miscellaneous': 0, 'total': 0},
 +
'Lincolnshire': {'path': 20, 'Robin Hood name': 0, 'Local tradition': 0, 'Literary locale': 0, 'Artifacts': 0, 'Miscellaneous': 0, 'total': 0},
 +
'Middlesex': {'path': 21, 'Robin Hood name': 0, 'Local tradition': 0, 'Literary locale': 0, 'Artifacts': 0, 'Miscellaneous': 0, 'total': 0},
 +
'Norfolk': {'path': 22, 'Robin Hood name': 0, 'Local tradition': 0, 'Literary locale': 0, 'Artifacts': 0, 'Miscellaneous': 0, 'total': 0},
 +
'Northamptonshire': {'path': 23, 'Robin Hood name': 0, 'Local tradition': 0, 'Literary locale': 0, 'Artifacts': 0, 'Miscellaneous': 0, 'total': 0},
 +
'Northumberland': {'path': 24, 'Robin Hood name': 0, 'Local tradition': 0, 'Literary locale': 0, 'Artifacts': 0, 'Miscellaneous': 0, 'total': 0},
 +
'Nottinghamshire': {'path': 25, 'Robin Hood name': 0, 'Local tradition': 0, 'Literary locale': 0, 'Artifacts': 0, 'Miscellaneous': 0, 'total': 0},
 +
'Oxfordshire': {'path': 38, 'Robin Hood name': 0, 'Local tradition': 0, 'Literary locale': 0, 'Artifacts': 0, 'Miscellaneous': 0, 'total': 0},
 +
'Rutland': {'path': 26, 'Robin Hood name': 0, 'Local tradition': 0, 'Literary locale': 0, 'Artifacts': 0, 'Miscellaneous': 0, 'total': 0},
 +
'Shropshire': {'path': 27, 'Robin Hood name': 0, 'Local tradition': 0, 'Literary locale': 0, 'Artifacts': 0, 'Miscellaneous': 0, 'total': 0},
 +
'Somerset': {'path': 28, 'Robin Hood name': 0, 'Local tradition': 0, 'Literary locale': 0, 'Artifacts': 0, 'Miscellaneous': 0, 'total': 0},
 +
'Staffordshire': {'path': 29, 'Robin Hood name': 0, 'Local tradition': 0, 'Literary locale': 0, 'Artifacts': 0, 'Miscellaneous': 0, 'total': 0},
 +
'Suffolk': {'path': 30, 'Robin Hood name': 0, 'Local tradition': 0, 'Literary locale': 0, 'Artifacts': 0, 'Miscellaneous': 0, 'total': 0},
 +
'Surrey': {'path': 31, 'Robin Hood name': 0, 'Local tradition': 0, 'Literary locale': 0, 'Artifacts': 0, 'Miscellaneous': 0, 'total': 0},
 +
'Sussex': {'path': 32, 'Robin Hood name': 0, 'Local tradition': 0, 'Literary locale': 0, 'Artifacts': 0, 'Miscellaneous': 0, 'total': 0},
 +
'Warwickshire': {'path': 33, 'Robin Hood name': 0, 'Local tradition': 0, 'Literary locale': 0, 'Artifacts': 0, 'Miscellaneous': 0, 'total': 0},
 +
'Westmorland': {'path': 34, 'Robin Hood name': 0, 'Local tradition': 0, 'Literary locale': 0, 'Artifacts': 0, 'Miscellaneous': 0, 'total': 0},
 +
'Wiltshire': {'path': 35, 'Robin Hood name': 0, 'Local tradition': 0, 'Literary locale': 0, 'Artifacts': 0, 'Miscellaneous': 0, 'total': 0},
 +
'Worcestershire': {'path': 36, 'Robin Hood name': 0, 'Local tradition': 0, 'Literary locale': 0, 'Artifacts': 0, 'Miscellaneous': 0, 'total': 0},
 +
'Yorkshire': {'path': 37, 'Robin Hood name': 0, 'Local tradition': 0, 'Literary locale': 0, 'Artifacts': 0, 'Miscellaneous': 0, 'total': 0}
 +
};
 +
 +
 +
function getColour(val) {
 +
var colour = "#000000";
 +
for (var i = 0; i < nCols; i++) {
 +
var cur = d.colours[i];
 +
if (val <= cur.limit) {
 +
colour = "#" + cur.rgb;
 +
break;
 +
}
 +
}
 +
return colour;
 +
}
 +
 +
 +
function calcCountyIndexes() {
 +
d.England.pnperkm2idx = 100;
 +
for (var key in d.counties) {
 +
var cur = d.counties[key];
 +
cur.pnperkm2 = cur.total / cur.area;
 +
cur.km2perpn = cur.area / cur.total;
 +
cur.pnperkm2idx = (cur.pnperkm2 / d.England.pnperkm2) * 100;
 +
var expected = d.England.pnperkm2 * cur.area;
 +
expected = (expected / d.England.popdensity) * cur.popdensity;
 +
cur.pnperkm2popidx = (cur.total / expected) * 100;
 +
cur.totalidx = cur.total !== 0 ? (cur.total / d.England.pnpercounty) * 100 : 0;
 +
cur.totalcol = getColour(cur.totalidx);
 +
cur.pnperkm2col = getColour(cur.pnperkm2idx);
 +
cur.pnperkm2popcol = getColour(cur.pnperkm2popidx);
 +
}
 +
}
 +
 +
 +
function calcCounties(res) {
 +
var counties = res.query.results;
 +
var numCounties = 0;
 +
var engArea = 0, engPop = 0;
 +
for (var key in counties) {
 +
if (key.indexOf("Riding") === -1 && key.indexOf("London") === -1) {
 +
var cty = key.replace(" place-names", "");
 +
numCounties++;
 +
var a = counties[key].printouts.Area[0];
 +
d.counties[cty].area = a;
 +
engArea += a;
 +
var p = counties[key].printouts.Population[0];
 +
d.counties[cty].population = p;
 +
d.counties[cty].popdensity = p / a;
 +
d.counties[cty].km2perperson = a / p;
 +
engPop += p;
 +
}
 +
}
 +
d.England.area = engArea;
 +
d.England.population = engPop;
 +
d.England.popdensity = engPop / engArea;
 +
d.England.km2perperson = engArea / engPop;
 +
d.England.counties = numCounties;
 +
}
 +
 +
 +
function calcPlaceNames(res) {
 +
var PNs = res.query.results;
 +
for (var key in PNs) {
 +
var admdiv = PNs[key].printouts.Pnadmdiv[0];
 +
var interest = PNs[key].printouts.Pninterest[0];
 +
var admdivObj = d.counties[admdiv];
 +
admdivObj.total++;
 +
d.England.total++;
 +
admdivObj[interest]++;
 +
d.England[interest]++;
 +
}
 +
d.England.km2perpn = d.England.area / d.England.total;
 +
d.England.pnperkm2 = d.England.total / d.England.area;
 +
d.England.pnpercounty = d.England.total / d.England.counties;
 +
calcCountyIndexes();
 +
}
 +
 +
 +
d.calcStats = function() {
 +
if (!d.stats) {
 +
w.map.addSpinner(get(w.map.curMap));
 +
var url = "https://www.irhb.org/wiki/api.php?action=ask&query=[[Category:English%20counties%20with%20place-names]]|?Area|?Population&format=json";
 +
fetch(url).then(res => res.json()).then((jsnCty) => {
 +
calcCounties(jsnCty);
 +
url = "https://www.irhb.org/wiki/api.php?action=ask&query=[[Category:English%20place-names]]|?Pninterest|?Pnadmdiv|sort=Pnadmdiv&format=json";
 +
fetch(url).then(res => res.json()).then((jsnPN) => {
 +
calcPlaceNames(jsnPN);
 +
d.stats = true;
 +
w.map.prepareMap();
 +
prepareDataTable();
 +
}).catch(err => { throw err });
 +
}).catch(err => { throw err });
 +
} else {
 +
w.map.prepareMap();
 +
}
 +
};
 +
 +
 +
function prepareDataTable() {
 +
var e = window.IRHB.data.England;
 +
var c = window.IRHB.data.counties;
 +
 +
var tables = '<table class="bordertable expando" id="choropleth1" style="margin-bottom:-1px;">' +
 +
'<tbody id="england">' +
 +
'<tr class="header"><th colspan="2">England</th><th><span class="expando-c">&#x2014;&#xa0;</span><span class="expando-o">&#x25a2;&#xa0;</span></th></tr>' +
 +
'<tr><td>Artifact</td><td colspan="2">' + e.Artifacts + '</td></tr>' +
 +
'<tr><td>Literary locale</td><td colspan="2">' + e["Literary locale"] + '</td></tr>' +
 +
'<tr><td>Local tradition</td><td colspan="2">' + e["Local tradition"] + '</td></tr>' +
 +
'<tr><td>Miscellaneous</td><td colspan="2">' + e.Miscellaneous + '</td></tr>' +
 +
'<tr><td>Robin Hood name</td><td colspan="2">' + e["Robin Hood name"] + '</td></tr>' +
 +
'<tr><td>Area (km<sup>2</sup>)</td><td colspan="2">' + e.area + '</td></tr>' +
 +
'<tr><td>Population</td><td colspan="2">' + e.population + '</td></tr>' +
 +
'<tr><td>Pop. density/km<sup>2</sup></td><td colspan="2">' + e.popdensity + '</td></tr>' +
 +
'<tr><td>Place-names (PN)</td><td colspan="2">' + e.total + '</td></tr>' +
 +
'<tr><td>Counties</td><td colspan="2">' + e.counties + '</td></tr>' +
 +
'<tr><td>Km<sup>2</sup>/person</td><td colspan="2">' + e.km2perperson + '</td></tr>' +
 +
'<tr><td>Km<sup>2</sup>/PN</td><td colspan="2">' + e.km2perpn + '</td></tr>' +
 +
'<tr><td>PN/km<sup>2</sup></td><td colspan="2">' + e.pnperkm2 + '</td></tr>' +
 +
'<tr><td>PN/km<sup>2</sup> idx</td><td colspan="2">' + e.pnperkm2idx + '</td></tr>' +
 +
'<tr><td>PN/county avg.</td><td colspan="2">' + e.pnpercounty + '</td></tr>' +
 +
'</tbody>';
 +
 +
var counties = Object.keys(c);
 +
var nCounties = counties.length;
 +
var idxLeics = 19;
 +
for (var i = 0; i < idxLeics; i++) {
 +
var cty = counties[i];
 +
tables += getCountyData(cty, c[cty]);
 +
}
 +
tables += '</table>';
 +
tables += '<table class="bordertable expando" id="choropleth2">';
 +
for (var i = idxLeics; i < nCounties; i++) {
 +
var cty = counties[i];
 +
tables += getCountyData(cty, c[cty]);
 +
}
 +
tables += '</table>';
 +
get("dSet").innerHTML = tables;
 +
window.IRHB.expando.init();
 +
};
 +
 +
 +
function getCountyData(cty, d) {
 +
var txt = '<tbody id="' + cty + '"><tr class="header"><th colspan="2">' + cty + '</th>' +
 +
'<th><span class="expando-c">—&nbsp;</span><span class="expando-o">▢&nbsp;</span></th></tr>' +
 +
'<tr><td style="width:129px;">Area (km<sup>2</sup>)</td><td colspan="2">' + d.area + '</td></tr>' +
 +
'<tr><td>Population</td><td colspan="2">' + d.population + '</td></tr>' +
 +
'<tr><td>Pop. density/km<sup>2</sup></td><td colspan="2">' + d.popdensity + '</td></tr>' +
 +
'<tr><td>Km<sup>2</sup>/PN</td><td colspan="2">' + d.km2perpn + '</td></tr>' +
 +
'<tr><td>PN/km<sup>2</sup></td><td colspan="2">' + d.pnperkm2 + '</td></tr>' +
 +
'<tr><td>RH names</td><td colspan="2">' + d["Robin Hood name"] + '</td></tr>' +
 +
'<tr><td>Local traditions</td><td colspan="2">' + d["Local tradition"] + '</td></tr>' +
 +
'<tr><td>Literary locales</td><td colspan="2">' + d["Literary locale"] + '</td></tr>' +
 +
'<tr><td>Artifacts</td><td colspan="2">' + d.Artifacts+ '</td></tr>' +
 +
'<tr><td>Miscellaneous</td><td colspan="2">' + d.Miscellaneous + '</td></tr>' +
 +
'<tr><td>Total count</td><td colspan="2">' + d.total + '</td></tr>' +
 +
'<tr><td>Count idx.</td><td>' + d.totalidx + '</td><td style="background-color:#' + d.totalcol + '; width:1.5em;"></td></tr>' +
 +
'<tr><td>PN/km<sup>2</sup> idx.</td><td>' + d.pnperkm2idx + '</td><td style="background-color:#' + d.pnperkm2col + '; width:1.5em;"></td></tr>' +
 +
'<tr><td>PN/km<sup>2</sup>/pop. idx.</td><td>' + d.pnperkm2popidx + '</td><td style="background-color:#' + d.pnperkm2popcol + '; width:1.5em;"></td></tr>' +
 +
'</tbody>';
 +
return txt;
 +
}
 +
 
}() );
 
}() );
 
</script></includeonly>
 
</script></includeonly>

Revision as of 11:44, 2 June 2019

This widget displays the navigation map on country and administrative division landing pages in the place-names section of IRHB. It takes no parameters.