Make intelligent use of objects instead of arrays within the preprocessor.

This commit is contained in:
Jan Philipp Timme 2017-12-08 12:53:55 +01:00
parent 5a8ca5e687
commit 9a7676affe
2 changed files with 47 additions and 13 deletions

View File

@ -27,14 +27,17 @@ var preprocessor = {
// data/circuits.csv // data/circuits.csv
fetchCircuits: function (callback) { fetchCircuits: function (callback) {
d3.csv('data/circuits.csv', function(data) { d3.csv('data/circuits.csv', function(data) {
var result = {};
// preprocess data // preprocess data
data.forEach(function(d, i) { data.forEach(function(d, i) {
d["circuitId"] = parseInt(d["circuitId"]); d["circuitId"] = parseInt(d["circuitId"]);
d["lat"] = parseFloat(d["lat"]); d["lat"] = parseFloat(d["lat"]);
d["lng"] = parseFloat(d["lng"]); d["lng"] = parseFloat(d["lng"]);
d["alt"] = parseInt(d["alt"]); d["alt"] = parseInt(d["alt"]);
// store processed data by its primary key in an object
result[d["circuitId"]] = d;
}); });
preprocessor.results.circuits = data; // Store results preprocessor.results.circuits = result; // Store results
loadingDialog.itemFinished(); // Update loading dialog progress bar loadingDialog.itemFinished(); // Update loading dialog progress bar
callback(null); // Tell the queue we're done. callback(null); // Tell the queue we're done.
}); });
@ -43,6 +46,7 @@ var preprocessor = {
// data/constructorResults.csv // data/constructorResults.csv
fetchConstructorResults: function (callback) { fetchConstructorResults: function (callback) {
d3.csv('data/constructorResults.csv', function(data) { d3.csv('data/constructorResults.csv', function(data) {
var result = {};
// preprocess data // preprocess data
data.forEach(function(d, i) { data.forEach(function(d, i) {
d["constructorId"] = parseInt(d["constructorId"]); d["constructorId"] = parseInt(d["constructorId"]);
@ -50,8 +54,10 @@ var preprocessor = {
d["points"] = parseInt(d["points"]); d["points"] = parseInt(d["points"]);
d["raceId"] = parseInt(d["raceId"]); d["raceId"] = parseInt(d["raceId"]);
d["status"] = parseInt(d["status"]); d["status"] = parseInt(d["status"]);
// store processed data by its primary key in an object
result[d["constructorResultsId"]] = d;
}); });
preprocessor.results.constructorResults = data; // Store results preprocessor.results.constructorResults = result; // Store results
loadingDialog.itemFinished(); // Update loading dialog progress bar loadingDialog.itemFinished(); // Update loading dialog progress bar
callback(null); // Tell the queue we're done. callback(null); // Tell the queue we're done.
}); });
@ -60,11 +66,14 @@ var preprocessor = {
// data/constructors.csv // data/constructors.csv
fetchConstructors: function (callback) { fetchConstructors: function (callback) {
d3.csv('data/constructors.csv', function(data) { d3.csv('data/constructors.csv', function(data) {
var result = {};
// preprocess data // preprocess data
data.forEach(function(d, i) { data.forEach(function(d, i) {
d["constructorId"] = parseInt(d["constructorId"]); d["constructorId"] = parseInt(d["constructorId"]);
// store processed data by its primary key in an object
result[d["constructorId"]] = d;
}); });
preprocessor.results.constructors = data; // Store results preprocessor.results.constructors = result; // Store results
loadingDialog.itemFinished(); // Update loading dialog progress bar loadingDialog.itemFinished(); // Update loading dialog progress bar
callback(null); // Tell the queue we're done. callback(null); // Tell the queue we're done.
}); });
@ -73,6 +82,7 @@ var preprocessor = {
// data/constructorStandings.csv // data/constructorStandings.csv
fetchConstructorStandings: function (callback) { fetchConstructorStandings: function (callback) {
d3.csv('data/constructorStandings.csv', function(data) { d3.csv('data/constructorStandings.csv', function(data) {
var result = {};
// preprocess data // preprocess data
data.forEach(function(d, i) { data.forEach(function(d, i) {
d["constructorId"] = parseInt(d["constructorId"]); d["constructorId"] = parseInt(d["constructorId"]);
@ -81,8 +91,10 @@ var preprocessor = {
d["position"] = parseInt(d["position"]); d["position"] = parseInt(d["position"]);
d["raceId"] = parseInt(d["raceId"]); d["raceId"] = parseInt(d["raceId"]);
d["wins"] = parseInt(d["wins"]); d["wins"] = parseInt(d["wins"]);
// store processed data by its primary key in an object
result[d["constructorStandingsId"]] = d;
}); });
preprocessor.results.constructorStandings = data; // Store results preprocessor.results.constructorStandings = result; // Store results
loadingDialog.itemFinished(); // Update loading dialog progress bar loadingDialog.itemFinished(); // Update loading dialog progress bar
callback(null); // Tell the queue we're done. callback(null); // Tell the queue we're done.
}); });
@ -91,13 +103,16 @@ var preprocessor = {
// data/drivers.csv // data/drivers.csv
fetchDrivers: function (callback) { fetchDrivers: function (callback) {
d3.csv('data/drivers.csv', function(data) { d3.csv('data/drivers.csv', function(data) {
var result = {};
// preprocess data // preprocess data
data.forEach(function(d, i) { data.forEach(function(d, i) {
d["dob"] = new Date(d["dob"]); d["dob"] = new Date(d["dob"]);
d["driverId"] = parseInt(d["driverId"]); d["driverId"] = parseInt(d["driverId"]);
d["number"] = parseInt(d["number"]); d["number"] = parseInt(d["number"]);
// store processed data by its primary key in an object
result[d["driverId"]] = d;
}); });
preprocessor.results.drivers = data; // Store results preprocessor.results.drivers = result; // Store results
loadingDialog.itemFinished(); // Update loading dialog progress bar loadingDialog.itemFinished(); // Update loading dialog progress bar
callback(null); // Tell the queue we're done. callback(null); // Tell the queue we're done.
}); });
@ -106,6 +121,7 @@ var preprocessor = {
// data/driverStandings.csv // data/driverStandings.csv
fetchDriverStandings: function (callback) { fetchDriverStandings: function (callback) {
d3.csv('data/driverStandings.csv', function(data) { d3.csv('data/driverStandings.csv', function(data) {
var result = {};
// preprocess data // preprocess data
data.forEach(function(d, i) { data.forEach(function(d, i) {
d["driverId"] = parseInt(d["driverId"]); d["driverId"] = parseInt(d["driverId"]);
@ -114,8 +130,10 @@ var preprocessor = {
d["position"] = parseInt(d["position"]); d["position"] = parseInt(d["position"]);
d["raceID"] = parseInt(d["raceId"]); d["raceID"] = parseInt(d["raceId"]);
d["wins"] = parseInt(d["wins"]); d["wins"] = parseInt(d["wins"]);
// store processed data by its primary key in an object
result[d["driverStandingsId"]] = d;
}); });
preprocessor.results.driverStandings = data; // Store results preprocessor.results.driverStandings = result; // Store results
loadingDialog.itemFinished(); // Update loading dialog progress bar loadingDialog.itemFinished(); // Update loading dialog progress bar
callback(null); // Tell the queue we're done. callback(null); // Tell the queue we're done.
}); });
@ -159,6 +177,7 @@ var preprocessor = {
// data/qualifying.csv // data/qualifying.csv
fetchQualifying: function (callback) { fetchQualifying: function (callback) {
d3.csv('data/qualifying.csv', function(data) { d3.csv('data/qualifying.csv', function(data) {
var result = {};
// preprocess data // preprocess data
data.forEach(function(d, i) { data.forEach(function(d, i) {
d["constructorId"] = parseInt(d["constructorId"]); d["constructorId"] = parseInt(d["constructorId"]);
@ -170,8 +189,10 @@ var preprocessor = {
d["q3"] = new Date(d["q3"]); d["q3"] = new Date(d["q3"]);
d["qualifyId"] = parseInt(d["qualifyId"]); d["qualifyId"] = parseInt(d["qualifyId"]);
d["raceId"] = parseInt(d["raceId"]); d["raceId"] = parseInt(d["raceId"]);
// store processed data by its primary key in an object
result[d["qualifyId"]] = d;
}); });
preprocessor.results.qualifying = data; // Store results preprocessor.results.qualifying = result; // Store results
loadingDialog.itemFinished(); // Update loading dialog progress bar loadingDialog.itemFinished(); // Update loading dialog progress bar
callback(null); // Tell the queue we're done. callback(null); // Tell the queue we're done.
}); });
@ -180,6 +201,7 @@ var preprocessor = {
// data/races.csv // data/races.csv
fetchRaces: function (callback) { fetchRaces: function (callback) {
d3.csv('data/races.csv', function(data) { d3.csv('data/races.csv', function(data) {
var result = {};
// preprocess data // preprocess data
data.forEach(function(d, i) { data.forEach(function(d, i) {
d["circuitId"] = parseInt(d["circuitId"]); d["circuitId"] = parseInt(d["circuitId"]);
@ -188,8 +210,10 @@ var preprocessor = {
d["round"] = parseInt(d["round"]); d["round"] = parseInt(d["round"]);
d["time"] = new Date(d["time"]); d["time"] = new Date(d["time"]);
d["year"] = parseInt(d["year"]); d["year"] = parseInt(d["year"]);
// store processed data by its primary key in an object
result[d["raceId"]] = d;
}); });
preprocessor.results.races = data; // Store results preprocessor.results.races = result; // Store results
loadingDialog.itemFinished(); // Update loading dialog progress bar loadingDialog.itemFinished(); // Update loading dialog progress bar
callback(null); // Tell the queue we're done. callback(null); // Tell the queue we're done.
}); });
@ -198,6 +222,7 @@ var preprocessor = {
// data/results.csv // data/results.csv
fetchResults: function (callback) { fetchResults: function (callback) {
d3.csv('data/results.csv', function(data) { d3.csv('data/results.csv', function(data) {
var result = {};
// preprocess data // preprocess data
data.forEach(function(d, i) { data.forEach(function(d, i) {
d["constructorId"] = parseInt(d["constructorId"]); d["constructorId"] = parseInt(d["constructorId"]);
@ -217,8 +242,10 @@ var preprocessor = {
d["resultId"] = parseInt(d["resultId"]); d["resultId"] = parseInt(d["resultId"]);
d["statusId"] = parseInt(d["statusId"]); d["statusId"] = parseInt(d["statusId"]);
d["time"] = new Date(d["time"]); d["time"] = new Date(d["time"]);
// store processed data by its primary key in an object
result[d["resultId"]] = d;
}); });
preprocessor.results.results = data; // Store results preprocessor.results.results = result; // Store results
loadingDialog.itemFinished(); // Update loading dialog progress bar loadingDialog.itemFinished(); // Update loading dialog progress bar
callback(null); // Tell the queue we're done. callback(null); // Tell the queue we're done.
}); });
@ -227,11 +254,14 @@ var preprocessor = {
// data/seasons.csv // data/seasons.csv
fetchSeasons: function (callback) { fetchSeasons: function (callback) {
d3.csv('data/seasons.csv', function(data) { d3.csv('data/seasons.csv', function(data) {
var result = {};
// preprocess data // preprocess data
data.forEach(function(d, i) { data.forEach(function(d, i) {
d["year"] = parseInt(d["year"]); d["year"] = parseInt(d["year"]);
// store processed data by its primary key in an object
result[d["year"]] = d;
}); });
preprocessor.results.seasons = data; // Store results preprocessor.results.seasons = result; // Store results
loadingDialog.itemFinished(); // Update loading dialog progress bar loadingDialog.itemFinished(); // Update loading dialog progress bar
callback(null); // Tell the queue we're done. callback(null); // Tell the queue we're done.
}); });
@ -240,11 +270,14 @@ var preprocessor = {
// data/status.csv // data/status.csv
fetchStatus: function (callback) { fetchStatus: function (callback) {
d3.csv('data/status.csv', function(data) { d3.csv('data/status.csv', function(data) {
var result = {};
// preprocess data // preprocess data
data.forEach(function(d, i) { data.forEach(function(d, i) {
d["statusId"] = parseInt(d["statusId"]); d["statusId"] = parseInt(d["statusId"]);
// store processed data by its primary key in an object
result[d["statusId"]] = d;
}); });
preprocessor.results.status = data; // Store results preprocessor.results.status = result; // Store results
loadingDialog.itemFinished(); // Update loading dialog progress bar loadingDialog.itemFinished(); // Update loading dialog progress bar
callback(null); // Tell the queue we're done. callback(null); // Tell the queue we're done.
}); });

View File

@ -14,14 +14,15 @@ var queries = {
// Extract interesting data from raw data. // Extract interesting data from raw data.
var nationalities = {}; var nationalities = {};
rawData.drivers.forEach(function(d, i) { for(var i in rawData.drivers) {
var d = rawData.drivers[i];
var nationality = d["nationality"]; var nationality = d["nationality"];
if(nationalities[nationality] === undefined) { if(nationalities[nationality] === undefined) {
nationalities[nationality] = 1; nationalities[nationality] = 1;
} else { } else {
nationalities[nationality] += 1; nationalities[nationality] += 1;
} }
}); }
// Transform into d3 dataset format // Transform into d3 dataset format
var dataset = []; var dataset = [];