f1vis/js/queries.js

127 lines
3.0 KiB
JavaScript
Raw Normal View History

"use strict";
/*
* This file defines functions that create d3 datasets from the rawData provided by preprocessor.getResults()
*/
var queries = {
/*
* Count drivers belonging to nationalities
*/
getDriversByNationality: function() {
var rawData = preprocessor.getResults();
// Extract interesting data from raw data.
var nationalities = {};
for(var i in rawData.drivers) {
var d = rawData.drivers[i];
var nationality = d["nationality"];
if(nationalities[nationality] === undefined) {
nationalities[nationality] = 1;
} else {
nationalities[nationality] += 1;
}
}
// Transform into d3 dataset format
var dataset = [];
var n;
for(n in nationalities) {
dataset.push({
label: n,
count: nationalities[n]
});
}
// Return d3 dataset
return dataset;
},
2017-12-08 12:44:34 +01:00
getDriverById: function(driverId) {
var rawData = preprocessor.getResults();
return rawData.drivers[driverId];
},
2017-12-08 12:44:34 +01:00
getDriversByRaceId: function(raceId) {
var rawData = preprocessor.getResults();
2017-12-08 12:44:34 +01:00
var result = queries.getResultsByRaceId(raceId).map((cur) => queries.getDriverById(cur.driverId));
2017-12-08 12:44:34 +01:00
return result;
},
getResultsByRaceId: function(raceId) {
2017-12-08 12:44:34 +01:00
var rawData = preprocessor.getResults();
var tempList = [];
for(var key in rawData.results){
if(rawData.results[key].raceId == raceId){
tempList.push(rawData.results[key]);
}
}
2017-12-08 12:44:34 +01:00
tempList.sort((o1,o2) => o1["positionOrder"] - o2["positionOrder"]);
2017-12-08 12:44:34 +01:00
return tempList;
},
2017-12-08 12:44:34 +01:00
getPitStopsByRaceId: function(raceId) {
var rawData = preprocessor.getResults();
var tempList = [];
for(var key in rawData.pitStops){
if(rawData.pitStops[key].raceId == raceId){
tempList.push(rawData.pitStops[key]);
}
}
//var tempList = rawData.pitStops.filter(cur => cur.raceId == raceId);
return tempList;
2017-12-08 12:44:34 +01:00
},
getLapDataByRaceId: function(raceId) {
var rawData = preprocessor.getResults();
var lapTimes = [];
for(var key in rawData.lapTimes){
if(rawData.lapTimes[key].raceId == raceId){
lapTimes.push(rawData.lapTimes[key]);
}
}
2017-12-08 12:44:34 +01:00
var myMap = new Map();
lapTimes.forEach(function(d,i) {
var lapNum = d["lap"];
var driverId = d["driverId"];
2017-12-08 12:44:34 +01:00
if(!myMap.has(lapNum)){
var data = [];
myMap.set(lapNum,data);
}
var dt = myMap.get(lapNum);
dt.push(d);
myMap.set(lapNum,dt);
});
2017-12-08 12:44:34 +01:00
myMap.forEach((d,i) => d.sort((o1,o2) => o1["position"] - o2["position"]));
return myMap;
},
getQualifingDataByRaceId: function(raceId) {
var rawData = preprocessor.getResults();
var qualifingData = [];
for(var key in rawData.qualifying){
if(rawData.qualifying[key].raceId == raceId){
qualifingData.push(rawData.qualifying[key]);
}
}
//var result = rawData.qualifying.filter((cur) => (cur.raceId == raceId));
qualifingData.sort((o1,o2) => o1["position"] - o2["position"]);
return qualifingData;
}
};