Skip to content

Instantly share code, notes, and snippets.

@GerardoFurtado
Last active December 4, 2015 14:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save GerardoFurtado/c62eb5c9db364fafd73f to your computer and use it in GitHub Desktop.
Save GerardoFurtado/c62eb5c9db364fafd73f to your computer and use it in GitHub Desktop.
Performance of Formula 1 teams
teamname 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014
Vanwall 71.3 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
Ferrari 71.3 59.4 37.5 81.3 22.2 28.9 54.4 30.0 39.5 20.2 29.6 7.1 47.0 33.3 30.6 8.9 48.1 57.1 57.6 63.4 40.3 50.2 3.8 15.1 30.8 39.6 23.8 34.2 15.4 22.1 27.1 24.6 45.8 21.9 8.2 10.9 27.7 26.8 27.3 37.5 52.0 50.0 62.5 65.8 81.3 54.9 80.9 29.2 62.0 66.7 53.1 22.9 48.5 45.9 46.5 43.3 26.4
Cooper 38.8 82.8 80.6 28.1 45.7 28.9 17.8 15.6 43.2 28.3 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0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0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.3 2.6 12.0 2.6 1.6 5.0 3.0 4.0 3.7
Brawn 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 56.2 0.0 0.0 0.0 0.0 0.0
Force India 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 4.2 8.3 8.4 12.7 9.4 19.0
Mercedes 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 26.2 20.2 16.5 44.1 85.8
<!DOCTYPE html>
<html lang="en">
<head>
<link href='https://fonts.googleapis.com/css?family=Roboto' rel='stylesheet' type='text/css'>
<meta charset="utf-8">
<title>Formula 1 Teams</title>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.6/d3.min.js"></script>
<style type="text/css">
body {
margin: 0;
background-color: gainsboro;
font-family: "Roboto";
font-weight: 100;
}
#container {
width: 1000px;
margin-left: auto;
margin-right: auto;
margin-top: 20px;
padding: 1px 20px 10px 20px;
background-color: white;
box-shadow: 1px 1px 1px 1px #fff;
}
img.displayed {
display: block;
margin-left: auto;
margin-right: auto;
}
h1 {
font-weight: 550;
color: darkslategray;
font-size: 50px;
margin-bottom: 10px;
margin-top: 20px;
}
h2 {
font-weight: 550;
color: darkslategray;
font-size: 32px;
margin-bottom: 20px;
padding-bottom: 0px;
}
p {
font-size: 18px;
margin: 15px 0 10px 0;
}
p.footer {
font-size: 14px;
}
a:link {
text-decoration: none;
color: gray;
}
a:hover {
text-decoration: underline;
}
a:visited {
color: gray;
}
a:active {
color: steelBlue;
}
svg {
background-color: white;
}
path.area:hover {
fill:maroon;
}
.button:hover {
fill: maroon;
stroke: darkslategray;
cursor: pointer;
}
#teamlabel {
position: relative;
left: 30px;
font-family: "Roboto";
color: #2f3b44;
font-size: 22px;
text-align: left;
font-weight: bold;
}
#teamlabel p.defaultText {
font-family: "Roboto";
font-size: 22px;
text-align: left;
font-weight: bold;
margin:0px;
}
.axis path,
.axis line {
fill: none;
stroke: black;
shape-rendering: crispEdges;
}
.axis text {
font-family: sans-serif;
font-size: 11px;
}
.y.axis line {
stroke: #777;
stroke-dasharray: 2,2;
}
div.tooltip {
position: absolute;
text-align: center;
white-space: normal;
padding: 2px;
font-size: 14px;
background: ivory;
border: 1px solid lightgray;
border-radius: 4px;
pointer-events: none;
cursor: none;
}
circle {
pointer-events: none;
}
</style>
</head>
<body>
<div id="container">
<h1>Performance of Formula 1 Teams</h1>
<img src="http://41.media.tumblr.com/30b4358c448de0777076360b63a4afae/tumblr_nxfbpyrg0p1rtst4no2_1280.jpg" align = "middle" width = 1000 height = 240>
<br>
<h2>Percentage of the maximum possible number of points</h2>
<p>The Formula One World Constructors' Championship, introduced in 1958, is awarded by the FIA to the most successful Formula One constructor over a season, as determined by a points system based on Grand Prix results. But in this chart the history of Formula One teams is shown differently: it doesn't show the number of points scored per season or the final position of each team, but <b>what percentage of the maximum number of points a team was able to score</b>.</p>
<p>All the teams are presented together, in what is called a "stacked area chart" (not very useful). Hover the stacked areas to select a team. Click on a team to see its performance. Vertical axis shows the percentage of the maximum number of points, horizontal axis shows the years. Hover the new isolated area to see the performance in a given year. Click the box next to "back to all teams" to, well, go back to all teams!</p>
<br>
<div id="teamlabel"><p class="defaultText">Team :</p></div>
<div id="svganchor"></div>
<br>
<p>Aditional explanations:</p>
<ol>
<li>The maximum number of points is obtained if a team wins every race of the season, and after 1979 if a team gets a one-two win in every race (up to 1978, only the best placed car for each team was eligible to score points).</li>
<li>As this chart shows how a team performed regarding the maximum number of points it could score and not regarding the total points distributed in that season, the sum of all teams in a season (the stacked areas) will be more than 100% of the maximum number of points.</li>
<li>All the data was collected on Wikipedia. To avoid a big number of small teams, I arbitrarily cut off all teams that never achieved 10% of the maximum number of points in a season (sorry, Arrows fans). All the races in a season were considered, differently from FIA system to award Formula One World Constructors' Championship.</li>
<li>Except for Ferrari, all other teams' graphs begin or end at the base (that is, zero points). This doesn't necessarily mean that that team was competing and scored zero points, that's just the way D3 creates areas. I'll improve the chart regarding this.</li>
</ol>
<p class="footer">Created by Gerardo Furtado (and all the people whose code I copy-pasted).</p>
</div>
<script type="text/javascript">
//width, height, padding
var w = 1000;
var h = 530;
var padding = [ 50, 30, 20, 30 ]; //Top, right, bottom, left
//Set up stack method
var stack = d3.layout.stack()
.values(function(d) {
return d.points;
})
.order("inside-out");
//Set up date format function (years)
var dateFormat = d3.time.format("%Y");
//Set up scales
var xScale = d3.time.scale()
.range([ padding[3], w - padding[1] ]);
var yScale = d3.scale.linear()
.range([ padding[0], h - padding[2] ]);
//Configure axis generators
var xAxis = d3.svg.axis()
.scale(xScale)
.orient("bottom")
.ticks(15)
.tickFormat(function(d) {
return dateFormat(d);
});
var yAxis = d3.svg.axis()
.scale(yScale)
.orient("left")
// .attr("opacity", 0)
.ticks(10);
//Configure area generator
var area = d3.svg.area()
.interpolate("linear")
.x(function(d) {
return xScale(dateFormat.parse(d.x));
})
.y0(function(d) {
return yScale(d.y0); //Updated
})
.y1(function(d) {
return yScale(d.y0 + d.y); //updated
});
//Configure a flat area
var areaflat = d3.svg.area()
.interpolate("linear")
.x(function(d) {
return xScale(dateFormat.parse(d.x));
})
.y0(function(d) {
return yScale(0);
})
.y1(function(d) {
return yScale(0);
});
//Configure baseline area
var areabase = d3.svg.area()
.interpolate("linear")
.x(function(d) {
return xScale(dateFormat.parse(d.x));
})
.y0(function(d) {
return yScale(0)
})
.y1(function(d) {
return yScale(d.y);
});
//Easy colors accessible via a 20-step ordinal scale
var color = d3.scale.category20b();
//Creates the SVG
var svg = d3.select("#svganchor")
.append("svg")
.attr("width", w)
.attr("height", h);
//Load the data
d3.csv("f1teams.csv", function(data) {
var years = ["1958", "1959", "1960", "1961", "1962", "1963", "1964", "1965", "1966", "1967", "1968", "1969", "1970", "1971", "1972", "1973", "1974", "1975", "1976", "1977", "1978", "1979", "1980", "1981", "1982", "1983", "1984", "1985", "1986", "1987", "1988", "1989", "1990", "1991", "1992", "1993", "1994", "1995", "1996", "1997", "1998", "1999", "2000", "2001", "2002", "2003", "2004", "2005", "2006", "2007", "2008", "2009", "2010", "2011", "2012", "2013", "2014"];
//Create a new dataset for the restructured data
var dataset = [];
//Loop once for each row in data
for (var i = 0; i < data.length; i++) {
//Create new object with this team's name and empty array
dataset[i] = {
team: data[i].teamname,
points: []
};
//Loop through all the years
for (var j = 0; j < years.length; j++) {
//Default value, used in case no value is present
var amount = null;
// If value is not empty
if (data[i][years[j]]) {
amount = +data[i][years[j]];
}
//Add a new object to the emissions data array
//for this country
dataset[i].points.push({
x: years[j],
y: amount
});
}
}
//Stack the data!
stack(dataset);
//Just checking if everything is ok...
// console.log(dataset);
//min and max to x scale
xScale.domain([
d3.min(years, function(d) {
return dateFormat.parse(d);
}),
d3.max(years, function(d) {
return dateFormat.parse(d);
})
]);
//Loop once for each year, and get the total value
//of percentage of points
var totals = [];
for (i = 0; i < years.length; i++) {
totals[i] = 0;
for (j = 0; j < dataset.length; j++) {
totals[i] += dataset[j].points[i].y;
}
}
yScale.domain([ d3.max(totals), 0 ]);
//again, just checking
//console.log(totals);
//Make a path for each team
var paths = svg.selectAll("path")
.data(dataset)
.enter()
.append("path")
.attr("class", "area")
.attr("d", function(d) {
return area(d.points);
})
.attr("stroke", "lightgray")
.attr("fill", function(d, i) {
return color(i);
});
//initial opacity of tooltip and circles
var ttop = 0.9;
var ttopc = 0;
//Displays the name of team on mouseover
paths.on("mouseover", function(d) {
d3.select("#teamlabel")
.html("Team: " + d.team)
});
paths.on("mousemove", function(d) {
var tttext = "<strong>" + d.team + "</strong>";
tt.html(tttext)
.style('top', d3.event.pageY - 12 + 'px')
.style('left', d3.event.pageX + 25 + 'px')
.style("opacity", ttop);
});
//Removes the name of team on mouseout
paths.on("mouseout", function(d) {
d3.select("#teamlabel")
.html("Team:");
tt.style("opacity", 0);
});
var tt = d3.select("#svganchor").append("div")
.attr("class", "tooltip")
.style("opacity", 0);
//the circles on the top of the path
var cir = svg.append("circle")
.attr("r", 5)
.attr("stroke", "black")
.attr("opacity", 0);
//Changing the baseline after click
d3.selectAll("path")
.on("click", function() {
yScale.domain([100, 0])
//transitioning all paths to flat
d3.selectAll("path")
.attr("stroke", "none")
.data(dataset)
.transition()
.duration(500)
.attr("d", function(d) {
return areaflat(d.points);
});
//Selecting the path I clicked
d3.select(this)
.attr("stroke", "black")
.transition()
.duration(500)
.attr("d", function(d) {
return areabase(d.points);
})
.attr("class", "pathbase");
//showing tooltips and circles
paths.on("mousemove", function(d) {
var checkpath = document.getElementsByClassName("pathbase");
if (checkpath.length > 0) {
var ttop = 0.9;
var ttopc = 0.7;
} else {
var ttop = 0.9;
var ttopc = 0;
};
var mousex = d3.mouse(this);
mousex = mousex[0] + 10;
var invertedx = xScale.invert(mousex);
invertedx = invertedx.getFullYear();
var mousedate = years.indexOf(String(invertedx));
var pon = d.points[mousedate].y;
var posx = invertedx.toString();
cir.attr("cy", yScale(pon))
.attr("cx", xScale(dateFormat.parse(posx)))
.attr("opacity", ttopc)
.attr("fill", d3.select(this).style("fill"));
var tttext = "In <strong>" + invertedx + "</strong> " + d.team + " scored <strong>" + pon + "</strong>%<br> of the maximum number of points";
tt.html(tttext)
.style('top', d3.event.pageY - 12 + 'px')
.style('left', d3.event.pageX + 15 + 'px')
.style("opacity", ttop);
});
yAxis.innerTickSize(-(w-60))
.tickValues([20,40,60,80]);
paths.on("mouseout", function() {
tt.style("opacity", 0);
cir.attr("opacity", 0);
d3.select("#teamlabel")
.html("Team:");
});
d3.transition(svg).select(".y.axis")
.transition()
.duration(500)
.attr("opacity", 1)
.call(yAxis);
});
//Going back to normal chart with a button
svg.append("rect")
.attr("x", 30)
.attr("y", 10)
.attr("height", 20)
.attr("width", 20)
.attr("fill", "darkslategray")
.attr("stroke-width", "2px")
.attr("class", "button")
.on("click", function() {
var ttopc = 0;
paths.on("mousemove", function(d) {
var tttext = "<strong>" + d.team + "</strong>";
tt.html(tttext)
.style('top', d3.event.pageY - 12 + 'px')
.style('left', d3.event.pageX + 25 + 'px')
.style("opacity", ttop);
});
yScale.domain([ d3.max(totals), 0 ]);
d3.selectAll("path")
.data(dataset)
.attr("class", "area")
.attr("stroke", "lightgray")
.transition()
.duration(500)
.attr("d", function(d) {
return area(d.points);
});
d3.transition(svg).select(".y.axis")
.transition()
.duration(500)
.attr("opacity", 0)
.call(yAxis);
});
svg.append("text")
.attr("x", 54)
.attr("y", 25)
.attr("font-size", 16)
.attr("font-family", "Charter")
.text("Back to all teams");
//Create axes
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + (h - padding[2]) + ")")
.call(xAxis);
svg.append("g")
.attr("class", "y axis")
.attr("opacity", 0)
.attr("transform", "translate(" + padding[3] + ",0)")
.call(yAxis);
});
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment