Modified EnhancedLapData per Driver, added Dots and simple Hover Logic

This commit is contained in:
sirsandmann 2017-12-16 01:18:24 +01:00
parent 720bdb713d
commit da3c852058
3 changed files with 67 additions and 32 deletions

View File

@ -3,6 +3,8 @@
// https://bl.ocks.org/mbostock/3884955 // https://bl.ocks.org/mbostock/3884955
function createLineGraph(containerId, raceData){ function createLineGraph(containerId, raceData){
console.log(raceData);
var height = 1000; var height = 1000;
var width = 1000; var width = 1000;
@ -15,8 +17,6 @@ function createLineGraph(containerId, raceData){
var x = d3.scaleLinear().range([0, width]); var x = d3.scaleLinear().range([0, width]);
var y = d3.scaleLinear().range([height, 0]); var y = d3.scaleLinear().range([height, 0]);
var allLineData = transformRaceDataToLineData(raceData);
// defines how the passed in Data, at "svg.append" shall be interpreted // defines how the passed in Data, at "svg.append" shall be interpreted
var lineDataDefinition = d3.line() var lineDataDefinition = d3.line()
.x(function(d) { return x(d.lap); }) .x(function(d) { return x(d.lap); })
@ -36,14 +36,29 @@ function createLineGraph(containerId, raceData){
x.domain([0, raceData.lapTimes.size]); x.domain([0, raceData.lapTimes.size]);
y.domain([raceData.drivers.length, 1]); y.domain([raceData.drivers.length, 1]);
var enhancedLapData = processor.getEnhancedLapDataPerDriver(raceData);
console.log(enhancedLapData);
// Adds all lines // Adds all lines
allLineData.forEach((singleLineData, i) => { enhancedLapData.forEach((driverLapData, driverIndex) => {
svg.append("path") svg.append("path")
.data([singleLineData]) .data([driverLapData.laps])
.attr("class", "line") .attr("class", "line")
.attr("stroke", getColorValue(i, allLineData.length) ) .attr("stroke", getColorValue(driverIndex, enhancedLapData.length) )
.attr("d", lineDataDefinition) .attr("d", lineDataDefinition)
;
//Appends a circle for each datapoint
svg.selectAll(".linepoint")
.data(driverLapData.laps)
.enter().append("circle") // Uses the enter().append() method
.attr("class", "dot") // Assign a class for styling
.attr("fill", getColorValue(driverIndex, enhancedLapData.length))
.attr("cx", function(d, i) {return x(d.lap) })
.attr("cy", function(d, i) { return y(d.position) })
.attr("r", 3)
.on("mouseover", handleMouseOverLinePoint)
.on("mouseout", handleMouseOutLinePoint);
}); });
// Add the X Axis // Add the X Axis
@ -58,14 +73,35 @@ function createLineGraph(containerId, raceData){
.call(d3.axisRight(y)) .call(d3.axisRight(y))
.attr("transform", "translate( " + (width) + ", 0 )"); .attr("transform", "translate( " + (width) + ", 0 )");
console.log(transformPitStopDataToPointData(raceData)); function handleMouseOverLinePoint(d, i) {
// 12. Appends a circle for each datapoint console.log(d);
svg.selectAll(".dot") console.log(i);
.data(transformPitStopDataToPointData(raceData)) // Add interactivity
.enter().append("circle") // Uses the enter().append() method // Use D3 to select element, change color and size
.attr("class", "dot") // Assign a class for styling d3.select(this).attr({
.attr("cx", function(d, i) { return x(d.lap) }) fill: "orange",
.attr("cy", function(d) { return y(d.position) }) r: 3 * 2
.attr("r", 5); });
// Specify where to put label of text
svg.append("text")
.attr("id", "t" + d.lap + "-" + d.positon + "-" + i)// Create an id for text so we can select it later for removing on mouseout
.attr("x", function() { return x(d.lap) - 30; })
.attr("y", function() { return y(d.position) - 15; })
.text(function() {
return [d.lap, d.position]; // Value of the text
});
}
function handleMouseOutLinePoint(d, i) {
// Use D3 to select element, change color back to normal
d3.select(this).attr({
fill: "black",
r: 3
});
// Select text by id and then remove
d3.select("#t" + d.lap + "-" + d.positon + "-" + i).remove(); // Remove text location
}
} }

View File

@ -10,7 +10,7 @@ var processor = {
qualifying: null, qualifying: null,
results: null, results: null,
raceInfo: null, raceInfo: null,
}; };
race.drivers = queries.getDriversByRaceId(raceId); race.drivers = queries.getDriversByRaceId(raceId);
@ -31,22 +31,21 @@ getEnhancedLapDataPerDriver: function(raceData) {
laps: [], laps: [],
}; };
lapData.driver = driver; lapData.driver = driver;
//Attach Qualifying Data
lapData.qualifying = processor.getQualifyingForDriver(raceData, driver);
//add Qualifying Data to the Laps
lapData.laps.push({'driverId': driver.driverId, 'lap': 0, 'position': lapData.qualifying.position})
raceData.lapTimes.forEach(lap => { raceData.lapTimes.forEach(lap => {
lap.forEach(curLap => { lap.forEach(curLap => {
if( curLap.driverId == driver.driverId ){ if( curLap.driverId == driver.driverId ){
var pitstop = raceData.pitStops.filter(pitstop => pitstop.driverId == driver.driverId && pitstop.lap == curLap.lap); var pitstop = raceData.pitStops.filter(pitstop => pitstop.driverId == driver.driverId && pitstop.lap == curLap.lap);
if(pitstop.length > 0){ if(pitstop.length > 0){
curLap.pitStop = pitstop[0]; curLap.pitStop = pitstop[0];
} }
lapData.laps.push(curLap); lapData.laps.push(curLap);
} }
}); });
}); });
result.push(lapData); result.push(lapData);
}); });
return result; return result;
@ -58,7 +57,7 @@ getEnhancedLapDataPerDriver: function(raceData) {
}, },
//Gets the position of Driver with driverid in specific lap //Gets the position of Driver with driverid in specific lap
// lapData: an array of the lap data for one lap // lapData: an array of the lap data for one lap
getPositionOfDriver: function(driver, lapData, defaultReturn){ getPositionOfDriver: function(driver, lapData, defaultReturn){
var lapEntryWithDrivId = lapData.filter( drivLap => drivLap.driverId == driver.driverId ); var lapEntryWithDrivId = lapData.filter( drivLap => drivLap.driverId == driver.driverId );
if(lapEntryWithDrivId.length > 0){ if(lapEntryWithDrivId.length > 0){
@ -68,4 +67,9 @@ getEnhancedLapDataPerDriver: function(raceData) {
} }
}, },
getQualifyingForDriver: function(raceData, driver){
var qualData = raceData.qualifying.filter( qualData => qualData.driverId == driver.driverId);
return qualData[0];
}
}; };

View File

@ -66,7 +66,7 @@ function transformPitStopDataToPointData(raceData){
for(var i = 0; i< randomData.length;i++){ for(var i = 0; i< randomData.length;i++){
if(randomData[i].driverId == pitStop.driverId){ if(randomData[i].driverId == pitStop.driverId){
position = randomData[i].position; position = randomData[i].position;
break; break;
} }
} }
//console.log(position); //console.log(position);
@ -75,11 +75,6 @@ for(var i = 0; i< randomData.length;i++){
return pointData; return pointData;
} }
function getPositionOfQualifying(raceData, driver){
var qualData = raceData.qualifying.filter( qualData => qualData.driverId == driver.driverId);
return qualData[0].position;
}
// eigentlich war für TickData gedacht // eigentlich war für TickData gedacht
function getDriverCodeAndPositionArray(raceData, lapNumber){ function getDriverCodeAndPositionArray(raceData, lapNumber){
var posDriverCode = []; var posDriverCode = [];