Note: These business units are not shown on the steamgraph:
- Animal Control
- Transit
- Rangers/River Valley Trails
- Capitial City Clean Up - Community Services
- Waste
These particular business units don't contribute a lot on a montly basis. Also view the flattened file, you will notice compared to other business units they have minimal values.
Original file (Open Data) is large thus loading in pre-processed data (flattened file)
311(July-21-2018).csv file has two columns, Date Created and Business Unit
Processed 311(July-21-2018) file as follows:
// loading large open data with only Date Created and Business Unit Columns
// Has 304,845 rows
d3.csv("311(July-21-2018).csv")
.then(makeData);
// months order
const months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
function makeData(response) {
// log(response);
// formating data - Adding month attribute
response.forEach(d => d.month = months[new Date(d["Date Created"]).getMonth()]);
let total;
let categories;
let monthsBizUnitsNest = d3.nest().key(d => d.month)
.key(d => d["Business Unit"]).entries(response);
/* formating data - Recipe to ensure all months have the same bizunits
this is not the case by default */
monthsBizUnitsNest.forEach((d, i) => {
const values = d.values;
const len = values.length;
const keys = values.map(p => p.key);
// 0 month (January has all categories)
// Thus get all the categories only from January
if(i == 0) {
total = len;
categories = keys;
};
// Months with less categories than January add Object
// Object will be {key: missing category, values: []}
if(len != total) {
const janSet = new Set(categories);
const monthSet = new Set(keys);
const missingCategories = [...difference(janSet, monthSet)];
missingCategories.forEach(p => {
const object = {};
object.key = p;
object.values = [];
values.push(object);
});
};
});
// formating data - Each month and it's Stats for 311 Requests categories
const monthsAgg = monthsBizUnitsNest.map(d => {
let object = {month: d.key};
d.values.forEach(p => object[p.key] = p.values.length);
return object;
}).sort((a, b) => months.indexOf(a.month) - months.indexOf(b.month));
// flattened-JSON
// 311(July-21-2018)-Flattened.json
const flattenedJson = {data: monthsAgg};
// finds the diffrence of setB from SetA
function difference(setA, setB) {
var _difference = new Set(setA);
setB.forEach(elem => {
_difference.delete(elem);
});
return _difference;
};
};
Above code produced 311(July-21-2018)-Flattened.json (flattened data).