|
<!DOCTYPE html>
|
|
<head>
|
|
<meta charset="utf-8">
|
|
<title>Kobe Bryant 2012-13 Game Log Line Graph</title>
|
|
<style>
|
|
body {
|
|
font: 10px sans-serif;
|
|
}
|
|
|
|
|
|
.axis path,
|
|
.axis line {
|
|
fill: none;
|
|
stroke: #000;
|
|
shape-rendering: crispEdges;
|
|
}
|
|
|
|
.legend-box {
|
|
cursor: pointer;
|
|
}
|
|
|
|
.line {
|
|
fill: none;
|
|
stroke: steelblue;
|
|
stroke-width: 1.5px;
|
|
}
|
|
|
|
</style>
|
|
</head>
|
|
<body>
|
|
<script src="http://d3js.org/d3.v3.js"></script>
|
|
<script>
|
|
|
|
var margin = {top: 20, right: 80, bottom: 30, left: 50},
|
|
width = 960 - margin.left - margin.right,
|
|
height = 500 - margin.top - margin.bottom;
|
|
|
|
var parseDate = d3.time.format("%Y-%m-%d").parse;
|
|
|
|
var xScale = d3.time.scale()
|
|
.range([0, width]);
|
|
|
|
var yScale = d3.scale.linear()
|
|
.range([height, 0]);
|
|
|
|
var color = d3.scale.category20();
|
|
|
|
var xAxis = d3.svg.axis()
|
|
.scale(xScale)
|
|
.orient("bottom");
|
|
|
|
var yAxis = d3.svg.axis()
|
|
.scale(yScale)
|
|
.orient("left");
|
|
|
|
var line = d3.svg.line()
|
|
.interpolate("monotone")
|
|
.x(function(d) { return xScale(d.date); })
|
|
.y(function(d) { return yScale(d.stat); });
|
|
|
|
var maxY;
|
|
|
|
var svg = d3.select("body").append("svg")
|
|
.attr("width", width + margin.left + margin.right)
|
|
.attr("height", height + margin.top + margin.bottom)
|
|
.append("g")
|
|
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
|
|
|
|
d3.csv("kobegamelog.csv", function(error, data) {
|
|
color.domain(d3.keys(data[0]).filter(function(key) {
|
|
var exclude = ["Rk","G","Date","Age","Tm","","Opp","","GS"], // blacklist
|
|
include = ["MP","FG","FGA","FG%","3P","3PA","FT","FTA",
|
|
"ORB","DRB","TRB","AST","STL","BLK","TOV","PF","PTS","GmSc"]; // whitelist
|
|
return (include.indexOf(key) >= 0);
|
|
}));
|
|
|
|
data.forEach(function(d) {
|
|
d.Date = parseDate(d.Date);
|
|
});
|
|
|
|
var categories = color.domain().map(function(name) {
|
|
return {
|
|
name: name,
|
|
values: data.map(function(d) {
|
|
return {
|
|
date: d.Date,
|
|
stat: parseFloat(d[name]),
|
|
result: d.Res,
|
|
opponent: d.Opp,
|
|
};
|
|
}),
|
|
visible: (name === "PTS" ? true : false)
|
|
};
|
|
});
|
|
|
|
xScale.domain(d3.extent(data, function(d) { return d.Date; }));
|
|
|
|
yScale.domain([0,
|
|
d3.max(categories, function(c) { return d3.max(c.values, function(v) { return v.stat; }); })
|
|
]);
|
|
|
|
// draw line graph
|
|
svg.append("g")
|
|
.attr("class", "x axis")
|
|
.attr("transform", "translate(0," + height + ")")
|
|
.call(xAxis);
|
|
|
|
svg.append("g")
|
|
.attr("class", "y axis")
|
|
.call(yAxis)
|
|
.append("text")
|
|
.attr("transform", "rotate(-90)")
|
|
.attr("y", 6)
|
|
.attr("dy", ".71em")
|
|
.style("text-anchor", "end")
|
|
.text("Stat");
|
|
|
|
var statistic = svg.selectAll(".statistic")
|
|
.data(categories)
|
|
.enter().append("g")
|
|
.attr("class", "statistic");
|
|
|
|
statistic.append("path")
|
|
.attr("class", "line")
|
|
.attr("id", function(d) {
|
|
return "line-" + d.name.replace("%", "P");
|
|
})
|
|
.attr("d", function(d) {
|
|
return d.visible ? line(d.values) : null;
|
|
})
|
|
.style("stroke", function(d) { return color(d.name); });
|
|
|
|
// draw legend
|
|
statistic.append("rect")
|
|
.attr("height",10)
|
|
.attr("width", 25)
|
|
.attr("x",width+10)
|
|
.attr("y",function(d,i) {return i * 20 - 8;})
|
|
.attr("stroke", function(d) { return color(d.name); })
|
|
.attr("fill",function(d) {
|
|
return d.visible ? color(d.name) : "white";
|
|
})
|
|
.attr("class", "legend-box")
|
|
|
|
.on("click", function(d){
|
|
d.visible = !d.visible;
|
|
|
|
maxY = findMaxY(categories);
|
|
yScale.domain([0,maxY]);
|
|
svg.select(".y.axis")
|
|
.transition()
|
|
.call(yAxis);
|
|
|
|
statistic.select("path").transition()
|
|
.attr("d", function(d){
|
|
return d.visible ? line(d.values) : null;
|
|
})
|
|
|
|
statistic.select("rect")
|
|
.transition()
|
|
.attr("fill", function(d) {
|
|
return d.visible ? color(d.name) : "white";
|
|
});
|
|
})
|
|
|
|
.on("mouseover", function(d){
|
|
d3.select("#line-" + d.name.replace("%", "P"))
|
|
.transition()
|
|
.style("stroke-width", 3);
|
|
})
|
|
|
|
.on("mouseout", function(d){
|
|
d3.select("#line-" + d.name.replace("%", "P"))
|
|
.transition()
|
|
.style("stroke-width", 1.5);
|
|
})
|
|
|
|
|
|
statistic.append("text")
|
|
.attr("x", function (d) {
|
|
return width + 25 + 15
|
|
})
|
|
.attr("y", function(d,i) {
|
|
return i * 20;
|
|
})
|
|
.text(function(d) { return d.name; })
|
|
|
|
});
|
|
|
|
function findMaxY(data){
|
|
var maxValues = data.map(function(d) {
|
|
if (d.visible){
|
|
return d3.max(d.values, function(value) {
|
|
return value.stat; })
|
|
}
|
|
});
|
|
return d3.max(maxValues);
|
|
}
|
|
|
|
|
|
</script>
|
|
</html> |