General refactoring

master
Aadhavan Srinivasan 1 month ago
parent a38657ddf1
commit 7112874a06

@ -117,7 +117,7 @@ const district2lang = { // Should override state colors
"Hapur": languages["urdu"], "Hapur": languages["urdu"],
"Kutch": languages["sindhi"], "Kutch": languages["sindhi"],
"Godda": languages["santali"], "Godda": languages["santali"],
"Deoghar": languages["santali"], "Deoghar": languages["santali"],
"Dumka": languages["santali"], "Dumka": languages["santali"],
@ -142,6 +142,7 @@ const district2lang = { // Should override state colors
"Bahraich": languages["awadhi"], "Bahraich": languages["awadhi"],
} }
// Functions for calculating and dealing with language boundaries
function reverseCoordArrays(coords) { function reverseCoordArrays(coords) {
if (!Array.isArray(coords)) { if (!Array.isArray(coords)) {
return coords; return coords;
@ -157,14 +158,13 @@ function reverseCoordArrays(coords) {
return coords.map(reverseCoordArrays); return coords.map(reverseCoordArrays);
} }
function getOuterBoundaryPolygon(features) { function getOuterBoundaryPolygon(features) {
// Check if we have features to process // Check if we have features to process
if (!features || features.length === 0) { if (!features || features.length === 0) {
console.warn("No features to process for boundary"); console.warn("No features to process for boundary");
return null; return null;
} }
// Handle single feature case // Handle single feature case
if (features.length === 1) { if (features.length === 1) {
return features[0]; return features[0];
@ -176,6 +176,15 @@ function getOuterBoundaryPolygon(features) {
return combined; return combined;
} }
function district2langFunc(d) {
if (district2lang.hasOwnProperty(d.properties.district)) {
return district2lang[d.properties.district];
} else if (state2lang.hasOwnProperty(d.properties.st_nm)) {
return state2lang[d.properties.st_nm];
} else {
return undefined;
}
}
function stateOrDistrictOrLanguage(d) { function stateOrDistrictOrLanguage(d) {
if (typeof d.properties.district !== 'undefined') { if (typeof d.properties.district !== 'undefined') {
@ -204,24 +213,18 @@ function drawMap(world) {
.attr("d", path) .attr("d", path)
.attr("class", d => stateOrDistrictOrLanguage(d)) .attr("class", d => stateOrDistrictOrLanguage(d))
.attr("fill", function(d) { .attr("fill", function(d) {
if (stateOrDistrictOrLanguage(d) === "district") { if (stateOrDistrictOrLanguage(d) === "district") {
if (district2lang.hasOwnProperty(d.properties.district)) { const districtLang = district2langFunc(d)
return district2lang[d.properties.district].color; if (typeof districtLang !== 'undefined') {
} else if (state2lang.hasOwnProperty(d.properties.st_nm)) { return districtLang.color
return state2lang[d.properties.st_nm].color; } else {
} else { return defaultColor;
return defaultColor; }
} }
}
}) })
.each(function(d) { .each(function(d) {
if (stateOrDistrictOrLanguage(d) === "district") { if (stateOrDistrictOrLanguage(d) === "district") {
let districtLang; const districtLang = district2langFunc(d);
if (district2lang.hasOwnProperty(d.properties.district)) {
districtLang = district2lang[d.properties.district];
} else if (state2lang.hasOwnProperty(d.properties.st_nm)) {
districtLang = state2lang[d.properties.st_nm];
}
if (typeof districtLang !== 'undefined') { if (typeof districtLang !== 'undefined') {
districtLang.districts.push(d) districtLang.districts.push(d)
} }
@ -248,7 +251,7 @@ function drawMap(world) {
if (stateOrDistrictOrLanguage(d) == "language") { if (stateOrDistrictOrLanguage(d) == "language") {
return d.properties.lang_name; return d.properties.lang_name;
} else { } else {
d3.select(this).remove() // Only add text if the element is a language d3.select(this).remove() // Only add this attribute if the element is a language
} }
}); });

Loading…
Cancel
Save