Skip to content

Instantly share code, notes, and snippets.

@captainelaine
Last active March 14, 2016 17:57
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 captainelaine/879ed6a6364d6831ea24 to your computer and use it in GitHub Desktop.
Save captainelaine/879ed6a6364d6831ea24 to your computer and use it in GitHub Desktop.
My Update Plot
Driver wage2015 point2015 Age CareerPoint CareerYear
Fernando Alonso 350 11 35 1778 15
Kimi Raikkonen 180 150 37 1174 15
Sebastian Vettel 280 278 29 1896 9
Lewis Hamilton 250 381 31 1867 9
Jenson Button 100 16 36 1214 16
Nico Rosberg 135 322 31 1209.5 10
Felipe Massa 40 121 35 1071 14
Nico Hulkenberg 40 58 29 290 6
Sergio Perez 40 78 26 266 5
Romain Grosjean 40 51 30 287 7
Pastor Maldonado 40 27 31 76 5
Daniel Ricciardo 15 92 27 360 5
Valtteri Bottas 20 136 27 326 3
Daniil Kvyat 7.5 95 22 103 2
Max Verstappen 2.5 49 19 49 1
Carlos Sainz 2.5 18 22 18 1
Felipe Nasr 2 27 24 27 1
Display the source blob
Display the rendered blob
Raw
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
<link href='https://fonts.googleapis.com/css?family=Ubuntu:300italic,500|Josefin+Sans:400italic' rel='stylesheet' type='text/css'>
<!DOCTYPE html>
<html>
<head>
<title>Formula One World</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 {
padding: 50px;
font-family: 'Ubuntu',sans-serif;
font-size: 12pt;
text-align: center;
}
button {
margin: 5px;
font-size: 15pt;
padding: 3px;
cursor: pointer;
}
input {
margin: 5px;
font-size: 15pt;
padding: 3px;
}
.mybartooltip {
position: absolute;
z-index: 10;
}
.mybartooltip p {
background-color:rgb(244,239,243);
border: none;
padding: 2px;
font-size: 12px;
}
#chart {
background-color:rgb(252,250,250 );
z-index: -10000px;
}
#menu {
padding: 20px 75px;
}
.labels {
fill: black;
font-size: 10px;
font-family: 'Josefin Sans', sans-serif;
}
.labelsdrivername {
font-size: 10px;
font-family: 'Josefin Sans', sans-serif;
}
.axis {
shape-rendering: crispEdges;
font-size: 10px;
}
.axis path {
fill: none;
stroke: none;
}
.x.axis path {
fill: none;
stroke: white;
stroke-opacity:0;
}
.x.axis line {
stroke: gray;
stroke-opacity: .8;
}
.y.axis path {
stroke: black;
}
.focused {
opacity: 0.9;
}
.unfocused {
opacity: 0.4;
}
.hidden {
display: none;
}
</style>
</head>
<body>
<div class = "intro">
<h1>Formula One World</h1>
<p><em>As below, you can see many different kinds of imformation of drivers who attended the 2015 Season</em></p>
<p><em>You can take a look at different drivers' wage, won points and so on from high to low.</em></p>
<p>Source:<a href = "http://www.crash.net/f1/news/221215/1/f1-2015-driver-salaries-published-but-who-earns-most.html">Crash.Net</a> <a href="https://en.wikipedia.org/wiki/2015_Formula_One_season">Wikipedia-FormulaOne</a></p>
</div>
<div id="menu">
<select>
<option value ="Age">Age of drivers</option>
<option value="wage2015">Wage of 2015(hundred thousand)</option>
<option value="point2015">Points won in 2015</option>
<option value="CareerPoint">Career total won points</option>
<option value ="CareerYear">Career Age</option>
</select>
</div>
<div id="chart">
</div>
<script type="text/javascript">
var fullwidth = 900;
var fullheight = 500;
var margin = {top: 20, right: 50, bottom: 20, left: 100};
var height = fullheight - margin.top - margin.bottom;
var width = fullwidth - margin.left - margin.right;
var vis = d3.select("#chart").append("svg");
var svg = vis
.attr("width", fullwidth)
.attr("height", fullheight)
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
svg.append("g")
.attr("class", "x axis");
svg.append("g")
.attr("class", "xbottom")
var xScale = d3.scale.linear()
.range([0, width]);
var yScale = d3.scale.ordinal()
.rangeRoundBands([0, height], .1);
var xAxis = d3.svg.axis()
.scale(xScale)
.orient("top")
.ticks(10)
.innerTickSize([5])
.outerTickSize([5]);
var mybartooltip = d3.select("body")
.append("div")
.attr("class", "mybartooltip");
d3.csv("f1driverwage.csv", function(error, data) {
var column = d3.select("#menu select").property("value");
var dataset = bigfirst(data, column);
console.log(column);
//setup our ui -- requires access to data variable, so inside csv
d3.select("#menu select")
.on("change", function() {
column = d3.select("#menu select").property("value");
dataset = bigfirst(data, column);
redraw(dataset, column);
});
});
function bigfirst(data, column){
data.sort(function(a,b){
return d3.descending(+a[column],+b[column]);
})
redraw(data,column);
}
//get the descending values;
function redraw(data, column) {
var max = d3.max(data, function(d) {return +d[column];});
xScale.domain([0, max]);
yScale.domain(d3.range(data.length));
var bars = svg.selectAll("rect.bar")
.data(data, function (d) {
return d.Driver;
});
bars
.attr("fill", "gray")
.attr("opacity","0.2");
//enter - new bars get set to darkorange when we "redraw."
bars.enter()
.append("rect")
.attr("class", "bar")
.attr("fill", function(d){
if (d.Driver==="Kimi Raikkonen" ||d.Driver==="Sebastian Vettel") {
return "rgb(250,67,52)";
} else if (d.Driver ==="Lewis Hamilton" || d.Driver ==="Nico Rosberg") {
return "rgb(137,141,142)";
} else if (d.Driver === "Felipe Massa" || d.Driver ==="Valtteri Bottas") { return "rgb (51,133,229)";
} else if (d.Driver ==="Fernando Alonso" || d.Driver === "Janson Button") {
return "rgb(248,160,53)";
} else if (d.Driver === "Daniel Ricciardo" || d.Driver ==="Daniil Kvyat") {
return "rgb(172,62,100)";
} else {
return "rgb(184,192,201)";
}
})
.attr("opacity","0.4")
.on("mouseover", mouseoverBar)
.on("mouseout", mouseoutBar);
//exit -- remove ones that aren't in the index set
bars.exit()
.transition()
.duration(300)
.attr("width", 0)
.remove();
//at goes here at the end of exit?
// transition -- move the bars to proper widths and location
bars
.transition()
.duration(500)
.ease("quad")
.attr("width", function(d) {
return xScale(+d[column]);
})
.attr("height", yScale.rangeBand())//TODO: In an ordinal scale bar chart, what goes here?)
.attr("fill", function(d){
if (d.Driver==="Kimi Raikkonen" ||d.Driver==="Sebastian Vettel") {
return "red";
} else if (d.Driver ==="Lewis Hamilton" || d.Driver ==="Nico Rosberg") {
return "rgb(137,141,142)";
} else if (d.Driver === "Felipe Massa" || d.Driver ==="Valtteri Bottas") { return "rgb(9,103,159)";
} else if (d.Driver ==="Fernando Alonso" || d.Driver === "Jenson Button") {
return "rgb(248,160,53)";
} else if (d.Driver === "Daniel Ricciardo" || d.Driver ==="Daniil Kvyat") {
return "rgb(172,62,100)";
} else {
return "rgb(184,192,201)";
}
})
.attr("opacity","0.4")// do what you like with the colors
.attr("transform", function(d,i) {
return "translate(0," + yScale(i) + ")";
});
svg.transition().select(".x.axis")
.call(xAxis);
// We are attaching the labels separately, not in a group with the bars...
var labels = svg.selectAll("text.labels")
.data(data, function (d) {
return d.Driver;
});
// everything gets a class and a text field. But we assign attributes in the transition.
labels.enter()
.append("text")
.attr("class", "labels");
labels.exit()
.remove();
labels.transition()
.duration(500)//: How long do you want this to last?)
.text(function(d) {
return +d[column];
})
.attr("x", function(d){
return xScale(+d[column]);
})
.attr("y", function(d,i) {
return yScale(i);
})
.attr("dy", "1.2em")
.attr("dx", function(d){
if (+d[column]> 1000) {
return "30px";
} else {
return "20px";
}
})
.attr("text-anchor", "end");
var labelsdrivername = svg.selectAll("text.labelsdrivername")
.data(data,function(d){
return d.Driver;
});
labelsdrivername.enter()
.append("text")
.attr("class", "labelsdrivername");
labelsdrivername.transition()
.duration(500)
.text(function(d){
return d.Driver;
})
.attr("x",-3)
.attr("transform", function(d,i) {
return "translate(" + 0+ "," + yScale(i) + ")"
})
.attr("dy", "15px")
.attr("dx", "-3px")
.attr("text-anchor", "end");
function mouseoverBar(d) {
d3.select(this)
.classed("focused", true)
.classed("unfocused", false)
mybartooltip
.style("display", null)
.html("<p>Driver:" + " " +d.Driver +
"</p>")
.style("top", (d3.event.pageY - 10) + "px" )
.style("left", (d3.event.pageX + 10) + "px");
}
function mouseoutBar(d) {
d3.select(this)
.classed("focused", false)
.classed("unfocused", true)
mybartooltip
.style("display","none");
}
}
svg.append("circle")
.attr("cx",650)
.attr("cy",340)
.style("fill","rgb(250,67,52)")
.attr("r",6)
svg.append("text")
.attr("x",660)
.attr("y",346)
.attr("font-size","16px")
.attr("font-weight","bold")
.style("text-anchor", "left")
.text("Ferrair");
svg.append("circle")
.attr("cx",650)
.attr("cy",360)
.style("fill","rgb(137,141,142)")
.attr("r",6)
svg.append("text")
.attr("x",660)
.attr("y",366)
.attr("font-size","16px")
.attr("font-weight","bold")
.style("text-anchor", "left")
.text("Mercedes");
svg.append("circle")
.attr("cx",650)
.attr("cy",380)
.attr("r",6)
.style("fill","rgb(9,103,159)")
svg.append("text")
.attr("x",660)
.attr("y",386)
.attr("font-size","16px")
.attr("font-weight","bold")
.style("text-anchor", "left")
.text("Williams");
svg.append("circle")
.attr("cx",650)
.attr("cy",400)
.attr("r",6)
.style("fill","rgb(248,160,53)")
svg.append("text")
.attr("x",660)
.attr("y",406)
.attr("font-size","16px")
.attr("font-weight","bold")
.style("text-anchor", "left")
.text("McLaren");
svg.append("circle")
.attr("cx",650)
.attr("cy",420)
.attr("r",6)
.style("fill","rgb(172,62,100)")
svg.append("text")
.attr("x",660)
.attr("y",426)
.attr("font-size","16px")
.attr("font-weight","bold")
.style("text-anchor", "left")
.text("Red Bull");
svg.append("circle")
.attr("cx",650)
.attr("cy",440)
.attr("r",6)
.style("fill","rgb(184,192,201)")
svg.append("text")
.attr("x",660)
.attr("y",446)
.attr("font-size","16px")
.attr("font-weight","bold")
.style("text-anchor", "left")
.text("Other Teams");
</script>
</body>
</html>
Display the source blob
Display the rendered blob
Raw
<?xml version="1.0" encoding="utf-8"?>
<!-- Generator: Adobe Illustrator 12.0.0, SVG Export Plug-In . SVG Version: 6.00 Build 51448) -->
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd" [
<!ENTITY ns_svg "http://www.w3.org/2000/svg">
<!ENTITY ns_xlink "http://www.w3.org/1999/xlink">
]>
<svg version="1.1" id="Layer_1" xmlns="&ns_svg;" xmlns:xlink="&ns_xlink;" width="825.646" height="175.646"
viewBox="0 0 825.646 175.646" overflow="visible" enable-background="new 0 0 825.646 175.646" xml:space="preserve">
<g>
<polygon opacity="0" fill-rule="evenodd" clip-rule="evenodd" fill="#FFFFFF" points="0,0 825.646,0 825.646,175.646 0,175.646
0,0 "/>
<path fill-rule="evenodd" clip-rule="evenodd" d="M200.122,91.081L76.85,90.914l-40.424,45.771h70.492l24.554-27.813h52.785
L200.122,91.081L200.122,91.081L200.122,91.081z M87.792,80.724h123.523l33.326-37.333h23.806v93.293h-57.131V43.391h-90.202
L87.792,80.724L87.792,80.724L87.792,80.724z"/>
<path fill-rule="evenodd" clip-rule="evenodd" d="M336.642,109.147h26.207l16.08-18.121v18.033l19.99,0.088l16.251-18.121v18.033
h24.759V71.969h-27.06l-13.44,15.059V71.969h-29.691L336.642,109.147L336.642,109.147L336.642,109.147z M544.398,104.806
l3.233,1.79l3.149,1.443l2.979,0.937l2.807,0.682l2.72,0.255l2.472,0.255l2.38-0.255l2.042-0.255l1.957-0.343l1.615-0.51l1.53-0.51
l1.28-0.51l0.933-0.51l0.682-0.422l0.427-0.255l0.168-0.088l0.682,3.15h22.969V78.775h-22.459l-0.171,1.021l-0.168,1.021
l-0.255,1.021l-0.171,1.021l-1.787-1.698l-2.129-1.28l-2.552-0.937l-2.807-0.594l-3.062-0.255l-3.233,0.171l-3.233,0.427
l-3.063,0.761l-3.062,1.109l-2.895,1.275l-2.464,1.699l-2.129,1.958l-1.703,2.213l-1.104,2.472l-0.514,2.635l0.259,2.978
l-17.188-0.088V72.224h-25.266v37.434h47.13V104.806L544.398,104.806L544.398,104.806z M570.434,88.387l-1.615-0.083l-1.447,0.083
l-1.447,0.255l-1.275,0.598l-1.108,0.761l-0.85,0.937l-0.678,1.108l-0.255,1.188v1.28l0.339,1.104l0.594,1.192l0.938,1.021
l1.021,0.937l1.359,0.765l1.363,0.594l1.703,0.422h1.614h1.531l1.447-0.422l1.275-0.594l1.104-0.765l0.854-0.854l0.594-1.021
l0.255-1.276v-1.192l-0.255-1.192l-0.594-1.188l-0.938-1.108l-1.104-0.937l-1.28-0.761l-1.443-0.598L570.434,88.387L570.434,88.387
L570.434,88.387z M492.843,88.981l-0.083-11.228l-5.191-0.937l-4.932-0.682l-4.681-0.422h-4.426l-4.166,0.339l-3.916,0.598
l-3.572,0.761l-3.233,0.937l-2.978,1.276l-2.635,1.359l-2.297,1.447l-2.045,1.703l-1.615,1.786l-1.275,1.874l-1.021,1.874
l-0.51,2.037l-0.171,1.87l0.171,2.045l0.594,1.87l1.104,1.958l1.364,1.87l1.87,1.619l2.301,1.703l2.719,1.447l3.15,1.276
l3.66,1.104l4.083,1.021l4.593,0.682l5.02,0.339l5.53,0.255l6.04-0.255l6.551-0.427v-10.035l-6.894,0.506h-5.953l-4.848-0.594
l-3.999-1.021l-2.979-1.192l-2.041-1.531l-1.109-1.614l-0.167-1.619l0.594-1.531l1.447-1.531l2.38-1.275l3.15-0.85l3.911-0.594
h4.853l5.442,0.594L492.843,88.981L492.843,88.981L492.843,88.981z M643.088,85.664v-1.703v-1.698v-1.79v-1.698h-0.766h-0.85
h-0.766h-0.682h-0.854h-0.761h-0.77h-0.766h-0.766h-0.85h-0.682h-0.849h-0.766h-0.682h-0.85h-0.766l-4.425,3.572v-3.572h-22.459
v30.883h22.459v-11.57V98v-0.51l0.083-0.594l0.255-0.766l0.344-0.766l0.682-0.682l1.021-0.427l1.275-0.167h1.36h1.447h1.359h1.363
h1.359h1.364h1.359h1.359v3.488l0.598,1.786l0.933,1.531l1.025,1.447l1.275,1.275l1.447,1.188l1.531,1.024l1.698,0.933l1.874,0.849
l1.958,0.682l2.041,0.598l2.213,0.594l2.125,0.343l2.38,0.422l2.213,0.172l2.301,0.339l2.296,0.087l2.297,0.084h2.297h2.125h2.212
l2.042-0.084h1.957l1.87-0.259l1.702-0.083l1.531-0.167l1.447-0.088l1.276-0.083l1.021-0.088l0.937-0.167l0.594-0.084l0.344-0.088
h0.167l0.255-0.083l0.511-0.171l0.682-0.51l0.766-0.766l0.682-1.021l0.594-1.192l0.344-1.786l-0.084-1.87h-0.26l-0.933,0.167
l-1.447,0.172l-1.958,0.083l-2.212,0.088l-2.552,0.083h-2.724l-2.895-0.171l-2.807-0.167l-2.719-0.255l-2.728-0.255l-2.38-0.51
l-2.125-0.515l-1.703-0.678l-1.191-0.765l-0.678-0.937v-1.104h33.263l0.682-1.619l0.423-1.531l0.088-1.702l-0.255-1.443
l-0.511-1.447l-0.682-1.447l-1.021-1.276l-1.192-1.276l-1.359-1.276l-1.619-1.024l-1.786-1.188l-1.869-0.854l-2.042-0.933
l-2.213-0.766l-2.296-0.766l-2.385-0.598l-2.468-0.51l-2.464-0.422l-2.556-0.255l-2.635-0.171h-2.64l-2.464,0.083l-2.64,0.255
l-2.552,0.259l-2.468,0.51l-2.38,0.594L652.7,79.54l-2.209,0.933l-2.041,1.108l-1.958,1.108l-1.786,1.443L643.088,85.664
L643.088,85.664L643.088,85.664z M674.48,90.6h6.89l-0.084-1.192l-0.422-1.021l-0.682-0.849l-0.85-0.937l-1.024-0.682l-1.188-0.51
l-1.275-0.339l-1.364-0.088l-1.447,0.088l-1.275,0.339l-1.188,0.51l-1.108,0.682l-0.849,0.937l-0.599,0.849l-0.51,1.021
L667.42,90.6H674.48L674.48,90.6L674.48,90.6z M760.663,87.111v22.291h-21.777V93.407l-0.255-1.104l-0.427-0.938l-0.766-0.853
l-0.853-0.594l-1.188-0.426l-1.192-0.339l-1.275-0.084h-1.275l-1.364,0.255l-1.275,0.343l-1.104,0.511l-1.021,0.761l-0.938,0.853
l-0.682,0.933l-0.339,1.108l-0.083,1.359v14.209h-22.802v-30.46h1.447h1.443h1.363h1.447h1.447h1.359h1.443h1.535h1.359h1.447
h1.447h1.359h1.447h1.443h1.363h1.448v4.087v-0.088l0.167-0.594l0.088-0.594l0.422-0.77l0.511-0.761l0.682-0.682l0.938-0.51h1.614
h1.702l1.703-0.088h1.698h1.702h1.79h1.703h1.781h1.791h1.702l1.782,0.088h1.702h1.786l1.702,0.083h1.703l1.698,0.088l1.957,0.933
l1.447,1.359l1.021,1.447l0.594,1.531l0.259,1.364l0.168,0.849V87.111L760.663,87.111L760.663,87.111z"/>
<path fill-rule="evenodd" clip-rule="evenodd" fill="#ED1C24" d="M308.085,36.284H789.22v101.073H308.085V36.284L308.085,36.284
L308.085,36.284z M782.121,130.673H315.188V42.968h466.934V130.673L782.121,130.673L782.121,130.673z"/>
</g>
</svg>
Display the source blob
Display the rendered blob
Raw
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment