Skip to content
Snippets Groups Projects

Refactor

Merged Bjarke Madsen requested to merge refactor into develop
1 file
+ 30
12
Compare changes
  • Side-by-side
  • Inline
@@ -109,6 +109,7 @@ export const createTrafficVolumeDataset = (data: TrafficVolume[]) => {
@@ -109,6 +109,7 @@ export const createTrafficVolumeDataset = (data: TrafficVolume[]) => {
export const createFundingSourceDataset = (data: FundingSource[]) => {
export const createFundingSourceDataset = (data: FundingSource[]) => {
const dataLookup = createDataLookup(data);
const dataLookup = createDataLookup(data);
 
const colorMap = getColorMap();
const labelsYear = [...new Set(data.map((item: FundingSource) => item.year))].sort();
const labelsYear = [...new Set(data.map((item: FundingSource) => item.year))].sort();
const labelsNREN = [...new Set(data.map((item: FundingSource) => item.nren))].sort();
const labelsNREN = [...new Set(data.map((item: FundingSource) => item.nren))].sort();
@@ -119,22 +120,39 @@ export const createFundingSourceDataset = (data: FundingSource[]) => {
@@ -119,22 +120,39 @@ export const createFundingSourceDataset = (data: FundingSource[]) => {
"GOV/PUBLIC_BODIES",
"GOV/PUBLIC_BODIES",
"OTHER"
"OTHER"
];
];
const fundingSourcesPerYear = cartesianProduct(fundingSources, labelsYear);
const fundingSourcesPerYear: { [key: string]: { [key: string]: number } } = cartesianProduct(fundingSources, labelsYear)
.reduce((obj, [fundingSource, year]) => {
const colorMap = getColorMap();
const key = `${fundingSource},${year}`
const fundingSourceDataset = fundingSourcesPerYear.map(function ([fundingSource, year]) {
obj[key] = {};
 
return obj
 
}, {});
const color = colorMap.get(fundingSource)!;
dataLookup.forEach((yearMap, nren) => {
 
yearMap.forEach((fundingSource, year) => {
 
const valuesPerSource = fundingSources.map(source => {
 
const key = source.toLowerCase().replaceAll("/", "_").replaceAll(" ", "_")
 
return fundingSource[key] || 0;
 
});
 
const sum = valuesPerSource.reduce((a, b) => a + b, 0);
 
if (sum === 0) {
 
return;
 
}
 
for (const fundingSource of fundingSources) {
 
const key = `${fundingSource},${year}`;
 
const index = fundingSources.indexOf(fundingSource);
 
fundingSourcesPerYear[key][nren] = valuesPerSource[index];
 
}
 
});
 
});
 
const fundingSourceDataset = Array.from(Object.entries(fundingSourcesPerYear)).map(([key, data]) => {
 
const [fundingSource, year] = key.split(",");
 
const color = colorMap.get(fundingSource) || 'black'; // if we get black, we have a problem
return {
return {
backgroundColor: color,
backgroundColor: color,
label: fundingSource + "(" + year + ")",
label: fundingSource + "(" + year + ")",
data: labelsNREN.map(nren => {
data: labelsNREN.map(nren => {
// ensure that the data is in the same order as the labels
// ensure that the data is in the same order as the labels
const dataForYear = dataLookup.get(nren)!.get(year)
return data[nren];
if (!dataForYear) {
return 0
}
return dataForYear[fundingSource.toLowerCase().replaceAll("/", "_").replaceAll(" ", "_")] ?? 0
}),
}),
stack: year,
stack: year,
borderSkipped: true,
borderSkipped: true,
@@ -143,7 +161,7 @@ export const createFundingSourceDataset = (data: FundingSource[]) => {
@@ -143,7 +161,7 @@ export const createFundingSourceDataset = (data: FundingSource[]) => {
categoryPercentage: 0.8,
categoryPercentage: 0.8,
hidden: false,
hidden: false,
datalabels: {
datalabels: {
display: fundingSource == fundingSources[0],
display: fundingSource == fundingSources[0], // workaround for datalabels being duplicated for each stack
formatter: function (value, context) {
formatter: function (value, context) {
return context.dataset.stack;
return context.dataset.stack;
},
},
@@ -159,7 +177,7 @@ export const createFundingSourceDataset = (data: FundingSource[]) => {
@@ -159,7 +177,7 @@ export const createFundingSourceDataset = (data: FundingSource[]) => {
}
}
}
}
}
}
})
});
const dataResponse: BarChartDataset = {
const dataResponse: BarChartDataset = {
datasets: fundingSourceDataset,
datasets: fundingSourceDataset,
Loading