allDataUnion = {
let array = [];
let colours = {
Historic: "#4B79A8",
Memorial: "#F48520",
Park: "#E35656",
Waters: "#72B7B2",
}
for (let year = 1900; year < 2020; year++) {
let gasPriceThisYear = gasPrice.filter(d => d.year == year);
if (gasPriceThisYear.length > 0) gasPriceThisYear = gasPriceThisYear[0].price;
else gasPriceThisYear = 0;
let incomeThisYear = income.filter(d => d.year == year);
if (incomeThisYear.length > 0) incomeThisYear = incomeThisYear[0].income;
else incomeThisYear = 0;
let populationThisYear = population.filter(d => d.year == year);
if (populationThisYear.length > 0) populationThisYear = d3.sum(populationThisYear.map(d => d.pop));
else populationThisYear = 0;
let tourismThisYear = tourismIn.filter(d => d.year == year);
if (tourismThisYear.length > 0) tourismThisYear = d3.sum(tourismThisYear.map(d => d.population));
else tourismThisYear = 0;
let numberOfParksThisYear = park.filter(d => d.year == year);
if (numberOfParksThisYear.length > 0) numberOfParksThisYear = d3.sum(numberOfParksThisYear.map(d => d.count));
else numberOfParksThisYear = 0;
// parks for that year
let parkVisits = park.filter(d => d.year == year);
// if (parkVisits.length > 0) parkVisits = d3.sum(parkVisits.map(d => d.visitors))
// else parkVisits = 0;
// list for per year aggregation and calculation
let tempList = [];
for (let c = 0; c < parkVisits.length; c++) {
// custom type filter. if it does not match our filter, skip to next part
let type = filter(parkVisits[c].type);
if (type === null) {
continue;
}
// set color to color of type
let color = colours[type];
// set up data for entry
let d = {
year: year,
parkVisits: parseFloat(parkVisits[c].visitors),
parkState: parkVisits[c].state,
parkType: type,
parkName: parkVisits[c].parkname,
gasPrice: gasPriceThisYear,
income: incomeThisYear,
population: populationThisYear,
tourist: tourismThisYear,
color: color,
numberOfParks: numberOfParksThisYear
}
// add to list
tempList.push(d);
}
// count how many parks are there per type
let typeCount = d3.nest()
.key(d => d.parkType)
.rollup(v => v.length)
.entries(tempList);
// total part visits per type
let typeVisitorsCount = d3.nest()
.key(d => d.parkType)
.rollup(v => {
return v.reduce((acc, d) => acc += d.parkVisits, 0)
})
.entries(tempList);
for (let c = 0; c < tempList.length; c++) {
// get the park
let parkTemp = tempList[c];
// get how many parks were there for the same part type
let typeCountTemp = typeCount.filter(d => d.key == parkTemp.parkType)[0].value;
// get how may park visits for that park type in total
let typeVisitorsCountTemp = typeVisitorsCount.filter(d => d.key == parkTemp.parkType)[0].value;
// type's percentage
parkTemp["percentPerTypePerYear"] = typeCountTemp / tempList.length * 100;
// ratio between how many parks there are and its total visits number.
parkTemp["perTypePerYearRatioWithVisitor"] = typeVisitorsCountTemp / tempList.filter(
d => d.parkType == parkTemp.parkType
).length;
}
// add temp list into final array
array = array.concat(tempList);
}
array.filter(d => d.parkVisits > 0);
return array;
}