2017-11-16 23:57:21 +01:00
|
|
|
"use strict";
|
|
|
|
|
2017-12-11 01:26:27 +01:00
|
|
|
// https://bl.ocks.org/mbostock/3884955
|
2017-12-31 01:38:18 +01:00
|
|
|
// https://bl.ocks.org/mbostock/34f08d5e11952a80609169b7917d4172
|
2017-12-11 01:26:27 +01:00
|
|
|
function createLineGraph(containerId, raceData){
|
2017-12-16 01:18:24 +01:00
|
|
|
|
2017-12-22 12:53:46 +01:00
|
|
|
// Rough input validation
|
2017-12-29 17:11:02 +01:00
|
|
|
if(raceData === undefined || raceData.raceInfo === undefined) {
|
2017-12-22 12:53:46 +01:00
|
|
|
console.error(["Sorry, that raceData is empty. :-(", raceData]);
|
|
|
|
return; // early return to avoid errors
|
|
|
|
} else {
|
2017-12-29 17:13:17 +01:00
|
|
|
console.log(["raceData", raceData]);
|
2017-12-22 12:53:46 +01:00
|
|
|
}
|
2017-12-16 01:18:24 +01:00
|
|
|
|
2017-12-22 12:53:46 +01:00
|
|
|
var enhancedLapData = processor.getEnhancedLapDataPerDriver(raceData);
|
2017-12-22 13:17:10 +01:00
|
|
|
|
2017-12-30 01:18:26 +01:00
|
|
|
attachRaceStatistics(enhancedLapData, raceData);
|
|
|
|
|
2017-12-22 12:53:46 +01:00
|
|
|
// Configuration
|
2017-12-30 20:49:44 +01:00
|
|
|
var graphHeight = 720;
|
|
|
|
var smallGraphHeight = 200;
|
2017-12-30 19:12:05 +01:00
|
|
|
var svgWidth = 1300;
|
2017-12-17 23:00:33 +01:00
|
|
|
var linePointSize = 5;
|
2017-12-30 13:09:45 +01:00
|
|
|
var rectSize = 16;
|
2017-12-17 23:00:33 +01:00
|
|
|
var amountClickedLines = 0;
|
2017-11-16 23:57:21 +01:00
|
|
|
|
2017-12-29 16:42:41 +01:00
|
|
|
//ElemTypes of this graph
|
|
|
|
var elemTypes = {
|
|
|
|
line: "line",
|
|
|
|
linepoint: "linepoint",
|
|
|
|
pitstoppoint: "pitstoppoint",
|
|
|
|
endpoint: "endpoint"
|
|
|
|
};
|
|
|
|
|
2017-12-30 18:48:54 +01:00
|
|
|
// -----------------------------------------------------------------------
|
2017-12-11 01:26:27 +01:00
|
|
|
// set the dimensions and margins of the graph
|
2017-12-30 23:22:43 +01:00
|
|
|
var marginGraph = {top: 30.0, right: 50, bottom: 10, left: 50.0},
|
2017-12-30 20:49:44 +01:00
|
|
|
marginSmallGraph = {top: 10, right: marginGraph.right, bottom: 50, left: marginGraph.left},
|
|
|
|
graphPosWidth = {posX: marginGraph.left, posY: marginGraph.top, width: svgWidth - (marginGraph.left + marginGraph.right), height : graphHeight, totalHeight: marginGraph.top + graphHeight + marginGraph.bottom},
|
|
|
|
smallGraphPosWidth = {posX: marginSmallGraph.left, posY: graphPosWidth.totalHeight + graphPosWidth.posY + marginSmallGraph.top, width: svgWidth - (marginSmallGraph.left + marginSmallGraph.right), height : smallGraphHeight , totalHeight: marginSmallGraph.top + smallGraphHeight + marginSmallGraph.bottom},
|
|
|
|
svgHeight = smallGraphPosWidth.totalHeight + graphPosWidth.totalHeight;
|
2017-11-16 23:57:21 +01:00
|
|
|
|
2017-12-11 01:26:27 +01:00
|
|
|
// set the ranges
|
2017-12-30 20:49:44 +01:00
|
|
|
var x = d3.scaleLinear().range([0, graphPosWidth.width]),
|
|
|
|
x2 = d3.scaleLinear().range([0, smallGraphPosWidth.width]),
|
|
|
|
y = d3.scaleLinear().range([graphPosWidth.height, 0]),
|
|
|
|
y2 = d3.scaleLinear().range([smallGraphPosWidth.height, 0]);
|
2017-12-30 18:48:54 +01:00
|
|
|
|
2017-12-31 01:03:16 +01:00
|
|
|
// Gather lapNumbers for xAxis tickvalues
|
|
|
|
var lapNumbers = [];
|
|
|
|
for(var i=0; i<raceData.lapTimes.size; i++) lapNumbers.push(i);
|
|
|
|
|
|
|
|
var xAxis = d3.axisBottom(x).tickValues(lapNumbers),
|
2017-12-30 18:48:54 +01:00
|
|
|
yAxis = d3.axisLeft(y);
|
|
|
|
|
2017-12-30 23:57:15 +01:00
|
|
|
var xAxisGridlines = d3.axisBottom(x)
|
2017-12-31 01:03:16 +01:00
|
|
|
.tickValues(lapNumbers) // One gridline for each lap
|
2017-12-30 23:57:15 +01:00
|
|
|
.tickSize(-graphPosWidth.height)
|
|
|
|
.tickFormat("");
|
|
|
|
|
2017-12-30 18:48:54 +01:00
|
|
|
var brush = d3.brushX()
|
2017-12-30 20:49:44 +01:00
|
|
|
.extent([[0, 0], [smallGraphPosWidth.width, smallGraphPosWidth.height]])
|
2017-12-30 18:48:54 +01:00
|
|
|
.on("brush end", brushed);
|
|
|
|
|
|
|
|
var zoom = d3.zoom()
|
2017-12-30 23:22:43 +01:00
|
|
|
.scaleExtent([1, 5])
|
2017-12-30 20:49:44 +01:00
|
|
|
.translateExtent([[0, 0], [graphPosWidth.width, graphPosWidth.height]])
|
|
|
|
.extent([[0, 0], [graphPosWidth.width, graphPosWidth.height]])
|
2017-12-30 18:48:54 +01:00
|
|
|
.on("zoom", zoomed);
|
|
|
|
|
2017-12-31 00:19:51 +01:00
|
|
|
// -----------------------------------------------------------------------
|
2017-11-19 14:57:53 +01:00
|
|
|
|
2017-12-11 01:26:27 +01:00
|
|
|
// defines how the passed in Data, at "svg.append" shall be interpreted
|
|
|
|
var lineDataDefinition = d3.line()
|
|
|
|
.x(function(d) { return x(d.lap); })
|
|
|
|
.y(function(d) { return y(d.position); });
|
2017-12-30 23:22:43 +01:00
|
|
|
|
|
|
|
var lineDataDefinitionSmall = d3.line()
|
|
|
|
.x(function(d) { return x2(d.lap); })
|
|
|
|
.y(function(d) { return y2(d.position); });
|
2017-11-19 14:57:53 +01:00
|
|
|
|
2017-12-11 01:26:27 +01:00
|
|
|
// append the svg obgect to the body of the page
|
|
|
|
// appends a 'group' element to 'svg'
|
|
|
|
// moves the 'group' element to the top left margin
|
|
|
|
var svg = d3.select(containerId).append("svg")
|
2017-12-30 20:49:44 +01:00
|
|
|
.attr("width", svgWidth)
|
|
|
|
.attr("height", svgHeight);
|
2017-12-31 00:19:51 +01:00
|
|
|
|
2017-12-30 18:48:54 +01:00
|
|
|
//----------------------------------------------------------------------
|
2017-12-31 01:36:02 +01:00
|
|
|
// make sure we have no overlapping
|
2017-12-30 18:48:54 +01:00
|
|
|
svg.append("defs").append("clipPath")
|
|
|
|
.attr("id", "clip")
|
|
|
|
.append("rect")
|
2017-12-30 20:49:44 +01:00
|
|
|
.attr("width", graphPosWidth.width)
|
2017-12-31 14:35:19 +01:00
|
|
|
.attr("height", graphPosWidth.height + 16) // Add 16 pixels of height ...
|
|
|
|
.attr("transform", "translate(0,-8)"); // ... so we can have 8 pixels above and below the graph
|
|
|
|
// This way, the lines and dots are not cut off
|
2017-12-31 01:34:51 +01:00
|
|
|
|
2017-12-30 18:48:54 +01:00
|
|
|
var focus = svg.append("g")
|
|
|
|
.attr("class", "focus")
|
2017-12-30 20:49:44 +01:00
|
|
|
.attr("transform", "translate(" + graphPosWidth.posX + "," + graphPosWidth.posY + ")");
|
2017-12-31 01:34:51 +01:00
|
|
|
|
2017-12-31 15:25:42 +01:00
|
|
|
focus.append("rect")
|
|
|
|
.attr("class", "zoom")
|
|
|
|
.attr("width", svgWidth)
|
|
|
|
.attr("height", graphHeight)
|
|
|
|
.attr("transform", "translate(" + graphPosWidth.posX + "," + graphPosWidth.posY + ")")
|
|
|
|
.call(zoom);
|
|
|
|
|
2017-12-30 18:48:54 +01:00
|
|
|
var context = svg.append("g")
|
|
|
|
.attr("class", "context")
|
2017-12-30 20:49:44 +01:00
|
|
|
.attr("height", smallGraphPosWidth.height)
|
|
|
|
.attr("transform", "translate(" + smallGraphPosWidth.posX + "," + smallGraphPosWidth.posY + ")");
|
2017-12-30 18:48:54 +01:00
|
|
|
|
|
|
|
// -----------------------------------------------------------------------
|
2017-12-11 01:26:27 +01:00
|
|
|
|
2017-12-31 01:48:35 +01:00
|
|
|
var contextXAxis = d3.axisBottom(x2).tickValues(0);
|
2017-12-31 00:19:51 +01:00
|
|
|
context.append("g")
|
|
|
|
.attr("class", "axis axis--x")
|
|
|
|
.attr("transform", "translate(0," + smallGraphPosWidth.height + ")")
|
2017-12-31 01:48:35 +01:00
|
|
|
.call(contextXAxis);
|
2017-12-31 00:19:51 +01:00
|
|
|
|
|
|
|
context.append("g")
|
|
|
|
.attr("class", "brush")
|
|
|
|
.call(brush)
|
|
|
|
.call(brush.move, x.range());
|
|
|
|
|
|
|
|
// -----------------------------------------------------------------------
|
|
|
|
|
2017-12-11 01:26:27 +01:00
|
|
|
// Scale the range of the data
|
|
|
|
x.domain([0, raceData.lapTimes.size]);
|
|
|
|
y.domain([raceData.drivers.length, 1]);
|
2017-12-30 18:48:54 +01:00
|
|
|
x2.domain(x.domain());
|
|
|
|
y2.domain(y.domain());
|
2017-12-11 01:26:27 +01:00
|
|
|
|
2017-12-28 14:59:07 +01:00
|
|
|
var enhancedLapData = processor.getEnhancedLapDataPerDriver(raceData);
|
2017-12-29 20:46:13 +01:00
|
|
|
//console.log(["enhancedLapData", enhancedLapData]);
|
2017-12-28 14:59:07 +01:00
|
|
|
|
2017-12-30 23:22:43 +01:00
|
|
|
|
2017-12-11 01:26:27 +01:00
|
|
|
// Adds all lines
|
2017-12-16 01:18:24 +01:00
|
|
|
enhancedLapData.forEach((driverLapData, driverIndex) => {
|
2017-12-17 23:19:00 +01:00
|
|
|
//console.log(driverLapData);
|
2017-12-30 18:48:54 +01:00
|
|
|
focus.append("path")
|
2017-12-16 01:18:24 +01:00
|
|
|
.data([driverLapData.laps])
|
2017-12-30 23:22:43 +01:00
|
|
|
.attr("class", "line pathLines")
|
2017-12-31 01:55:20 +01:00
|
|
|
.attr("clip-path","url(#clip)")
|
2017-12-31 01:34:51 +01:00
|
|
|
.attr("data-line", driverLapData.driver.driverId) // custom data to specify the line
|
2017-12-17 23:45:51 +01:00
|
|
|
.attr("data-opacitychange", 1)
|
2017-12-17 23:19:00 +01:00
|
|
|
.attr("data-highlighted", 0)
|
2017-12-29 16:42:41 +01:00
|
|
|
.attr("data-elemtype", elemTypes.line)
|
2017-12-16 01:18:24 +01:00
|
|
|
.attr("stroke", getColorValue(driverIndex, enhancedLapData.length) )
|
2017-12-31 15:28:40 +01:00
|
|
|
.attr("d", lineDataDefinition)
|
|
|
|
.call(zoom);
|
2017-12-16 01:18:24 +01:00
|
|
|
|
2017-12-30 20:13:36 +01:00
|
|
|
context.append("path")
|
|
|
|
.data([driverLapData.laps])
|
2017-12-30 23:22:43 +01:00
|
|
|
.attr("class", "line-context")
|
2017-12-30 20:13:36 +01:00
|
|
|
.attr("data-line", driverLapData.driver.driverId) // custom data to specify the line
|
|
|
|
.attr("data-opacitychange", 1)
|
|
|
|
.attr("data-highlighted", 0)
|
|
|
|
.attr("data-elemtype", elemTypes.line)
|
|
|
|
.attr("stroke", getColorValue(driverIndex, enhancedLapData.length) )
|
2017-12-30 23:22:43 +01:00
|
|
|
.attr("d", lineDataDefinitionSmall);
|
2017-12-30 20:13:36 +01:00
|
|
|
|
2017-12-31 14:46:34 +01:00
|
|
|
//Appends a circle for each datapoint
|
|
|
|
focus.selectAll(".linepoint")
|
|
|
|
.data(driverLapData.laps)
|
|
|
|
.enter().append("circle") // Uses the enter().append() method
|
|
|
|
.attr("class", "linedot") // Assign a class for styling
|
|
|
|
.attr("clip-path","url(#clip)")
|
|
|
|
.attr("id", function(d, i) { return "circle-linepoint-" + d.lap + "-" + d.driverId; })
|
|
|
|
.attr("data-line", driverLapData.driver.driverId)
|
|
|
|
.attr("data-opacitychange", 0)
|
|
|
|
.attr("data-highlighted", 0)
|
|
|
|
.attr("data-elemtype", elemTypes.linepoint)
|
|
|
|
.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", linePointSize)
|
2017-12-31 15:28:40 +01:00
|
|
|
.style("opacity", 0)
|
|
|
|
.call(zoom);
|
2017-12-31 14:46:34 +01:00
|
|
|
|
|
|
|
//Appends a circle for each datapoint
|
|
|
|
focus.selectAll(".invisiblelinepoint")
|
|
|
|
.data(driverLapData.laps)
|
|
|
|
.enter().append("circle") // Uses the enter().append() method
|
|
|
|
.attr("class", "linedot") // Assign a class for styling
|
|
|
|
.attr("clip-path","url(#clip)")
|
|
|
|
.attr("circle-id", function(d, i) { return "circle-linepoint-" + d.lap + "-" + d.driverId; })
|
|
|
|
.attr("data-line", driverLapData.driver.driverId)
|
|
|
|
.attr("data-opacitychange", 0)
|
|
|
|
.attr("data-highlighted", 0)
|
|
|
|
.attr("data-elemtype", elemTypes.linepoint)
|
|
|
|
.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", linePointSize*2.4)
|
|
|
|
.on("click", handleClickOnPoint)
|
|
|
|
.on("mouseover", handleMouseOverLinePoint)
|
|
|
|
.on("mouseout", handleMouseOutLinePoint)
|
2017-12-31 15:28:40 +01:00
|
|
|
.style("opacity", 0)
|
|
|
|
.call(zoom);
|
2017-12-31 14:46:34 +01:00
|
|
|
|
2017-12-29 20:46:13 +01:00
|
|
|
driverLapData.laps.forEach((singleLap, singleLapIndex)=> {
|
|
|
|
//console.log(["driverLaps.forEach", singleLap, singleLapIndex]);
|
|
|
|
if(singleLap.pitStop){
|
|
|
|
//Appends a circle for each datapoint
|
2017-12-30 18:48:54 +01:00
|
|
|
focus.selectAll(".pitstoppoint")
|
2017-12-29 20:46:13 +01:00
|
|
|
.data([singleLap])
|
|
|
|
.enter().append("circle") // Uses the enter().append() method
|
2017-12-30 23:22:43 +01:00
|
|
|
.attr("class", "pitstopdot") // Assign a class for styling
|
2017-12-31 01:34:51 +01:00
|
|
|
.attr("clip-path","url(#clip)")
|
2017-12-29 20:46:13 +01:00
|
|
|
.attr("data-line", driverLapData.driver.driverId)
|
|
|
|
.attr("data-opacitychange", 1)
|
|
|
|
.attr("data-highlighted", 0)
|
|
|
|
.attr("data-elemtype", elemTypes.pitstoppoint)
|
|
|
|
.attr("fill", getColorValue(driverIndex, enhancedLapData.length))
|
|
|
|
.attr("cx", function(d, i) {return x(d.lap) })
|
|
|
|
.attr("cy", function(d, i) { return y(d.position) })
|
2017-12-31 14:44:35 +01:00
|
|
|
.attr("r", linePointSize * 1.4)
|
2017-12-29 20:46:13 +01:00
|
|
|
.on("click", handleClickOnPoint)
|
|
|
|
.on("mouseover", handleMouseOverLinePoint)
|
2017-12-31 15:28:40 +01:00
|
|
|
.on("mouseout", handleMouseOutLinePoint)
|
|
|
|
.call(zoom);
|
2017-12-30 20:13:36 +01:00
|
|
|
|
|
|
|
context.selectAll(".pitstoppoint-context")
|
|
|
|
.data([singleLap])
|
|
|
|
.enter().append("circle") // Uses the enter().append() method
|
2017-12-30 23:22:43 +01:00
|
|
|
.attr("class", "pitstopdot") // Assign a class for styling
|
2017-12-30 20:13:36 +01:00
|
|
|
.attr("data-line", driverLapData.driver.driverId)
|
|
|
|
.attr("data-opacitychange", 1)
|
|
|
|
.attr("data-highlighted", 0)
|
|
|
|
.attr("data-elemtype", elemTypes.pitstoppoint)
|
|
|
|
.attr("fill", getColorValue(driverIndex, enhancedLapData.length))
|
|
|
|
.attr("cx", function(d, i) {return x2(d.lap) })
|
|
|
|
.attr("cy", function(d, i) { return y2(d.position) })
|
|
|
|
.attr("r", linePointSize * 0.5)
|
2017-12-30 23:22:43 +01:00
|
|
|
.attr("d", lineDataDefinitionSmall);
|
2017-12-29 23:48:22 +01:00
|
|
|
// Remove data from driverLapData, since we don't need a generic datapoint for this
|
2017-12-30 23:37:29 +01:00
|
|
|
//driverLapData.laps[singleLapIndex] = {};
|
2017-12-29 20:46:13 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2017-12-28 14:59:07 +01:00
|
|
|
// in case the driver ended the race too early, get the status why he quit
|
|
|
|
/*TODO: Mouseover for Rectangle*/
|
|
|
|
var resultOfDriver = raceData.results.filter((result) => { return result.driverId == driverLapData.driver.driverId; });
|
|
|
|
if(resultOfDriver.length > 0 && getValidEndingStatusIds().indexOf(resultOfDriver[0].statusId) < 0){
|
|
|
|
var triangle = d3.symbol()
|
|
|
|
.type(d3.symbolTriangle)
|
|
|
|
.size(25);
|
|
|
|
//get Data for last round
|
2017-12-30 18:48:54 +01:00
|
|
|
focus.selectAll(".endpoint")
|
2017-12-28 14:59:07 +01:00
|
|
|
.data([driverLapData.laps[driverLapData.laps.length - 1]])
|
|
|
|
.enter().append("rect") // Uses the enter().append() method
|
2017-12-30 23:22:43 +01:00
|
|
|
.attr("class", "endpointdot") // Assign a class for styling
|
2017-12-31 01:34:51 +01:00
|
|
|
.attr("clip-path","url(#clip)")
|
2017-12-28 14:59:07 +01:00
|
|
|
.attr("data-line", driverLapData.driver.driverId)
|
|
|
|
.attr("data-opacitychange", 1)
|
|
|
|
.attr("data-highlighted", 0)
|
2017-12-29 16:42:41 +01:00
|
|
|
.attr("data-elemtype", elemTypes.endpoint)
|
2017-12-28 14:59:07 +01:00
|
|
|
.attr("fill", getColorValue(driverIndex, enhancedLapData.length))
|
2017-12-30 13:14:40 +01:00
|
|
|
.attr("x", function(d, i) { return x(d.lap) - rectSize * 1/2; })
|
|
|
|
.attr("y", function(d, i) { return y(d.position) - rectSize * 1/2; })
|
2017-12-28 14:59:07 +01:00
|
|
|
.attr("height", rectSize)
|
2017-12-29 16:42:41 +01:00
|
|
|
.attr("width", rectSize)
|
|
|
|
.on("click", handleClickOnPoint)
|
|
|
|
.on("mouseover", handleMouseOverLinePoint)
|
2017-12-31 15:28:40 +01:00
|
|
|
.on("mouseout", handleMouseOutLinePoint)
|
|
|
|
.call(zoom);
|
2017-12-28 14:59:07 +01:00
|
|
|
|
2017-12-30 20:13:36 +01:00
|
|
|
context.selectAll(".endpoint")
|
|
|
|
.data([driverLapData.laps[driverLapData.laps.length - 1]])
|
|
|
|
.enter().append("rect") // Uses the enter().append() method
|
|
|
|
//.attr("class", "endpoint") // Assign a class for styling
|
2017-12-31 01:34:51 +01:00
|
|
|
.attr("class", "zoomable")
|
2017-12-30 20:13:36 +01:00
|
|
|
.attr("data-line", driverLapData.driver.driverId)
|
|
|
|
.attr("data-opacitychange", 1)
|
|
|
|
.attr("data-highlighted", 0)
|
|
|
|
.attr("data-elemtype", elemTypes.endpoint)
|
|
|
|
.attr("fill", getColorValue(driverIndex, enhancedLapData.length))
|
2017-12-31 14:42:39 +01:00
|
|
|
.attr("x", function(d, i) { return x2(d.lap) - rectSize * 1/4; })
|
|
|
|
.attr("y", function(d, i) { return y2(d.position) - rectSize * 1/4; })
|
2017-12-30 20:13:36 +01:00
|
|
|
.attr("height", rectSize * 0.4)
|
|
|
|
.attr("width", rectSize * 0.4)
|
2017-12-30 23:22:43 +01:00
|
|
|
.attr("d", lineDataDefinition);
|
2017-12-30 20:13:36 +01:00
|
|
|
|
2017-12-28 14:59:07 +01:00
|
|
|
/* tried with Cross, didn't work, don't know why
|
|
|
|
svg.selectAll(".endpoint")
|
|
|
|
.data([driverLapData.laps[driverLapData.laps.length - 1]])
|
|
|
|
.enter().append("symbolCircle") // Uses the enter().append() method
|
|
|
|
.attr("size", 300)
|
|
|
|
.attr("class", "endpoint") // Assign a class for styling
|
|
|
|
.attr("fill", getColorValue(driverIndex, enhancedLapData.length))
|
|
|
|
.attr("transform", function(d) { return "translate(" + x(d.lap) + "," + y(d.position) + ")"; });
|
|
|
|
*/
|
|
|
|
}
|
|
|
|
|
2017-12-11 01:26:27 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
// Add the X Axis
|
2017-12-31 13:07:41 +01:00
|
|
|
var testXAxis = focus.append("g")
|
2017-12-30 23:57:15 +01:00
|
|
|
.attr("class", "axis axis--x")
|
|
|
|
.attr("transform", "translate(0," + graphPosWidth.height + ")")
|
2017-12-31 01:55:20 +01:00
|
|
|
.attr("clip-path","url(#clip)")
|
2017-12-30 23:57:15 +01:00
|
|
|
.call(xAxis);
|
2017-12-11 01:26:27 +01:00
|
|
|
|
2017-12-31 13:07:41 +01:00
|
|
|
// Add the Y Axis on left side
|
2017-12-30 18:48:54 +01:00
|
|
|
focus.append("g")
|
2017-12-31 13:07:41 +01:00
|
|
|
.attr("class","axis--y-left")
|
2017-12-19 21:35:47 +01:00
|
|
|
.call(
|
|
|
|
d3.axisLeft(y)
|
|
|
|
.ticks(raceData.drivers.length)
|
|
|
|
.tickFormat(function(d) {
|
|
|
|
return getDriverCodeFromPosAndLap(raceData, 0, d) + " " + d;
|
|
|
|
})
|
|
|
|
);
|
|
|
|
|
2017-12-30 23:57:15 +01:00
|
|
|
//Add driver information on the right side of the graph.
|
2017-12-30 18:48:54 +01:00
|
|
|
focus.append("g")
|
2017-12-31 13:07:41 +01:00
|
|
|
.attr("class","axis--y-right")
|
2017-12-19 21:35:47 +01:00
|
|
|
.call(
|
|
|
|
d3.axisRight(y)
|
|
|
|
.ticks(raceData.drivers.length)
|
|
|
|
.tickFormat(function(d) {
|
2017-12-31 16:42:49 +01:00
|
|
|
var driverCode = getDriverCodeFromPosAndLap(raceData, raceData.lapTimes.size, d);
|
2017-12-29 14:20:58 +01:00
|
|
|
return d + " " + driverCode ;
|
2017-12-19 21:35:47 +01:00
|
|
|
})
|
|
|
|
)
|
2017-12-30 20:49:44 +01:00
|
|
|
.attr("transform", "translate( " + (graphPosWidth.width) + ", 0 )");
|
2017-12-31 13:07:41 +01:00
|
|
|
|
|
|
|
// Add gridlines on x axis to better figure out laps
|
|
|
|
focus.append("g")
|
|
|
|
.attr("class", "xAxisGridlines")
|
|
|
|
.attr("transform", "translate(0," + graphPosWidth.height + ")")
|
|
|
|
.style("opacity", 0.06)
|
|
|
|
.call(xAxisGridlines);
|
2017-12-13 00:12:11 +01:00
|
|
|
|
2017-12-30 18:48:54 +01:00
|
|
|
|
2017-12-17 23:19:00 +01:00
|
|
|
function handleClickOnPoint(d,i){
|
|
|
|
//select elements that are highlightable but are not highlighted
|
2017-12-17 23:45:51 +01:00
|
|
|
d3.selectAll("[data-opacitychange='" + 1 +"'][data-highlighted='" + 0 +"']")
|
2017-12-29 20:51:06 +01:00
|
|
|
.style("opacity", 0.10);
|
2017-12-17 23:19:00 +01:00
|
|
|
|
|
|
|
// if clicked on already highlighted line, remove highlight
|
|
|
|
if(this.getAttribute("data-highlighted") == 1){
|
2017-12-17 23:45:51 +01:00
|
|
|
d3.selectAll("[data-line='" + d.driverId +"'][data-opacitychange='" + 1 +"']")
|
2017-12-29 20:51:06 +01:00
|
|
|
.style("opacity", 0.10);
|
2017-12-17 23:45:51 +01:00
|
|
|
|
|
|
|
d3.selectAll("[data-line='" + d.driverId +"']")
|
|
|
|
.attr("data-highlighted", 0);
|
2017-12-17 23:19:00 +01:00
|
|
|
}else{
|
|
|
|
//select elements that belong to line and are highlightable
|
2017-12-17 23:45:51 +01:00
|
|
|
d3.selectAll("[data-line='" + d.driverId +"'][data-opacitychange='" + 1 +"']")
|
2017-12-17 23:19:00 +01:00
|
|
|
.style("opacity", 1);
|
2017-12-17 23:45:51 +01:00
|
|
|
|
|
|
|
d3.selectAll("[data-line='" + d.driverId +"']")
|
|
|
|
.attr("data-highlighted", 1);
|
2017-12-17 23:19:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// if no element highlighted, then everything normal again
|
2017-12-17 23:45:51 +01:00
|
|
|
var highlightedElements = d3.selectAll("[data-opacitychange='" + 1 +"'][data-highlighted='" + 1 +"']");
|
2017-12-17 23:19:00 +01:00
|
|
|
if(highlightedElements.size() == 0){
|
|
|
|
//select elements that are highlightable but are not highlighted
|
2017-12-17 23:45:51 +01:00
|
|
|
d3.selectAll("[data-opacitychange='" + 1 +"'][data-highlighted='" + 0 +"']")
|
2017-12-17 23:19:00 +01:00
|
|
|
.style("opacity", 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-31 15:12:02 +01:00
|
|
|
function handleMouseOverLinePoint(d, i, e) {
|
2017-12-29 17:11:02 +01:00
|
|
|
var elemType = d3.select(this).attr("data-elemtype");
|
2017-12-29 16:42:41 +01:00
|
|
|
//depending on Pitstop and lap different Texts
|
|
|
|
var textArr = [];
|
2017-12-31 14:54:52 +01:00
|
|
|
var textMaxLength = 1;
|
2017-12-16 01:18:24 +01:00
|
|
|
// Add interactivity
|
|
|
|
// Use D3 to select element, change color and size
|
2017-12-29 17:11:02 +01:00
|
|
|
if(elemType === elemTypes.linepoint){
|
2017-12-30 12:33:11 +01:00
|
|
|
var circleId = "circle-linepoint-" + d.lap + "-" + d.driverId;
|
|
|
|
var circle = d3.select("#" + circleId);
|
2017-12-29 16:28:34 +01:00
|
|
|
circle
|
2017-12-16 21:54:07 +01:00
|
|
|
.style("opacity", 1);
|
2017-12-29 16:42:41 +01:00
|
|
|
textArr = getLapTextArray(raceData,d);
|
2017-12-29 17:11:02 +01:00
|
|
|
}else if(elemType === elemTypes.pitstoppoint){
|
2017-12-30 12:33:11 +01:00
|
|
|
d3.select(this)
|
2017-12-16 21:54:07 +01:00
|
|
|
.attr("r", linePointSize * 2);
|
|
|
|
textArr = getPitStopTextArray(raceData,d);
|
2017-12-29 17:11:02 +01:00
|
|
|
}else if(elemType === elemTypes.endpoint){
|
2017-12-30 13:14:40 +01:00
|
|
|
var newRectSize = rectSize * 1.5;
|
2017-12-29 16:42:41 +01:00
|
|
|
d3.select(this)
|
2017-12-30 13:14:40 +01:00
|
|
|
.attr("height", newRectSize)
|
|
|
|
.attr("width", newRectSize)
|
|
|
|
.attr("x", function(d, i) { return x(d.lap) - newRectSize * 1/2; })
|
|
|
|
.attr("y", function(d, i) { return y(d.position) - newRectSize * 1/2; });
|
2017-12-29 16:42:41 +01:00
|
|
|
textArr = getEndPointTextArray(raceData,d);
|
2017-12-16 21:54:07 +01:00
|
|
|
}
|
|
|
|
|
2017-12-31 14:54:52 +01:00
|
|
|
// Calculate tooltip width based on text
|
|
|
|
for(var lineI in textArr) {
|
|
|
|
var line = textArr[lineI];
|
|
|
|
if(line.length > textMaxLength) textMaxLength = line.length;
|
|
|
|
}
|
|
|
|
textMaxLength = (2/3) * textMaxLength;
|
|
|
|
|
2017-12-31 15:12:02 +01:00
|
|
|
// Figure out relative tooltip position on x axis
|
|
|
|
var cxValue = d3.select(e[i]).attr("cx");
|
|
|
|
var xValue = (cxValue === null) ? d3.select(e[i]).attr("x") : cxValue;
|
|
|
|
var xRatio = xValue / svgWidth;
|
|
|
|
|
2017-12-29 23:23:37 +01:00
|
|
|
var tooltipGroup = svg.append("g")
|
|
|
|
.attr("id", "t" + d.lap + "-" + d.position + "-" + i); // Unique id so it can be removed later
|
|
|
|
|
|
|
|
tooltipGroup.append("rect")
|
|
|
|
.attr("style", "fill:rgb(225,225,225);stroke:black;stroke-width:2;")
|
2017-12-31 14:54:52 +01:00
|
|
|
.attr("width", textMaxLength + "em")
|
2017-12-29 23:52:12 +01:00
|
|
|
.attr("height", (textArr.length + 1) + "em")
|
2017-12-31 15:12:02 +01:00
|
|
|
.attr("x", function() { if(xRatio < 0.70) return d3.mouse(this)[0] + 10; else return d3.mouse(this)[0] - 130; })
|
2017-12-31 16:42:30 +01:00
|
|
|
.attr("y", function() { return d3.mouse(this)[1] + 25; });
|
2017-12-29 23:23:37 +01:00
|
|
|
|
2017-12-16 21:54:07 +01:00
|
|
|
//Necessary to add Text for each Line
|
|
|
|
textArr.forEach((text, textIndex) =>{
|
|
|
|
// Specify where to put label of text
|
2017-12-29 23:23:37 +01:00
|
|
|
tooltipGroup.append("text")
|
|
|
|
.attr("dy", (textIndex + 1) + "em")
|
2017-12-31 15:12:02 +01:00
|
|
|
.attr("x", function() { if(xRatio < 0.70) return d3.mouse(this)[0] + 15; else return d3.mouse(this)[0] - 125; })
|
2017-12-31 16:42:30 +01:00
|
|
|
.attr("y", function() { return d3.mouse(this)[1] + 30; })
|
2017-12-16 21:54:07 +01:00
|
|
|
.text(function() {
|
|
|
|
return text; // Value of the text
|
|
|
|
});
|
2017-12-16 01:18:24 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
function handleMouseOutLinePoint(d, i) {
|
2017-12-29 16:42:41 +01:00
|
|
|
var dataType = d3.select(this).attr("data-elemtype");
|
|
|
|
//depending on Pitstop and lap different Texts
|
|
|
|
var textArr = [];
|
2017-12-16 01:18:24 +01:00
|
|
|
// Use D3 to select element, change color back to normal
|
2017-12-29 16:42:41 +01:00
|
|
|
if(dataType === elemTypes.linepoint){
|
2017-12-30 12:33:11 +01:00
|
|
|
var circleId = "circle-linepoint-" + d.lap + "-" + d.driverId;
|
|
|
|
var circle = d3.select("#" + circleId);
|
2017-12-29 16:28:34 +01:00
|
|
|
circle
|
2017-12-16 21:54:07 +01:00
|
|
|
.attr("r", linePointSize)
|
|
|
|
.style("opacity", 0);
|
2017-12-29 16:42:41 +01:00
|
|
|
textArr = getLapTextArray(raceData,d);
|
|
|
|
}else if(dataType === elemTypes.pitstoppoint){
|
2017-12-30 12:33:11 +01:00
|
|
|
d3.select(this)
|
2017-12-31 16:11:39 +01:00
|
|
|
.attr("r", linePointSize * 1.4);
|
2017-12-16 21:54:07 +01:00
|
|
|
textArr = getPitStopTextArray(raceData,d);
|
2017-12-29 16:42:41 +01:00
|
|
|
}else if(dataType === elemTypes.endpoint){
|
|
|
|
d3.select(this)
|
|
|
|
.attr("height", rectSize)
|
2017-12-30 13:14:40 +01:00
|
|
|
.attr("width", rectSize)
|
|
|
|
.attr("x", function(d, i) {return x(d.lap) - rectSize * 1/2 })
|
|
|
|
.attr("y", function(d, i) { return y(d.position) - rectSize * 1/2 });
|
2017-12-29 16:42:41 +01:00
|
|
|
textArr = getEndPointTextArray(raceData,d);
|
2017-12-16 21:54:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
textArr.forEach((text, textIndex)=> {
|
|
|
|
// Select text by id and then remove
|
2017-12-29 23:23:37 +01:00
|
|
|
d3.select("#t" + d.lap + "-" + d.position + "-" + i).remove(); // Remove text location
|
2017-12-16 01:18:24 +01:00
|
|
|
});
|
2017-12-16 21:54:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
function getLapTextArray(raceData, d){
|
2017-12-29 16:42:41 +01:00
|
|
|
var driverText = getDriverCodeById(raceData,d.driverId);
|
2017-12-16 21:54:07 +01:00
|
|
|
var lapText = "Lap: " + d.lap;
|
|
|
|
var posText = "Pos: " + d.position;
|
2017-12-29 17:12:01 +01:00
|
|
|
var returnArr = [driverText, lapText, posText];
|
|
|
|
if(d.time){
|
|
|
|
var timeText = "Time: " + d.time;
|
|
|
|
returnArr.push(timeText);
|
|
|
|
}
|
|
|
|
return returnArr;
|
2017-12-16 21:54:07 +01:00
|
|
|
}
|
2017-12-16 01:18:24 +01:00
|
|
|
|
2017-12-16 21:54:07 +01:00
|
|
|
function getPitStopTextArray(raceData, d){
|
|
|
|
var lapTextArr = getLapTextArray(raceData,d);
|
|
|
|
lapTextArr.push("Stop Nr: " + d.pitStop.stop);
|
|
|
|
lapTextArr.push("Duration: " + d.pitStop.duration);
|
|
|
|
return lapTextArr;
|
2017-12-16 01:18:24 +01:00
|
|
|
}
|
2017-12-13 00:12:11 +01:00
|
|
|
|
2017-12-29 16:42:41 +01:00
|
|
|
function getEndPointTextArray(raceData, d){
|
|
|
|
var status = "";
|
|
|
|
var endTextArr = getLapTextArray(raceData,d);
|
|
|
|
var allStatus = queries.getStatus();
|
|
|
|
for(var key in allStatus){
|
|
|
|
if(key === undefined) continue;
|
|
|
|
if(allStatus[key].statusId === d.finished.statusId){
|
|
|
|
status = allStatus[key].status;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
endTextArr.push("Reason: " + status);
|
|
|
|
return endTextArr;
|
|
|
|
}
|
|
|
|
|
2017-12-19 21:35:47 +01:00
|
|
|
function getDriverCodeFromPosAndLap(raceData, lapNr, pos){
|
|
|
|
var driverCode = "";
|
|
|
|
if(lapNr == 0){
|
2017-12-30 18:14:51 +01:00
|
|
|
var qualifying = raceData.qualifying[pos - 1];
|
|
|
|
if(qualifying === undefined) return "XXX"; // TODO: Do a real fix
|
|
|
|
driverCode = getDriverCodeById(raceData, qualifying.driverId);
|
2017-12-19 21:35:47 +01:00
|
|
|
}else{
|
2017-12-31 16:42:49 +01:00
|
|
|
if(lapNr == raceData.lapTimes.size){ // are at the end of the graph. Only draw driver who finished
|
|
|
|
var found = false;
|
|
|
|
var _pos = pos-1;
|
|
|
|
var _lap = lapNr;
|
|
|
|
while(!found){
|
|
|
|
if(raceData.lapTimes.get(_lap)[_pos]){
|
|
|
|
var driverID = raceData.lapTimes.get(_lap)[_pos].driverId;
|
|
|
|
|
|
|
|
for(var key in raceData.results){
|
|
|
|
if(raceData.results[key].driverId == driverID){
|
|
|
|
if(isNaN(raceData.results[key].position) || raceData.results[key].laps != _lap){
|
|
|
|
return driverCode;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
driverCode = getDriverCodeById(raceData, raceData.lapTimes.get(_lap)[_pos].driverId);
|
|
|
|
found = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
_lap--;
|
2017-12-31 16:54:30 +01:00
|
|
|
if(_lap <= 0) break;
|
2017-12-31 16:42:49 +01:00
|
|
|
}
|
|
|
|
}else{
|
|
|
|
if(raceData.lapTimes.get(lapNr)[pos-1]){
|
|
|
|
driverCode = getDriverCodeById(raceData, raceData.lapTimes.get(lapNr)[pos-1].driverId);
|
|
|
|
}
|
2017-12-29 14:20:58 +01:00
|
|
|
}
|
2017-12-19 21:35:47 +01:00
|
|
|
}
|
|
|
|
return driverCode;
|
|
|
|
}
|
|
|
|
|
2017-12-30 18:48:54 +01:00
|
|
|
function brushed() {
|
|
|
|
if (d3.event.sourceEvent && d3.event.sourceEvent.type === "zoom") return; // ignore brush-by-zoom
|
|
|
|
var s = d3.event.selection || x2.range();
|
|
|
|
x.domain(s.map(x2.invert, x2));
|
2017-12-31 13:15:11 +01:00
|
|
|
updateElements();
|
2017-12-30 23:53:56 +01:00
|
|
|
// Update the "preview" rectangle
|
2017-12-30 18:48:54 +01:00
|
|
|
svg.select(".zoom").call(zoom.transform, d3.zoomIdentity
|
2017-12-30 23:22:43 +01:00
|
|
|
.scale(graphPosWidth.width / (s[1] - s[0]))
|
|
|
|
.translate(-s[0], 0));
|
2017-12-30 18:48:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
function zoomed() {
|
|
|
|
if (d3.event.sourceEvent && d3.event.sourceEvent.type === "brush") return; // ignore zoom-by-brush
|
|
|
|
var t = d3.event.transform;
|
2017-12-30 23:52:55 +01:00
|
|
|
x.domain(t.rescaleX(x2).domain());
|
|
|
|
updateElements();
|
2017-12-31 13:15:11 +01:00
|
|
|
|
2017-12-30 23:52:55 +01:00
|
|
|
//call the brush function
|
2017-12-30 23:41:37 +01:00
|
|
|
context.select(".brush").call(brush.move, x.range().map(t.invertX, t));
|
|
|
|
}
|
|
|
|
|
|
|
|
function updateElements(){
|
2017-12-30 23:58:33 +01:00
|
|
|
// Update the data!
|
2017-12-30 23:22:43 +01:00
|
|
|
focus.selectAll(".pathLines").attr("d", lineDataDefinition);
|
2017-12-30 23:52:25 +01:00
|
|
|
focus.selectAll(".pitstopdot").attr("cx", function(d, i) {return x(d.lap) });
|
|
|
|
focus.selectAll(".endpointdot").attr("x", function(d, i) { return x(d.lap) - rectSize * 1/2; });
|
2017-12-31 00:28:17 +01:00
|
|
|
focus.selectAll(".linedot").attr("cx", function(d, i) {return x(d.lap) });
|
2017-12-30 23:58:33 +01:00
|
|
|
// Update xAxis
|
2017-12-30 23:52:25 +01:00
|
|
|
focus.select(".axis--x").call(xAxis);
|
2017-12-31 13:15:11 +01:00
|
|
|
|
|
|
|
updateYAxisText();
|
2017-12-31 13:07:41 +01:00
|
|
|
|
2017-12-31 13:15:11 +01:00
|
|
|
// Update gridlines
|
|
|
|
focus.select(".xAxisGridlines").call(xAxisGridlines);
|
|
|
|
}
|
|
|
|
|
|
|
|
function updateYAxisText(){
|
2017-12-31 13:07:41 +01:00
|
|
|
var xAxisValues = d3.extent(x.domain());
|
|
|
|
if(xAxisValues[1] > 1){
|
2017-12-31 13:15:11 +01:00
|
|
|
// Get the min/max lap.
|
2017-12-31 13:07:41 +01:00
|
|
|
var startLap = Math.floor(xAxisValues[0]);
|
|
|
|
var endLap = Math.floor(xAxisValues[1]);
|
|
|
|
|
2017-12-31 13:15:11 +01:00
|
|
|
// Redraw the left Y axis
|
|
|
|
focus.select(".axis--y-left").call(d3.axisLeft(y).ticks(raceData.drivers.length).tickFormat(function(d) {
|
|
|
|
return getDriverCodeFromPosAndLap(raceData, startLap, d) + " " + d;
|
|
|
|
}));
|
|
|
|
|
|
|
|
// Redraw the right Y axis
|
2017-12-31 13:07:41 +01:00
|
|
|
focus.select(".axis--y-right").call(
|
2017-12-31 13:15:11 +01:00
|
|
|
d3.axisRight(y)
|
|
|
|
.ticks(raceData.drivers.length)
|
|
|
|
.tickFormat(function(d) {
|
2017-12-31 16:42:49 +01:00
|
|
|
var driverCode = getDriverCodeFromPosAndLap(raceData, endLap, d);
|
2017-12-31 13:15:11 +01:00
|
|
|
return d + " " + driverCode ;
|
|
|
|
}));
|
2017-12-31 13:07:41 +01:00
|
|
|
}
|
2017-12-30 18:48:54 +01:00
|
|
|
}
|
|
|
|
|
2017-11-16 23:57:21 +01:00
|
|
|
}
|