Simple Time Series based on code from d3noob and Scott Murray.
Block created by blockbuilder.
xxxxxxxxxx
<head>
<meta charset="utf-8">
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.5/d3.min.js"></script>
<style>
body { font: 12px Arial;}
path {
stroke: steelblue;
stroke-width: 1;
fill: none;
}
.axis path,
.axis line {
fill: none;
stroke: grey;
stroke-width: 2;
shape-rendering: crispEdges;
}
.legend {
font-size: 16px;
font-weight: bold;
text-anchor: middle;
}
</style>
<body>
<h1>Change in Searches for New York Yankees and Mets 2004-2015</h1>
<p>Data sourced from Google Trends</p>
<script>
var margin = {top: 30, right: 20, bottom: 70, left: 50},
width = 1100 - margin.left - margin.right,
height = 700 - margin.top - margin.bottom;
var parseDate = d3.time.format("%m/%d/%Y").parse;
var x = d3.time.scale().range([0, width]);
var y = d3.scale.linear().range([height, 0]);
var xAxis = d3.svg.axis().scale(x)
.orient("bottom").ticks(5);
var yAxis = d3.svg.axis().scale(y)
.orient("left").ticks(5);
var dateline = d3.svg.line()
.x(function(d) { return x(d.date); })
.y(function(d) { return y(d.val); });
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 + ")");
// Get the data
d3.csv("gtrends4.txt", function(error, data) {
data.forEach(function(d) {
d.date = parseDate(d.date);
d.val = +d.val;
});
// Scale the range of the data
x.domain(d3.extent(data, function(d) { return d.date; })).nice();
y.domain([0, d3.max(data, function(d) { return d.val; })]);
// Nest the entries by symbol
var dataNest = d3.nest()
.key(function(d) {return d.team;})
.entries(data);
var color = d3.scale.category10();
legendSpace = width/dataNest.length;
// Loop through each team
dataNest.forEach(function(d,i) {
svg.append("path")
.attr("class", "line")
.style("stroke", function() {
return d.color = color(d.key); })
.attr("d", dateline(d.values))
;
svg.append("text")
.attr("x", (legendSpace/2)+i*legendSpace) // space legend
.attr("y", height + (margin.bottom/2)+ 5)
.attr("class", "legend") // style the legend
.style("fill", function() { // Add the colours dynamically
return d.color = color(d.key); })
.text(d.key);
});
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis);
svg.append("g")
.attr("class", "y axis")
.call(yAxis);
});
</script>
</body>
https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.5/d3.min.js